15b80c8781f2be06d3c99cf3706e55e13217334d
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / SqlParserTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Database;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Testcase for TYPO3\CMS\Core\Database\SqlParser
19 *
20 * @author Patrick Schriner <patrick.schriner@diemedialen.de>
21 */
22 class SqlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * @var \TYPO3\CMS\Core\Database\SqlParser|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
26 */
27 protected $subject;
28
29 protected function setUp() {
30 $this->subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Database\SqlParser::class, array('dummy'));
31 }
32
33 /**
34 * Regression test
35 *
36 * @test
37 */
38 public function compileWhereClauseDoesNotDropClauses() {
39 $clauses = array(
40 0 => array(
41 'modifier' => '',
42 'table' => 'pages',
43 'field' => 'fe_group',
44 'calc' => '',
45 'comparator' => '=',
46 'value' => array(
47 0 => '',
48 1 => '\''
49 )
50 ),
51 1 => array(
52 'operator' => 'OR',
53 'modifier' => '',
54 'func' => array(
55 'type' => 'IFNULL',
56 'default' => array(
57 0 => '1',
58 1 => '\''
59 ),
60 'table' => 'pages',
61 'field' => 'fe_group'
62 )
63 ),
64 2 => array(
65 'operator' => 'OR',
66 'modifier' => '',
67 'table' => 'pages',
68 'field' => 'fe_group',
69 'calc' => '',
70 'comparator' => '=',
71 'value' => array(
72 0 => '0',
73 1 => '\''
74 )
75 ),
76 3 => array(
77 'operator' => 'OR',
78 'modifier' => '',
79 'func' => array(
80 'type' => 'FIND_IN_SET',
81 'str' => array(
82 0 => '0',
83 1 => '\''
84 ),
85 'table' => 'pages',
86 'field' => 'fe_group'
87 ),
88 'comparator' => ''
89 ),
90 4 => array(
91 'operator' => 'OR',
92 'modifier' => '',
93 'func' => array(
94 'type' => 'FIND_IN_SET',
95 'str' => array(
96 0 => '-1',
97 1 => '\''
98 ),
99 'table' => 'pages',
100 'field' => 'fe_group'
101 ),
102 'comparator' => ''
103 )
104 );
105 $output = $this->subject->compileWhereClause($clauses);
106 $parts = explode(' OR ', $output);
107 $this->assertSame(count($clauses), count($parts));
108 $this->assertContains('IFNULL', $output);
109 }
110
111 /**
112 * Data provider for trimSqlReallyTrimsAllWhitespace
113 *
114 * @see trimSqlReallyTrimsAllWhitespace
115 */
116 public function trimSqlReallyTrimsAllWhitespaceDataProvider() {
117 return array(
118 'Nothing to trim' => array('SELECT * FROM test WHERE 1=1;', 'SELECT * FROM test WHERE 1=1 '),
119 'Space after ;' => array('SELECT * FROM test WHERE 1=1; ', 'SELECT * FROM test WHERE 1=1 '),
120 'Space before ;' => array('SELECT * FROM test WHERE 1=1 ;', 'SELECT * FROM test WHERE 1=1 '),
121 'Space before and after ;' => array('SELECT * FROM test WHERE 1=1 ; ', 'SELECT * FROM test WHERE 1=1 '),
122 'Linefeed after ;' => array('SELECT * FROM test WHERE 1=1' . LF . ';', 'SELECT * FROM test WHERE 1=1 '),
123 'Linefeed before ;' => array('SELECT * FROM test WHERE 1=1;' . LF, 'SELECT * FROM test WHERE 1=1 '),
124 'Linefeed before and after ;' => array('SELECT * FROM test WHERE 1=1' . LF . ';' . LF, 'SELECT * FROM test WHERE 1=1 '),
125 'Tab after ;' => array('SELECT * FROM test WHERE 1=1' . TAB . ';', 'SELECT * FROM test WHERE 1=1 '),
126 'Tab before ;' => array('SELECT * FROM test WHERE 1=1;' . TAB, 'SELECT * FROM test WHERE 1=1 '),
127 'Tab before and after ;' => array('SELECT * FROM test WHERE 1=1' . TAB . ';' . TAB, 'SELECT * FROM test WHERE 1=1 '),
128 );
129 }
130
131 /**
132 * @test
133 * @dataProvider trimSqlReallyTrimsAllWhitespaceDataProvider
134 * @param string $sql The SQL to trim
135 * @param string $expected The expected trimmed SQL with single space at the end
136 */
137 public function trimSqlReallyTrimsAllWhitespace($sql, $expected) {
138 $result = $this->subject->_call('trimSQL', $sql);
139 $this->assertSame($expected, $result);
140 }
141
142 }