2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 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 ********************************************************************************/
38 require_once 'modules/DynamicFields/templates/Fields/TemplateInt.php';
39 require_once 'modules/DynamicFields/templates/Fields/TemplateDate.php';
40 require_once 'include/SearchForm/SearchForm2.php';
41 require_once 'modules/Opportunities/Opportunity.php';
43 class RangeSearchTest extends Sugar_PHPUnit_Framework_TestCase
45 private $hasExistingCustomSearchFields = false;
50 public function setUp()
52 if(file_exists('custom/modules/Opportunities/metadata/SearchFields.php'))
54 $this->hasExistingCustomSearchFields = true;
55 copy('custom/modules/Opportunities/metadata/SearchFields.php', 'custom/modules/Opportunities/metadata/SearchFields.php.bak');
56 unlink('custom/modules/Opportunities/metadata/SearchFields.php');
57 } else if(!file_exists('custom/modules/Opportunities/metadata')) {
58 mkdir_recursive('custom/modules/Opportunities/metadata');
61 //Setup Opportunities module and date_closed field
62 $_REQUEST['view_module'] = 'Opportunities';
63 $_REQUEST['name'] = 'date_closed';
64 $templateDate = new TemplateDate();
65 $templateDate->enable_range_search = true;
66 $templateDate->populateFromPost();
67 include('custom/modules/Opportunities/metadata/SearchFields.php');
70 $seed = new Opportunity();
71 $module = 'Opportunities';
72 $this->searchForm = new SearchForm($seed, $module);
73 $this->searchForm->searchFields = array(
74 'range_date_closed' => array
76 'query_type' => 'default',
77 'enable_range_search' => 1,
79 'value' => '[this_year]',
80 'operator' => 'this_year',
82 'start_range_date_closed' => array
84 'query_type' => 'default',
85 'enable_range_search' => 1,
88 'end_range_date_closed' => array
90 'query_type' => 'default',
91 'enable_range_search' => 1,
94 'range_amount' => array
96 'query_type' => 'default',
97 'enable_range_search' => true
99 'start_range_amount' => array
101 'query_type' => 'default',
102 'enable_range_search' => true
104 'end_range_amount' => array (
105 'query_type' => 'default',
106 'enable_range_search' => true
110 $this->originalDbType = $GLOBALS['db']->dbType;
113 public function tearDown()
115 $GLOBALS['db']->dbType = $this->originalDbType;
117 if(!$this->hasExistingCustomSearchFields)
119 unlink('custom/modules/Opportunities/metadata/SearchFields.php');
122 if(file_exists('custom/modules/Opportunities/metadata/SearchFields.php.bak')) {
123 copy('custom/modules/Opportunities/metadata/SearchFields.php.bak', 'custom/modules/Opportunities/metadata/SearchFields.php');
124 unlink('custom/modules/Opportunities/metadata/SearchFields.php.bak');
127 if(file_exists($this->smartyTestFile))
129 unlink($this->smartyTestFile);
134 public function testRangeNumberSearches()
136 $GLOBALS['db']->dbType = 'mysql';
137 unset($this->searchForm->searchFields['range_date_closed']);
138 $this->searchForm->searchFields['range_amount'] = array (
139 'query_type' => 'default',
140 'enable_range_search' => 1,
142 'operator' => 'greater_than',
145 $where_clauses = $this->searchForm->generateSearchWhere();
146 $this->assertEquals("opportunities.amount > 10000", $where_clauses[0]);
148 $this->searchForm->searchFields['range_amount'] = array (
149 'query_type' => 'default',
150 'enable_range_search' => 1,
152 'operator' => 'less_than',
155 $where_clauses = $this->searchForm->generateSearchWhere();
156 $this->assertEquals("opportunities.amount < 10000", $where_clauses[0]);
158 $this->searchForm->searchFields['range_amount'] = array (
159 'query_type' => 'default',
160 'enable_range_search' => 1,
162 'operator' => 'greater_than_equals',
165 $where_clauses = $this->searchForm->generateSearchWhere();
166 $this->assertEquals("opportunities.amount >= 10000", $where_clauses[0]);
168 $this->searchForm->searchFields['range_amount'] = array (
169 'query_type' => 'default',
170 'enable_range_search' => 1,
172 'operator' => 'less_than_equals',
175 $where_clauses = $this->searchForm->generateSearchWhere();
176 $this->assertEquals("opportunities.amount <= 10000", $where_clauses[0]);
178 $this->searchForm->searchFields['range_amount'] = array (
179 'query_type' => 'default',
180 'enable_range_search' => 1,
182 'operator' => 'not_equal',
185 $where_clauses = $this->searchForm->generateSearchWhere();
186 $this->assertEquals("(opportunities.amount IS NULL OR opportunities.amount != 10000)", $where_clauses[0]);
188 $this->searchForm->searchFields['range_amount'] = array (
189 'query_type' => 'default',
190 'enable_range_search' => 1,
196 $where_clauses = $this->searchForm->generateSearchWhere();
197 $this->assertEquals("(opportunities.amount >= 9999.99 AND opportunities.amount <= 10000.01)", $where_clauses[0]);
202 * testRangeSearchWithSavedReportValues
203 * This test attempts to simulate testing what would happen should a saved report be invoked against
204 * a range search field
207 public function testRangeSearchWithSavedReportValues()
209 require_once('include/SugarFields/Fields/Datetime/SugarFieldDatetime.php');
210 $parentFieldArray = 'fields';
213 $vardef['name'] = 'date_closed_advanced';
214 $vardef['vname'] = 'LBL_DATE_CLOSED';
216 $opportunity = new Opportunity();
217 $vardef = $opportunity->field_defs['date_closed'];
218 $vardef['name'] = 'date_closed_advanced';
219 $vardef['options'] = array
222 'not_equal' => 'Not On',
223 'greater_than' => ' After',
224 'less_than' => ' Before',
225 'last_7_days' => ' Last 7 Days',
226 'next_7_days' => ' Next 7 Days',
227 'last_30_days' => ' Last 30 Days',
228 'next_30_days' => ' Next 30 Days',
229 'last_month' => ' Last Month',
230 'this_month' => ' This Month',
231 'next_month' => ' Next Month',
232 'last_year' => ' Last Year',
233 'this_year' => ' This Year',
234 'next_year' => ' Next Year',
235 'between' => ' Is Between',
239 $displayParams = array('labelSpan'=>'', 'fieldSpan'=>'');
242 $sugarFieldDatetime = new SugarFieldDatetime('Datetime');
244 $_REQUEST['action'] = 'SearchForm';
245 $html = $sugarFieldDatetime->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
247 //Write this widget's contents to a file
248 $this->smartyTestFile = 'tests/include/SearchForm/RangeSearchTest.tpl';
249 $handle = sugar_fopen($this->smartyTestFile, 'wb');
250 fwrite($handle, $html);
252 //Check that the template exists before we proceed with further tests
253 $this->assertTrue(file_exists($this->smartyTestFile));
255 //Stuff Smarty variables
256 $vardef['value'] = '';
258 $fields['date_closed_advanced'] = $vardef;
260 //Create Smarty instance
261 require_once('include/Sugar_Smarty.php');
262 $ss = new Sugar_Smarty();
264 //Assign Smarty variables
265 $ss->assign('fields', $fields);
266 $ss->assign('APP', $GLOBALS['app_strings']);
267 $ss->assign('CALENDAR_FORMAT', 'm-d-Y');
269 //Simulate the request with saved report value
270 $_REQUEST['date_closed_advanced'] = '07-03-2009';
272 $output = $ss->fetch($this->smartyTestFile);
273 $this->assertRegExp('/range_date_closed_advanced\"\s+?value\s*?\=s*?\'07\-03\-2009\'/', $output);
275 //Simulate the request with range search value
276 $_REQUEST['range_date_closed_advanced'] = '07-04-2009';
278 $output = $ss->fetch($this->smartyTestFile);
279 $this->assertRegExp('/range_date_closed_advanced\"\s+?value\s*?\=s*?\'07\-04\-2009\'/', $output);