Raised DBAL version from 1.1.7 to 1.1.8
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / tests / dbGeneralTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Xavier Perseguers <typo3@perseguers.ch>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 require_once('BaseTestCase.php');
27
28 /**
29 * Testcase for class ux_t3lib_db.
30 *
31 * $Id$
32 *
33 * @author Xavier Perseguers <typo3@perseguers.ch>
34 *
35 * @package TYPO3
36 * @subpackage dbal
37 */
38 class dbGeneralTest extends BaseTestCase {
39
40 /**
41 * @var t3lib_db
42 */
43 protected $db;
44
45 /**
46 * @var array
47 */
48 protected $loadedExtensions;
49
50 /**
51 * @var array
52 */
53 protected $temporaryFiles;
54
55 /**
56 * Prepares the environment before running a test.
57 */
58 public function setUp() {
59 // Backup list of loaded extensions
60 $this->loadedExtensions = $GLOBALS['TYPO3_LOADED_EXT'];
61 // Backup database connection
62 $this->db = $GLOBALS['TYPO3_DB'];
63 $this->temporaryFiles = array();
64
65 $className = self::buildAccessibleProxy('ux_t3lib_db');
66 $GLOBALS['TYPO3_DB'] = new $className;
67 $GLOBALS['TYPO3_DB']->lastHandlerKey = '_DEFAULT';
68 }
69
70 /**
71 * Cleans up the environment after running a test.
72 */
73 public function tearDown() {
74 // Clear DBAL-generated cache files
75 $GLOBALS['TYPO3_DB']->clearCachedFieldInfo();
76 // Delete temporary files
77 foreach ($this->temporaryFiles as $filename) unlink($filename);
78 // Restore DB connection
79 $GLOBALS['TYPO3_DB'] = $this->db;
80 // Restore list of loaded extensions
81 $GLOBALS['TYPO3_LOADED_EXT'] = $this->loadedExtensions;
82 }
83
84 /**
85 * Cleans a SQL query.
86 *
87 * @param mixed $sql
88 * @return mixed (string or array)
89 */
90 private function cleanSql($sql) {
91 if (!is_string($sql)) {
92 return $sql;
93 }
94
95 $sql = str_replace("\n", ' ', $sql);
96 $sql = preg_replace('/\s+/', ' ', $sql);
97 return trim($sql);
98 }
99
100 /**
101 * Creates a fake extension with a given table definition.
102 *
103 * @param string $tableDefinition SQL script to create the extension's tables
104 * @return void
105 */
106 protected function createFakeExtension($tableDefinition) {
107 // Prepare a fake extension configuration
108 $ext_tables = t3lib_div::tempnam('ext_tables');
109 t3lib_div::writeFile($ext_tables, $tableDefinition);
110 $this->temporaryFiles[] = $ext_tables;
111
112 $GLOBALS['TYPO3_LOADED_EXT']['test_dbal'] = array(
113 'ext_tables.sql' => $ext_tables
114 );
115
116 // Append our test table to the list of existing tables
117 $GLOBALS['TYPO3_DB']->clearCachedFieldInfo();
118 $GLOBALS['TYPO3_DB']->_call('initInternalVariables');
119 }
120
121 /**
122 * @test
123 * @see http://bugs.typo3.org/view.php?id=12515
124 */
125 public function concatCanBeParsedAfterLikeOperator() {
126 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
127 '*',
128 'sys_refindex, tx_dam_file_tracking',
129 'sys_refindex.tablename = \'tx_dam_file_tracking\''
130 . ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path, tx_dam_file_tracking.file_name)'
131 ));
132 $expected = 'SELECT * FROM sys_refindex, tx_dam_file_tracking WHERE sys_refindex.tablename = \'tx_dam_file_tracking\'';
133 $expected .= ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path, tx_dam_file_tracking.file_name)';
134 $this->assertEquals($expected, $query);
135 }
136
137 /**
138 * @test
139 * @see http://bugs.typo3.org/view.php?id=10965
140 */
141 public function floatNumberCanBeStoredInDatabase() {
142 $this->createFakeExtension('
143 CREATE TABLE tx_test_dbal (
144 foo double default \'0\',
145 foobar integer default \'0\'
146 );
147 ');
148 $data = array(
149 'foo' => 99.12,
150 'foobar' => -120,
151 );
152 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTquery('tx_test_dbal', $data));
153 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'99.12\', \'-120\' )';
154 $this->assertEquals($expected, $query);
155 }
156
157 /**
158 * @test
159 * @see http://bugs.typo3.org/view.php?id=11093
160 */
161 public function positive64BitIntegerIsSupported() {
162 $this->createFakeExtension('
163 CREATE TABLE tx_test_dbal (
164 foo int default \'0\',
165 foobar bigint default \'0\'
166 );
167 ');
168 $data = array(
169 'foo' => 9223372036854775807,
170 'foobar' => 9223372036854775807,
171 );
172 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTquery('tx_test_dbal', $data));
173 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'9223372036854775807\', \'9223372036854775807\' )';
174 $this->assertEquals($expected, $query);
175 }
176
177 /**
178 * @test
179 * @see http://bugs.typo3.org/view.php?id=11093
180 */
181 public function negative64BitIntegerIsSupported() {
182 $this->createFakeExtension('
183 CREATE TABLE tx_test_dbal (
184 foo int default \'0\',
185 foobar bigint default \'0\'
186 );
187 ');
188 $data = array(
189 'foo' => -9223372036854775808,
190 'foobar' => -9223372036854775808,
191 );
192 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTquery('tx_test_dbal', $data));
193 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'-9223372036854775808\', \'-9223372036854775808\' )';
194 $this->assertEquals($expected, $query);
195 }
196
197 /**
198 * @test
199 * http://bugs.typo3.org/view.php?id=12858
200 */
201 public function sqlForInsertWithMultipleRowsIsValid() {
202 $fields = array('uid', 'pid', 'title', 'body');
203 $rows = array(
204 array('1', '2', 'Title #1', 'Content #1'),
205 array('3', '4', 'Title #2', 'Content #2'),
206 array('5', '6', 'Title #3', 'Content #3'),
207 );
208 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTmultipleRows('tt_content', $fields, $rows));
209
210 $expected = 'INSERT INTO tt_content (uid, pid, title, body) VALUES ';
211 $expected .= "('1', '2', 'Title #1', 'Content #1'), ";
212 $expected .= "('3', '4', 'Title #2', 'Content #2'), ";
213 $expected .= "('5', '6', 'Title #3', 'Content #3')";
214
215 $this->assertEquals($expected, $query);
216 }
217
218 /**
219 * @test
220 * @see http://bugs.typo3.org/view.php?id=4493
221 */
222 public function minFunctionAndInOperatorCanBeParsed() {
223 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
224 '*',
225 'pages',
226 'MIN(uid) IN (1,2,3,4)'
227 ));
228 $expected = 'SELECT * FROM pages WHERE MIN(uid) IN (1,2,3,4)';
229 $this->assertEquals($expected, $query);
230 }
231
232 /**
233 * @test
234 * @see http://bugs.typo3.org/view.php?id=4493
235 */
236 public function maxFunctionAndInOperatorCanBeParsed() {
237 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
238 '*',
239 'pages',
240 'MAX(uid) IN (1,2,3,4)'
241 ));
242 $expected = 'SELECT * FROM pages WHERE MAX(uid) IN (1,2,3,4)';
243 $this->assertEquals($expected, $query);
244 }
245 }
246 ?>