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