generalize skip fields & testing linked-entities
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / RecurringEntityTest.php
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
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 * @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
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() {
53 parent::setUp();
54 }
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() {}
63
64 /**
65 * Testing Activity Generation through Entity Recursion
66 */
67 function testActivityGeneration() {
68 //Activity set initial params
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();
74
75 $recursion = new CRM_Core_BAO_RecurringEntity();
76 $recursion->entity_id = $daoActivity->id;
77 $recursion->entity_table = 'civicrm_activity';
78 $recursion->dateColumns = array('activity_date_time');
79 $recursion->schedule = array(
80 'entity_value' => $daoActivity->id,
81 'start_action_date' => $daoActivity->activity_date_time,
82 'entity_status' => 'fourth saturday',
83 'repetition_frequency_unit' => 'month',
84 'repetition_frequency_interval' => 3,
85 'start_action_offset' => 5,
86 'used_for' => 'activity'
87 );
88
89 $generatedEntities = $recursion->generate();
90 foreach ($generatedEntities['civicrm_activity'] as $entityID) {
91 $this->assertDBNotNull('CRM_Activity_DAO_Activity', $entityID, 'id',
92 'id', 'Check DB if repeating activities were created'
93 );
94 }
95
96 // set mode to ALL, i.e any change to changing activity affects all related recurring activities
97 $recursion->mode(3);
98
99 // lets change subject of initial activity that we created in begining
100 $daoActivity->find(TRUE);
101 $daoActivity->subject = 'Changed Activity';
102 $daoActivity->save();
103
104 // check if other activities were affected
105 foreach ($generatedEntities['civicrm_activity'] as $entityID) {
106 $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $entityID, 'subject', 'id', 'Changed Activity', 'Check if subject was updated');
107 }
108 }
109
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'));
120 $daoEvent->end_date = date('YmdHis', strtotime('2014-09-26 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 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');
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');
149 $recursion->schedule = array (
150 'entity_value' => $daoEvent->id,
151 'start_action_date' => $daoEvent->start_date,
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
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
198 $generatedEntities = $recursion->generate();
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
233
234 // set mode to ALL, i.e any change to changing event affects all related recurring activities
235 $recursion->mode(3);
236
237 $daoEvent->find(TRUE);
238 $daoEvent->title = 'Event Changed';
239 $daoEvent->save();
240
241 // check if other events were affected
242 foreach ($generatedEntities['civicrm_event'] as $entityID) {
243 $this->assertDBCompareValue('CRM_Event_DAO_Event', $entityID, 'title', 'id', 'Event Changed', 'Check if title was updated');
244 }
245 }
246 }