From: Tim Otten Date: Wed, 17 Dec 2014 01:41:06 +0000 (-0800) Subject: CRM-15578 - MailingAB API - Test recipients_update more thoroughly X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b0f9e1df1ae9b11bbcc32365895a1d2361eda844;p=civicrm-core.git CRM-15578 - MailingAB API - Test recipients_update more thoroughly --- diff --git a/api/v3/MailingAB.php b/api/v3/MailingAB.php index a761960c6a..88cebb28b0 100755 --- a/api/v3/MailingAB.php +++ b/api/v3/MailingAB.php @@ -97,9 +97,10 @@ function civicrm_api3_mailing_a_b_recipients_update($params) { //update recipients for mailing_id_c CRM_Mailing_BAO_Mailing::getRecipients($mailingAB['mailing_id_c'], $mailingAB['mailing_id_c'], NULL, NULL, TRUE); - //calulate total number of random recipients for mail C from group_percentage selected + //calculate total number of random recipients for mail C from group_percentage selected $totalCount = civicrm_api3('MailingRecipients', 'getcount', array('mailing_id' => $mailingAB['mailing_id_c'])); - $totalSelected = round(($totalCount * $mailingAB['group_percentage'])/100); + //we need at least 1 recipient for each test mailing, even if totalCount*group_percentage =~ 0 + $totalSelected = max(1, round(($totalCount * $mailingAB['group_percentage'])/100)); foreach (array('mailing_id_a', 'mailing_id_b') as $columnName) { CRM_Mailing_BAO_Recipients::updateRandomRecipients($mailingAB['mailing_id_c'], $mailingAB[$columnName], $totalSelected); @@ -235,4 +236,4 @@ function civicrm_api3_mailing_a_b_graph_stats($params) { } return civicrm_api3_create_success($graphStats); -} \ No newline at end of file +} diff --git a/tests/phpunit/api/v3/MailingABTest.php b/tests/phpunit/api/v3/MailingABTest.php index 1f2c20fb46..90d1d6a1cf 100755 --- a/tests/phpunit/api/v3/MailingABTest.php +++ b/tests/phpunit/api/v3/MailingABTest.php @@ -106,25 +106,46 @@ class api_v3_MailingABTest extends CiviUnitTestCase { )); } - public function testMailingABRecipientsUpdate() { - //create 100 contacts for group $this->_groupID - $totalGroupContacts = 100; + public function groupPctProvider() { + $cases = array(); // array(int $totalSize, int $groupPct, int $expectedCountA, $expectedCountB, $expectedCountC) + $cases[] = array(400, 7, 28, 28, 344); + $cases[] = array(100, 10, 10, 10, 80); + $cases[] = array(50, 20, 10, 10, 30); + $cases[] = array(50, 10, 5, 5, 40); + $cases[] = array(3, 10, 1, 1, 1); + $cases[] = array(2, 10, 1, 1, 0); + $cases[] = array(1, 10, 1, 0, 0); + return $cases; + } + /** + * @param $totalGroupContacts + * @param $groupPct + * @param $expectedCountA + * @param $expectedCountB + * @param $expectedCountC + * @dataProvider groupPctProvider + */ + public function testMailingABRecipientsUpdate($totalGroupContacts, $groupPct, $expectedCountA, $expectedCountB, $expectedCountC) { $result = $this->groupContactCreate($this->_groupID, $totalGroupContacts); - //check if 100 group contacts are included on desired group $this->assertEquals($totalGroupContacts, $result['added'], "in line " . __LINE__); - $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); - $totalSelectedContacts = round(($totalGroupContacts * $result['values'][$result['id']]['group_percentage'])/100); + $params = $this->_params; + $params['group_percentage'] = $groupPct; + $result = $this->callAPISuccess($this->_entity, 'create', $params); $params = array('id' => $result['id'], 'groups' => array('include' => array($this->_groupID))); $this->callAPISuccess('MailingAB', 'recipients_update', $params); //check total number of A/B mail recipients is what selected percentage of Mail C - $countA = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_A)); - $this->assertEquals($countA, $totalSelectedContacts, "in line " . __LINE__); - $countB = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_B)); - $this->assertEquals($countB, $totalSelectedContacts, "in line " . __LINE__); + $countA = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_A)); + $this->assertEquals($expectedCountA, $countA, "check mailing recipients A in line " . __LINE__); + + $countB = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_B)); + $this->assertEquals($expectedCountB, $countB, "check mailing recipients B in line " . __LINE__); + + $countC = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_C)); + $this->assertEquals($expectedCountC, $countC, "check mailing recipients C in line " . __LINE__); } }