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