2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
39 require_once 'modules/Notes/Note.php';
40 require_once 'include/SearchForm/SearchForm2.php';
45 class Bug45966 extends Sugar_PHPUnit_Framework_TestCase {
47 var $module = 'Notes';
48 var $action = 'index';
53 public function setUp() {
54 require('include/modules.php');
55 $GLOBALS['beanList'] = $beanList;
56 $GLOBALS['beanFiles'] = $beanFiles;
58 require "modules/".$this->module."/metadata/searchdefs.php";
59 require "modules/".$this->module."/metadata/SearchFields.php";
60 require "modules/".$this->module."/metadata/listviewdefs.php";
62 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
63 $GLOBALS['current_user']->setPreference('timezone', 'EDT');
64 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
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);
71 'module'=>$this->module,
72 'action'=>$this->action,
73 'searchFormTab'=>'advanced_search',
75 'date_entered_advanced_range_choice'=>'',
76 'range_date_entered_advanced' => '',
77 'start_range_date_entered_advanced' => '',
78 'end_range_date_entered_advanced' => '',
82 public function tearDown()
87 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
88 unset($GLOBALS['current_user']);
91 public function testSearchDateEqualsAdjustsForTimeZone() {
93 $user = $GLOBALS['current_user'];
95 $testDate = '12/31/2011';
97 $this->array['date_entered_advanced_range_choice'] = '=';
98 $this->array['range_date_entered_advanced'] = $testDate;
100 $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
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');
105 $this->form->populateFromArray($this->array);
106 $query = $this->form->generateSearchWhere($this->seed, $this->module);
108 $this->assertContains($expected, $query[0]);
111 public function testSearchNotOnDateAdjustsForTimeZone() {
113 $user = $GLOBALS['current_user'];
115 $testDate = '12/31/2011';
117 $this->array['date_entered_advanced_range_choice'] = 'not_equal';
118 $this->array['range_date_entered_advanced'] = $testDate;
120 $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
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');
125 $this->form->populateFromArray($this->array);
126 $query = $this->form->generateSearchWhere($this->seed, $this->module);
128 $this->assertContains($expected, $query[0]);
131 public function testSearchAfterDateAdjustsForTimeZone() {
133 $user = $GLOBALS['current_user'];
135 $testDate = '12/31/2011';
137 $this->array['date_entered_advanced_range_choice'] = 'greater_than';
138 $this->array['range_date_entered_advanced'] = $testDate;
139 $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
141 $expected = "notes.date_entered > ".$user->db->convert($user->db->quoted($adjDate['end']), 'datetime');
143 $this->form->populateFromArray($this->array);
144 $query = $this->form->generateSearchWhere($this->seed, $this->module);
146 $this->assertContains($expected, $query[0]);
149 public function testSearchBeforeDateAdjustsForTimeZone() {
151 $user = $GLOBALS['current_user'];
153 $testDate = '01/01/2011';
155 $this->array['date_entered_advanced_range_choice'] = 'less_than';
156 $this->array['range_date_entered_advanced'] = $testDate;
157 $adjDate = $timedate->getDayStartEndGMT($testDate, $user);
159 $expected = "notes.date_entered < ".$user->db->convert($user->db->quoted($adjDate['start']), 'datetime');
161 $this->form->populateFromArray($this->array);
162 $query = $this->form->generateSearchWhere($this->seed, $this->module);
164 $this->assertContains($expected, $query[0]);
167 public function testSearchLastSevenDaysAdjustsForTimeZone() {
169 $user = $GLOBALS['current_user'];
171 $testDate = 'last_7_days';
173 $this->array['date_entered_advanced_range_choice'] = $testDate;
174 $this->array['range_date_entered_advanced'] = "[$testDate]";
176 $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
177 $adjStartDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("-6 days"));
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');
182 $this->form->populateFromArray($this->array);
183 $query = $this->form->generateSearchWhere($this->seed, $this->module);
185 $this->assertContains($expected, $query[0]);
188 public function testSearchNextSevenDaysAdjustsForTimeZone() {
190 $user = $GLOBALS['current_user'];
192 $testDate = 'next_7_days';
194 $this->array['date_entered_advanced_range_choice'] = $testDate;
195 $this->array['range_date_entered_advanced'] = "[$testDate]";
197 $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
198 $adjEndDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("+6 days"));
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');
203 $this->form->populateFromArray($this->array);
204 $query = $this->form->generateSearchWhere($this->seed, $this->module);
206 $this->assertContains($expected, $query[0]);
209 public function testSearchLastThirtyDaysAdjustsForTimeZone() {
211 $user = $GLOBALS['current_user'];
213 $testDate = 'last_30_days';
215 $this->array['date_entered_advanced_range_choice'] = $testDate;
216 $this->array['range_date_entered_advanced'] = "[$testDate]";
218 $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
219 $adjStartDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("-29 days"));
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');
224 $this->form->populateFromArray($this->array);
225 $query = $this->form->generateSearchWhere($this->seed, $this->module);
227 $this->assertContains($expected, $query[0]);
230 public function testSearchNextThirtyDaysAdjustsForTimeZone() {
232 $user = $GLOBALS['current_user'];
234 $testDate = 'next_30_days';
236 $this->array['date_entered_advanced_range_choice'] = $testDate;
237 $this->array['range_date_entered_advanced'] = "[$testDate]";
239 $adjToday = $timedate->getDayStartEndGMT($timedate->getNow(true));
240 $adjEndDate = $timedate->getDayStartEndGMT($timedate->getNow(true)->get("+29 days"));
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');
245 $this->form->populateFromArray($this->array);
246 $query = $this->form->generateSearchWhere($this->seed, $this->module);
248 $this->assertContains($expected, $query[0]);
251 public function testSearchLastMonthAdjustsForTimeZone() {
253 $user = $GLOBALS['current_user'];
255 $testDate = 'last_month';
257 $this->array['date_entered_advanced_range_choice'] = $testDate;
258 $this->array['range_date_entered_advanced'] = "[$testDate]";
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);
265 $adjThisMonthFirstDay = $timedate->getDayStartEndGMT($month);
266 $adjThisMonthLastDay = $timedate->getDayStartEndGMT($month->get_day_begin($month->days_in_month));
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');
271 $this->form->populateFromArray($this->array);
272 $query = $this->form->generateSearchWhere($this->seed, $this->module);
274 $this->assertContains($expected, $query[0]);
277 public function testSearchThisMonthAdjustsForTimeZone() {
279 $user = $GLOBALS['current_user'];
281 $testDate = 'this_month';
283 $this->array['date_entered_advanced_range_choice'] = $testDate;
284 $this->array['range_date_entered_advanced'] = "[$testDate]";
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));
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');
293 $this->form->populateFromArray($this->array);
294 $query = $this->form->generateSearchWhere($this->seed, $this->module);
296 $this->assertContains($expected, $query[0]);
299 public function testSearchNextMonthAdjustsForTimeZone() {
301 $user = $GLOBALS['current_user'];
303 $testDate = 'next_month';
305 $this->array['date_entered_advanced_range_choice'] = $testDate;
306 $this->array['range_date_entered_advanced'] = "[$testDate]";
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));
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');
316 $this->form->populateFromArray($this->array);
317 $query = $this->form->generateSearchWhere($this->seed, $this->module);
319 $this->assertContains($expected, $query[0]);
322 public function testSearchLastYearAdjustsForTimeZone() {
324 $user = $GLOBALS['current_user'];
326 $testDate = 'last_year';
328 $this->array['date_entered_advanced_range_choice'] = $testDate;
329 $this->array['range_date_entered_advanced'] = "[$testDate]";
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));
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');
339 $this->form->populateFromArray($this->array);
340 $query = $this->form->generateSearchWhere($this->seed, $this->module);
342 $this->assertContains($expected, $query[0]);
345 public function testSearchThisYearAdjustsForTimeZone() {
347 $user = $GLOBALS['current_user'];
349 $testDate = 'this_year';
351 $this->array['date_entered_advanced_range_choice'] = $testDate;
352 $this->array['range_date_entered_advanced'] = "[$testDate]";
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));
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');
361 $this->form->populateFromArray($this->array);
362 $query = $this->form->generateSearchWhere($this->seed, $this->module);
364 $this->assertContains($expected, $query[0]);
367 public function testSearchNextYearAdjustsForTimeZone() {
369 $user = $GLOBALS['current_user'];
371 $testDate = 'next_year';
373 $this->array['date_entered_advanced_range_choice'] = $testDate;
374 $this->array['range_date_entered_advanced'] = "[$testDate]";
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));
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');
384 $this->form->populateFromArray($this->array);
385 $query = $this->form->generateSearchWhere($this->seed, $this->module);
387 $this->assertContains($expected, $query[0]);
390 public function testSearchDateIsBetweenAdjustsForTimeZone() {
392 $user = $GLOBALS['current_user'];
394 $testStartDate = '01/01/2011';
395 $testEndDate = '12/31/2011';
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;
401 $adjStartDate = $timedate->getDayStartEndGMT($testStartDate, $user);
402 $adjEndDate = $timedate->getDayStartEndGMT($testEndDate, $user);
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');
407 $this->form->populateFromArray($this->array);
408 $query = $this->form->generateSearchWhere($this->seed, $this->module);
410 $this->assertContains($expected, $query[0]);