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() { |
690bf076 |
68 | //create an activity |
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 | $this->assertDBNotNull('CRM_Activity_DAO_Activity', $daoActivity->id, 'id', |
76 | 'id', 'Check DB if activity was created' |
77 | ); |
25b72113 |
78 | |
37b28176 |
79 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
690bf076 |
80 | $recursion->entity_id = $daoActivity->id; |
81 | $recursion->entity_table = 'civicrm_activity'; |
82 | $recursion->dateColumns = array('activity_date_time'); |
83 | $recursion->scheduleDBParams = array( |
72e95527 |
84 | 'entity_value' => $daoActivity->id, |
85 | 'entity_status' => $daoActivity->activity_date_time, |
37b28176 |
86 | 'start_action_date' => 'fourth saturday', |
87 | 'repetition_frequency_unit' => 'month', |
88 | 'repetition_frequency_interval' => 3, |
89 | 'start_action_offset' => 5, |
690bf076 |
90 | 'used_for' => 'activity' |
91 | ); |
37b28176 |
92 | $generatedEntities = $recursion->generate(); |
37b28176 |
93 | foreach ($generatedEntities as $entityID) { |
94 | $this->assertDBNotNull('CRM_Activity_DAO_Activity', $entityID, 'id', |
95 | 'id', 'Check DB if repeating activities were created' |
96 | ); |
25b72113 |
97 | } |
25b72113 |
98 | |
37b28176 |
99 | // try changing something |
690bf076 |
100 | $recursion->mode(3); // sets ->mode var & saves in DB |
37b28176 |
101 | |
102 | // lets change subject of initial activity that we created in begining |
25b72113 |
103 | $daoActivity->find(TRUE); |
690bf076 |
104 | $daoActivity->subject = 'Changed Activity'; |
25b72113 |
105 | $daoActivity->save(); |
25b72113 |
106 | |
37b28176 |
107 | // check if other activities were affected |
108 | foreach ($generatedEntities as $entityID) { |
690bf076 |
109 | $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $entityID, 'subject', 'id', 'Changed Activity', 'Check if subject was updated'); |
6caeea75 |
110 | } |
6caeea75 |
111 | } |
112 | |
690bf076 |
113 | /** |
114 | * Testing Event Generation through Entity Recursion |
115 | */ |
116 | function testEventGeneration() { |
117 | //Event set initial params |
118 | $daoEvent = new CRM_Event_DAO_Event(); |
119 | $daoEvent->title = 'Test event for Recurring Entity'; |
120 | $daoEvent->event_type_id = 3; |
121 | $daoEvent->is_public = 1; |
122 | $daoEvent->start_date = date('YmdHis', strtotime('2014-09-24 10:30:00')); |
123 | $daoEvent->end_date = date('YmdHis', strtotime('2014-09-26 10:30:00')); |
124 | $daoEvent->created_date = date('YmdHis'); |
125 | $daoEvent->is_active = 1; |
126 | $daoEvent->save(); |
127 | |
128 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
129 | $recursion->entity_id = $daoEvent->id; |
130 | $recursion->entity_table = 'civicrm_event'; |
131 | $recursion->dateColumns = array('start_date'); |
132 | $recursion->scheduleDBParams = array ( |
133 | 'entity_value' => $daoEvent->id, |
134 | 'entity_status' => $daoEvent->start_date, |
135 | 'start_action_condition' => 'wednesday', |
136 | 'repetition_frequency_unit' => 'week', |
137 | 'repetition_frequency_interval' => 1, |
138 | 'start_action_offset' => 4, |
139 | 'used_for' => 'event' |
140 | ); |
141 | |
142 | //$interval = $recursion->getInterval($daoEvent->start_date, $daoEvent->end_date); |
143 | //$recursion->intervalDateColumns = array('end_date' => $interval); |
144 | |
145 | //$recursion->excludeDates = array(date('Ymd', strtotime('2014-10-02')), '20141008');// = array('date1', date2, date2) |
146 | //$recursion->excludeDateRangeColumns = array('start_date', 'end_date'); |
147 | |
148 | $generatedEntities = $recursion->generate(); |
149 | CRM_Core_Error::debug_var('$generatedEntities', $generatedEntities); |
150 | |
151 | // try changing something |
152 | $recursion->mode(3); // sets ->mode var & saves in DB |
153 | |
154 | $daoEvent->find(TRUE); |
155 | $daoEvent->title = 'Event Changed'; |
156 | $daoEvent->save(); |
157 | |
158 | // check if other events were affected |
159 | foreach ($generatedEntities as $entityID) { |
160 | $this->assertDBCompareValue('CRM_Event_DAO_Event', $entityID, 'title', 'id', 'Event Changed', 'Check if title was updated'); |
161 | } |
162 | } |
37b28176 |
163 | |
25b72113 |
164 | } |
165 | |
50b39b5b |
166 | |