]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SearchForm/Bug45966Test.php
Release 6.4.0beta3
[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         global $beanList;
55
56         require "modules/".$this->module."/metadata/searchdefs.php";
57         require "modules/".$this->module."/metadata/SearchFields.php";
58         require "modules/".$this->module."/metadata/listviewdefs.php";
59
60         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
61         $GLOBALS['current_user']->setPreference('timezone', 'EDT');
62         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
63
64         $this->seed = new $beanList[$this->module];
65         $this->form = new SearchForm($this->seed, $this->module, $this->action);
66         $this->form->setup($searchdefs, $searchFields, 'include/SearchForm/tpls/SearchFormGeneric.tpl', "advanced_search", $listViewDefs);
67
68         $this->array = array(
69             'module'=>$this->module,
70             'action'=>$this->action,
71             'searchFormTab'=>'advanced_search',
72             'query'=>'true',
73             'date_entered_advanced_range_choice'=>'',
74             'range_date_entered_advanced' => '',
75             'start_range_date_entered_advanced' => '',
76             'end_range_date_entered_advanced' => '',
77         );
78     }
79
80     public function tearDown()
81     {
82         unset($this->array);
83         unset($this->form);
84         unset($this->seed);
85         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
86         unset($GLOBALS['current_user']);
87     }
88
89     public function testSearchDateEqualsAdjustsForTimeZone() {
90         global $timedate;
91         $user = $GLOBALS['current_user'];
92
93         $testDate = '12/31/2011';
94
95         $this->array['date_entered_advanced_range_choice'] = '=';
96         $this->array['range_date_entered_advanced'] = $testDate;
97
98         $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
99
100         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjDate['start']), 'datetime').
101                 " AND ". strtolower($this->module).".date_entered <= ". $user->db->convert($user->db->quoted($adjDate['end']), 'datetime');
102
103         $this->form->populateFromArray($this->array);
104         $query = $this->form->generateSearchWhere($this->seed, $this->module);
105
106         $this->assertContains($expected, $query[0]);
107     }
108
109     public function testSearchNotOnDateAdjustsForTimeZone() {
110         global $timedate;
111         $user = $GLOBALS['current_user'];
112
113         $testDate = '12/31/2011';
114
115         $this->array['date_entered_advanced_range_choice'] = 'not_equal';
116         $this->array['range_date_entered_advanced'] = $testDate;
117
118         $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
119
120         $expected = "notes.date_entered IS NULL OR notes.date_entered < ". $user->db->convert($user->db->quoted($adjDate['start']), 'datetime').
121             " OR ". strtolower($this->module).".date_entered > ". $user->db->convert($user->db->quoted($adjDate['end']), 'datetime');
122
123         $this->form->populateFromArray($this->array);
124         $query = $this->form->generateSearchWhere($this->seed, $this->module);
125
126         $this->assertContains($expected, $query[0]);
127     }
128
129     public function testSearchAfterDateAdjustsForTimeZone() {
130         global $timedate;
131         $user = $GLOBALS['current_user'];
132
133         $testDate = '12/31/2011';
134
135         $this->array['date_entered_advanced_range_choice'] = 'greater_than';
136         $this->array['range_date_entered_advanced'] = $testDate;
137         $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
138
139         $expected = "notes.date_entered > ".$user->db->convert($user->db->quoted($adjDate['end']), 'datetime');
140
141         $this->form->populateFromArray($this->array);
142         $query = $this->form->generateSearchWhere($this->seed, $this->module);
143
144         $this->assertContains($expected, $query[0]);
145     }
146
147     public function testSearchBeforeDateAdjustsForTimeZone() {
148         global $timedate;
149         $user = $GLOBALS['current_user'];
150
151         $testDate = '01/01/2011';
152
153         $this->array['date_entered_advanced_range_choice'] = 'less_than';
154         $this->array['range_date_entered_advanced'] = $testDate;
155         $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
156
157         $expected = "notes.date_entered < ".$user->db->convert($user->db->quoted($adjDate['start']), 'datetime');
158
159         $this->form->populateFromArray($this->array);
160         $query = $this->form->generateSearchWhere($this->seed, $this->module);
161
162         $this->assertContains($expected, $query[0]);
163     }
164
165     public function testSearchLastSevenDaysAdjustsForTimeZone() {
166         global $timedate;
167         $user = $GLOBALS['current_user'];
168
169         $testDate = 'last_7_days';
170
171         $this->array['date_entered_advanced_range_choice'] = $testDate;
172         $this->array['range_date_entered_advanced'] = "[$testDate]";
173
174         $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
175         $adjStartDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("-6 days"));
176
177         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjStartDate['start']), 'datetime').
178                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjToday['end']), 'datetime');
179
180         $this->form->populateFromArray($this->array);
181         $query = $this->form->generateSearchWhere($this->seed, $this->module);
182
183         $this->assertContains($expected, $query[0]);
184     }
185
186     public function testSearchNextSevenDaysAdjustsForTimeZone() {
187         global $timedate;
188         $user = $GLOBALS['current_user'];
189
190         $testDate = 'next_7_days';
191
192         $this->array['date_entered_advanced_range_choice'] = $testDate;
193         $this->array['range_date_entered_advanced'] = "[$testDate]";
194
195         $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
196         $adjEndDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("+6 days"));
197
198         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjToday['start']), 'datetime').
199                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjEndDate['end']), 'datetime');
200
201         $this->form->populateFromArray($this->array);
202         $query = $this->form->generateSearchWhere($this->seed, $this->module);
203
204         $this->assertContains($expected, $query[0]);
205     }
206
207     public function testSearchLastThirtyDaysAdjustsForTimeZone() {
208         global $timedate;
209         $user = $GLOBALS['current_user'];
210
211         $testDate = 'last_30_days';
212
213         $this->array['date_entered_advanced_range_choice'] = $testDate;
214         $this->array['range_date_entered_advanced'] = "[$testDate]";
215
216         $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
217         $adjStartDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("-29 days"));
218
219         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjStartDate['start']), 'datetime').
220                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjToday['end']), 'datetime');
221
222         $this->form->populateFromArray($this->array);
223         $query = $this->form->generateSearchWhere($this->seed, $this->module);
224
225         $this->assertContains($expected, $query[0]);
226     }
227
228     public function testSearchNextThirtyDaysAdjustsForTimeZone() {
229         global $timedate;
230         $user = $GLOBALS['current_user'];
231
232         $testDate = 'next_30_days';
233
234         $this->array['date_entered_advanced_range_choice'] = $testDate;
235         $this->array['range_date_entered_advanced'] = "[$testDate]";
236
237         $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
238         $adjEndDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("+29 days"));
239
240         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjToday['start']), 'datetime').
241                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjEndDate['end']), 'datetime');
242
243         $this->form->populateFromArray($this->array);
244         $query = $this->form->generateSearchWhere($this->seed, $this->module);
245
246         $this->assertContains($expected, $query[0]);
247     }
248
249     public function testSearchLastMonthAdjustsForTimeZone() {
250         global $timedate;
251         $user = $GLOBALS['current_user'];
252
253         $testDate = 'last_month';
254
255         $this->array['date_entered_advanced_range_choice'] = $testDate;
256         $this->array['range_date_entered_advanced'] = "[$testDate]";
257
258         $now = $timedate->getNow(true);
259         $month = $now->get_day_begin(1, $now->month-1);
260         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
261         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin($month->days_in_month));
262
263         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
264                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
265
266         $this->form->populateFromArray($this->array);
267         $query = $this->form->generateSearchWhere($this->seed, $this->module);
268
269         $this->assertContains($expected, $query[0]);
270     }
271
272     public function testSearchThisMonthAdjustsForTimeZone() {
273         global $timedate;
274         $user = $GLOBALS['current_user'];
275
276         $testDate = 'this_month';
277
278         $this->array['date_entered_advanced_range_choice'] = $testDate;
279         $this->array['range_date_entered_advanced'] = "[$testDate]";
280
281         $month = $timedate->getNow(true)->get_day_begin(1);
282         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
283         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin($month->days_in_month));
284
285         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
286                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
287
288         $this->form->populateFromArray($this->array);
289         $query = $this->form->generateSearchWhere($this->seed, $this->module);
290
291         $this->assertContains($expected, $query[0]);
292     }
293
294     public function testSearchNextMonthAdjustsForTimeZone() {
295         global $timedate;
296         $user = $GLOBALS['current_user'];
297
298         $testDate = 'next_month';
299
300         $this->array['date_entered_advanced_range_choice'] = $testDate;
301         $this->array['range_date_entered_advanced'] = "[$testDate]";
302
303         $now = $timedate->getNow(true);
304         $month = $now->get_day_begin(1, $now->month+1);
305         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
306         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin($month->days_in_month));
307
308         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
309                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
310
311         $this->form->populateFromArray($this->array);
312         $query = $this->form->generateSearchWhere($this->seed, $this->module);
313
314         $this->assertContains($expected, $query[0]);
315     }
316
317     public function testSearchLastYearAdjustsForTimeZone() {
318         global $timedate;
319         $user = $GLOBALS['current_user'];
320
321         $testDate = 'last_year';
322
323         $this->array['date_entered_advanced_range_choice'] = $testDate;
324         $this->array['range_date_entered_advanced'] = "[$testDate]";
325
326         $now = $timedate->getNow(true);
327         $month = $now->get_day_begin(1, 1, $now->year-1);
328         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
329         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin(31, 12));
330
331         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
332                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
333
334         $this->form->populateFromArray($this->array);
335         $query = $this->form->generateSearchWhere($this->seed, $this->module);
336
337         $this->assertContains($expected, $query[0]);
338     }
339
340     public function testSearchThisYearAdjustsForTimeZone() {
341         global $timedate;
342         $user = $GLOBALS['current_user'];
343
344         $testDate = 'this_year';
345
346         $this->array['date_entered_advanced_range_choice'] = $testDate;
347         $this->array['range_date_entered_advanced'] = "[$testDate]";
348
349         $month = $timedate->getNow(true)->get_day_begin(1, 1);
350         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
351         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin(31, 12));
352
353         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
354                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
355
356         $this->form->populateFromArray($this->array);
357         $query = $this->form->generateSearchWhere($this->seed, $this->module);
358
359         $this->assertContains($expected, $query[0]);
360     }
361
362     public function testSearchNextYearAdjustsForTimeZone() {
363         global $timedate;
364         $user = $GLOBALS['current_user'];
365
366         $testDate = 'next_year';
367
368         $this->array['date_entered_advanced_range_choice'] = $testDate;
369         $this->array['range_date_entered_advanced'] = "[$testDate]";
370
371         $now = $timedate->getNow(true);
372         $month = $now->get_day_begin(1, 1, $now->year+1);
373         $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
374         $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin(31, 12));
375
376         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjThisMonthFirstDay['start']), 'datetime').
377                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjThisMonthLastDay['end']), 'datetime');
378
379         $this->form->populateFromArray($this->array);
380         $query = $this->form->generateSearchWhere($this->seed, $this->module);
381
382         $this->assertContains($expected, $query[0]);
383     }
384
385     public function testSearchDateIsBetweenAdjustsForTimeZone() {
386         global $timedate;
387         $user = $GLOBALS['current_user'];
388
389         $testStartDate = '01/01/2011';
390         $testEndDate = '12/31/2011';
391
392         $this->array['date_entered_advanced_range_choice'] = 'between';
393         $this->array['start_range_date_entered_advanced'] = $testStartDate;
394         $this->array['end_range_date_entered_advanced'] = $testEndDate;
395
396         $adjStartDate = $timedate->getDayStartEndGMT($testStartDate, $user);
397         $adjEndDate = $timedate->getDayStartEndGMT($testEndDate, $user);
398
399         $expected = strtolower($this->module).".date_entered >= ".$user->db->convert($user->db->quoted($adjStartDate['start']), 'datetime').
400                 " AND ". strtolower($this->module).".date_entered <= ".$user->db->convert($user->db->quoted($adjEndDate['end']), 'datetime');
401
402         $this->form->populateFromArray($this->array);
403         $query = $this->form->generateSearchWhere($this->seed, $this->module);
404
405         $this->assertContains($expected, $query[0]);
406     }
407
408 }