diff --git a/ext/flexmailer/src/API/MailingPreview.php b/ext/flexmailer/src/API/MailingPreview.php index cdd11ac4281a..e585eda77ab7 100644 --- a/ext/flexmailer/src/API/MailingPreview.php +++ b/ext/flexmailer/src/API/MailingPreview.php @@ -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 { + + 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, 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']);