]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/database/MysqlManagerTest.php
Release 6.4.0
[Github/sugarcrm.git] / tests / include / database / MysqlManagerTest.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 require_once 'include/database/MysqlManager.php';
38
39 class MysqlManagerTest extends Sugar_PHPUnit_Framework_TestCase
40 {
41     static public function setupBeforeClass()
42     {
43         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
44         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
45     }
46
47     static public function tearDownAfterClass()
48     {
49         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
50         unset($GLOBALS['current_user']);
51         unset($GLOBALS['app_strings']);
52     }
53
54     public function setUp()
55     {
56         if ( $GLOBALS['db']->dbType != 'mysql' ) {
57             $this->markTestSkipped('Only applies to MySQL');
58         }
59
60         $this->_db = new MysqlManager();
61     }
62
63     public function testQuote()
64     {
65         $string = "'dog eat ";
66         if(!$this->_db->valid()) $this->markTestSkipped("MySQL not enabled");
67         $this->assertEquals($this->_db->quote($string),"\\'dog eat ");
68     }
69
70     public function testArrayQuote()
71     {
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 "));
76     }
77
78     public function providerConvert()
79     {
80         $returnArray = array(
81             array(
82                 array('foo','nothing'),
83                 'foo'
84                 ),
85                 array(
86                     array('foo','today'),
87                     'CURDATE()'
88                     ),
89                 array(
90                     array('foo','left'),
91                     'LEFT(foo)'
92                 ),
93                 array(
94                     array('foo','left',array('1','2','3')),
95                     'LEFT(foo,1,2,3)'
96                     ),
97                 array(
98                     array('foo','date_format'),
99                     'DATE_FORMAT(foo,\'%Y-%m-%d\')'
100                         ),
101                 array(
102                     array('foo','date_format',array('1','2','3')),
103                     'DATE_FORMAT(foo,\'1\')'
104                     ),
105                 array(
106                     array('foo','date_format',array("'1'","'2'","'3'")),
107                     'DATE_FORMAT(foo,\'1\')'
108                     ),
109                     array(
110                     array('foo','datetime',array("'%Y-%m'")),
111                     'foo'
112                         ),
113                 array(
114                     array('foo','IFNULL'),
115                     'IFNULL(foo,\'\')'
116                     ),
117                 array(
118                     array('foo','IFNULL',array('1','2','3')),
119                     'IFNULL(foo,1,2,3)'
120                     ),
121                 array(
122                     array('foo','CONCAT',array('1','2','3')),
123                     'CONCAT(foo,1,2,3)'
124                     ),
125                 array(
126                     array(array('1','2','3'),'CONCAT'),
127                     'CONCAT(1,2,3)'
128                     ),
129                 array(
130                     array(array('1','2','3'),'CONCAT',array('foo', 'bar')),
131                     'CONCAT(1,2,3,foo,bar)'
132                     ),
133                 array(
134                     array('foo','text2char'),
135                     'foo'
136                 ),
137                 array(
138                     array('foo','length'),
139                     "LENGTH(foo)"
140                 ),
141                 array(
142                     array('foo','month'),
143                     "MONTH(foo)"
144                 ),
145                 array(
146                     array('foo','quarter'),
147                     "QUARTER(foo)"
148                 ),
149                 array(
150                     array('foo','add_date',array(1,'day')),
151                     "DATE_ADD(foo, INTERVAL 1 day)"
152                 ),
153                 array(
154                     array('foo','add_date',array(2,'week')),
155                     "DATE_ADD(foo, INTERVAL 2 week)"
156                 ),
157                 array(
158                     array('foo','add_date',array(3,'month')),
159                     "DATE_ADD(foo, INTERVAL 3 month)"
160                 ),
161                 array(
162                     array('foo','add_date',array(4,'quarter')),
163                     "DATE_ADD(foo, INTERVAL 4 quarter)"
164                 ),
165                 array(
166                     array('foo','add_date',array(5,'year')),
167                     "DATE_ADD(foo, INTERVAL 5 year)"
168                 ),
169         );
170         return $returnArray;
171     }
172
173     /**
174      * @ticket 33283
175      * @dataProvider providerConvert
176      */
177     public function testConvert(array $parameters, $result)
178     {
179         $this->assertEquals($result, call_user_func_array(array($this->_db, "convert"), $parameters));
180      }
181
182      /**
183       * @ticket 33283
184       */
185      public function testConcat()
186      {
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);
189      }
190
191      public function providerFromConvert()
192      {
193          $returnArray = array(
194              array(
195                  array('foo','nothing'),
196                  'foo'
197                  ),
198                  array(
199                      array('2009-01-01 12:00:00','date'),
200                      '2009-01-01 12:00:00'
201                      ),
202                  array(
203                      array('2009-01-01 12:00:00','time'),
204                      '2009-01-01 12:00:00'
205                      )
206                  );
207
208          return $returnArray;
209      }
210
211      /**
212       * @ticket 33283
213       * @dataProvider providerFromConvert
214       */
215      public function testFromConvert(
216          array $parameters,
217          $result
218          )
219      {
220          $this->assertEquals(
221              $this->_db->fromConvert($parameters[0],$parameters[1]),
222              $result);
223     }
224 }