Added unit tests for bug #13430: Cannot parse INSERT when VALUES has no space before...
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / tests / sqlparser_general_testcase.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_sqlparser
30 *
31 * $Id$
32 *
33 * @author Xavier Perseguers <typo3@perseguers.ch>
34 *
35 * @package TYPO3
36 * @subpackage dbal
37 */
38 class sqlparser_general_testcase extends BaseTestCase {
39
40 /**
41 * @var ux_t3lib_sqlparser (extended to make protected methods public)
42 */
43 protected $fixture;
44
45 /**
46 * Prepares the environment before running a test.
47 */
48 public function setUp() {
49 $className = self::buildAccessibleProxy('ux_t3lib_sqlparser');
50 $this->fixture = new $className;
51 }
52
53 /**
54 * Cleans up the environment after running a test.
55 */
56 public function tearDown() {
57 unset($this->fixture);
58 }
59
60 /**
61 * Cleans a SQL query.
62 *
63 * @param mixed $sql
64 * @return mixed (string or array)
65 */
66 private function cleanSql($sql) {
67 if (!is_string($sql)) {
68 return $sql;
69 }
70
71 $sql = str_replace("\n", ' ', $sql);
72 $sql = preg_replace('/\s+/', ' ', $sql);
73 return trim($sql);
74 }
75
76 /**
77 * @test
78 */
79 public function canExtractPartsOfAQuery() {
80 $parseString = "SELECT *\nFROM pages WHERE pid IN (1,2,3,4)";
81 $regex = '^SELECT[[:space:]]+(.*)[[:space:]]+';
82 $trimAll = TRUE;
83 $fields = $this->fixture->_callRef('nextPart', $parseString, $regex, $trimAll);
84
85 $this->assertEquals(
86 '*',
87 $fields
88 );
89 $this->assertEquals(
90 'FROM pages WHERE pid IN (1,2,3,4)',
91 $parseString
92 );
93
94 $regex = '^FROM ([^)]+) WHERE';
95 $table = $this->fixture->_callRef('nextPart', $parseString, $regex);
96
97 $this->assertEquals(
98 'pages',
99 $table
100 );
101 $this->assertEquals(
102 'pages WHERE pid IN (1,2,3,4)',
103 $parseString
104 );
105 }
106
107 /**
108 * @test
109 */
110 public function canGetIntegerValue() {
111 $parseString = '1024';
112 $value = $this->fixture->_callRef('getValue', $parseString);
113 $expected = array(1024);
114
115 $this->assertEquals($expected, $value);
116 }
117
118 /**
119 * @test
120 * @see http://bugs.typo3.org/view.php?id=13104
121 */
122 public function canGetStringValue() {
123 $parseString = '"some owner\\\'s string"';
124 $value = $this->fixture->_callRef('getValue', $parseString);
125 $expected = array('some owner\'s string', '"');
126
127 $this->assertEquals($expected, $value);
128 }
129
130 /**
131 * @test
132 * @see http://bugs.typo3.org/view.php?id=13104
133 */
134 public function canGetStringValueWithSingleQuote() {
135 $parseString = "'some owner\'s string'";
136 $value = $this->fixture->_callRef('getValue', $parseString);
137 $expected = array('some owner\'s string', "'");
138
139 $this->assertEquals($expected, $value);
140 }
141
142 /**
143 * @test
144 * @see http://bugs.typo3.org/view.php?id=13104
145 */
146 public function canGetStringValueWithDoubleQuote() {
147 $parseString = '"the \"owner\" is here"';
148 $value = $this->fixture->_callRef('getValue', $parseString);
149 $expected = array('the "owner" is here', '"');
150
151 $this->assertEquals($expected, $value);
152 }
153
154 /**
155 * @test
156 */
157 public function canGetListOfValues() {
158 $parseString = '( 1, 2, 3 ,4)';
159 $operator = 'IN';
160 $values = $this->fixture->_callRef('getValue', $parseString, $operator);
161 $expected = array(
162 array(1),
163 array(2),
164 array(3),
165 array(4)
166 );
167
168 $this->assertEquals($expected, $values);
169 }
170
171 /**
172 * @test
173 */
174 public function parseWhereClauseReturnsArray() {
175 $parseString = 'uid IN (1,2) AND (starttime < ' . time() . ' OR cruser_id + 10 < 20)';
176 $where = $this->fixture->parseWhereClause($parseString);
177
178 $this->assertTrue(is_array($where), $where);
179 $this->assertTrue(empty($parseString), 'parseString is not empty');
180 }
181
182 /**
183 * @test
184 */
185 public function canSelectAllFieldsFromPages() {
186 $sql = 'SELECT * FROM pages';
187 $expected = $sql;
188 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
189
190 $this->assertEquals($expected, $actual);
191 }
192
193 /**
194 * @test
195 * @see http://bugs.typo3.org/view.php?id=13412
196 */
197 public function canParseAndCompileBetweenOperator() {
198 $parseString = '((scheduled BETWEEN 1265068628 AND 1265068828 ) OR scheduled <= 1265068728) AND NOT exec_time AND NOT process_id AND page_id=1 AND parameters_hash = \'854e9a2a77\'';
199 $where = $this->fixture->parseWhereClause($parseString);
200
201 $this->assertTrue(is_array($where), $where);
202 $this->assertTrue(empty($parseString), 'parseString is not empty');
203
204 $whereClause = $this->cleanSql($this->fixture->compileWhereClause($where));
205 $expected = '((scheduled BETWEEN 1265068628 AND 1265068828) OR scheduled <= 1265068728) AND NOT exec_time AND NOT process_id AND page_id = 1 AND parameters_hash = \'854e9a2a77\'';
206 $this->assertEquals($expected, $whereClause);
207 }
208
209 /**
210 * @test
211 * http://bugs.typo3.org/view.php?id=13430
212 */
213 public function canParseInsertWithoutSpaceAfterValues() {
214 $parseString = "INSERT INTO static_country_zones VALUES('483', '0', 'NL', 'NLD', '528', 'DR', 'Drenthe', '');";
215 $components = $this->fixture->_callRef('parseINSERT', $parseString);
216
217 $this->assertTrue(is_array($components), $components);
218 $insert = $this->cleanSql($this->fixture->compileINSERT($components));
219 $expected = "INSERT INTO static_country_zones VALUES ('483', '0', 'NL', 'NLD', '528', 'DR', 'Drenthe', '')";
220 $this->assertEquals($expected, $insert);
221 }
222
223 /**
224 * @test
225 * http://bugs.typo3.org/view.php?id=13430
226 */
227 public function canParseInsertWithSpaceAfterValues() {
228 $parseString = "INSERT INTO static_country_zones VALUES ('483', '0', 'NL', 'NLD', '528', 'DR', 'Drenthe', '');";
229 $components = $this->fixture->_callRef('parseINSERT', $parseString);
230
231 $this->assertTrue(is_array($components), $components);
232 $insert = $this->cleanSql($this->fixture->compileINSERT($components));
233 $expected = "INSERT INTO static_country_zones VALUES ('483', '0', 'NL', 'NLD', '528', 'DR', 'Drenthe', '')";
234 $this->assertEquals($expected, $insert);
235 }
236
237 ///////////////////////////////////////
238 // Tests concerning JOINs
239 ///////////////////////////////////////
240
241 /**
242 * @test
243 */
244 public function parseFromTablesWithInnerJoinReturnsArray() {
245 $parseString = 'be_users INNER JOIN pages ON pages.cruser_id = be_users.uid';
246 $tables = $this->fixture->parseFromTables($parseString);
247
248 $this->assertTrue(is_array($tables), $tables);
249 $this->assertTrue(empty($parseString), 'parseString is not empty');
250 }
251
252 /**
253 * @test
254 */
255 public function parseFromTablesWithLeftOuterJoinReturnsArray() {
256 $parseString = 'be_users LEFT OUTER JOIN pages ON be_users.uid = pages.cruser_id';
257 $tables = $this->fixture->parseFromTables($parseString);
258
259 $this->assertTrue(is_array($tables), $tables);
260 $this->assertTrue(empty($parseString), 'parseString is not empty');
261 }
262
263 /**
264 * @test
265 * @see http://bugs.typo3.org/view.php?id=12596
266 */
267 public function parseFromTablesWithRightOuterJoinReturnsArray() {
268 $parseString = 'tx_powermail_fieldsets RIGHT JOIN tt_content ON tx_powermail_fieldsets.tt_content = tt_content.uid';
269 $tables = $this->fixture->parseFromTables($parseString);
270
271 $this->assertTrue(is_array($tables), $tables);
272 $this->assertTrue(empty($parseString), 'parseString is not empty');
273 }
274
275 /**
276 * @test
277 */
278 public function parseFromTablesWithMultipleJoinsReturnsArray() {
279 $parseString = 'be_users LEFT OUTER JOIN pages ON be_users.uid = pages.cruser_id INNER JOIN cache_pages cp ON cp.page_id = pages.uid';
280 $tables = $this->fixture->parseFromTables($parseString);
281
282 $this->assertTrue(is_array($tables), $tables);
283 $this->assertTrue(empty($parseString), 'parseString is not empty');
284 }
285
286 /**
287 * @test
288 * @see http://bugs.typo3.org/view.php?id=12596
289 */
290 public function parseFromTablesWithMultipleJoinsAndParenthesesReturnsArray() {
291 $parseString = 'tx_powermail_fieldsets RIGHT JOIN tt_content ON tx_powermail_fieldsets.tt_content = tt_content.uid LEFT JOIN tx_powermail_fields ON tx_powermail_fieldsets.uid = tx_powermail_fields.fieldset';
292 $tables = $this->fixture->parseFromTables($parseString);
293
294 $this->assertTrue(is_array($tables), $tables);
295 $this->assertTrue(empty($parseString), 'parseString is not empty');
296 }
297
298 /**
299 * @test
300 */
301 public function canUseInnerJoinInSelect() {
302 $sql = 'SELECT pages.uid, be_users.username FROM be_users INNER JOIN pages ON pages.cruser_id = be_users.uid';
303 $expected = 'SELECT pages.uid, be_users.username FROM be_users INNER JOIN pages ON pages.cruser_id=be_users.uid';
304 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
305
306 $this->assertEquals($expected, $actual);
307 }
308
309 /**
310 * @test
311 */
312 public function canUseMultipleInnerJoinsInSelect() {
313 $sql = 'SELECT * FROM tt_news_cat INNER JOIN tt_news_cat_mm ON tt_news_cat.uid = tt_news_cat_mm.uid_foreign INNER JOIN tt_news ON tt_news.uid = tt_news_cat_mm.uid_local';
314 $expected = 'SELECT * FROM tt_news_cat INNER JOIN tt_news_cat_mm ON tt_news_cat.uid=tt_news_cat_mm.uid_foreign INNER JOIN tt_news ON tt_news.uid=tt_news_cat_mm.uid_local';
315 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
316
317 $this->assertEquals($expected, $actual);
318 }
319
320 ///////////////////////////////////////
321 // Tests concerning DB management
322 ///////////////////////////////////////
323
324 /**
325 * @test
326 * @see http://bugs.typo3.org/view.php?id=4466
327 */
328 public function indexMayContainALengthRestriction() {
329 $parseString = '
330 CREATE TABLE tx_realurl_uniqalias (
331 uid int(11) NOT NULL auto_increment,
332 tstamp int(11) DEFAULT \'0\' NOT NULL,
333 tablename varchar(60) DEFAULT \'\' NOT NULL,
334 field_alias varchar(255) DEFAULT \'\' NOT NULL,
335 field_id varchar(60) DEFAULT \'\' NOT NULL,
336 value_alias varchar(255) DEFAULT \'\' NOT NULL,
337 value_id int(11) DEFAULT \'0\' NOT NULL,
338 lang int(11) DEFAULT \'0\' NOT NULL,
339 expire int(11) DEFAULT \'0\' NOT NULL,
340
341 PRIMARY KEY (uid),
342 KEY tablename (tablename),
343 KEY bk_realurl01 (field_alias,field_id,value_id,lang,expire),
344 KEY bk_realurl02 (tablename,field_alias,field_id,value_alias(220),expire)
345 );
346 ';
347
348 $createTables = $this->fixture->_callRef('parseCREATETABLE', $parseString);
349 $this->assertTrue(is_array($createTables), $createTables);
350 }
351
352 ///////////////////////////////////////
353 // Tests concerning subqueries
354 ///////////////////////////////////////
355
356 /**
357 * @test
358 * @see http://bugs.typo3.org/view.php?id=12758
359 */
360 public function inWhereClauseSupportsSubquery() {
361 $parseString = 'process_id IN (SELECT process_id FROM tx_crawler_process WHERE active=0 AND deleted=0)';
362 $whereParts = $this->fixture->parseWhereClause($parseString);
363
364 $this->assertTrue(is_array($whereParts), $whereParts);
365 $this->assertTrue(empty($parseString), 'parseString is not empty');
366 }
367
368 /**
369 * @test
370 * @see http://bugs.typo3.org/view.php?id=12758
371 */
372 public function inWhereClauseWithSubqueryIsProperlyCompiled() {
373 $sql = 'SELECT * FROM tx_crawler_queue WHERE process_id IN (SELECT process_id FROM tx_crawler_process WHERE active=0 AND deleted=0)';
374 $expected = 'SELECT * FROM tx_crawler_queue WHERE process_id IN (SELECT process_id FROM tx_crawler_process WHERE active = 0 AND deleted = 0)';
375 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
376
377 $this->assertEquals($expected, $actual);
378 }
379
380 /**
381 * @test
382 * @see http://bugs.typo3.org/view.php?id=12758
383 */
384 public function whereClauseSupportsExistsKeyword() {
385 $parseString = 'EXISTS (SELECT * FROM tx_crawler_queue WHERE tx_crawler_queue.process_id = tx_crawler_process.process_id AND tx_crawler_queue.exec_time = 0)';
386 $whereParts = $this->fixture->parseWhereClause($parseString);
387
388 $this->assertTrue(is_array($whereParts), $whereParts);
389 $this->assertTrue(empty($parseString), 'parseString is not empty');
390 }
391
392 /**
393 * @test
394 * @see http://bugs.typo3.org/view.php?id=12758
395 */
396 public function existsClauseIsProperlyCompiled() {
397 $sql = 'SELECT * FROM tx_crawler_process WHERE active = 0 AND NOT EXISTS (SELECT * FROM tx_crawler_queue WHERE tx_crawler_queue.process_id = tx_crawler_process.process_id AND tx_crawler_queue.exec_time = 0)';
398 $expected = 'SELECT * FROM tx_crawler_process WHERE active = 0 AND NOT EXISTS (SELECT * FROM tx_crawler_queue WHERE tx_crawler_queue.process_id = tx_crawler_process.process_id AND tx_crawler_queue.exec_time = 0)';
399 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
400
401 $this->assertEquals($expected, $actual);
402 }
403
404 ///////////////////////////////////////
405 // Tests concerning advanced operators
406 ///////////////////////////////////////
407
408 /**
409 * @test
410 * @see http://bugs.typo3.org/view.php?id=13135
411 */
412 public function caseWithBooleanConditionIsSupportedInFields() {
413 $parseString = 'CASE WHEN 1>0 THEN 2 ELSE 1 END AS foo, other_column';
414 $fieldList = $this->fixture->parseFieldList($parseString);
415
416 $this->assertTrue(is_array($fieldList), $fieldList);
417 $this->assertTrue(empty($parseString), 'parseString is not empty');
418 }
419
420 /**
421 * @test
422 * @see http://bugs.typo3.org/view.php?id=13135
423 */
424 public function caseWithBooleanConditionIsProperlyCompiled() {
425 $sql = 'SELECT CASE WHEN 1>0 THEN 2 ELSE 1 END AS foo, other_column FROM mytable';
426 $expected = 'SELECT CASE WHEN 1 > 0 THEN 2 ELSE 1 END AS foo, other_column FROM mytable';
427 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
428
429 $this->assertEquals($expected, $actual);
430 }
431
432 /**
433 * @test
434 * @see http://bugs.typo3.org/view.php?id=13135
435 */
436 public function caseWithMultipleWhenIsSupportedInFields() {
437 $parseString = 'CASE column WHEN 1 THEN \'one\' WHEN 2 THEN \'two\' ELSE \'out of range\' END AS number';
438 $fieldList = $this->fixture->parseFieldList($parseString);
439
440 $this->assertTrue(is_array($fieldList), $fieldList);
441 $this->assertTrue(empty($parseString), 'parseString is not empty');
442 }
443
444 /**
445 * @test
446 * @see http://bugs.typo3.org/view.php?id=13135
447 */
448 public function caseWithMultipleWhenIsProperlyCompiled() {
449 $sql = 'SELECT CASE column WHEN 1 THEN \'one\' WHEN 2 THEN \'two\' ELSE \'out of range\' END AS number FROM mytable';
450 $expected = 'SELECT CASE column WHEN 1 THEN \'one\' WHEN 2 THEN \'two\' ELSE \'out of range\' END AS number FROM mytable';
451 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
452
453 $this->assertEquals($expected, $actual);
454 }
455
456 /**
457 * @test
458 * @see http://bugs.typo3.org/view.php?id=13134
459 */
460 public function locateIsSupported() {
461 $sql = 'SELECT * FROM tx_templavoila_tmplobj WHERE LOCATE(\'(fce)\', datastructure)>0';
462 $expected = 'SELECT * FROM tx_templavoila_tmplobj WHERE LOCATE(\'(fce)\', datastructure) > 0';
463 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
464
465 $this->assertEquals($expected, $actual);
466 }
467
468 /**
469 * @test
470 * @see http://bugs.typo3.org/view.php?id=13134
471 */
472 public function locateWithPositionIsSupported() {
473 $sql = 'SELECT * FROM tx_templavoila_tmplobj WHERE LOCATE(\'(fce)\' , datastructure ,10)>0';
474 $expected = 'SELECT * FROM tx_templavoila_tmplobj WHERE LOCATE(\'(fce)\', datastructure, 10) > 0';
475 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
476
477 $this->assertEquals($expected, $actual);
478 }
479
480 /**
481 * @test
482 * @see http://bugs.typo3.org/view.php?id=13134
483 * @see http://bugs.typo3.org/view.php?id=13135
484 */
485 public function locateWithinCaseIsSupported() {
486 $sql = 'SELECT *, CASE WHEN LOCATE(\'(fce)\', datastructure)>0 THEN 2 ELSE 1 END AS scope FROM tx_templavoila_tmplobj';
487 $expected = 'SELECT *, CASE WHEN LOCATE(\'(fce)\', datastructure) > 0 THEN 2 ELSE 1 END AS scope FROM tx_templavoila_tmplobj';
488 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
489
490 $this->assertEquals($expected, $actual);
491 }
492 }
493 ?>