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