additional fixes and optimisation
[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
37 /**
6f3a35e0 38 * Test CRM_Mailing_BAO_Mailing::getRecipients() on sms mode
9f0a25d7
J
39 */
40 public function testgetRecipients() {
9f0a25d7
J
41 // Tests for SMS bulk mailing recipients
42 // +CRM-21320 Ensure primary mobile number is selected over non-primary
43
44 // Setup
6f3a35e0 45 $smartGroupParams = array(
46 'formValues' => array('contact_type' => array('IN' => array('Individual'))),
47 );
48 $group = $this->smartGroupCreate($smartGroupParams);
9f0a25d7
J
49 $sms_provider = $this->callAPISuccess('SmsProvider', 'create', array(
50 'sequential' => 1,
51 'name' => 1,
52 'title' => "Test",
53 'username' => "Test",
54 'password' => "Test",
55 'api_type' => 1,
56 'is_active' => 1,
57 ));
58
6f3a35e0 59 // Create Contact 1 and add in group
60 $contactID1 = $this->individualCreate(array(), 0);
9f0a25d7
J
61 $this->callAPISuccess('GroupContact', 'Create', array(
62 'group_id' => $group,
6f3a35e0 63 'contact_id' => $contactID1,
9f0a25d7
J
64 ));
65
6f3a35e0 66 // Create contact 2 and add in group
67 $contactID2 = $this->individualCreate(array(), 1);
9f0a25d7
J
68 $this->callAPISuccess('GroupContact', 'Create', array(
69 'group_id' => $group,
6f3a35e0 70 'contact_id' => $contactID2,
9f0a25d7
J
71 ));
72
6f3a35e0 73 $contactIDPhoneRecords = array(
74 $contactID1 => array(
75 'primary_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
76 'contact_id' => $contactID1,
77 'phone' => "01 01",
78 'location_type_id' => "Home",
79 'phone_type_id' => "Mobile",
80 'is_primary' => 1,
81 ))),
82 'other_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
83 'contact_id' => $contactID1,
84 'phone' => "01 02",
85 'location_type_id' => "Work",
86 'phone_type_id' => "Mobile",
87 'is_primary' => 0,
88 ))),
89 ),
90 $contactID2 => array(
91 'primary_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
92 'contact_id' => $contactID2,
93 'phone' => "02 01",
94 'location_type_id' => "Home",
95 'phone_type_id' => "Mobile",
96 'is_primary' => 1,
97 ))),
98 'other_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
99 'contact_id' => $contactID2,
100 'phone' => "02 02",
101 'location_type_id' => "Work",
102 'phone_type_id' => "Mobile",
103 'is_primary' => 0,
104 ))),
105 ),
106 );
107
9f0a25d7 108 // Prepare expected results
6f3a35e0 109 $checkPhoneIDs = array(
110 $contactID1 => $contactIDPhoneRecords[$contactID1]['primary_phone_id'],
111 $contactID2 => $contactIDPhoneRecords[$contactID2]['primary_phone_id'],
9f0a25d7
J
112 );
113
114 // Create mailing
115 $mailing = $this->callAPISuccess('Mailing', 'create', array('sms_provider_id' => $sms_provider['id']));
116 $mailing_include_group = $this->callAPISuccess('MailingGroup', 'create', array(
117 'mailing_id' => $mailing['id'],
118 'group_type' => "Include",
119 'entity_table' => "civicrm_group",
120 'entity_id' => $group,
121 ));
122
123 // Populate the recipients table (job id doesn't matter)
6f3a35e0 124 CRM_Mailing_BAO_Mailing::getRecipients($mailing['id']);
9f0a25d7
J
125
126 // Get recipients
127 $recipients = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $mailing['id']));
128
129 // Check the count is correct
130 $this->assertEquals(2, $recipients['count'], 'Check recipient count');
131
132 // Check we got the 'primary' mobile for both contacts
133 foreach ($recipients['values'] as $value) {
6f3a35e0 134 $this->assertEquals($value['phone_id'], $checkPhoneIDs[$value['contact_id']], 'Check correct phone number for contact ' . $value['contact_id']);
9f0a25d7
J
135 }
136
137 // Tidy up
138 $this->deleteMailing($mailing['id']);
139 $this->callAPISuccess('SmsProvider', 'Delete', array('id' => $sms_provider['id']));
140 $this->groupDelete($group);
6f3a35e0 141 $this->contactDelete($contactID1);
142 $this->contactDelete($contactID2);
9f0a25d7
J
143 }
144
145}