3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 require_once 'CRM/Core/BAO/RecurringEntity.php';
32 * Class CRM_Core_BAO_RecurringEntityTest
34 class CRM_Core_BAO_RecurringEntityTest
extends CiviUnitTestCase
{
40 'name' => 'Recurring Entity BAOs',
41 'description' => 'Test all CRM_Event_BAO_RecurringEntity methods.',
42 'group' => 'CiviCRM BAO Tests',
47 * Sets up the fixture, for example, opens a network connection.
48 * This method is called before a test is executed.
52 protected function setUp() {
57 * Tears down the fixture, for example, closes a network connection.
58 * This method is called after a test is executed.
62 protected function tearDown() {}
65 * Testing Activity Generation through Entity Recursion
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');
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'
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'
96 // set mode to ALL, i.e any change to changing activity affects all related recurring activities
99 // lets change subject of initial activity that we created in begining
100 $daoActivity->find(TRUE);
101 $daoActivity->subject
= 'Changed Activity';
102 $daoActivity->save();
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');
111 * Testing Event Generation through Entity Recursion
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;
124 $this->assertDBNotNull('CRM_Event_DAO_Event', $daoEvent->id
, 'id', 'id', 'Check DB if event was created');
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';
134 $this->assertDBNotNull('CRM_Core_DAO_UFJoin', $daoUF->id
, 'id', 'id', 'Check DB if profile was created');
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');
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'
159 $recursion->linkedEntities
= array(
161 'table' => 'civicrm_price_set_entity',
162 'findCriteria' => array(
163 'entity_id' => $recursion->entity_id
,
164 'entity_table' => 'civicrm_event'
166 'linkedColumns' => array('entity_id'),
167 'isRecurringEntityRecord' => FALSE,
170 'table' => 'civicrm_uf_join',
171 'findCriteria' => array(
172 'entity_id' => $recursion->entity_id
,
173 'entity_table' => 'civicrm_event'
175 'linkedColumns' => array('entity_id'),
176 'isRecurringEntityRecord' => FALSE,
179 'table' => 'civicrm_tell_friend',
180 'findCriteria' => array(
181 'entity_id' => $recursion->entity_id
,
182 'entity_table' => 'civicrm_event'
184 'linkedColumns' => array('entity_id'),
185 'isRecurringEntityRecord' => TRUE,
188 'table' => 'civicrm_pcp_block',
189 'findCriteria' => array(
190 'entity_id' => $recursion->entity_id
,
191 'entity_table' => 'civicrm_event'
193 'linkedColumns' => array('entity_id'),
194 'isRecurringEntityRecord' => TRUE,
198 $generatedEntities = $recursion->generate();
199 $this->assertArrayHasKey('civicrm_event', $generatedEntities, 'Check if generatedEntities has civicrm_event as required key');
201 if(CRM_Utils_Array
::value('civicrm_event', $generatedEntities)){
202 $expCountEvent = count($generatedEntities['civicrm_event']);
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');
208 $this->assertCount($expCountEvent, $actCountEvent, 'Check if the number of events created are right');
211 if(CRM_Utils_Array
::value('civicrm_uf_join', $generatedEntities) && CRM_Utils_Array
::value('civicrm_event', $generatedEntities)){
212 $expCountUFJoin = count($generatedEntities['civicrm_uf_join']);
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');
219 $this->assertCount($expCountUFJoin, $actCountUFJoin, 'Check if the number of profiles created are right');
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++
;
230 $this->assertCount($expCountTellAFriend, $actCountTellAFriend, 'Check if the number of friends created are right');
234 // set mode to ALL, i.e any change to changing event affects all related recurring activities
237 $daoEvent->find(TRUE);
238 $daoEvent->title
= 'Event Changed';
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');