]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/utils/MiscUtilsTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / include / utils / MiscUtilsTest.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 'include/utils.php';
39
40 class MiscUtilsTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42         public function providerIsGuid()
43         {
44                 return array(
45             array('yuckyuck',false),
46             array('1d6a784b-e082-8522-9b1c-4d48e25c5e03',true),
47             array('1d6a784b-e082-8522-9b1c-4d48e2c5e03',false),
48         );
49     }
50     
51         /**
52      * @dataProvider providerIsGuid
53      */
54         public function testIsGuid(
55             $guid, 
56             $expectedResult
57             )
58         {
59             $this->assertEquals($expectedResult,is_guid($guid));
60         }
61         
62         public function testGenerateWhereStatement()
63         {
64                 $where = array("dog = '1'","cat = '3'");
65                 
66                 $this->assertEquals("dog = '1' and cat = '3'",generate_where_statement($where));
67     }
68     
69     public function testAppendWhereClause()
70     {
71         $where = array();
72         $_REQUEST['dog'] = 'yuck';
73         
74         append_where_clause($where,'dog');
75         
76         unset($_REQUEST['dog']);
77         
78         $this->assertEquals("dog like 'yuck%'",$where[0]);
79     }
80     
81     public function testAppendWhereClauseDifferentColumnName()
82     {
83         $where = array();
84         $_REQUEST['dog'] = 'yuck';
85         
86         append_where_clause($where,'dog','cat');
87         
88         unset($_REQUEST['dog']);
89         
90         $this->assertEquals("cat like 'yuck%'",$where[0]);
91     }
92     
93     public function testReturnSessionOrDefaultWhenSessionSet()
94     {
95         $_SESSION['yellow'] = 'blue';
96         
97         $result = return_session_value_or_default('yellow','red');
98         
99         unset($_SESSION['yellow']);
100         
101         $this->assertEquals('blue',$result);
102     }
103     
104     public function testReturnSessionOrDefaultWhenSessionIsNotSet()
105     {
106         $result = return_session_value_or_default('yellow','red');
107         
108         $this->assertEquals('red',$result);
109     }
110     
111     public function testGetVariableFromQueryString()
112     {
113         $this->assertEquals('123',getVariableFromQueryString('great','ok=12&great=123&bad=234'));
114     }
115     
116     public function testGetVariableFromQueryStringWhenNoVariableFound()
117     {
118         $this->assertFalse(getVariableFromQueryString('horrible','ok=12&great=123&bad=234'));
119     }
120     
121     public function testSugarUcfirst()
122     {
123         $this->assertEquals('John',sugar_ucfirst('John'));
124     }
125     
126     public function testFilterInboundEmailPopSelection()
127     {
128         $protocals = array('pop3' => 'POP3','imap' => 'IMAP');
129         
130         if ( isset($GLOBALS['sugar_config']['allow_pop_inbound']) ) {
131             $oldsetting = $GLOBALS['sugar_config']['allow_pop_inbound'];
132         }
133         $GLOBALS['sugar_config']['allow_pop_inbound'] = false;
134         
135         $protocals = filterInboundEmailPopSelection($protocals);
136         
137         if ( isset($oldsetting) ) {
138             $GLOBALS['sugar_config']['allow_pop_inbound'] = $oldsetting;
139         }
140         else {
141             unset($GLOBALS['sugar_config']['allow_pop_inbound']);
142         }
143         
144         $this->assertEquals(array('imap' => 'IMAP'),$protocals);
145     }
146     
147     public function testFilterInboundEmailPopSelectionWhenItShouldBethere()
148     {
149         $protocals = array('imap' => 'IMAP');
150         
151         if ( isset($GLOBALS['sugar_config']['allow_pop_inbound']) ) {
152             $oldsetting = $GLOBALS['sugar_config']['allow_pop_inbound'];
153         }
154         $GLOBALS['sugar_config']['allow_pop_inbound'] = true;
155         
156         $protocals = filterInboundEmailPopSelection($protocals);
157         
158         if ( isset($oldsetting) ) {
159             $GLOBALS['sugar_config']['allow_pop_inbound'] = $oldsetting;
160         }
161         else {
162             unset($GLOBALS['sugar_config']['allow_pop_inbound']);
163         }
164         
165         $this->assertEquals(array('imap' => 'IMAP','pop3' => 'POP3',),$protocals);
166     }
167     
168     public function testIsTouchScreenReturnsTrueIfCookieSet()
169     {
170         $_COOKIE['touchscreen'] = '1';
171         
172         $result = isTouchScreen();
173         
174         unset($_COOKIE['touchscreen']);
175         
176         $this->assertTrue($result);
177     }
178     
179     public function testIsTouchScreenReturnsTrueIfUserAgentIsIpad()
180     {
181         $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10';
182         
183         $result = isTouchScreen();
184         
185         unset($_SERVER['HTTP_USER_AGENT']);
186         
187         $this->assertTrue($result);
188     }
189     
190     public function testIsTouchScreenReturnsTrueIfUserAgentIsNotIpad()
191     {
192         $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3';
193         
194         $result = isTouchScreen();
195         
196         unset($_SERVER['HTTP_USER_AGENT']);
197         
198         $this->assertFalse($result);
199     }
200
201     public function testValuesToKeys()
202     {
203         $arr = array('apples', 'oranges', 'lemons', 'strawberries');
204         $newArr = values_to_keys($arr);
205         $this->assertArrayHasKey('lemons', $newArr, 'The conversion to hash did not work properly');
206     }
207
208     public function testNumberEmpty()
209     {
210         $num1 = 0;
211         $num2 = '0';
212         $num3 = -1;
213         $num4 = 'true';
214         $num5 = 'false';
215         $num6 = false;
216         $num7 = 10;
217         $num8 = '10';
218         $num9 = '';
219
220         $this->assertEquals(false, number_empty($num1), "Found 0 to be empty");
221         $this->assertEquals(false, number_empty($num2), "Found '0' to be empty");
222         $this->assertEquals(false, number_empty($num3), "Found -1 to be empty");
223         $this->assertEquals(false, number_empty($num4), "Found 'true' to be empty");
224         $this->assertEquals(false, number_empty($num5), "Found 'false' to be empty");
225         $this->assertEquals(false, number_empty($num6), "Found false to be empty");
226         $this->assertEquals(false, number_empty($num7), "Found 10 to be empty");
227         $this->assertEquals(false, number_empty($num8), "Found '10' to be empty");
228         $this->assertEquals(true, number_empty($num9), "Did not find empty string to be empty");
229     }
230 }