From 7ada237624ee46da1a89cbbb2c51eb6f8ddb77a4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 21 Nov 2014 14:03:23 -0800 Subject: [PATCH] CRM-15578 - Mailing API - Add unit-test for send_test with group ID --- api/v3/Mailing.php | 5 ++++ tests/phpunit/api/v3/MailingTest.php | 34 +++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/api/v3/Mailing.php b/api/v3/Mailing.php index 3347891a40..91439eaa79 100755 --- a/api/v3/Mailing.php +++ b/api/v3/Mailing.php @@ -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" ); diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index 503311cf96..8ce4ddf0c8 100755 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -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 -- 2.25.1