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