From 6ff6f4f22f47c1e3560e092d9813269513484453 Mon Sep 17 00:00:00 2001 From: JKingsnorth Date: Tue, 27 Feb 2018 12:22:15 +0000 Subject: [PATCH] CRM-21739 [NFC] Refactoring for SMS recipient test --- tests/phpunit/CRM/Mailing/BAO/MailingTest.php | 95 +++++++++---------- 1 file changed, 44 insertions(+), 51 deletions(-) diff --git a/tests/phpunit/CRM/Mailing/BAO/MailingTest.php b/tests/phpunit/CRM/Mailing/BAO/MailingTest.php index 63c12748b4..c58d21a4eb 100644 --- a/tests/phpunit/CRM/Mailing/BAO/MailingTest.php +++ b/tests/phpunit/CRM/Mailing/BAO/MailingTest.php @@ -34,6 +34,48 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase { parent::setUp(); } + /** + * Helper function to assert whether the calculated recipients of a mailing + * match the expected list + * + * @param $mailingID + * @param $expectedRecipients array + * Array of contact ID that should be in the recipient list. + */ + private function assertRecipientsCorrect($mailingID, $expectedRecipients) { + + // Reset keys to ensure match + $expectedRecipients = array_values($expectedRecipients); + + // Load the recipients as a list of contact IDs + CRM_Mailing_BAO_Mailing::getRecipients($mailingID); + $recipients = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $mailingID)); + $contactIDs = array(); + foreach ($recipients['values'] as $recipient) { + $contactIDs[] = $recipient['contact_id']; + } + + // Check the lists match + $this->assertTreeEquals($expectedRecipients, $contactIDs); + } + + /** + * Helper function to create a mailing include/exclude group. + * + * @param $mailingID + * @param $groupID + * @param string $type + * @return array|int + */ + private function createMailingGroup($mailingID, $groupID, $type = 'Include') { + return $this->callAPISuccess('MailingGroup', 'create', array( + 'mailing_id' => $mailingID, + 'group_type' => $type, + 'entity_table' => "civicrm_group", + 'entity_id' => $groupID, + )); + } + /** * @todo Missing tests: * - Ensure opt out emails are not mailed @@ -159,48 +201,6 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase { } - /** - * Helper function to assert whether the calculated recipients of a mailing - * match the expected list - * - * @param $mailingID - * @param $expectedRecipients array - * Array of contact ID that should be in the recipient list. - */ - private function assertRecipientsCorrect($mailingID, $expectedRecipients) { - - // Reset keys to ensure match - $expectedRecipients = array_values($expectedRecipients); - - // Load the recipients as a list of contact IDs - CRM_Mailing_BAO_Mailing::getRecipients($mailingID); - $recipients = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $mailingID)); - $contactIDs = array(); - foreach ($recipients['values'] as $recipient) { - $contactIDs[] = $recipient['contact_id']; - } - - // Check the lists match - $this->assertTreeEquals($expectedRecipients, $contactIDs); - } - - /** - * Helper function to create a mailing include/exclude group. - * - * @param $mailingID - * @param $groupID - * @param string $type - * @return array|int - */ - private function createMailingGroup($mailingID, $groupID, $type = 'Include') { - return $this->callAPISuccess('MailingGroup', 'create', array( - 'mailing_id' => $mailingID, - 'group_type' => $type, - 'entity_table' => "civicrm_group", - 'entity_id' => $groupID, - )); - } - /** * Test CRM_Mailing_BAO_Mailing::getRecipients() on sms mode */ @@ -281,17 +281,10 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase { // Create mailing $mailing = $this->callAPISuccess('Mailing', 'create', array('sms_provider_id' => $sms_provider['id'])); - $mailing_include_group = $this->callAPISuccess('MailingGroup', 'create', array( - 'mailing_id' => $mailing['id'], - 'group_type' => "Include", - 'entity_table' => "civicrm_group", - 'entity_id' => $group, - )); - - // Populate the recipients table (job id doesn't matter) - CRM_Mailing_BAO_Mailing::getRecipients($mailing['id']); + $mailingInclude = $this->createMailingGroup($mailing['id'], $group); // Get recipients + CRM_Mailing_BAO_Mailing::getRecipients($mailing['id']); $recipients = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $mailing['id'])); // Check the count is correct -- 2.25.1