[TASK] Merge submodule workspaces into core
[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 * Copyright notice
6 *
7 * (c) 2011-2013 Patrick Schriner <patrick.schriner@diemedialen.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Testcase for TYPO3\CMS\Core\Database\SqlParser
28 *
29 * @author Patrick Schriner <patrick.schriner@diemedialen.de>
30 */
31 class SqlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
32
33 /**
34 * @var \TYPO3\CMS\Core\Database\SqlParser|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
35 */
36 private $fixture;
37
38 public function setUp() {
39 $this->fixture = $this->getAccessibleMock('\\TYPO3\\CMS\\Core\\Database\\SqlParser', array('dummy'));
40 }
41
42 public function tearDown() {
43 unset($this->fixture);
44 }
45
46 /**
47 * Regression test
48 *
49 * @test
50 */
51 public function compileWhereClauseDoesNotDropClauses() {
52 $clauses = array(
53 0 => array(
54 'modifier' => '',
55 'table' => 'pages',
56 'field' => 'fe_group',
57 'calc' => '',
58 'comparator' => '=',
59 'value' => array(
60 0 => '',
61 1 => '\''
62 )
63 ),
64 1 => array(
65 'operator' => 'OR',
66 'modifier' => '',
67 'func' => array(
68 'type' => 'IFNULL',
69 'default' => array(
70 0 => '1',
71 1 => '\''
72 ),
73 'table' => 'pages',
74 'field' => 'fe_group'
75 )
76 ),
77 2 => array(
78 'operator' => 'OR',
79 'modifier' => '',
80 'table' => 'pages',
81 'field' => 'fe_group',
82 'calc' => '',
83 'comparator' => '=',
84 'value' => array(
85 0 => '0',
86 1 => '\''
87 )
88 ),
89 3 => array(
90 'operator' => 'OR',
91 'modifier' => '',
92 'func' => array(
93 'type' => 'FIND_IN_SET',
94 'str' => array(
95 0 => '0',
96 1 => '\''
97 ),
98 'table' => 'pages',
99 'field' => 'fe_group'
100 ),
101 'comparator' => ''
102 ),
103 4 => array(
104 'operator' => 'OR',
105 'modifier' => '',
106 'func' => array(
107 'type' => 'FIND_IN_SET',
108 'str' => array(
109 0 => '-1',
110 1 => '\''
111 ),
112 'table' => 'pages',
113 'field' => 'fe_group'
114 ),
115 'comparator' => ''
116 )
117 );
118 $output = $this->fixture->compileWhereClause($clauses);
119 $parts = explode(' OR ', $output);
120 $this->assertSame(count($clauses), count($parts));
121 $this->assertContains('IFNULL', $output);
122 }
123
124 /**
125 * Data provider for trimSqlReallyTrimsAllWhitespace
126 *
127 * @see trimSqlReallyTrimsAllWhitespace
128 */
129 public function trimSqlReallyTrimsAllWhitespaceDataProvider() {
130 return array(
131 'Nothing to trim' => array('SELECT * FROM test WHERE 1=1;', 'SELECT * FROM test WHERE 1=1 '),
132 'Space after ;' => array('SELECT * FROM test WHERE 1=1; ', 'SELECT * FROM test WHERE 1=1 '),
133 'Space before ;' => array('SELECT * FROM test WHERE 1=1 ;', 'SELECT * FROM test WHERE 1=1 '),
134 'Space before and after ;' => array('SELECT * FROM test WHERE 1=1 ; ', 'SELECT * FROM test WHERE 1=1 '),
135 'Linefeed after ;' => array('SELECT * FROM test WHERE 1=1' . LF . ';', 'SELECT * FROM test WHERE 1=1 '),
136 'Linefeed before ;' => array('SELECT * FROM test WHERE 1=1;' . LF, 'SELECT * FROM test WHERE 1=1 '),
137 'Linefeed before and after ;' => array('SELECT * FROM test WHERE 1=1' . LF . ';' . LF, 'SELECT * FROM test WHERE 1=1 '),
138 'Tab after ;' => array('SELECT * FROM test WHERE 1=1' . TAB . ';', 'SELECT * FROM test WHERE 1=1 '),
139 'Tab before ;' => array('SELECT * FROM test WHERE 1=1;' . TAB, 'SELECT * FROM test WHERE 1=1 '),
140 'Tab before and after ;' => array('SELECT * FROM test WHERE 1=1' . TAB . ';' . TAB, 'SELECT * FROM test WHERE 1=1 '),
141 );
142 }
143
144 /**
145 * @test
146 * @dataProvider trimSqlReallyTrimsAllWhitespaceDataProvider
147 * @param string $sql The SQL to trim
148 * @param string $expected The expected trimmed SQL with single space at the end
149 */
150 public function trimSqlReallyTrimsAllWhitespace($sql, $expected) {
151 $result = $this->fixture->_call('trimSQL', $sql);
152 $this->assertSame($expected, $result);
153 }
154
155 }
156
157 ?>