]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SearchForm/RangeSearchTest.php
Release 6.4.0
[Github/sugarcrm.git] / tests / include / SearchForm / RangeSearchTest.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 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';
42
43 class RangeSearchTest extends Sugar_PHPUnit_Framework_TestCase
44 {
45     private $hasExistingCustomSearchFields = false;
46     var $searchForm;
47     var $originalDbType;
48     var $smartyTestFile;
49
50     public function setUp()
51     {
52                 if(file_exists('custom/modules/Opportunities/metadata/SearchFields.php'))
53                 {
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');
59                 }
60
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');
68
69                 //Prepare SearchForm
70         $seed = new Opportunity();
71         $module = 'Opportunities';
72                 $this->searchForm = new SearchForm($seed, $module);
73                 $this->searchForm->searchFields = array(
74                         'range_date_closed' => array
75                 (
76                     'query_type' => 'default',
77                     'enable_range_search' => 1,
78                     'is_date_field' => 1,
79                     'value' => '[this_year]',
80                     'operator' => 'this_year',
81                 ),
82                 'start_range_date_closed' => array
83                 (
84                     'query_type' => 'default',
85                     'enable_range_search' => 1,
86                     'is_date_field' => 1,
87                 ),
88                 'end_range_date_closed' => array
89                 (
90                     'query_type' => 'default',
91                     'enable_range_search' => 1,
92                     'is_date_field' => 1,
93                 ),
94                 'range_amount' => array
95                 (
96                         'query_type' => 'default',
97                         'enable_range_search' => true
98                 ),
99                         'start_range_amount' => array
100                 (
101                         'query_type' => 'default',
102                         'enable_range_search' => true
103                 ),
104                 'end_range_amount' => array (
105                         'query_type' => 'default',
106                         'enable_range_search' => true
107                 ),
108                 );
109
110                 $this->originalDbType = $GLOBALS['db']->dbType;
111     }
112
113     public function tearDown()
114     {
115                 $GLOBALS['db']->dbType = $this->originalDbType;
116
117         if(!$this->hasExistingCustomSearchFields)
118                 {
119                    unlink('custom/modules/Opportunities/metadata/SearchFields.php');
120                 }
121
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');
125                 }
126
127                 if(file_exists($this->smartyTestFile))
128                 {
129                         unlink($this->smartyTestFile);
130                 }
131
132     }
133
134     public function testRangeNumberSearches()
135     {
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,
141                     'value' => '10000',
142                     'operator' => 'greater_than',
143             );
144
145                 $where_clauses = $this->searchForm->generateSearchWhere();
146                 $this->assertEquals("opportunities.amount > 10000", $where_clauses[0]);
147
148                 $this->searchForm->searchFields['range_amount'] = array (
149                     'query_type' => 'default',
150                     'enable_range_search' => 1,
151                     'value' => '10000',
152                     'operator' => 'less_than',
153             );
154
155                 $where_clauses = $this->searchForm->generateSearchWhere();
156                 $this->assertEquals("opportunities.amount < 10000", $where_clauses[0]);
157
158                 $this->searchForm->searchFields['range_amount'] = array (
159                     'query_type' => 'default',
160                     'enable_range_search' => 1,
161                     'value' => '10000',
162                     'operator' => 'greater_than_equals',
163             );
164
165                 $where_clauses = $this->searchForm->generateSearchWhere();
166                 $this->assertEquals("opportunities.amount >= 10000", $where_clauses[0]);
167
168                 $this->searchForm->searchFields['range_amount'] = array (
169                     'query_type' => 'default',
170                     'enable_range_search' => 1,
171                     'value' => '10000',
172                     'operator' => 'less_than_equals',
173             );
174
175                 $where_clauses = $this->searchForm->generateSearchWhere();
176                 $this->assertEquals("opportunities.amount <= 10000", $where_clauses[0]);
177
178                 $this->searchForm->searchFields['range_amount'] = array (
179                     'query_type' => 'default',
180                     'enable_range_search' => 1,
181                     'value' => '10000',
182                     'operator' => 'not_equal',
183             );
184
185                 $where_clauses = $this->searchForm->generateSearchWhere();
186                 $this->assertEquals("(opportunities.amount IS NULL OR opportunities.amount != 10000)", $where_clauses[0]);
187
188                 $this->searchForm->searchFields['range_amount'] = array (
189                     'query_type' => 'default',
190                     'enable_range_search' => 1,
191                     'value' => '10000',
192                     'operator' => '=',
193
194             );    
195
196                 $where_clauses = $this->searchForm->generateSearchWhere();              
197                 $this->assertEquals("(opportunities.amount >= 9999.99 AND opportunities.amount <= 10000.01)", $where_clauses[0]);
198                 
199     }
200     
201     /**
202      * testRangeSearchWithSavedReportValues
203      * This test attempts to simulate testing what would happen should a saved report be invoked against
204      * a range search field
205      *
206      */
207     public function testRangeSearchWithSavedReportValues()
208     {
209         require_once('include/SugarFields/Fields/Datetime/SugarFieldDatetime.php');
210         $parentFieldArray = 'fields';
211
212         $vardef = array();
213         $vardef['name'] = 'date_closed_advanced';
214         $vardef['vname'] = 'LBL_DATE_CLOSED';
215
216                 $opportunity = new Opportunity();
217                 $vardef = $opportunity->field_defs['date_closed'];
218                 $vardef['name'] = 'date_closed_advanced';
219                 $vardef['options'] = array
220         (
221             '=' => 'Equals',
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',
236         );
237
238
239                 $displayParams = array('labelSpan'=>'', 'fieldSpan'=>'');
240                 $tabindex = '';
241
242                 $sugarFieldDatetime = new SugarFieldDatetime('Datetime');
243
244                 $_REQUEST['action'] = 'SearchForm';
245                 $html = $sugarFieldDatetime->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
246
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);
251
252         //Check that the template exists before we proceed with further tests
253         $this->assertTrue(file_exists($this->smartyTestFile));
254
255         //Stuff Smarty variables
256         $vardef['value'] = '';
257         $fields = array();
258         $fields['date_closed_advanced'] = $vardef;
259
260         //Create Smarty instance
261         require_once('include/Sugar_Smarty.php');
262         $ss = new Sugar_Smarty();
263
264         //Assign Smarty variables
265         $ss->assign('fields', $fields);
266         $ss->assign('APP', $GLOBALS['app_strings']);
267         $ss->assign('CALENDAR_FORMAT', 'm-d-Y');
268
269         //Simulate the request with saved report value
270         $_REQUEST['date_closed_advanced'] = '07-03-2009';
271
272                 $output = $ss->fetch($this->smartyTestFile);
273         $this->assertRegExp('/range_date_closed_advanced\"\s+?value\s*?\=s*?\'07\-03\-2009\'/', $output);
274
275         //Simulate the request with range search value
276         $_REQUEST['range_date_closed_advanced'] = '07-04-2009';
277
278                 $output = $ss->fetch($this->smartyTestFile);
279         $this->assertRegExp('/range_date_closed_advanced\"\s+?value\s*?\=s*?\'07\-04\-2009\'/', $output);
280     }
281
282 }
283 ?>