25b72113 |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 4.5 | |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
25b72113 |
28 | require_once 'CiviTest/CiviUnitTestCase.php'; |
060402a9 |
29 | require_once 'CRM/Core/BAO/RecurringEntity.php'; |
25b72113 |
30 | |
31 | /** |
060402a9 |
32 | * Class CRM_Core_BAO_RecurringEntityTest |
25b72113 |
33 | */ |
060402a9 |
34 | class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase { |
25b72113 |
35 | /** |
36 | * @return array |
37 | */ |
38 | function get_info() { |
39 | return array( |
40 | 'name' => 'Recurring Entity BAOs', |
41 | 'description' => 'Test all CRM_Event_BAO_RecurringEntity methods.', |
42 | 'group' => 'CiviCRM BAO Tests', |
43 | ); |
44 | } |
45 | |
6caeea75 |
46 | /** |
47 | * Sets up the fixture, for example, opens a network connection. |
48 | * This method is called before a test is executed. |
49 | * |
50 | * @access protected |
51 | */ |
52 | protected function setUp() { |
25b72113 |
53 | parent::setUp(); |
25b72113 |
54 | } |
6caeea75 |
55 | |
56 | /** |
57 | * Tears down the fixture, for example, closes a network connection. |
58 | * This method is called after a test is executed. |
59 | * |
60 | * @access protected |
61 | */ |
62 | protected function tearDown() {} |
25b72113 |
63 | |
64 | /** |
6caeea75 |
65 | * Testing Activity Generation through Entity Recursion |
25b72113 |
66 | */ |
6caeea75 |
67 | function testActivityGeneration() { |
206c0c43 |
68 | //Activity set initial params |
37b28176 |
69 | $daoActivity = new CRM_Activity_DAO_Activity(); |
70 | $daoActivity->activity_type_id = 1; |
71 | $daoActivity->subject = "Initial Activity"; |
72 | $daoActivity->activity_date_time = date('YmdHis'); |
73 | $daoActivity->save(); |
25b72113 |
74 | |
37b28176 |
75 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
690bf076 |
76 | $recursion->entity_id = $daoActivity->id; |
77 | $recursion->entity_table = 'civicrm_activity'; |
78 | $recursion->dateColumns = array('activity_date_time'); |
84f02991 |
79 | $recursion->schedule = array( |
72e95527 |
80 | 'entity_value' => $daoActivity->id, |
a1a821bc |
81 | 'start_action_date' => $daoActivity->activity_date_time, |
82 | 'entity_status' => 'fourth saturday', |
37b28176 |
83 | 'repetition_frequency_unit' => 'month', |
84 | 'repetition_frequency_interval' => 3, |
85 | 'start_action_offset' => 5, |
206c0c43 |
86 | 'used_for' => 'activity' |
690bf076 |
87 | ); |
206c0c43 |
88 | |
37b28176 |
89 | $generatedEntities = $recursion->generate(); |
84f02991 |
90 | foreach ($generatedEntities['civicrm_activity'] as $entityID) { |
37b28176 |
91 | $this->assertDBNotNull('CRM_Activity_DAO_Activity', $entityID, 'id', |
92 | 'id', 'Check DB if repeating activities were created' |
93 | ); |
25b72113 |
94 | } |
25b72113 |
95 | |
206c0c43 |
96 | // set mode to ALL, i.e any change to changing activity affects all related recurring activities |
97 | $recursion->mode(3); |
37b28176 |
98 | |
99 | // lets change subject of initial activity that we created in begining |
25b72113 |
100 | $daoActivity->find(TRUE); |
690bf076 |
101 | $daoActivity->subject = 'Changed Activity'; |
25b72113 |
102 | $daoActivity->save(); |
25b72113 |
103 | |
37b28176 |
104 | // check if other activities were affected |
84f02991 |
105 | foreach ($generatedEntities['civicrm_activity'] as $entityID) { |
690bf076 |
106 | $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $entityID, 'subject', 'id', 'Changed Activity', 'Check if subject was updated'); |
6caeea75 |
107 | } |
6caeea75 |
108 | } |
109 | |
690bf076 |
110 | /** |
111 | * Testing Event Generation through Entity Recursion |
112 | */ |
113 | 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-09-24 10:30:00')); |
206c0c43 |
120 | $daoEvent->end_date = date('YmdHis', strtotime('2014-09-26 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'); |
125 | |
126 | //Create profile for event |
127 | $daoUF = new CRM_Core_DAO_UFJoin(); |
128 | $daoUF->is_active = 1; |
129 | $daoUF->entity_table = 'civicrm_event'; |
130 | $daoUF->entity_id = $daoEvent->id; |
131 | $daoUF->uf_group_id = 12; |
132 | $daoUF->module = 'Test'; |
133 | $daoUF->save(); |
134 | $this->assertDBNotNull('CRM_Core_DAO_UFJoin', $daoUF->id, 'id', 'id', 'Check DB if profile was created'); |
135 | |
136 | //Create tell a friend for event |
137 | $daoTellAFriend = new CRM_Friend_DAO_Friend(); |
138 | $daoTellAFriend->entity_table = 'civicrm_event'; |
139 | $daoTellAFriend->entity_id = $daoEvent->id; |
140 | $daoTellAFriend->title = 'Testing tell a friend'; |
141 | $daoTellAFriend->is_active = 1; |
142 | $daoTellAFriend->save(); |
143 | $this->assertDBNotNull('CRM_Friend_DAO_Friend', $daoTellAFriend->id, 'id', 'id', 'Check DB if tell a freind was created'); |
690bf076 |
144 | |
145 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
146 | $recursion->entity_id = $daoEvent->id; |
147 | $recursion->entity_table = 'civicrm_event'; |
148 | $recursion->dateColumns = array('start_date'); |
84f02991 |
149 | $recursion->schedule = array ( |
690bf076 |
150 | 'entity_value' => $daoEvent->id, |
cd3f28a7 |
151 | 'start_action_date' => $daoEvent->start_date, |
690bf076 |
152 | 'start_action_condition' => 'wednesday', |
153 | 'repetition_frequency_unit' => 'week', |
154 | 'repetition_frequency_interval' => 1, |
155 | 'start_action_offset' => 4, |
156 | 'used_for' => 'event' |
157 | ); |
158 | |
342c3f9e |
159 | $recursion->linkedEntities = array( |
160 | array( |
161 | 'table' => 'civicrm_price_set_entity', |
162 | 'findCriteria' => array( |
163 | 'entity_id' => $recursion->entity_id, |
164 | 'entity_table' => 'civicrm_event' |
165 | ), |
166 | 'linkedColumns' => array('entity_id'), |
167 | 'isRecurringEntityRecord' => FALSE, |
168 | ), |
169 | array( |
170 | 'table' => 'civicrm_uf_join', |
171 | 'findCriteria' => array( |
172 | 'entity_id' => $recursion->entity_id, |
173 | 'entity_table' => 'civicrm_event' |
174 | ), |
175 | 'linkedColumns' => array('entity_id'), |
176 | 'isRecurringEntityRecord' => FALSE, |
177 | ), |
178 | array( |
179 | 'table' => 'civicrm_tell_friend', |
180 | 'findCriteria' => array( |
181 | 'entity_id' => $recursion->entity_id, |
182 | 'entity_table' => 'civicrm_event' |
183 | ), |
184 | 'linkedColumns' => array('entity_id'), |
185 | 'isRecurringEntityRecord' => TRUE, |
186 | ), |
187 | array( |
188 | 'table' => 'civicrm_pcp_block', |
189 | 'findCriteria' => array( |
190 | 'entity_id' => $recursion->entity_id, |
191 | 'entity_table' => 'civicrm_event' |
192 | ), |
193 | 'linkedColumns' => array('entity_id'), |
194 | 'isRecurringEntityRecord' => TRUE, |
195 | ), |
196 | ); |
197 | |
690bf076 |
198 | $generatedEntities = $recursion->generate(); |
cd3f28a7 |
199 | $this->assertArrayHasKey('civicrm_event', $generatedEntities, 'Check if generatedEntities has civicrm_event as required key'); |
200 | |
201 | if(CRM_Utils_Array::value('civicrm_event', $generatedEntities)){ |
202 | $expCountEvent = count($generatedEntities['civicrm_event']); |
203 | $actCountEvent = 0; |
204 | foreach($generatedEntities['civicrm_event'] as $key => $val){ |
205 | $this->assertDBNotNull('CRM_Event_DAO_Event', $val, 'id', 'id', 'Check if events were created in loop'); |
206 | $actCountEvent++; |
207 | } |
208 | $this->assertCount($expCountEvent, $actCountEvent, 'Check if the number of events created are right'); |
209 | } |
210 | |
211 | if(CRM_Utils_Array::value('civicrm_uf_join', $generatedEntities) && CRM_Utils_Array::value('civicrm_event', $generatedEntities)){ |
212 | $expCountUFJoin = count($generatedEntities['civicrm_uf_join']); |
213 | $actCountUFJoin = 0; |
214 | foreach($generatedEntities['civicrm_uf_join'] as $key => $val){ |
215 | $this->assertDBNotNull('CRM_Core_DAO_UFJoin', $val, 'id', 'id', 'Check if profile were created in loop'); |
216 | $this->assertDBCompareValue('CRM_Core_DAO_UFJoin', $val, 'entity_id', 'id', $generatedEntities['civicrm_event'][$key], 'Check DB if correct FK was maintained with event for UF Join'); |
217 | $actCountUFJoin++; |
218 | } |
219 | $this->assertCount($expCountUFJoin, $actCountUFJoin, 'Check if the number of profiles created are right'); |
220 | } |
221 | |
222 | if(CRM_Utils_Array::value('civicrm_tell_friend', $generatedEntities) && CRM_Utils_Array::value('civicrm_event', $generatedEntities)){ |
223 | $expCountTellAFriend = count($generatedEntities['civicrm_tell_friend']); |
224 | $actCountTellAFriend = 0; |
225 | foreach($generatedEntities['civicrm_tell_friend'] as $key => $val){ |
226 | $this->assertDBNotNull('CRM_Friend_DAO_Friend', $val, 'id', 'id', 'Check if friends were created in loop'); |
227 | $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'); |
228 | $actCountTellAFriend++; |
229 | } |
230 | $this->assertCount($expCountTellAFriend, $actCountTellAFriend, 'Check if the number of friends created are right'); |
231 | } |
232 | |
690bf076 |
233 | |
206c0c43 |
234 | // set mode to ALL, i.e any change to changing event affects all related recurring activities |
235 | $recursion->mode(3); |
690bf076 |
236 | |
237 | $daoEvent->find(TRUE); |
238 | $daoEvent->title = 'Event Changed'; |
239 | $daoEvent->save(); |
240 | |
241 | // check if other events were affected |
84f02991 |
242 | foreach ($generatedEntities['civicrm_event'] as $entityID) { |
690bf076 |
243 | $this->assertDBCompareValue('CRM_Event_DAO_Event', $entityID, 'title', 'id', 'Event Changed', 'Check if title was updated'); |
244 | } |
245 | } |
25b72113 |
246 | } |