Fix primary mobile number test for CRM-21320
[civicrm-core.git] / tests / phpunit / CRM / Mailing / BAO / MailingTest.php
CommitLineData
9f0a25d7
J
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 * Class CRM_Mailing_BAO_MailingTest
30 */
31class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
32
33 public function setUp() {
34 parent::setUp();
35 }
36
251a8849
J
37 /**
38 * Things to test:
39 * - include static group(s)
40 * - include smart group(s)
41 * - exclude static group(s)
42 * - exclude smart group(s)
43 * - include previous recipients(ss)
44 * - exclude previous recipients(ss)
45 * Repeat for SMS.
46 */
47
9f0a25d7 48 /**
6f3a35e0 49 * Test CRM_Mailing_BAO_Mailing::getRecipients() on sms mode
9f0a25d7
J
50 */
51 public function testgetRecipients() {
9f0a25d7
J
52 // Tests for SMS bulk mailing recipients
53 // +CRM-21320 Ensure primary mobile number is selected over non-primary
54
55 // Setup
6f3a35e0 56 $smartGroupParams = array(
57 'formValues' => array('contact_type' => array('IN' => array('Individual'))),
58 );
59 $group = $this->smartGroupCreate($smartGroupParams);
9f0a25d7
J
60 $sms_provider = $this->callAPISuccess('SmsProvider', 'create', array(
61 'sequential' => 1,
62 'name' => 1,
63 'title' => "Test",
64 'username' => "Test",
65 'password' => "Test",
66 'api_type' => 1,
67 'is_active' => 1,
68 ));
69
6f3a35e0 70 // Create Contact 1 and add in group
71 $contactID1 = $this->individualCreate(array(), 0);
9f0a25d7
J
72 $this->callAPISuccess('GroupContact', 'Create', array(
73 'group_id' => $group,
6f3a35e0 74 'contact_id' => $contactID1,
9f0a25d7
J
75 ));
76
6f3a35e0 77 // Create contact 2 and add in group
78 $contactID2 = $this->individualCreate(array(), 1);
9f0a25d7
J
79 $this->callAPISuccess('GroupContact', 'Create', array(
80 'group_id' => $group,
6f3a35e0 81 'contact_id' => $contactID2,
9f0a25d7
J
82 ));
83
6f3a35e0 84 $contactIDPhoneRecords = array(
85 $contactID1 => array(
86 'primary_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
87 'contact_id' => $contactID1,
88 'phone' => "01 01",
89 'location_type_id' => "Home",
90 'phone_type_id' => "Mobile",
91 'is_primary' => 1,
92 ))),
93 'other_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
94 'contact_id' => $contactID1,
95 'phone' => "01 02",
96 'location_type_id' => "Work",
97 'phone_type_id' => "Mobile",
98 'is_primary' => 0,
99 ))),
100 ),
251a8849 101 // Create the non-primary with a lower ID than the primary, to test CRM-21320
6f3a35e0 102 $contactID2 => array(
251a8849 103 'other_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
6f3a35e0 104 'contact_id' => $contactID2,
105 'phone' => "02 01",
106 'location_type_id' => "Home",
107 'phone_type_id' => "Mobile",
251a8849 108 'is_primary' => 0,
6f3a35e0 109 ))),
251a8849 110 'primary_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
6f3a35e0 111 'contact_id' => $contactID2,
112 'phone' => "02 02",
113 'location_type_id' => "Work",
114 'phone_type_id' => "Mobile",
251a8849 115 'is_primary' => 1,
6f3a35e0 116 ))),
117 ),
118 );
119
9f0a25d7 120 // Prepare expected results
6f3a35e0 121 $checkPhoneIDs = array(
122 $contactID1 => $contactIDPhoneRecords[$contactID1]['primary_phone_id'],
123 $contactID2 => $contactIDPhoneRecords[$contactID2]['primary_phone_id'],
9f0a25d7
J
124 );
125
126 // Create mailing
127 $mailing = $this->callAPISuccess('Mailing', 'create', array('sms_provider_id' => $sms_provider['id']));
128 $mailing_include_group = $this->callAPISuccess('MailingGroup', 'create', array(
129 'mailing_id' => $mailing['id'],
130 'group_type' => "Include",
131 'entity_table' => "civicrm_group",
132 'entity_id' => $group,
133 ));
134
135 // Populate the recipients table (job id doesn't matter)
6f3a35e0 136 CRM_Mailing_BAO_Mailing::getRecipients($mailing['id']);
9f0a25d7
J
137
138 // Get recipients
139 $recipients = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $mailing['id']));
140
141 // Check the count is correct
142 $this->assertEquals(2, $recipients['count'], 'Check recipient count');
143
144 // Check we got the 'primary' mobile for both contacts
145 foreach ($recipients['values'] as $value) {
6f3a35e0 146 $this->assertEquals($value['phone_id'], $checkPhoneIDs[$value['contact_id']], 'Check correct phone number for contact ' . $value['contact_id']);
9f0a25d7
J
147 }
148
149 // Tidy up
150 $this->deleteMailing($mailing['id']);
151 $this->callAPISuccess('SmsProvider', 'Delete', array('id' => $sms_provider['id']));
152 $this->groupDelete($group);
6f3a35e0 153 $this->contactDelete($contactID1);
154 $this->contactDelete($contactID2);
9f0a25d7
J
155 }
156
157}