2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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($where_clauses[0], "opportunities.amount > '10000'");
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($where_clauses[0], "opportunities.amount < '10000'");
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($where_clauses[0], "opportunities.amount >= '10000'");
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($where_clauses[0], "opportunities.amount <= '10000'");
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($where_clauses[0], "opportunities.amount != '10000'");
188 $this->searchForm->searchFields['range_amount'] = array (
189 'query_type' => 'default',
190 'enable_range_search' => 1,
195 $where_clauses = $this->searchForm->generateSearchWhere();
196 $this->assertEquals($where_clauses[0], "opportunities.amount >= '9999.99' AND opportunities.amount <= '10000.01'");
201 * testRangeSearchWithSavedReportValues
202 * This test attempts to simulate testing what would happen should a saved report be invoked against
203 * a range search field
206 public function testRangeSearchWithSavedReportValues()
208 require_once('include/SugarFields/Fields/Datetime/SugarFieldDatetime.php');
209 $parentFieldArray = 'fields';
212 $vardef['name'] = 'date_closed_advanced';
213 $vardef['vname'] = 'LBL_DATE_CLOSED';
215 $opportunity = new Opportunity();
216 $vardef = $opportunity->field_defs['date_closed'];
217 $vardef['name'] = 'date_closed_advanced';
218 $vardef['options'] = array
221 'not_equal' => 'Not On',
222 'greater_than' => ' After',
223 'less_than' => ' Before',
224 'last_7_days' => ' Last 7 Days',
225 'next_7_days' => ' Next 7 Days',
226 'last_30_days' => ' Last 30 Days',
227 'next_30_days' => ' Next 30 Days',
228 'last_month' => ' Last Month',
229 'this_month' => ' This Month',
230 'next_month' => ' Next Month',
231 'last_year' => ' Last Year',
232 'this_year' => ' This Year',
233 'next_year' => ' Next Year',
234 'between' => ' Is Between',
238 $displayParams = array('labelSpan'=>'', 'fieldSpan'=>'');
241 $sugarFieldDatetime = new SugarFieldDatetime('Datetime');
243 $_REQUEST['action'] = 'SearchForm';
244 $html = $sugarFieldDatetime->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
246 //Write this widget's contents to a file
247 $this->smartyTestFile = 'tests/include/SearchForm/RangeSearchTest.tpl';
248 $handle = sugar_fopen($this->smartyTestFile, 'wb');
249 fwrite($handle, $html);
251 //Check that the template exists before we proceed with further tests
252 $this->assertTrue(file_exists($this->smartyTestFile));
254 //Stuff Smarty variables
255 $vardef['value'] = '';
257 $fields['date_closed_advanced'] = $vardef;
259 //Create Smarty instance
260 require_once('include/Sugar_Smarty.php');
261 $ss = new Sugar_Smarty();
263 //Assign Smarty variables
264 $ss->assign('fields', $fields);
265 $ss->assign('APP', $GLOBALS['app_strings']);
266 $ss->assign('CALENDAR_FORMAT', 'm-d-Y');
268 //Simulate the request with saved report value
269 $_REQUEST['date_closed_advanced'] = '07-03-2009';
271 $output = $ss->fetch($this->smartyTestFile);
272 $this->assertRegExp("/range_date_closed_advanced\"\s+?value\s*?\=s*?\'07\-03\-2009\'/", $output);
274 //Simulate the request with range search value
275 $_REQUEST['range_date_closed_advanced'] = '07-04-2009';
277 $output = $ss->fetch($this->smartyTestFile);
278 $this->assertRegExp("/range_date_closed_advanced\"\s+?value\s*?\=s*?\'07\-04\-2009\'/", $output);