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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions ext/flexmailer/src/API/MailingPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.


public function insert() {
throw new \RuntimeException('MailingJob is just a preview. It cannot be saved.');
}

public function update($dataObject = FALSE) {
throw new \RuntimeException('MailingJob is just a preview. It cannot be saved.');
}

public function save($hook = TRUE) {
throw new \RuntimeException('MailingJob is just a preview. It cannot be saved.');
}

};
$job->mailing_id = $mailing->id ?: NULL;
$job->is_test = 1;
$job->status = 'Complete';
// $job->save();

$flexMailer = new FlexMailer(array(
'is_preview' => TRUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function testMailerPreview(): void {
$this->assertMaxIds($maxIDs);

$previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
$this->assertEquals("[CiviMail Draft] Hello $displayName",
$this->assertEquals("Hello $displayName",
$previewResult['values']['subject']);

$this->assertStringContainsString("This is $displayName", $previewResult['values']['body_text']);
Expand Down Expand Up @@ -112,7 +112,7 @@ public function testMailerPreviewWithoutId(): void {
$previewResult = $this->callAPISuccess('mailing', 'preview', $params);
$this->assertMaxIds($maxIDs);

$this->assertEquals("[CiviMail Draft] Hello $displayName",
$this->assertEquals("Hello $displayName",
$previewResult['values']['subject']);

$this->assertStringContainsString("This is $displayName", $previewResult['values']['body_text']);
Expand Down