From: Tim Otten Date: Wed, 18 Feb 2015 18:03:26 +0000 (-0800) Subject: CRM-15578 - Mailing API - Fix bug with resaving mailings X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d8e9212d100a1bf590ea61121dc569086c21fb4d;p=civicrm-core.git CRM-15578 - Mailing API - Fix bug with resaving mailings The bug manifested as failure to validate the required tokens. When submitting (resaving with scheduled_date, etal), the BAO's create() function incorrectly applied defaults and overwrote the value of footer_id (among others) -- which subsequently caused checkSendable() to fail. --- diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index 8fb0bb55b0..946558d301 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -1608,6 +1608,10 @@ ORDER BY civicrm_email.is_bulkmail DESC * @throws \Exception */ public static function create(&$params, $ids = array()) { + // WTH $ids + if (empty($ids) && isset($params['id'])) { + $ids['id'] = $params['id']; + } // CRM-12430 // Do the below only for an insert diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index 0ded316411..0c33e8f0bb 100755 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -39,6 +39,12 @@ class api_v3_MailingTest extends CiviUnitTestCase { protected $_entity = 'Mailing'; protected $_contactID; + /** + * APIv3 result from creating an example footer + * @var array + */ + protected $footer; + public function setUp() { parent::setUp(); $this->useTransaction(); @@ -55,6 +61,11 @@ class api_v3_MailingTest extends CiviUnitTestCase { 'header_id' => '', 'footer_id' => '', ); + + $this->footer = civicrm_api3('MailingComponent', 'create', array( + 'body_html' => '

From {domain.address}. To opt out, go to {action.optOutUrl}.

', + 'body_text' => 'From {domain.address}. To opt out, go to {action.optOutUrl}.', + )); } public function tearDown() { @@ -372,7 +383,20 @@ class api_v3_MailingTest extends CiviUnitTestCase { "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./", // expectedFailure 0, // expectedJobCount ); - + $cases[] = array( + TRUE, //useLogin + array('body_text' => 'Look ma, magic tokens in the text!', 'footer_id' => '%FOOTER%'), // createParams + array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), + FALSE, // expectedFailure + 1, // expectedJobCount + ); + $cases[] = array( + TRUE, //useLogin + array('body_html' => '

Look ma, magic tokens in the markup!

', 'footer_id' => '%FOOTER%'), // createParams + array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), + FALSE, // expectedFailure + 1, // expectedJobCount + ); return $cases; } @@ -389,6 +413,10 @@ class api_v3_MailingTest extends CiviUnitTestCase { $this->createLoggedInUser(); } + if (isset($createParams['footer_id']) && $createParams['footer_id'] == '%FOOTER%') { + $createParams['footer_id'] = $this->footer['id']; + } + $id = $this->createDraftMailing($createParams); $submitParams['id'] = $id;