Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
(dev/mail#20) Mailing.preview - Fix regression circa Civi 5.6.x
Browse files Browse the repository at this point in the history
Flexmailer overrides the `Mailing.preview` API.  In
civicrm/civicrm-core#12509 (5.6), it became possible
to call `Mailing.preview` without an `id` for the mailing.

This update Flexmailer `Mailing.preview` to also work without an ID,
and it expands unit-test coverage to ensure correct operation with
or without an ID.
  • Loading branch information
totten committed Apr 29, 2019
1 parent 88bbc06 commit 812d53e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 24 deletions.
16 changes: 12 additions & 4 deletions src/API/MailingPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ public static function preview($apiRequest) {
$params = $apiRequest['params'];

/** @var \CRM_Mailing_BAO_Mailing $mailing */
$mailing = \CRM_Mailing_BAO_Mailing::findById($params['id']);
$mailing = new \CRM_Mailing_BAO_Mailing();
$mailingID = \CRM_Utils_Array::value('id', $params);
if ($mailingID) {
$mailing->id = $mailingID;
$mailing->find(TRUE);
}
else {
$mailing->copyValues($params);
}

if (!Abdicator::isFlexmailPreferred($mailing)) {
require_once 'api/v3/Mailing.php';
Expand All @@ -34,10 +42,10 @@ public static function preview($apiRequest) {
\CRM_Core_Session::singleton()->get('userID'));

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

$flexMailer = new FlexMailer(array(
'is_preview' => TRUE,
Expand All @@ -57,7 +65,7 @@ public static function preview($apiRequest) {
$flexMailer->fireComposeBatch(array($task));

return civicrm_api3_create_success(array(
'id' => $params['id'],
'id' => isset($params['id']) ? $params['id'] : NULL,
'contact_id' => $contactID,
'subject' => $task->getMailParam('Subject'),
'body_html' => $task->getMailParam('html'),
Expand Down
85 changes: 65 additions & 20 deletions tests/phpunit/Civi/FlexMailer/MailingPreviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public function setUp() {
$this->_email = 'test@test.test';
$this->_params = array(
'subject' => 'Hello {contact.display_name}',
'body_text' => "This is {contact.display_name}.\nhttps://civicrm.org\n{domain.address}{action.optOutUrl}",
'body_html' => "<p>This is {contact.display_name}.</p><p><a href='https://civicrm.org/'>CiviCRM.org</a></p><p>{domain.address}{action.optOutUrl}</p>",
'body_text' => "This is {contact.display_name}.\nhttps://civicrm.org\nda=({domain.address}) optout=({action.optOutUrl})",
'body_html' => "<p>This is {contact.display_name}.</p><p><a href='https://civicrm.org/'>CiviCRM.org</a></p><p>da=({domain.address}) optout=({action.optOutUrl})</p>",
'name' => 'mailing name',
'created_id' => $this->_contactID,
'header_id' => '',
Expand Down Expand Up @@ -76,30 +76,75 @@ public function testMailerPreview() {
$params['options']['force_rollback'] = 1;
// END SAMPLE DATA

$maxIDs = array(
'mailing' => \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
'job' => \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
'group' => \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
'recipient' => \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
);
$maxIDs = $this->getMaxIds();
$result = $this->callAPISuccess('mailing', 'create', $params);
$this->assertDBQuery($maxIDs['mailing'],
'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
$this->assertDBQuery($maxIDs['job'],
'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
$this->assertDBQuery($maxIDs['group'],
'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
$this->assertDBQuery($maxIDs['recipient'],
'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records'
$this->assertMaxIds($maxIDs);

$previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
$this->assertEquals("[CiviMail Draft] Hello $displayName",
$previewResult['values']['subject']);
$this->assertContains("This is $displayName",
$previewResult['values']['body_text']);
$this->assertContains("<p>This is $displayName.</p>",
$previewResult['values']['body_html']);

$this->assertContains("This is $displayName", $previewResult['values']['body_text']);
$this->assertContains("civicrm/mailing/optout", $previewResult['values']['body_text']);
$this->assertContains("&jid=&qid=&h=fakehash", $previewResult['values']['body_text']);

$this->assertContains("<p>This is $displayName.</p>", $previewResult['values']['body_html']);
$this->assertContains("civicrm/mailing/optout", $previewResult['values']['body_html']);
$this->assertContains("&amp;jid=&amp;qid=&amp;h=fakehash", $previewResult['values']['body_html']);

$this->assertEquals('flexmailer', $previewResult['values']['_rendered_by_']);
}

public function testMailerPreviewWithoutId() {
// BEGIN SAMPLE DATA
$contactID = $this->createLoggedInUser();
$displayName = $this->callAPISuccess('contact', 'get', ['id' => $contactID]);
$displayName = $displayName['values'][$contactID]['display_name'];
$this->assertTrue(!empty($displayName));
$params = $this->_params;
// END SAMPLE DATA

$maxIDs = $this->getMaxIds();
$previewResult = $this->callAPISuccess('mailing', 'preview', $params);
$this->assertMaxIds($maxIDs);

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

$this->assertContains("This is $displayName", $previewResult['values']['body_text']);
$this->assertContains("civicrm/mailing/optout", $previewResult['values']['body_text']);
$this->assertContains("&jid=&qid=&h=fakehash", $previewResult['values']['body_text']);

$this->assertContains("<p>This is $displayName.</p>", $previewResult['values']['body_html']);
$this->assertContains("civicrm/mailing/optout", $previewResult['values']['body_html']);
$this->assertContains("&amp;jid=&amp;qid=&amp;h=fakehash", $previewResult['values']['body_html']);

$this->assertEquals('flexmailer', $previewResult['values']['_rendered_by_']);
}

/**
* @return array
* Array(string $table => int $maxID).
*/
protected function getMaxIds() {
return array(
'civicrm_mailing' => \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
'civicrm_mailing_job' => \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
'civicrm_mailing_group' => \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
'civicrm_mailing_recipients' => \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
);
}

/**
* Assert that the given tables have the given extant IDs.
*
* @param array $expectMaxIds
* Array(string $table => int $maxId).
*/
protected function assertMaxIds($expectMaxIds) {
foreach ($expectMaxIds as $table => $maxId) {
$this->assertDBQuery($expectMaxIds[$table], 'SELECT MAX(id) FROM ' . $table, [], "Table $table should have a maximum ID of $maxId");
}
}

}

0 comments on commit 812d53e

Please sign in to comment.