Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flexmailer - Update [CiviMail Draft] prefix to match BAO behavior #21715

Merged
merged 2 commits into from
Oct 4, 2021

Conversation

totten
Copy link
Member

@totten totten commented Oct 3, 2021

Overview

When drafting a mail-blast, one may use in-app previews and/or email test-messages. This refines the way in which the [CiviMail Draft] prefix is applied - and updates Flexmailer to more precisely match BAO.

Before

Use Case Action BAO Behavior Flexmailer Behavior
Send a test mailing over SMTP send_test Has prefix Has prefix
Preview a test mailing in browser preview Omits prefix Has prefix

After

Use Case Action BAO Behavior Flexmailer Behavior
Send a test mailing over SMTP send_test Has prefix Has prefix
Preview a test mailing in browser preview Omits prefix Omits prefix

Technical Details

The behavior of [CiviMail Draft] prefix is keyed off the property civicrm_mailing_job.is_test. The functional change here comes from twiddling is_test=1 => is_test=0 for preview action.

In the case of previewing, there is no persistent civicrm_mailing_job, but there is a placeholder/mock-job (allowing the preview to more completely exercise representative code-paths). It doesn't matter much what data we put in this mock job - as long as it isn't saved. The is_test=1 was a slight mitigation in case it was accidentally saved. This patch replaces that with a more forthright mitigation to prevent accidental saving of the mock-job.

@civibot
Copy link

civibot bot commented Oct 3, 2021

(Standard links)

@eileenmcnaughton
Copy link
Contributor

@totten Stacktrace
Civi\FlexMailer\MailingPreviewTest::testMailerPreview
Failure in api call for mailing create: Error in call to Mailing_preview : Declaration of class@anonymous::save() should be compatible with CRM_Core_DAO::save($hook = true)
#0 /home/jenkins/bknix-dfl/build/core-21715-7d6a/web/sites/all/modules/civicrm/ext/flexmailer/src/API/MailingPreview.php(44): PHPUnit\Util\ErrorHandler->__invoke(2, 'Declaration of ...', '/home/jenkins/b...', 44, Array)
#1 [internal function]: Civi\FlexMailer\API\MailingPreview::preview(Array)
#2 /home/jenkins/bknix-dfl/build/core-21715-7d6a/web/sites/all/modules/civicrm/Civi/API/Provider/AdhocProvider.php(119): call_user_func('\Civi\FlexMaile...', Array)
#3 /home/jenkins/bknix-dfl/build/core-21715-7d6a/web/sites/all/modules/civicrm/Civi/API/Kernel.php(149): Civi\API\Provider\AdhocProvider->invoke(Array)
#4 /home/jenkins/bknix-dfl/build/core-21715-7d6a/web/sites/all/modules/civicrm/Civi/API/Kernel.php(81): Civi\API\Kernel->runRequest(Array)
#5 /home/jenkins/bknix-dfl/build/core-21715-7d6a/web/sites/all/modules/civi

@@ -41,11 +41,23 @@ public static function preview($apiRequest) {
$contactID = \CRM_Utils_Array::value('contact_id', $params,
\CRM_Core_Session::singleton()->get('userID'));

$job = new \CRM_Mailing_BAO_MailingJob();
$job = new class extends \CRM_Mailing_BAO_MailingJob {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non - blocking - but I definitely find it much easier to read / search for when we define classes in their own file rather than this on-the-fly way

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I do see the slippery-slope argument where some class may be misjudged as "anonymous/strictly-hands-off to others" -- and then create some cost around discovering that this anonymous class is actually a supported interface.

At the same time, suppose we did a class CRM_Mailing_DAO_MailingJob_SaveProhibited. Future-person would be just as likely to read too much into it -- eg taking it as an exemplar to emulate in new CRM_*_DAO_*_SaveProhibited classes, and/or rightly objecting that SaveProhibited hierarchy conflicts with the BAO hierarchy, and/or feeling an obligation to support some external evil like if ($dao instance of CRM_Mailing_DAO_MailingJob_SaveProhibited) { activateThirdPartyBits(); }.

It seems to me - if one wants to be a good precedent - then that's undertaking a bigger task: systemically adding a freeze-mode toCRM_Core_DAO. Maybe that's a good feature? But it seems a bit by-the-by?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@totten to be honest I don't really understand this code - I merged based on the tests - I find the cognitve load of classes declared on the fly to be really high.

If you had declared an actual class in a file like
class CRM_Mailing_BAO_Mailing_Job_PreviewMode extends \CRM_Mailing_BAO_MailingJob

it would have been easy to read.

I've always thought stuff like this was 'complicated stuff that Tim understands' but I actually had to debug some code like this the other day & I found that if I re-wrote constructs like this before trying to read it & also renaming all the abbreviations to words I could make sense of it.

@eileenmcnaughton
Copy link
Contributor

Merging - this seems to 'do the trick' & per my comment the readability issue is non-blocking

@eileenmcnaughton eileenmcnaughton merged commit 558b41a into civicrm:master Oct 4, 2021
@totten totten deleted the master-no-test branch October 4, 2021 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants