CRM-15855 - Allow mailings to be saved (but not sent) without name+subject.
[civicrm-core.git] / tests / phpunit / api / v3 / MailingTest.php
index c185da7571cb4b9597f06dea18047cca3086e249..c908fbd93435fcc35e017de8ab1ced2b93f8cb35 100755 (executable)
@@ -79,10 +79,23 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     // BEGIN SAMPLE DATA
     $groupIDs['a'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
     $groupIDs['b'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
-    $contactIDs['a'] = $this->individualCreate(array('email' => 'include.me@example.org', 'first_name' => 'Includer', 'last_name' => 'Person'));
-    $contactIDs['b'] = $this->individualCreate(array('email' => 'exclude.me@example.org', 'last_name' => 'Excluder', 'last_name' => 'Excluder'));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['a'], 'contact_id' => $contactIDs['a']));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['b'], 'contact_id' => $contactIDs['b']));
+    $contactIDs['a'] = $this->individualCreate(array(
+        'email' => 'include.me@example.org',
+        'first_name' => 'Includer',
+        'last_name' => 'Person',
+      ));
+    $contactIDs['b'] = $this->individualCreate(array(
+        'email' => 'exclude.me@example.org',
+        'last_name' => 'Excluder',
+      ));
+    $this->callAPISuccess('GroupContact', 'create', array(
+        'group_id' => $groupIDs['a'],
+        'contact_id' => $contactIDs['a'],
+      ));
+    $this->callAPISuccess('GroupContact', 'create', array(
+        'group_id' => $groupIDs['b'],
+        'contact_id' => $contactIDs['b'],
+      ));
     // END SAMPLE DATA
 
     // ** Pass 1: Create
@@ -95,21 +108,21 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $getGroup1 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
     $getGroup1_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup1['values']));
     $this->assertEquals(array($groupIDs['a']), $getGroup1_ids);
-    $getRecip1 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
-    $getRecip1_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecip1['values']));
-    $this->assertEquals(array($contactIDs['a']), $getRecip1_ids);
+    $getRecipient1 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
+    $getRecipient1_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient1['values']));
+    $this->assertEquals(array($contactIDs['a']), $getRecipient1_ids);
 
     // ** Pass 2: Update without any changes to groups[include]
-    $nullopParams = $createParams;
-    $nullopParams['id'] = $createResult['id'];
+    $nullOpParams = $createParams;
+    $nullOpParams['id'] = $createResult['id'];
     $updateParams['api.mailing_job.create'] = 1;
-    unset($nullopParams['groups']['include']);
-    $this->callAPISuccess('Mailing', 'create', $nullopParams);
+    unset($nullOpParams['groups']['include']);
+    $this->callAPISuccess('Mailing', 'create', $nullOpParams);
     $getGroup2 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
     $getGroup2_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup2['values']));
     $this->assertEquals(array($groupIDs['a']), $getGroup2_ids);
-    $getRecip2 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
-    $getRecip2_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecip2['values']));
+    $getRecipient2 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
+    $getRecip2_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient2['values']));
     $this->assertEquals(array($contactIDs['a']), $getRecip2_ids);
 
     // ** Pass 3: Update with different groups[include]
@@ -121,9 +134,9 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $getGroup3 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
     $getGroup3_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup3['values']));
     $this->assertEquals(array($groupIDs['b']), $getGroup3_ids);
-    $getRecip3 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
-    $getRecip3_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecip3['values']));
-    $this->assertEquals(array($contactIDs['b']), $getRecip3_ids);
+    $getRecipient3 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
+    $getRecipient3_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient3['values']));
+    $this->assertEquals(array($contactIDs['b']), $getRecipient3_ids);
   }
 
   public function testMailerPreview() {
@@ -145,13 +158,13 @@ class api_v3_MailingTest extends CiviUnitTestCase {
       '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'),
-      'recip' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
+      'recipient' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
     );
     $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['recip'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records'
+    $this->assertDBQuery($maxIDs['recipient'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records'
 
     $previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
     $this->assertEquals("Hello $displayName", $previewResult['values']['subject']);
@@ -163,11 +176,27 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     // BEGIN SAMPLE DATA
     $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
     $groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
-    $contactIDs['includeme'] = $this->individualCreate(array('email' => 'include.me@example.org', 'first_name' => 'Includer', 'last_name' => 'Person'));
-    $contactIDs['excludeme'] = $this->individualCreate(array('email' => 'exclude.me@example.org', 'last_name' => 'Excluder', 'last_name' => 'Excluder'));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['includeme']));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['excludeme']));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['exc'], 'contact_id' => $contactIDs['excludeme']));
+    $contactIDs['include_me'] = $this->individualCreate(array(
+        'email' => 'include.me@example.org',
+        'first_name' => 'Includer',
+        'last_name' => 'Person',
+      ));
+    $contactIDs['exclude_me'] = $this->individualCreate(array(
+        'email' => 'exclude.me@example.org',
+        'last_name' => 'Excluder',
+      ));
+    $this->callAPISuccess('GroupContact', 'create', array(
+        'group_id' => $groupIDs['inc'],
+        'contact_id' => $contactIDs['include_me'],
+      ));
+    $this->callAPISuccess('GroupContact', 'create', array(
+        'group_id' => $groupIDs['inc'],
+        'contact_id' => $contactIDs['exclude_me'],
+      ));
+    $this->callAPISuccess('GroupContact', 'create', array(
+        'group_id' => $groupIDs['exc'],
+        'contact_id' => $contactIDs['exclude_me'],
+      ));
 
     $params = $this->_params;
     $params['groups']['include'] = array($groupIDs['inc']);
@@ -198,15 +227,22 @@ class api_v3_MailingTest extends CiviUnitTestCase {
 
     $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
     $previewIds = array_values(CRM_Utils_Array::collect('contact_id', $preview['values']));
-    $this->assertEquals(array((string) $contactIDs['includeme']), $previewIds);
+    $this->assertEquals(array((string) $contactIDs['include_me']), $previewIds);
     $previewEmails = array_values(CRM_Utils_Array::collect('api.email.getvalue', $preview['values']));
     $this->assertEquals(array('include.me@example.org'), $previewEmails);
     $previewNames = array_values(CRM_Utils_Array::collect('api.contact.getvalue', $preview['values']));
     $this->assertTrue((bool) preg_match('/Includer Person/', $previewNames[0]), "Name 'Includer Person' should appear in '" . $previewNames[0] . '"');
   }
 
+  /**
+   *
+   */
   public function testMailerSendTest_email() {
-    $contactIDs['alice'] = $this->individualCreate(array('email' => 'alice@example.org', 'first_name' => 'Alice', 'last_name' => 'Person'));
+    $contactIDs['alice'] = $this->individualCreate(array(
+        'email' => 'alice@example.org',
+        'first_name' => 'Alice',
+        'last_name' => 'Person',
+      ));
 
     $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
 
@@ -221,15 +257,39 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $this->assertEquals(array('alice@example.org'), $deliveredEmails);
   }
 
+  /**
+   *
+   */
   public function testMailerSendTest_group() {
     // BEGIN SAMPLE DATA
     $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
-    $contactIDs['alice'] = $this->individualCreate(array('email' => 'alice@example.org', 'first_name' => 'Alice', 'last_name' => 'Person'));
-    $contactIDs['bob'] = $this->individualCreate(array('email' => 'bob@example.org', 'first_name' => 'Bob', 'last_name' => 'Person'));
-    $contactIDs['carol'] = $this->individualCreate(array('email' => 'carol@example.org', 'first_name' => 'Carol', 'last_name' => 'Person'));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['alice']));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['bob']));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['carol']));
+    $contactIDs['alice'] = $this->individualCreate(array(
+        'email' => 'alice@example.org',
+        'first_name' => 'Alice',
+        'last_name' => 'Person',
+      ));
+    $contactIDs['bob'] = $this->individualCreate(array(
+        'email' => 'bob@example.org',
+        'first_name' => 'Bob',
+        'last_name' => 'Person',
+      ));
+    $contactIDs['carol'] = $this->individualCreate(array(
+        'email' => 'carol@example.org',
+        'first_name' => 'Carol',
+        'last_name' => 'Person',
+      ));
+    $this->callAPISuccess('GroupContact', 'create', array(
+        'group_id' => $groupIDs['inc'],
+        'contact_id' => $contactIDs['alice'],
+      ));
+    $this->callAPISuccess('GroupContact', 'create', array(
+        'group_id' => $groupIDs['inc'],
+        'contact_id' => $contactIDs['bob'],
+      ));
+    $this->callAPISuccess('GroupContact', 'create', array(
+        'group_id' => $groupIDs['inc'],
+        'contact_id' => $contactIDs['carol'],
+      ));
     // END SAMPLE DATA
 
     $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
@@ -247,65 +307,83 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $this->assertEquals(array('alice@example.org', 'bob@example.org', 'carol@example.org'), $deliveredEmails);
   }
 
+  /**
+   * @return array
+   */
   public function submitProvider() {
     $cases = array(); // $useLogin, $params, $expectedFailure, $expectedJobCount
     $cases[] = array(
       TRUE, //useLogin
+      array(), // createParams
       array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
       FALSE, // expectedFailure
       1, // expectedJobCount
     );
     $cases[] = array(
       FALSE, //useLogin
+      array(), // createParams
       array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
       "/Failed to determine current user/", // expectedFailure
       0, // expectedJobCount
     );
     $cases[] = array(
       TRUE, //useLogin
+      array(), // createParams
       array('scheduled_date' => '2014-12-13 10:00:00'),
       FALSE, // expectedFailure
       1, // expectedJobCount
     );
     $cases[] = array(
       TRUE, //useLogin
+      array(), // createParams
       array(),
       "/Missing parameter scheduled_date and.or approval_date/", // expectedFailure
       0, // expectedJobCount
     );
+    $cases[] = array(
+      TRUE, //useLogin
+      array('name' => ''), // createParams
+      array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
+      "/Mailing cannot be sent. There are missing fields \\(name\\)./", // expectedFailure
+      0, // expectedJobCount
+    );
     return $cases;
   }
 
   /**
    * @param bool $useLogin
-   * @param array $params
+   * @param array $createParams
+   * @param array $submitParams
    * @param null|string $expectedFailure
    * @param int $expectedJobCount
    * @dataProvider submitProvider
    */
-  public function testMailerSubmit($useLogin, $params, $expectedFailure, $expectedJobCount) {
+  public function testMailerSubmit($useLogin, $createParams, $submitParams, $expectedFailure, $expectedJobCount) {
     if ($useLogin) {
       $this->createLoggedInUser();
     }
 
-    $id = $this->createDraftMailing();
+    $id = $this->createDraftMailing($createParams);
 
-    $params['id'] = $id;
+    $submitParams['id'] = $id;
     if ($expectedFailure) {
-      $submitResult = $this->callAPIFailure('mailing', 'submit', $params);
+      $submitResult = $this->callAPIFailure('mailing', 'submit', $submitParams);
       $this->assertRegExp($expectedFailure, $submitResult['error_message']);
     }
     else {
-      $submitResult = $this->callAPIAndDocument('mailing', 'submit', $params, __FUNCTION__, __FILE__);
+      $submitResult = $this->callAPIAndDocument('mailing', 'submit', $submitParams, __FUNCTION__, __FILE__);
       $this->assertTrue(is_numeric($submitResult['id']));
       $this->assertTrue(is_numeric($submitResult['values'][$id]['scheduled_id']));
-      $this->assertEquals($params['scheduled_date'], $submitResult['values'][$id]['scheduled_date']);
+      $this->assertEquals($submitParams['scheduled_date'], $submitResult['values'][$id]['scheduled_date']);
     }
     $this->assertDBQuery($expectedJobCount, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
       1 => array($id, 'Integer'),
     ));
   }
 
+  /**
+   *
+   */
   public function testMailerStats() {
     $result = $this->groupContactCreate($this->_groupID, 100);
     $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
@@ -315,7 +393,7 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
     $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID);
     $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
-    $deliveredIds  = implode(',', array_keys($deliveredInfo['values']));
+    $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
 
     //Change the test mail into live
     $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}";
@@ -354,12 +432,13 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
     );
     $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
   }
+
   /**
    * Test civicrm_mailing_delete
    */
   public function testMailerDeleteSuccess() {
     $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
-    $jobs = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
+    $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
     $this->assertAPIDeleted($this->_entity, $result['id']);
   }
 
@@ -381,7 +460,7 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
       'body' => 'Body...',
       'time_stamp' => '20111109212100',
     );
-    $result = $this->callAPIFailure('mailing_event', 'bounce', $params,
+    $this->callAPIFailure('mailing_event', 'bounce', $params,
       'Queue event could not be found'
     );
   }
@@ -402,7 +481,7 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
       'event_subscribe_id' => '123',
       'time_stamp' => '20111111010101',
     );
-    $result = $this->callAPIFailure('mailing_event', 'confirm', $params,
+    $this->callAPIFailure('mailing_event', 'confirm', $params,
       'Confirmation failed'
     );
   }
@@ -425,7 +504,7 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
       'replyTo' => $this->_email,
       'time_stamp' => '20111111010101',
     );
-    $result = $this->callAPIFailure('mailing_event', 'reply', $params,
+    $this->callAPIFailure('mailing_event', 'reply', $params,
       'Queue event could not be found'
     );
   }
@@ -447,16 +526,18 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
       'email' => $this->_email,
       'time_stamp' => '20111111010101',
     );
-    $result = $this->callAPIFailure('mailing_event', 'forward', $params,
+    $this->callAPIFailure('mailing_event', 'forward', $params,
       'Queue event could not be found'
     );
   }
 
   /**
+   * @param array $params
+   *   Extra parameters for the draft mailing.
    * @return array|int
    */
-  public function createDraftMailing() {
-    $createParams = $this->_params;
+  public function createDraftMailing($params = array()) {
+    $createParams = array_merge($this->_params, $params);
     $createParams['api.mailing_job.create'] = 0; // note: exact match to API default
     $createResult = $this->callAPISuccess('mailing', 'create', $createParams, __FUNCTION__, __FILE__);
     $this->assertTrue(is_numeric($createResult['id']));