From 11bb748471f314a2f1475dc19f5762293e43ce11 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 20 Jul 2017 17:58:01 +1200 Subject: [PATCH] CRM-20919 adjust mailing.create to allow adding completed mailings. This is to support importing CiviMails from other systems, which may not have our tokens --- CRM/Mailing/BAO/Mailing.php | 7 ++++--- tests/phpunit/api/v3/MailingTest.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index 3d3cced20b..9314e19b23 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -1731,7 +1731,6 @@ ORDER BY civicrm_email.is_bulkmail DESC CRM_Contact_BAO_Contact_Utils::generateChecksum($mailing->id, NULL, NULL, NULL, 'mailing', 16); $groupTableName = CRM_Contact_BAO_Group::getTableName(); - $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName(); /* Create the mailing group record */ $mg = new CRM_Mailing_DAO_MailingGroup(); @@ -1759,7 +1758,7 @@ ORDER BY civicrm_email.is_bulkmail DESC CRM_Core_BAO_File::processAttachment($params, 'civicrm_mailing', $mailing->id); // If we're going to autosend, then check validity before saving. - if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) { + if (empty($params['is_completed']) && !empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) { $cb = Civi\Core\Resolver::singleton()->get($params['_evil_bao_validator_']); $errors = call_user_func($cb, $mailing); if (!empty($errors)) { @@ -1775,7 +1774,9 @@ ORDER BY civicrm_email.is_bulkmail DESC if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) { $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $mailing->id; - $job->status = 'Scheduled'; + // If we are creating a new Completed mailing (e.g. import from another system) set the job to completed. + // Keeping former behaviour when an id is present is precautionary and may warrant reconsideration later. + $job->status = ((empty($params['is_completed']) || !empty($params['id'])) ? 'Scheduled' : 'Complete'); $job->is_test = 0; if (!$job->find(TRUE)) { diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index e4918d0da7..5849d5e7ed 100644 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -85,6 +85,20 @@ class api_v3_MailingTest extends CiviUnitTestCase { $this->getAndCheck($this->_params, $result['id'], 'mailing'); } + /** + * Create a completed mailing (e.g when importing from a provider). + */ + public function testMailerCreateCompleted() { + $this->_params['body_html'] = 'I am completed so it does not matter if there is an opt out link since I have already been sent by another system'; + $this->_params['is_completed'] = 1; + $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + array('scheduled_date' => 'now'), __FUNCTION__, __FILE__); + $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id'])); + $this->assertEquals(1, $jobs['count']); + $this->assertEquals('Complete', $jobs['values'][$jobs['id']]['status']); + unset($this->_params['created_id']); // return isn't working on this in getAndCheck so lets not check it for now + $this->getAndCheck($this->_params, $result['id'], 'mailing'); + } + /** * Per CRM-20316 the mailing should still create without created_id (not mandatory). */ -- 2.25.1