Merge pull request #4607 from samuelsov/CRM-15637
[civicrm-core.git] / tests / phpunit / api / v3 / MailingTest.php
index 01f31a6f185b0ad98e0eb94a2d535f2e686ea8b9..1b86e3eddd01f061fe657eeafccd156b23c9ede0 100755 (executable)
@@ -37,42 +37,27 @@ class api_v3_MailingTest extends CiviUnitTestCase {
   protected $_apiversion = 3;
   protected $_params = array();
   protected $_entity = 'Mailing';
-  protected $_groupIDs; // array(string $pseudonym => int $id)
-  protected $_contactIDs; // array(string $pseudonym => int $id)
+  protected $_contactID;
 
-  /**
-   * @return array
-   */
-  function get_info() {
-    return array(
-      'name' => 'Mailer',
-      'description' => 'Test all Mailer methods.',
-      'group' => 'CiviCRM API Tests',
-    );
-  }
-
-  function setUp() {
+  public function setUp() {
     parent::setUp();
-    $this->_contactIDs = array();
+    $this->useTransaction();
+    CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW
+    $this->_contactID = $this->individualCreate();
     $this->_groupID = $this->groupCreate();
-    $this->_groupIDs = array();
     $this->_email = 'test@test.test';
     $this->_params = array(
-      'subject' => 'maild',
+      'subject' => 'Hello {contact.display_name}',
       'body_text' => "This is {contact.display_name}",
+      'body_html' => "<p>This is {contact.display_name}</p>",
       'name' => 'mailing name',
-      'created_id' => 1,
+      'created_id' => $this->_contactID,
     );
   }
 
-  function tearDown() {
-    foreach ($this->_contactIDs as $contactID) {
-      $this->contactDelete($contactID);
-    }
-    $this->groupDelete($this->_groupID);
-    foreach ($this->_groupIDs as $groupID) {
-      $this->groupDelete($groupID);
-    }
+  public function tearDown() {
+    CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW
+    parent::tearDown();
   }
 
   /**
@@ -86,35 +71,119 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $this->getAndCheck($this->_params, $result['id'], 'mailing');
   }
 
+  /**
+   * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]".
+   * Make sure these work
+   */
+  public function testMagicGroups_create_update() {
+    // 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']));
+    // END SAMPLE DATA
+
+    // ** Pass 1: Create
+    $createParams = $this->_params;
+    $createParams['groups']['include'] = array($groupIDs['a']);
+    $createParams['groups']['exclude'] = array();
+    $createParams['mailings']['include'] = array();
+    $createParams['mailings']['exclude'] = array();
+    $createResult = $this->callAPISuccess('Mailing', 'create', $createParams);
+    $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);
+
+    // ** Pass 2: Update without any changes to groups[include]
+    $nullopParams = $createParams;
+    $nullopParams['id'] = $createResult['id'];
+    $updateParams['api.mailing_job.create'] = 1;
+    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']));
+    $this->assertEquals(array($contactIDs['a']), $getRecip2_ids);
+
+    // ** Pass 3: Update with different groups[include]
+    $updateParams = $createParams;
+    $updateParams['id'] = $createResult['id'];
+    $updateParams['groups']['include'] = array($groupIDs['b']);
+    $updateParams['api.mailing_job.create'] = 1;
+    $this->callAPISuccess('Mailing', 'create', $updateParams);
+    $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);
+  }
+
   public function testMailerPreview() {
+    // BEGIN SAMPLE DATA
     $contactID =  $this->individualCreate();
     $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
     $displayName = $displayName['values'][$contactID]['display_name'];
+    $this->assertTrue(!empty($displayName));
 
-    $result = $this->callAPISuccess('mailing', 'create', $this->_params);
+    $params = $this->_params;
+    $params['api.Mailing.preview'] = array(
+      'id' => '$value.id',
+      'contact_id' => $contactID,
+    );
+    $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'),
+      'recip' => 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'
 
-    $params = array('id' => $result['id'], 'contact_id' => $contactID);
-    $result = $this->callAPISuccess('mailing', 'preview', $params);
-    $text = $result['values']['text'];
-    $this->assertEquals("This is $displayName", $text); // verify the text returned is correct, with replaced token
-    $this->deleteMailing($result['id']);
+    $previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
+    $this->assertEquals("Hello $displayName", $previewResult['values']['subject']);
+    $this->assertEquals("This is $displayName", $previewResult['values']['body_text']);
+    $this->assertContains("<p>This is $displayName</p>", $previewResult['values']['body_html']);
   }
 
   public function testMailerPreviewRecipients() {
     // BEGIN SAMPLE DATA
-    $this->groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
-    $this->groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
-    $this->contactIDs['includeme'] = $this->individualCreate(array('include.me@example.org'));
-    $this->contactIDs['excludeme'] = $this->individualCreate(array('exclude.me@example.org'));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs['inc'], 'contact_id' => $this->contactIDs['includeme']));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs['inc'], 'contact_id' => $this->contactIDs['excludeme']));
-    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs['exc'], 'contact_id' => $this->contactIDs['excludeme']));
+    $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']));
 
     $params = $this->_params;
-    $params['groups']['include'] = array($this->groupIDs['inc']);
-    $params['groups']['exclude'] = array($this->groupIDs['exc']);
+    $params['groups']['include'] = array($groupIDs['inc']);
+    $params['groups']['exclude'] = array($groupIDs['exc']);
     $params['mailings']['include'] = array();
     $params['mailings']['exclude'] = array();
+    $params['options']['force_rollback'] = 1;
+    $params['api.MailingRecipients.get'] = array(
+      'mailing_id' => '$value.id',
+      'api.contact.getvalue' => array(
+        'return' => 'display_name',
+      ),
+      'api.email.getvalue' => array(
+        'return' => 'email',
+      ),
+    );
     // END SAMPLE DATA
 
     $maxIDs =  array(
@@ -122,27 +191,119 @@ class api_v3_MailingTest extends CiviUnitTestCase {
       'job' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
       'group' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
     );
-    $preview = $this->callAPIAndDocument('Mailing', 'preview_recipients', $params, __FUNCTION__, __FILE__);
+    $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__, __FILE__);
     $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'
 
+    $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
     $previewIds = array_values(CRM_Utils_Array::collect('contact_id', $preview['values']));
-    $this->assertEquals(array((string)$this->contactIDs['includeme']), $previewIds);
+    $this->assertEquals(array((string)$contactIDs['includeme']), $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 testMailerSendTestMail() {
-    $contactID =  $this->individualCreate();
-    $result = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
-    $email = $result['values'][$contactID]['email'];
+  public function testMailerSendTest_email() {
+    $contactIDs['alice'] = $this->individualCreate(array('email' => 'alice@example.org', 'first_name' => 'Alice', 'last_name' => 'Person'));
 
     $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
 
-    $params = array('mailing_id' => $mail['id'], 'test_email' => $email, 'test_group' => NULL);
+    $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
     $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
     $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count
-    $this->assertEquals($contactID, $deliveredInfo['values'][$deliveredInfo['id']]['contact_id'], "in line " . __LINE__); //verify the contact_id of the recipient
-    $this->deleteMailing($mail['id']);
+
+    $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values']));
+    $this->assertEquals(array($contactIDs['alice']), $deliveredContacts);
+
+    $deliveredEmails = array_values(CRM_Utils_Array::collect('email', $deliveredInfo['values']));
+    $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']));
+    // END SAMPLE DATA
+
+    $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
+    $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', array(
+      'mailing_id' => $mail['id'],
+      'test_email' => NULL,
+      'test_group' => $groupIDs['inc'],
+    ));
+    $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count
+
+    $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values']));
+    $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts);
+
+    $deliveredEmails = array_values(CRM_Utils_Array::collect('email', $deliveredInfo['values']));
+    $this->assertEquals(array('alice@example.org', 'bob@example.org', 'carol@example.org'), $deliveredEmails);
+  }
+
+  public function submitProvider() {
+    $cases = array(); // $useLogin, $params, $expectedFailure, $expectedJobCount
+    $cases[] = array(
+      TRUE, //useLogin
+      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('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('scheduled_date' => '2014-12-13 10:00:00'),
+      FALSE, // expectedFailure
+      1, // expectedJobCount
+    );
+    $cases[] = array(
+      TRUE, //useLogin
+      array(),
+      "/Missing parameter scheduled_date and.or approval_date/", // expectedFailure
+      0, // expectedJobCount
+    );
+    return $cases;
+  }
+
+  /**
+   * @param bool $useLogin
+   * @param array $params
+   * @param null|string $expectedFailure
+   * @param int $expectedJobCount
+   * @dataProvider submitProvider
+   */
+  public function testMailerSubmit($useLogin, $params, $expectedFailure, $expectedJobCount) {
+    if ($useLogin) {
+      $this->createLoggedInUser();
+    }
+
+    $id = $this->createDraftMailing();
+
+    $params['id'] = $id;
+    if ($expectedFailure) {
+      $submitResult = $this->callAPIFailure('mailing', 'submit', $params);
+      $this->assertRegExp($expectedFailure, $submitResult['error_message']);
+    }
+    else {
+      $submitResult = $this->callAPIAndDocument('mailing', 'submit', $params, __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->assertDBQuery($expectedJobCount, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
+      1 => array($id, 'Integer')
+    ));
   }
 
   public function testMailerStats() {
@@ -192,7 +353,6 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
       'Unsubscribers' => 20
     );
     $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
-    $this->deleteMailing($mail['id']);
   }
   /**
    * Test civicrm_mailing_delete
@@ -292,6 +452,20 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
     );
   }
 
+  /**
+   * @return array|int
+   */
+  public function createDraftMailing() {
+    $createParams = $this->_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']));
+    $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
+      1 => array($createResult['id'], 'Integer')
+    ));
+    return $createResult['id'];
+  }
+
 //----------- civicrm_mailing_create ----------
 
 }