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