25b72113 |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 4.5 | |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
25b72113 |
28 | require_once 'CiviTest/CiviUnitTestCase.php'; |
060402a9 |
29 | require_once 'CRM/Core/BAO/RecurringEntity.php'; |
25b72113 |
30 | |
31 | /** |
060402a9 |
32 | * Class CRM_Core_BAO_RecurringEntityTest |
25b72113 |
33 | */ |
060402a9 |
34 | class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase { |
25b72113 |
35 | /** |
36 | * @return array |
37 | */ |
38 | function get_info() { |
39 | return array( |
40 | 'name' => 'Recurring Entity BAOs', |
41 | 'description' => 'Test all CRM_Event_BAO_RecurringEntity methods.', |
42 | 'group' => 'CiviCRM BAO Tests', |
43 | ); |
44 | } |
45 | |
6caeea75 |
46 | /** |
47 | * Sets up the fixture, for example, opens a network connection. |
48 | * This method is called before a test is executed. |
49 | * |
50 | * @access protected |
51 | */ |
52 | protected function setUp() { |
25b72113 |
53 | parent::setUp(); |
25b72113 |
54 | } |
6caeea75 |
55 | |
56 | /** |
57 | * Tears down the fixture, for example, closes a network connection. |
58 | * This method is called after a test is executed. |
59 | * |
60 | * @access protected |
61 | */ |
62 | protected function tearDown() {} |
25b72113 |
63 | |
64 | /** |
6caeea75 |
65 | * Testing Activity Generation through Entity Recursion |
25b72113 |
66 | */ |
6caeea75 |
67 | function testActivityGeneration() { |
206c0c43 |
68 | //Activity set initial params |
37b28176 |
69 | $daoActivity = new CRM_Activity_DAO_Activity(); |
70 | $daoActivity->activity_type_id = 1; |
71 | $daoActivity->subject = "Initial Activity"; |
f5f5a631 |
72 | $daoActivity->activity_date_time = '20141002103000'; |
37b28176 |
73 | $daoActivity->save(); |
25b72113 |
74 | |
37b28176 |
75 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
690bf076 |
76 | $recursion->entity_id = $daoActivity->id; |
77 | $recursion->entity_table = 'civicrm_activity'; |
78 | $recursion->dateColumns = array('activity_date_time'); |
84f02991 |
79 | $recursion->schedule = array( |
72e95527 |
80 | 'entity_value' => $daoActivity->id, |
a1a821bc |
81 | 'start_action_date' => $daoActivity->activity_date_time, |
82 | 'entity_status' => 'fourth saturday', |
37b28176 |
83 | 'repetition_frequency_unit' => 'month', |
84 | 'repetition_frequency_interval' => 3, |
85 | 'start_action_offset' => 5, |
206c0c43 |
86 | 'used_for' => 'activity' |
690bf076 |
87 | ); |
206c0c43 |
88 | |
37b28176 |
89 | $generatedEntities = $recursion->generate(); |
f5f5a631 |
90 | $this->assertEquals(5, count($generatedEntities['civicrm_activity']), "Cehck if number of iterations are 5"); |
91 | $expectedDates = array( |
92 | '20141025103000', |
93 | '20141227103000', |
94 | '20150328103000', |
95 | '20150627103000', |
96 | '20150926103000' |
97 | ); |
84f02991 |
98 | foreach ($generatedEntities['civicrm_activity'] as $entityID) { |
37b28176 |
99 | $this->assertDBNotNull('CRM_Activity_DAO_Activity', $entityID, 'id', |
100 | 'id', 'Check DB if repeating activities were created' |
101 | ); |
25b72113 |
102 | } |
25b72113 |
103 | |
206c0c43 |
104 | // set mode to ALL, i.e any change to changing activity affects all related recurring activities |
105 | $recursion->mode(3); |
37b28176 |
106 | |
107 | // lets change subject of initial activity that we created in begining |
25b72113 |
108 | $daoActivity->find(TRUE); |
690bf076 |
109 | $daoActivity->subject = 'Changed Activity'; |
25b72113 |
110 | $daoActivity->save(); |
25b72113 |
111 | |
37b28176 |
112 | // check if other activities were affected |
f5f5a631 |
113 | $actualDates = array(); |
84f02991 |
114 | foreach ($generatedEntities['civicrm_activity'] as $entityID) { |
690bf076 |
115 | $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $entityID, 'subject', 'id', 'Changed Activity', 'Check if subject was updated'); |
f5f5a631 |
116 | $actualDates[] = date('YmdHis', strtotime(CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $entityID, 'activity_date_time', 'id'))); |
6caeea75 |
117 | } |
f5f5a631 |
118 | $resultDates = array_diff($actualDates, $expectedDates); |
119 | $this->assertEquals(0, count($resultDates), "Check if all the value in expected array matches actual array"); |
120 | |
6caeea75 |
121 | } |
122 | |
690bf076 |
123 | /** |
124 | * Testing Event Generation through Entity Recursion |
125 | */ |
126 | function testEventGeneration() { |
2eac52b6 |
127 | //Event set initial params |
690bf076 |
128 | $daoEvent = new CRM_Event_DAO_Event(); |
129 | $daoEvent->title = 'Test event for Recurring Entity'; |
130 | $daoEvent->event_type_id = 3; |
131 | $daoEvent->is_public = 1; |
f5f5a631 |
132 | $daoEvent->start_date = date('YmdHis', strtotime('2014-10-26 10:30:00')); |
133 | $daoEvent->end_date = date('YmdHis', strtotime('2014-10-28 10:30:00')); |
690bf076 |
134 | $daoEvent->created_date = date('YmdHis'); |
135 | $daoEvent->is_active = 1; |
136 | $daoEvent->save(); |
cd3f28a7 |
137 | $this->assertDBNotNull('CRM_Event_DAO_Event', $daoEvent->id, 'id', 'id', 'Check DB if event was created'); |
138 | |
cd3f28a7 |
139 | //Create tell a friend for event |
140 | $daoTellAFriend = new CRM_Friend_DAO_Friend(); |
141 | $daoTellAFriend->entity_table = 'civicrm_event'; |
fb2120e7 |
142 | $daoTellAFriend->entity_id = $daoEvent->id; // join with event |
cd3f28a7 |
143 | $daoTellAFriend->title = 'Testing tell a friend'; |
144 | $daoTellAFriend->is_active = 1; |
145 | $daoTellAFriend->save(); |
f5f5a631 |
146 | $this->assertDBNotNull('CRM_Friend_DAO_Friend', $daoTellAFriend->id, 'id', 'id', 'Check DB if tell a friend was created'); |
690bf076 |
147 | |
fb2120e7 |
148 | // time to use recursion |
690bf076 |
149 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
150 | $recursion->entity_id = $daoEvent->id; |
151 | $recursion->entity_table = 'civicrm_event'; |
152 | $recursion->dateColumns = array('start_date'); |
84f02991 |
153 | $recursion->schedule = array ( |
690bf076 |
154 | 'entity_value' => $daoEvent->id, |
cd3f28a7 |
155 | 'start_action_date' => $daoEvent->start_date, |
f5f5a631 |
156 | 'start_action_condition' => 'monday', |
690bf076 |
157 | 'repetition_frequency_unit' => 'week', |
158 | 'repetition_frequency_interval' => 1, |
159 | 'start_action_offset' => 4, |
160 | 'used_for' => 'event' |
161 | ); |
162 | |
342c3f9e |
163 | $recursion->linkedEntities = array( |
342c3f9e |
164 | array( |
165 | 'table' => 'civicrm_tell_friend', |
166 | 'findCriteria' => array( |
167 | 'entity_id' => $recursion->entity_id, |
168 | 'entity_table' => 'civicrm_event' |
169 | ), |
170 | 'linkedColumns' => array('entity_id'), |
171 | 'isRecurringEntityRecord' => TRUE, |
172 | ), |
342c3f9e |
173 | ); |
f5f5a631 |
174 | |
175 | $interval = $recursion->getInterval($daoEvent->start_date, $daoEvent->end_date); |
176 | $recursion->intervalDateColumns = array('end_date' => $interval); |
690bf076 |
177 | $generatedEntities = $recursion->generate(); |
cd3f28a7 |
178 | $this->assertArrayHasKey('civicrm_event', $generatedEntities, 'Check if generatedEntities has civicrm_event as required key'); |
f5f5a631 |
179 | $expectedDates = array( |
180 | '20141027103000' => '20141029103000', |
181 | '20141103103000' => '20141105103000', |
182 | '20141110103000' => '20141112103000', |
183 | '20141117103000' => '20141119103000' |
184 | ); |
cd3f28a7 |
185 | |
fb2120e7 |
186 | $this->assertCount($recursion->schedule['start_action_offset'], $generatedEntities['civicrm_event'], 'Check if the number of events created are right'); |
f5f5a631 |
187 | $actualDates = array(); |
fb2120e7 |
188 | foreach($generatedEntities['civicrm_event'] as $key => $val) { |
189 | $this->assertDBNotNull('CRM_Event_DAO_Event', $val, 'id', 'id', 'Check if repeating events were created.'); |
f5f5a631 |
190 | $startDate = date('YmdHis', strtotime(CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $val, 'start_date', 'id'))); |
191 | $endDate = date('YmdHis', strtotime(CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $val, 'end_date', 'id'))); |
192 | $actualDates[$startDate] = $endDate; |
cd3f28a7 |
193 | } |
194 | |
f5f5a631 |
195 | $resultDates = array_diff($actualDates, $expectedDates); |
196 | $this->assertEquals(0, count($resultDates), "Check if all the value in expected array matches actual array"); |
197 | |
a50a97b8 |
198 | foreach($generatedEntities['civicrm_tell_friend'] as $key => $val) { |
fb2120e7 |
199 | $this->assertDBNotNull('CRM_Friend_DAO_Friend', $val, 'id', 'id', 'Check if friends were created in loop'); |
200 | $this->assertDBCompareValue('CRM_Friend_DAO_Friend', $val, 'entity_id', 'id', $generatedEntities['civicrm_event'][$key], 'Check DB if correct FK was maintained with event for Friend'); |
cd3f28a7 |
201 | } |
fb2120e7 |
202 | $this->assertCount($recursion->schedule['start_action_offset'], $generatedEntities['civicrm_tell_friend'], 'Check if the number of tell a friend records are right'); |
cd3f28a7 |
203 | |
206c0c43 |
204 | // set mode to ALL, i.e any change to changing event affects all related recurring activities |
205 | $recursion->mode(3); |
690bf076 |
206 | |
207 | $daoEvent->find(TRUE); |
208 | $daoEvent->title = 'Event Changed'; |
209 | $daoEvent->save(); |
210 | |
211 | // check if other events were affected |
84f02991 |
212 | foreach ($generatedEntities['civicrm_event'] as $entityID) { |
690bf076 |
213 | $this->assertDBCompareValue('CRM_Event_DAO_Event', $entityID, 'title', 'id', 'Event Changed', 'Check if title was updated'); |
214 | } |
2eac52b6 |
215 | |
216 | end($generatedEntities['civicrm_event']); |
217 | $key = key($generatedEntities['civicrm_event']); |
218 | |
219 | end($generatedEntities['civicrm_tell_friend']); |
220 | $actKey = key($generatedEntities['civicrm_tell_friend']); |
221 | |
222 | //Check if both(event/tell a friend) keys are same |
223 | $this->assertEquals($key, $actKey, "Check if both the keys are same"); |
224 | |
225 | //Cross check event exists before we test deletion |
f9f8eda7 |
226 | $searchParamsEventBeforeDelete = array( |
227 | 'entity_id' => $generatedEntities['civicrm_event'][$key], |
2eac52b6 |
228 | 'entity_table' => 'civicrm_event' |
f9f8eda7 |
229 | ); |
230 | $expectedValuesEventBeforeDelete = array( |
231 | 'entity_id' => $generatedEntities['civicrm_event'][$key], |
2eac52b6 |
232 | 'entity_table' => 'civicrm_event' |
f9f8eda7 |
233 | ); |
2eac52b6 |
234 | $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsEventBeforeDelete, $expectedValuesEventBeforeDelete); |
235 | |
236 | //Cross check event exists before we test deletion |
f9f8eda7 |
237 | $searchParamsTellAFriendBeforeDelete = array( |
238 | 'entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey], |
2eac52b6 |
239 | 'entity_table' => 'civicrm_tell_friend' |
f9f8eda7 |
240 | ); |
241 | $expectedValuesTellAFriendBeforeDelete = array( |
242 | 'entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey], |
2eac52b6 |
243 | 'entity_table' => 'civicrm_tell_friend' |
f9f8eda7 |
244 | ); |
2eac52b6 |
245 | $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsTellAFriendBeforeDelete, $expectedValuesTellAFriendBeforeDelete); |
246 | |
247 | //Delete an event from recurring set and respective linked entity should be deleted from civicrm_recurring_entity_table |
248 | $daoRecurEvent = new CRM_Event_DAO_Event(); |
249 | $daoRecurEvent->id = $generatedEntities['civicrm_event'][$key]; |
250 | if ($daoRecurEvent->find(TRUE)) { |
251 | $daoRecurEvent->delete(); |
252 | $daoRecurEvent->free(); |
253 | } |
254 | |
255 | //Check if this event_id was deleted |
256 | $this->assertDBNull('CRM_Event_DAO_Event', $generatedEntities['civicrm_event'][$key], 'id', 'id', 'Check if event was deleted'); |
f9f8eda7 |
257 | $searchParams = array( |
258 | 'entity_id' => $generatedEntities['civicrm_event'][$key], |
2eac52b6 |
259 | 'entity_table' => 'civicrm_event' |
f9f8eda7 |
260 | ); |
2eac52b6 |
261 | $compareParams = array(); |
262 | $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParams, $compareParams); |
f9f8eda7 |
263 | |
2eac52b6 |
264 | //Find tell_a_friend id if that was deleted from civicrm |
f9f8eda7 |
265 | $searchActParams = array( |
266 | 'entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey], |
2eac52b6 |
267 | 'entity_table' => 'civicrm_tell_friend' |
f9f8eda7 |
268 | ); |
2eac52b6 |
269 | $compareActParams = array(); |
270 | $this->assertDBCompareValues('CRM_Friend_DAO_Friend', $searchActParams, $compareActParams); |
690bf076 |
271 | } |
25b72113 |
272 | } |