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 ********************************************************************************/
37 require_once 'include/database/MysqlManager.php';
39 class MysqlManagerTest extends Sugar_PHPUnit_Framework_TestCase
41 static public function setupBeforeClass()
43 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
44 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
47 static public function tearDownAfterClass()
49 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
50 unset($GLOBALS['current_user']);
51 unset($GLOBALS['app_strings']);
54 public function setUp()
56 if ( $GLOBALS['db']->dbType != 'mysql' ) {
57 $this->markTestSkipped('Only applies to MySQL');
60 $this->_db = new MysqlManager();
63 public function testQuote()
65 $string = "'dog eat ";
66 if(!$this->_db->valid()) $this->markTestSkipped("MySQL not enabled");
67 $this->assertEquals($this->_db->quote($string),"\\'dog eat ");
70 public function testArrayQuote()
72 if(!$this->_db->valid()) $this->markTestSkipped("MySQL not enabled");
73 $string = array("'dog eat ");
74 $this->_db->arrayQuote($string);
75 $this->assertEquals($string,array("\\'dog eat "));
78 public function providerConvert()
82 array('foo','nothing'),
94 array('foo','left',array('1','2','3')),
98 array('foo','date_format'),
99 'DATE_FORMAT(foo,\'%Y-%m-%d\')'
102 array('foo','date_format',array('1','2','3')),
103 'DATE_FORMAT(foo,\'1\')'
106 array('foo','date_format',array("'1'","'2'","'3'")),
107 'DATE_FORMAT(foo,\'1\')'
110 array('foo','datetime',array("'%Y-%m'")),
114 array('foo','IFNULL'),
118 array('foo','IFNULL',array('1','2','3')),
122 array('foo','CONCAT',array('1','2','3')),
126 array(array('1','2','3'),'CONCAT'),
130 array(array('1','2','3'),'CONCAT',array('foo', 'bar')),
131 'CONCAT(1,2,3,foo,bar)'
134 array('foo','text2char'),
138 array('foo','length'),
142 array('foo','month'),
146 array('foo','quarter'),
150 array('foo','add_date',array(1,'day')),
151 "DATE_ADD(foo, INTERVAL 1 day)"
154 array('foo','add_date',array(2,'week')),
155 "DATE_ADD(foo, INTERVAL 2 week)"
158 array('foo','add_date',array(3,'month')),
159 "DATE_ADD(foo, INTERVAL 3 month)"
162 array('foo','add_date',array(4,'quarter')),
163 "DATE_ADD(foo, INTERVAL 4 quarter)"
166 array('foo','add_date',array(5,'year')),
167 "DATE_ADD(foo, INTERVAL 5 year)"
175 * @dataProvider providerConvert
177 public function testConvert(array $parameters, $result)
179 $this->assertEquals($result, call_user_func_array(array($this->_db, "convert"), $parameters));
185 public function testConcat()
187 $ret = $this->_db->concat('foo',array('col1','col2','col3'));
188 $this->assertEquals("LTRIM(RTRIM(CONCAT(IFNULL(foo.col1,''),' ',IFNULL(foo.col2,''),' ',IFNULL(foo.col3,''))))", $ret);
191 public function providerFromConvert()
193 $returnArray = array(
195 array('foo','nothing'),
199 array('2009-01-01 12:00:00','date'),
200 '2009-01-01 12:00:00'
203 array('2009-01-01 12:00:00','time'),
204 '2009-01-01 12:00:00'
213 * @dataProvider providerFromConvert
215 public function testFromConvert(
221 $this->_db->fromConvert($parameters[0],$parameters[1]),
225 public function providerFullTextQuery()
228 array(array('word1'), array(), array(),
229 "MATCH(unittest) AGAINST('word1' IN BOOLEAN MODE)"),
230 array(array("'word1'"), array(), array(),
231 "MATCH(unittest) AGAINST('\'word1\'' IN BOOLEAN MODE)"),
232 array(array('word1', 'word2'), array(), array(),
233 "MATCH(unittest) AGAINST('word1 word2' IN BOOLEAN MODE)"),
234 array(array('word1', 'word2'), array('mustword'), array(),
235 "MATCH(unittest) AGAINST('word1 word2 +mustword' IN BOOLEAN MODE)"),
236 array(array('word1', 'word2'), array('mustword', 'mustword2'), array(),
237 "MATCH(unittest) AGAINST('word1 word2 +mustword +mustword2' IN BOOLEAN MODE)"),
238 array(array(), array('mustword', 'mustword2'), array(),
239 "MATCH(unittest) AGAINST('+mustword +mustword2' IN BOOLEAN MODE)"),
240 array(array('word1'), array(), array('notword'),
241 "MATCH(unittest) AGAINST('word1 -notword' IN BOOLEAN MODE)"),
242 array(array('word1'), array(), array('notword', 'notword2'),
243 "MATCH(unittest) AGAINST('word1 -notword -notword2' IN BOOLEAN MODE)"),
244 array(array('word1', 'word2'), array('mustword', 'mustword2'), array('notword', 'notword2'),
245 "MATCH(unittest) AGAINST('word1 word2 +mustword +mustword2 -notword -notword2' IN BOOLEAN MODE)"),
251 * @dataProvider providerFullTextQuery
252 * @param array $terms
253 * @param string $result
255 public function testFullTextQuery($terms, $must_terms, $exclude_terms, $result)
257 $this->assertEquals($result,
258 $this->_db->getFulltextQuery('unittest', $terms, $must_terms, $exclude_terms));