CRM-15578 - Mailing API - Add unit-test for send_test with group ID
authorTim Otten <totten@civicrm.org>
Fri, 21 Nov 2014 22:03:23 +0000 (14:03 -0800)
committerTim Otten <totten@civicrm.org>
Fri, 21 Nov 2014 22:03:23 +0000 (14:03 -0800)
api/v3/Mailing.php
tests/phpunit/api/v3/MailingTest.php

index 3347891a4050ceea1576ee4a331601b1029910a3..91439eaa794ec1663c6a4093849fcf5a3f5086a9 100755 (executable)
@@ -354,6 +354,11 @@ function civicrm_api3_mailing_preview($params) {
   ));
 }
 
+function _civicrm_api3_mailing_send_test_spec(&$spec) {
+  $spec['test_group']['title'] = 'Test Group ID';
+  $spec['test_email']['title'] = 'Test Email Address';
+}
+
 function civicrm_api3_mailing_send_test($params) {
   if (!array_key_exists('test_group', $params) && !array_key_exists('test_email', $params)) {
     throw new API_Exception("Mandatory key(s) missing from params array: test_group and/or test_email field are required" );
index 503311cf9680eded8fe8f06521c83a1bf2411c72..8ce4ddf0c87a3447d83ac82ff27d30cb087eec4c 100755 (executable)
@@ -53,6 +53,7 @@ class api_v3_MailingTest extends CiviUnitTestCase {
 
   function setUp() {
     parent::setUp();
+    $this->useTransaction();
     CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW
     $this->_contactIDs = array();
     $this->_contactIDs[] = $this->individualCreate();
@@ -69,13 +70,6 @@ class api_v3_MailingTest extends CiviUnitTestCase {
   }
 
   function tearDown() {
-    foreach ($this->_contactIDs as $contactID) {
-      $this->contactDelete($contactID);
-    }
-    $this->groupDelete($this->_groupID);
-    foreach ($this->_groupIDs as $groupID) {
-      $this->groupDelete($groupID);
-    }
     CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW
     parent::tearDown();
   }
@@ -170,7 +164,7 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $this->assertTrue((bool)preg_match('/Includer Person/', $previewNames[0]), "Name 'Includer Person' should appear in '" . $previewNames[0] . '"');
   }
 
-  public function testMailerSendTestMail() {
+  public function testMailerSendTest_email() {
     $contactID =  $this->individualCreate();
     $result = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
     $email = $result['values'][$contactID]['email'];
@@ -184,6 +178,30 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $this->deleteMailing($mail['id']);
   }
 
+  public function testMailerSendTest_group() {
+    // BEGIN SAMPLE DATA
+    $this->groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
+    $this->contactIDs['alice'] = $this->individualCreate(array('email' => 'alice@example.org', 'first_name' => 'Alice', 'last_name' => 'Person'));
+    $this->contactIDs['bob'] = $this->individualCreate(array('email' => 'bob@example.org', 'first_name' => 'Bob', 'last_name' => 'Person'));
+    $this->contactIDs['carol'] = $this->individualCreate(array('email' => 'carol@example.org', 'first_name' => 'Carol', 'last_name' => 'Person'));
+    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs['inc'], 'contact_id' => $this->contactIDs['alice']));
+    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs['inc'], 'contact_id' => $this->contactIDs['bob']));
+    $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs['inc'], 'contact_id' => $this->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' => $this->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($this->contactIDs['alice'], $this->contactIDs['bob'], $this->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 testMailerStats() {
     $result = $this->groupContactCreate($this->_groupID, 100);
     $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group