3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class CRM_Core_BAO_RecurringEntityTest
31 class CRM_Core_BAO_RecurringEntityTest
extends CiviUnitTestCase
{
34 * Sets up the fixture, for example, opens a network connection.
35 * This method is called before a test is executed.
37 protected function setUp() {
42 * Tears down the fixture, for example, closes a network connection.
43 * This method is called after a test is executed.
45 protected function tearDown() {
49 * Testing Activity Generation through Entity Recursion.
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';
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',
73 $generatedEntities = $recursion->generate();
74 $this->assertEquals(5, count($generatedEntities['civicrm_activity']), "Cehck if number of iterations are 5");
75 $expectedDates = array(
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'
88 // set mode to ALL, i.e any change to changing activity affects all related recurring activities
91 // lets change subject of initial activity that we created in beginning
92 $daoActivity->find(TRUE);
93 $daoActivity->subject
= 'Changed Activity';
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')));
102 $resultDates = array_diff($actualDates, $expectedDates);
103 $this->assertEquals(0, count($resultDates), "Check if all the value in expected array matches actual array");
108 * Testing Event Generation through Entity Recursion.
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;
121 $this->assertDBNotNull('CRM_Event_DAO_Event', $daoEvent->id
, 'id', 'id', 'Check DB if event was created');
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');
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',
147 $recursion->linkedEntities
= array(
149 'table' => 'civicrm_tell_friend',
150 'findCriteria' => array(
151 'entity_id' => $recursion->entity_id
,
152 'entity_table' => 'civicrm_event',
154 'linkedColumns' => array('entity_id'),
155 'isRecurringEntityRecord' => TRUE,
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',
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;
179 $resultDates = array_diff($actualDates, $expectedDates);
180 $this->assertEquals(0, count($resultDates), "Check if all the value in expected array matches actual array");
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');
186 $this->assertCount($recursion->schedule
['start_action_offset'], $generatedEntities['civicrm_tell_friend'], 'Check if the number of tell a friend records are right');
188 // set mode to ALL, i.e any change to changing event affects all related recurring activities
191 $daoEvent->find(TRUE);
192 $daoEvent->title
= 'Event Changed';
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');
200 end($generatedEntities['civicrm_event']);
201 $key = key($generatedEntities['civicrm_event']);
203 end($generatedEntities['civicrm_tell_friend']);
204 $actKey = key($generatedEntities['civicrm_tell_friend']);
206 //Check if both(event/tell a friend) keys are same
207 $this->assertEquals($key, $actKey, "Check if both the keys are same");
209 //Cross check event exists before we test deletion
210 $searchParamsEventBeforeDelete = array(
211 'entity_id' => $generatedEntities['civicrm_event'][$key],
212 'entity_table' => 'civicrm_event',
214 $expectedValuesEventBeforeDelete = array(
215 'entity_id' => $generatedEntities['civicrm_event'][$key],
216 'entity_table' => 'civicrm_event',
218 $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsEventBeforeDelete, $expectedValuesEventBeforeDelete);
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',
225 $expectedValuesTellAFriendBeforeDelete = array(
226 'entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey],
227 'entity_table' => 'civicrm_tell_friend',
229 $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsTellAFriendBeforeDelete, $expectedValuesTellAFriendBeforeDelete);
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();
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',
245 $compareParams = array();
246 $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParams, $compareParams);
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',
253 $compareActParams = array();
254 $this->assertDBCompareValues('CRM_Friend_DAO_Friend', $searchActParams, $compareActParams);