ff7cb2e1faadad954158ad8b931dd0c526f5c732
[civicrm-core.git] / tests / phpunit / CRM / Activity / ActionMappingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 = [];
61
62 $cs[] = [
63 '2015-02-01 00:00:00',
64 'addAliceMeeting scheduleForAny startOnTime useHelloFirstName recipientIsActivitySource',
65 [
66 [
67 'time' => '2015-02-01 00:00:00',
68 'to' => ['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[] = [
89 '2015-02-01 00:00:00',
90 'addAliceMeeting addBobPhoneCall scheduleForMeeting startOnTime useHelloFirstName recipientIsActivitySource',
91 [
92 [
93 'time' => '2015-02-01 00:00:00',
94 'to' => ['alice@example.org'],
95 'subject' => '/Hello, Alice.*via subject/',
96 ],
97 ],
98 ];
99
100 $cs[] = [
101 '2015-02-01 00:00:00',
102 'addAliceMeeting addBobPhoneCall scheduleForAny startOnTime useHelloFirstName recipientIsActivitySource',
103 [
104 [
105 'time' => '2015-02-01 00:00:00',
106 'to' => ['alice@example.org'],
107 'subject' => '/Hello, Alice.*via subject/',
108 ],
109 [
110 'time' => '2015-02-01 00:00:00',
111 'to' => ['bob@example.org'],
112 'subject' => '/Hello, Bob.*via subject/',
113 ],
114 ],
115 ];
116
117 $cs[] = [
118 '2015-02-02 00:00:00',
119 'addAliceMeeting addBobPhoneCall scheduleForPhoneCall startWeekBefore repeatTwoWeeksAfter useHelloFirstName recipientIsActivitySource',
120 [
121 [
122 'time' => '2015-01-26 00:00:00',
123 'to' => ['bob@example.org'],
124 'subject' => '/Hello, Bob.*via subject/',
125 ],
126 [
127 'time' => '2015-02-02 00:00:00',
128 'to' => ['bob@example.org'],
129 'subject' => '/Hello, Bob.*via subject/',
130 ],
131 [
132 'time' => '2015-02-09 00:00:00',
133 'to' => ['bob@example.org'],
134 'subject' => '/Hello, Bob.*via subject/',
135 ],
136 [
137 'time' => '2015-02-16 00:00:00',
138 'to' => ['bob@example.org'],
139 'subject' => '/Hello, Bob.*via subject/',
140 ],
141 ],
142 ];
143
144 // No recipients: Dave has `do_not_email` and Edith is dead.
145 $cs[] = [
146 '2015-02-01 00:00:00',
147 'addDaveMeeting addEdithMeeting scheduleForMeeting startOnTime useHelloFirstName recipientIsActivitySource',
148 [],
149 ];
150
151 return $cs;
152 }
153
154 /**
155 * Create an activity record for Alice with type "Meeting".
156 */
157 public function addAliceMeeting() {
158 $this->callAPISuccess('Activity', 'create', [
159 'source_contact_id' => $this->contacts['alice']['id'],
160 'activity_type_id' => 'Meeting',
161 'subject' => 'Subject for Alice',
162 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
163 'status_id' => 2,
164 'assignee_contact_id' => [$this->contacts['carol']['id']],
165 ]);
166 }
167
168 /**
169 * Create a contribution record for Bob with type "Donation".
170 */
171 public function addBobPhoneCall() {
172 $this->callAPISuccess('Activity', 'create', [
173 'source_contact_id' => $this->contacts['bob']['id'],
174 'activity_type_id' => 'Phone Call',
175 'subject' => 'Subject for Bob',
176 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
177 'status_id' => 2,
178 'assignee_contact_id' => [$this->contacts['carol']['id']],
179 ]);
180 }
181
182 /**
183 * Create an activity record for Dave with type "Meeting". Dave has
184 * "do_not_email" set, so he should never receive an email reminder.
185 */
186 public function addDaveMeeting() {
187 $this->callAPISuccess('Activity', 'create', [
188 'source_contact_id' => $this->contacts['dave']['id'],
189 'activity_type_id' => 'Meeting',
190 'subject' => 'Subject for Dave',
191 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
192 'status_id' => 2,
193 'assignee_contact_id' => [$this->contacts['carol']['id']],
194 ]);
195 }
196
197 /**
198 * Create an activity record for Edith with type "Meeting". Edith is dead, so
199 * she should never receive an email reminder.
200 */
201 public function addEdithMeeting() {
202 $this->callAPISuccess('Activity', 'create', [
203 'source_contact_id' => $this->contacts['edith']['id'],
204 'activity_type_id' => 'Meeting',
205 'subject' => 'Subject for Edith',
206 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
207 'status_id' => 2,
208 'assignee_contact_id' => [$this->contacts['carol']['id']],
209 ]);
210 }
211
212 /**
213 * Schedule message delivery for activities of type "Meeting".
214 */
215 public function scheduleForMeeting() {
216 $actTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id');
217 $this->schedule->mapping_id = CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID;
218 $this->schedule->start_action_date = 'receive_date';
219 $this->schedule->entity_value = CRM_Utils_Array::implodePadded([array_search('Meeting', $actTypes)]);
220 $this->schedule->entity_status = CRM_Utils_Array::implodePadded([2]);
221 }
222
223 /**
224 * Schedule message delivery for activities of type "Phone Call".
225 */
226 public function scheduleForPhoneCall() {
227 $actTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id');
228 $this->schedule->mapping_id = CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID;
229 $this->schedule->start_action_date = 'receive_date';
230 $this->schedule->entity_value = CRM_Utils_Array::implodePadded([array_search('Phone Call', $actTypes)]);
231 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(NULL);
232 }
233
234 /**
235 * Schedule message delivery for any contribution, regardless of type.
236 */
237 public function scheduleForAny() {
238 $actTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id');
239 $this->schedule->mapping_id = CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID;
240 $this->schedule->start_action_date = 'receive_date';
241 $this->schedule->entity_value = CRM_Utils_Array::implodePadded(array_keys($actTypes));
242 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(NULL);
243 }
244
245 /**
246 * Set the recipient to "Choose Recipient(s): Bob".
247 */
248 public function recipientIsBob() {
249 $this->schedule->limit_to = 1;
250 $this->schedule->recipient = NULL;
251 $this->schedule->recipient_listing = NULL;
252 $this->schedule->recipient_manual = $this->contacts['bob']['id'];
253 }
254
255 /**
256 * Set the recipient to "Activity Assignee".
257 */
258 public function recipientIsActivityAssignee() {
259 $this->schedule->limit_to = 1;
260 $this->schedule->recipient = 1;
261 $this->schedule->recipient_listing = NULL;
262 $this->schedule->recipient_manual = NULL;
263 }
264
265 /**
266 * Set the recipient to "Activity Source".
267 */
268 public function recipientIsActivitySource() {
269 $this->schedule->limit_to = 1;
270 $this->schedule->recipient = 2;
271 $this->schedule->recipient_listing = NULL;
272 $this->schedule->recipient_manual = NULL;
273 }
274
275 }