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 ********************************************************************************/
38 require_once 'include/utils.php';
40 class MiscUtilsTest extends Sugar_PHPUnit_Framework_TestCase
42 public function providerIsGuid()
45 array('yuckyuck',false),
46 array('1d6a784b-e082-8522-9b1c-4d48e25c5e03',true),
47 array('1d6a784b-e082-8522-9b1c-4d48e2c5e03',false),
52 * @dataProvider providerIsGuid
54 public function testIsGuid(
59 $this->assertEquals($expectedResult,is_guid($guid));
62 public function testGenerateWhereStatement()
64 $where = array("dog = '1'","cat = '3'");
66 $this->assertEquals("dog = '1' and cat = '3'",generate_where_statement($where));
69 public function testAppendWhereClause()
72 $_REQUEST['dog'] = 'yuck';
74 append_where_clause($where,'dog');
76 unset($_REQUEST['dog']);
78 $this->assertEquals("dog like 'yuck%'",$where[0]);
81 public function testAppendWhereClauseDifferentColumnName()
84 $_REQUEST['dog'] = 'yuck';
86 append_where_clause($where,'dog','cat');
88 unset($_REQUEST['dog']);
90 $this->assertEquals("cat like 'yuck%'",$where[0]);
93 public function testReturnSessionOrDefaultWhenSessionSet()
95 $_SESSION['yellow'] = 'blue';
97 $result = return_session_value_or_default('yellow','red');
99 unset($_SESSION['yellow']);
101 $this->assertEquals('blue',$result);
104 public function testReturnSessionOrDefaultWhenSessionIsNotSet()
106 $result = return_session_value_or_default('yellow','red');
108 $this->assertEquals('red',$result);
111 public function testGetVariableFromQueryString()
113 $this->assertEquals('123',getVariableFromQueryString('great','ok=12&great=123&bad=234'));
116 public function testGetVariableFromQueryStringWhenNoVariableFound()
118 $this->assertFalse(getVariableFromQueryString('horrible','ok=12&great=123&bad=234'));
121 public function testSugarUcfirst()
123 $this->assertEquals('John',sugar_ucfirst('John'));
126 public function testFilterInboundEmailPopSelection()
128 $protocals = array('pop3' => 'POP3','imap' => 'IMAP');
130 if ( isset($GLOBALS['sugar_config']['allow_pop_inbound']) ) {
131 $oldsetting = $GLOBALS['sugar_config']['allow_pop_inbound'];
133 $GLOBALS['sugar_config']['allow_pop_inbound'] = false;
135 $protocals = filterInboundEmailPopSelection($protocals);
137 if ( isset($oldsetting) ) {
138 $GLOBALS['sugar_config']['allow_pop_inbound'] = $oldsetting;
141 unset($GLOBALS['sugar_config']['allow_pop_inbound']);
144 $this->assertEquals(array('imap' => 'IMAP'),$protocals);
147 public function testFilterInboundEmailPopSelectionWhenItShouldBethere()
149 $protocals = array('imap' => 'IMAP');
151 if ( isset($GLOBALS['sugar_config']['allow_pop_inbound']) ) {
152 $oldsetting = $GLOBALS['sugar_config']['allow_pop_inbound'];
154 $GLOBALS['sugar_config']['allow_pop_inbound'] = true;
156 $protocals = filterInboundEmailPopSelection($protocals);
158 if ( isset($oldsetting) ) {
159 $GLOBALS['sugar_config']['allow_pop_inbound'] = $oldsetting;
162 unset($GLOBALS['sugar_config']['allow_pop_inbound']);
165 $this->assertEquals(array('imap' => 'IMAP','pop3' => 'POP3',),$protocals);
168 public function testIsTouchScreenReturnsTrueIfCookieSet()
170 $_COOKIE['touchscreen'] = '1';
172 $result = isTouchScreen();
174 unset($_COOKIE['touchscreen']);
176 $this->assertTrue($result);
179 public function testIsTouchScreenReturnsTrueIfUserAgentIsIpad()
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';
183 $result = isTouchScreen();
185 unset($_SERVER['HTTP_USER_AGENT']);
187 $this->assertTrue($result);
190 public function testIsTouchScreenReturnsTrueIfUserAgentIsNotIpad()
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';
194 $result = isTouchScreen();
196 unset($_SERVER['HTTP_USER_AGENT']);
198 $this->assertFalse($result);
201 public function testValuesToKeys()
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');
208 public function testNumberEmpty()
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");