From f4dc5180d120715cc9a1f5622070c5461008edcf Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 3 Oct 2021 16:05:24 -0700 Subject: [PATCH 1/2] flexmailer - Preview jobs are strictly unsaved --- ext/flexmailer/src/API/MailingPreview.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ext/flexmailer/src/API/MailingPreview.php b/ext/flexmailer/src/API/MailingPreview.php index cdd11ac4281a..576fa1545e3d 100644 --- a/ext/flexmailer/src/API/MailingPreview.php +++ b/ext/flexmailer/src/API/MailingPreview.php @@ -41,11 +41,24 @@ 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 { + + 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, From e2b2d8af16418d75a682f0a8d4d7faa7998f3e3a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 3 Oct 2021 16:11:33 -0700 Subject: [PATCH 2/2] MailingPreview - We don't need the `[CiviMail Draft]` prefix for in-browser previews --- ext/flexmailer/src/API/MailingPreview.php | 1 - .../tests/phpunit/Civi/FlexMailer/MailingPreviewTest.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ext/flexmailer/src/API/MailingPreview.php b/ext/flexmailer/src/API/MailingPreview.php index 576fa1545e3d..e585eda77ab7 100644 --- a/ext/flexmailer/src/API/MailingPreview.php +++ b/ext/flexmailer/src/API/MailingPreview.php @@ -57,7 +57,6 @@ public function save($hook = TRUE) { }; $job->mailing_id = $mailing->id ?: NULL; - $job->is_test = 1; $job->status = 'Complete'; $flexMailer = new FlexMailer(array( diff --git a/ext/flexmailer/tests/phpunit/Civi/FlexMailer/MailingPreviewTest.php b/ext/flexmailer/tests/phpunit/Civi/FlexMailer/MailingPreviewTest.php index aad8443ce470..a12ddad7c576 100644 --- a/ext/flexmailer/tests/phpunit/Civi/FlexMailer/MailingPreviewTest.php +++ b/ext/flexmailer/tests/phpunit/Civi/FlexMailer/MailingPreviewTest.php @@ -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']); @@ -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']);