]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SearchForm/Bug45966Test.php
Release 6.4.0
[Github/sugarcrm.git] / tests / include / SearchForm / Bug45966Test.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38
39 require_once 'modules/Notes/Note.php';
40 require_once 'include/SearchForm/SearchForm2.php';
41
42 /**
43  * @group Bug45966
44  */
45 class Bug45966 extends Sugar_PHPUnit_Framework_TestCase {
46
47     var $module = 'Notes';
48     var $action = 'index';
49     var $seed;
50     var $form;
51     var $array;
52
53     public function setUp() {
54         require('include/modules.php');
55             $GLOBALS['beanList'] = $beanList;
56             $GLOBALS['beanFiles'] = $beanFiles;
57
58         require "modules/".$this->module."/metadata/searchdefs.php";
59         require "modules/".$this->module."/metadata/SearchFields.php";
60         require "modules/".$this->module."/metadata/listviewdefs.php";
61
62         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
63         $GLOBALS['current_user']->setPreference('timezone', 'EDT');
64         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
65
66         $this->seed = new $beanList[$this->module];
67         $this->form = new SearchForm($this->seed, $this->module, $this->action);
68         $this->form->setup($searchdefs, $searchFields, 'include/SearchForm/tpls/SearchFormGeneric.tpl', "advanced_search", $listViewDefs);
69
70         $this->array = array(
71             'module'=>$this->module,
72             'action'=>$this->action,
73             'searchFormTab'=>'advanced_search',
74             'query'=>'true',
75             'date_entered_advanced_range_choice'=>'',
76             'range_date_entered_advanced' => '',
77             'start_range_date_entered_advanced' => '',
78             'end_range_date_entered_advanced' => '',
79         );
80     }
81
82     public function tearDown()
83     {
84         unset($this->array);
85         unset($this->form);
86         unset($this->seed);
87         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
88         unset($GLOBALS['current_user']);
89     }
90
91     public function testSearchDateEqualsAdjustsForTimeZone() {
92         global $timedate;
93         $user = $GLOBALS['current_user'];
94
95         $testDate = '12/31/2011';
96
97         $this->array['date_entered_advanced_range_choice'] = '=';
98         $this->array['range_date_entered_advanced'] = $testDate;
99
100         $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
101
102         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjDate['start']), 'datetime').
103                 " AND ". strtolower($this->module).".date_entered <= ". $user->db->convert($user->db->quoted($adjDate['end']), 'datetime');
104
105         $this->form->populateFromArray($this->array);
106         $query = $this->form->generateSearchWhere($this->seed, $this->module);
107
108         $this->assertContains($expected, $query[0]);
109     }
110
111     public function testSearchNotOnDateAdjustsForTimeZone() {
112         global $timedate;
113         $user = $GLOBALS['current_user'];
114
115         $testDate = '12/31/2011';
116
117         $this->array['date_entered_advanced_range_choice'] = 'not_equal';
118         $this->array['range_date_entered_advanced'] = $testDate;
119
120         $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
121
122         $expected = "notes.date_entered IS NULL OR notes.date_entered < ". $user->db->convert($user->db->quoted($adjDate['start']), 'datetime').
123             " OR ". strtolower($this->module).".date_entered > ". $user->db->convert($user->db->quoted($adjDate['end']), 'datetime');
124
125         $this->form->populateFromArray($this->array);
126         $query = $this->form->generateSearchWhere($this->seed, $this->module);
127
128         $this->assertContains($expected, $query[0]);
129     }
130
131     public function testSearchAfterDateAdjustsForTimeZone() {
132         global $timedate;
133         $user = $GLOBALS['current_user'];
134
135         $testDate = '12/31/2011';
136
137         $this->array['date_entered_advanced_range_choice'] = 'greater_than';
138         $this->array['range_date_entered_advanced'] = $testDate;
139         $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
140
141         $expected = "notes.date_entered > ".$user->db->convert($user->db->quoted($adjDate['end']), 'datetime');
142
143         $this->form->populateFromArray($this->array);
144         $query = $this->form->generateSearchWhere($this->seed, $this->module);
145
146         $this->assertContains($expected, $query[0]);
147     }
148
149     public function testSearchBeforeDateAdjustsForTimeZone() {
150         global $timedate;
151         $user = $GLOBALS['current_user'];
152
153         $testDate = '01/01/2011';
154
155         $this->array['date_entered_advanced_range_choice'] = 'less_than';
156         $this->array['range_date_entered_advanced'] = $testDate;
157         $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
158
159         $expected = "notes.date_entered < ".$user->db->convert($user->db->quoted($adjDate['start']), 'datetime');
160
161         $this->form->populateFromArray($this->array);
162         $query = $this->form->generateSearchWhere($this->seed, $this->module);
163
164         $this->assertContains($expected, $query[0]);
165     }
166
167     public function testSearchLastSevenDaysAdjustsForTimeZone() {
168         global $timedate;
169         $user = $GLOBALS['current_user'];
170
171         $testDate = 'last_7_days';
172
173         $this->array['date_entered_advanced_range_choice'] = $testDate;
174         $this->array['range_date_entered_advanced'] = "[$testDate]";
175
176         $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
177         $adjStartDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("-6 days"));
178
179         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjStartDate['start']), 'datetime').
180                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjToday['end']), 'datetime');
181
182         $this->form->populateFromArray($this->array);
183         $query = $this->form->generateSearchWhere($this->seed, $this->module);
184
185         $this->assertContains($expected, $query[0]);
186     }
187
188     public function testSearchNextSevenDaysAdjustsForTimeZone() {
189         global $timedate;
190         $user = $GLOBALS['current_user'];
191
192         $testDate = 'next_7_days';
193
194         $this->array['date_entered_advanced_range_choice'] = $testDate;
195         $this->array['range_date_entered_advanced'] = "[$testDate]";
196
197         $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
198         $adjEndDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("+6 days"));
199
200         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjToday['start']), 'datetime').
201                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjEndDate['end']), 'datetime');
202
203         $this->form->populateFromArray($this->array);
204         $query = $this->form->generateSearchWhere($this->seed, $this->module);
205
206         $this->assertContains($expected, $query[0]);
207     }
208
209     public function testSearchLastThirtyDaysAdjustsForTimeZone() {
210         global $timedate;
211         $user = $GLOBALS['current_user'];
212
213         $testDate = 'last_30_days';
214
215         $this->array['date_entered_advanced_range_choice'] = $testDate;
216         $this->array['range_date_entered_advanced'] = "[$testDate]";
217
218         $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
219         $adjStartDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("-29 days"));
220
221         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjStartDate['start']), 'datetime').
222                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjToday['end']), 'datetime');
223
224         $this->form->populateFromArray($this->array);
225         $query = $this->form->generateSearchWhere($this->seed, $this->module);
226
227         $this->assertContains($expected, $query[0]);
228     }
229
230     public function testSearchNextThirtyDaysAdjustsForTimeZone() {
231         global $timedate;
232         $user = $GLOBALS['current_user'];
233
234         $testDate = 'next_30_days';
235
236         $this->array['date_entered_advanced_range_choice'] = $testDate;
237         $this->array['range_date_entered_advanced'] = "[$testDate]";
238
239         $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
240         $adjEndDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("+29 days"));
241
242         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjToday['start']), 'datetime').
243                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjEndDate['end']), 'datetime');
244
245         $this->form->populateFromArray($this->array);
246         $query = $this->form->generateSearchWhere($this->seed, $this->module);
247
248         $this->assertContains($expected, $query[0]);
249     }
250
251     public function testSearchLastMonthAdjustsForTimeZone() {
252         global $timedate;
253         $user = $GLOBALS['current_user'];
254
255         $testDate = 'last_month';
256
257         $this->array['date_entered_advanced_range_choice'] = $testDate;
258         $this->array['range_date_entered_advanced'] = "[$testDate]";
259
260         $now = $timedate->getNow(true);
261         $month_number = $now->month == 1 ? 12 : $now->month-1;
262         $year_number = $now->month == 1 ? $now->year - 1 : $now->year;
263         $month = $now->get_day_begin(1, $month_number, $year_number);
264
265         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
266         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin($month->days_in_month));
267
268         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
269                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
270
271         $this->form->populateFromArray($this->array);
272         $query = $this->form->generateSearchWhere($this->seed, $this->module);
273
274         $this->assertContains($expected, $query[0]);
275     }
276
277     public function testSearchThisMonthAdjustsForTimeZone() {
278         global $timedate;
279         $user = $GLOBALS['current_user'];
280
281         $testDate = 'this_month';
282
283         $this->array['date_entered_advanced_range_choice'] = $testDate;
284         $this->array['range_date_entered_advanced'] = "[$testDate]";
285
286         $month = $timedate->getNow(true)->get_day_begin(1);
287         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
288         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin($month->days_in_month));
289
290         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
291                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
292
293         $this->form->populateFromArray($this->array);
294         $query = $this->form->generateSearchWhere($this->seed, $this->module);
295
296         $this->assertContains($expected, $query[0]);
297     }
298
299     public function testSearchNextMonthAdjustsForTimeZone() {
300         global $timedate;
301         $user = $GLOBALS['current_user'];
302
303         $testDate = 'next_month';
304
305         $this->array['date_entered_advanced_range_choice'] = $testDate;
306         $this->array['range_date_entered_advanced'] = "[$testDate]";
307
308         $now = $timedate->getNow(true);
309         $month = $now->get_day_begin(1, $now->month+1);
310         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
311         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin($month->days_in_month));
312
313         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
314                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
315
316         $this->form->populateFromArray($this->array);
317         $query = $this->form->generateSearchWhere($this->seed, $this->module);
318
319         $this->assertContains($expected, $query[0]);
320     }
321
322     public function testSearchLastYearAdjustsForTimeZone() {
323         global $timedate;
324         $user = $GLOBALS['current_user'];
325
326         $testDate = 'last_year';
327
328         $this->array['date_entered_advanced_range_choice'] = $testDate;
329         $this->array['range_date_entered_advanced'] = "[$testDate]";
330
331         $now = $timedate->getNow(true);
332         $month = $now->get_day_begin(1, 1, $now->year-1);
333         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
334         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin(31, 12));
335
336         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
337                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
338
339         $this->form->populateFromArray($this->array);
340         $query = $this->form->generateSearchWhere($this->seed, $this->module);
341
342         $this->assertContains($expected, $query[0]);
343     }
344
345     public function testSearchThisYearAdjustsForTimeZone() {
346         global $timedate;
347         $user = $GLOBALS['current_user'];
348
349         $testDate = 'this_year';
350
351         $this->array['date_entered_advanced_range_choice'] = $testDate;
352         $this->array['range_date_entered_advanced'] = "[$testDate]";
353
354         $month = $timedate->getNow(true)->get_day_begin(1, 1);
355         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
356         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin(31, 12));
357
358         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
359                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
360
361         $this->form->populateFromArray($this->array);
362         $query = $this->form->generateSearchWhere($this->seed, $this->module);
363
364         $this->assertContains($expected, $query[0]);
365     }
366
367     public function testSearchNextYearAdjustsForTimeZone() {
368         global $timedate;
369         $user = $GLOBALS['current_user'];
370
371         $testDate = 'next_year';
372
373         $this->array['date_entered_advanced_range_choice'] = $testDate;
374         $this->array['range_date_entered_advanced'] = "[$testDate]";
375
376         $now = $timedate->getNow(true);
377         $month = $now->get_day_begin(1, 1, $now->year+1);
378         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
379         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin(31, 12));
380
381         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
382                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
383
384         $this->form->populateFromArray($this->array);
385         $query = $this->form->generateSearchWhere($this->seed, $this->module);
386
387         $this->assertContains($expected, $query[0]);
388     }
389
390     public function testSearchDateIsBetweenAdjustsForTimeZone() {
391         global $timedate;
392         $user = $GLOBALS['current_user'];
393
394         $testStartDate = '01/01/2011';
395         $testEndDate = '12/31/2011';
396
397         $this->array['date_entered_advanced_range_choice'] = 'between';
398         $this->array['start_range_date_entered_advanced'] = $testStartDate;
399         $this->array['end_range_date_entered_advanced'] = $testEndDate;
400
401         $adjStartDate = $timedate->getDayStartEndGMT($testStartDate, $user);
402         $adjEndDate = $timedate->getDayStartEndGMT($testEndDate, $user);
403
404         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjStartDate['start']), 'datetime').
405                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjEndDate['end']), 'datetime');
406
407         $this->form->populateFromArray($this->array);
408         $query = $this->form->generateSearchWhere($this->seed, $this->module);
409
410         $this->assertContains($expected, $query[0]);
411     }
412
413 }