Update Copywrite year to be 2019
[civicrm-core.git] / tests / phpunit / CRM / Activity / ActionMappingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class CRM_Activity_ActionMappingTest
32 * @group ActionSchedule
33 *
34 * This class tests various configurations of scheduled-reminders, with a focus on
35 * reminders for *activity types*. It follows a design/pattern described in
36 * AbstractMappingTest.
37 *
38 * NOTE: There are also pretty deep tests of activity-based reminders in
39 * CRM_Core_BAO_ActionScheduleTest.
40 *
41 * @see \Civi\ActionSchedule\AbstractMappingTest
42 * @see CRM_Core_BAO_ActionScheduleTest
43 */
44 class CRM_Activity_ActionMappingTest extends \Civi\ActionSchedule\AbstractMappingTest {
45
46 /**
47 * Generate a list of test cases, where each is a distinct combination of
48 * data, schedule-rules, and schedule results.
49 *
50 * @return array
51 * - targetDate: string; eg "2015-02-01 00:00:01"
52 * - setupFuncs: string, space-separated list of setup functions
53 * - messages: array; each item is a message that's expected to be sent
54 * each message may include keys:
55 * - time: approximate time (give or take a few seconds)
56 * - recipients: array of emails
57 * - subject: regex
58 */
59 public function createTestCases() {
60 $cs = array();
61
62 $cs[] = array(
63 '2015-02-01 00:00:00',
64 'addAliceMeeting scheduleForAny startOnTime useHelloFirstName recipientIsActivitySource',
65 array(
66 array(
67 'time' => '2015-02-01 00:00:00',
68 'to' => array('alice@example.org'),
69 'subject' => '/Hello, Alice.*via subject/',
70 ),
71 ),
72 );
73
74 // FIXME: CRM-19415: This test should pass...
75 // $cs[] = array(
76 // '2015-02-01 00:00:00',
77 // 'addAliceMeeting scheduleForAny startOnTime useHelloFirstName recipientIsBob',
78 // array(
79 // array(
80 // 'time' => '2015-02-01 00:00:00',
81 // 'to' => array('bob@example.org'),
82 // 'subject' => '/Hello, Bob.*via subject/',
83 // // It might make more sense to get Alice's details... but path of least resistance...
84 // ),
85 // ),
86 // );
87
88 $cs[] = array(
89 '2015-02-01 00:00:00',
90 'addAliceMeeting addBobPhoneCall scheduleForMeeting startOnTime useHelloFirstName recipientIsActivitySource',
91 array(
92 array(
93 'time' => '2015-02-01 00:00:00',
94 'to' => array('alice@example.org'),
95 'subject' => '/Hello, Alice.*via subject/',
96 ),
97 ),
98 );
99
100 $cs[] = array(
101 '2015-02-01 00:00:00',
102 'addAliceMeeting addBobPhoneCall scheduleForAny startOnTime useHelloFirstName recipientIsActivitySource',
103 array(
104 array(
105 'time' => '2015-02-01 00:00:00',
106 'to' => array('alice@example.org'),
107 'subject' => '/Hello, Alice.*via subject/',
108 ),
109 array(
110 'time' => '2015-02-01 00:00:00',
111 'to' => array('bob@example.org'),
112 'subject' => '/Hello, Bob.*via subject/',
113 ),
114 ),
115 );
116
117 $cs[] = array(
118 '2015-02-02 00:00:00',
119 'addAliceMeeting addBobPhoneCall scheduleForPhoneCall startWeekBefore repeatTwoWeeksAfter useHelloFirstName recipientIsActivitySource',
120 array(
121 array(
122 'time' => '2015-01-26 00:00:00',
123 'to' => array('bob@example.org'),
124 'subject' => '/Hello, Bob.*via subject/',
125 ),
126 array(
127 'time' => '2015-02-02 00:00:00',
128 'to' => array('bob@example.org'),
129 'subject' => '/Hello, Bob.*via subject/',
130 ),
131 array(
132 'time' => '2015-02-09 00:00:00',
133 'to' => array('bob@example.org'),
134 'subject' => '/Hello, Bob.*via subject/',
135 ),
136 array(
137 'time' => '2015-02-16 00:00:00',
138 'to' => array('bob@example.org'),
139 'subject' => '/Hello, Bob.*via subject/',
140 ),
141 ),
142 );
143
144 return $cs;
145 }
146
147 /**
148 * Create an activity record for Alice with type "Meeting".
149 */
150 public function addAliceMeeting() {
151 $this->callAPISuccess('Activity', 'create', array(
152 'source_contact_id' => $this->contacts['alice']['id'],
153 'activity_type_id' => 'Meeting',
154 'subject' => 'Subject for Alice',
155 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
156 'status_id' => 2,
157 'assignee_contact_id' => array($this->contacts['carol']['id']),
158 ));
159 }
160
161 /**
162 * Create a contribution record for Bob with type "Donation".
163 */
164 public function addBobPhoneCall() {
165 $this->callAPISuccess('Activity', 'create', array(
166 'source_contact_id' => $this->contacts['bob']['id'],
167 'activity_type_id' => 'Phone Call',
168 'subject' => 'Subject for Bob',
169 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
170 'status_id' => 2,
171 'assignee_contact_id' => array($this->contacts['carol']['id']),
172 ));
173 }
174
175 /**
176 * Schedule message delivery for activities of type "Meeting".
177 */
178 public function scheduleForMeeting() {
179 $actTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id');
180 $this->schedule->mapping_id = CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID;
181 $this->schedule->start_action_date = 'receive_date';
182 $this->schedule->entity_value = CRM_Utils_Array::implodePadded(array(array_search('Meeting', $actTypes)));
183 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(array(2));
184 }
185
186 /**
187 * Schedule message delivery for activities of type "Phone Call".
188 */
189 public function scheduleForPhoneCall() {
190 $actTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id');
191 $this->schedule->mapping_id = CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID;
192 $this->schedule->start_action_date = 'receive_date';
193 $this->schedule->entity_value = CRM_Utils_Array::implodePadded(array(array_search('Phone Call', $actTypes)));
194 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(NULL);
195 }
196
197 /**
198 * Schedule message delivery for any contribution, regardless of type.
199 */
200 public function scheduleForAny() {
201 $actTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id');
202 $this->schedule->mapping_id = CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID;
203 $this->schedule->start_action_date = 'receive_date';
204 $this->schedule->entity_value = CRM_Utils_Array::implodePadded(array_keys($actTypes));
205 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(NULL);
206 }
207
208 /**
209 * Set the recipient to "Choose Recipient(s): Bob".
210 */
211 public function recipientIsBob() {
212 $this->schedule->limit_to = 1;
213 $this->schedule->recipient = NULL;
214 $this->schedule->recipient_listing = NULL;
215 $this->schedule->recipient_manual = $this->contacts['bob']['id'];
216 }
217
218 /**
219 * Set the recipient to "Activity Assignee".
220 */
221 public function recipientIsActivityAssignee() {
222 $this->schedule->limit_to = 1;
223 $this->schedule->recipient = 1;
224 $this->schedule->recipient_listing = NULL;
225 $this->schedule->recipient_manual = NULL;
226 }
227
228 /**
229 * Set the recipient to "Activity Source".
230 */
231 public function recipientIsActivitySource() {
232 $this->schedule->limit_to = 1;
233 $this->schedule->recipient = 2;
234 $this->schedule->recipient_listing = NULL;
235 $this->schedule->recipient_manual = NULL;
236 }
237
238 }