[TASK] Tests: Refactor and activate dbal tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / Tests / Unit / Database / DatabaseConnectionMssqlTest.php
1 <?php
2 namespace TYPO3\CMS\Dbal\Tests\Unit\Database;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2014 Xavier Perseguers <xavier@typo3.org>
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 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * Test case
31 */
32 class DatabaseConnectionMssqlTest extends AbstractTestCase {
33
34 /**
35 * @var \TYPO3\CMS\Dbal\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
36 */
37 protected $subject;
38
39 /**
40 * Prepare a DatabaseConnection subject ready to parse mssql queries
41 *
42 * @return \TYPO3\CMS\Dbal\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
43 */
44 public function setUp() {
45 $configuration = array(
46 'handlerCfg' => array(
47 '_DEFAULT' => array(
48 'type' => 'adodb',
49 'config' => array(
50 'driver' => 'mssql',
51 ),
52 ),
53 ),
54 'mapping' => array(
55 'tx_templavoila_tmplobj' => array(
56 'mapFieldNames' => array(
57 'datastructure' => 'ds',
58 ),
59 ),
60 'Members' => array(
61 'mapFieldNames' => array(
62 'pid' => '0',
63 'cruser_id' => '1',
64 'uid' => 'MemberID',
65 ),
66 ),
67 ),
68 );
69 $this->subject = $this->prepareSubject('mssql', $configuration);
70 }
71
72 /**
73 * @test
74 */
75 public function runningADOdbDriverReturnsTrueWithMssqlForMssqlDefaultDriverConfiguration() {
76 $this->assertTrue($this->subject->runningADOdbDriver('mssql'));
77 }
78
79 /**
80 * @test
81 * @see http://forge.typo3.org/issues/23087
82 */
83 public function findInSetIsProperlyRemapped() {
84 $expected = 'SELECT * FROM "fe_users" WHERE \',\'+"usergroup"+\',\' LIKE \'%,10,%\'';
85 $result = $this->subject->SELECTquery('*', 'fe_users', 'FIND_IN_SET(10, usergroup)');
86 $this->assertEquals($expected, $this->cleanSql($result));
87 }
88
89 /**
90 * @test
91 * @see http://forge.typo3.org/issues/27858
92 */
93 public function canParseSingleQuote() {
94 $parseString = 'SELECT * FROM pages WHERE title=\'1\'\'\' AND deleted=0';
95 $components = $this->subject->SQLparser->_callRef('parseSELECT', $parseString);
96
97 $this->assertTrue(is_array($components), $components);
98 $this->assertEmpty($components['parseString']);
99 }
100
101 ///////////////////////////////////////
102 // Tests concerning remapping with
103 // external (non-TYPO3) databases
104 ///////////////////////////////////////
105 /**
106 * @test
107 * @see http://forge.typo3.org/issues/22096
108 */
109 public function canRemapPidToZero() {
110 $selectFields = 'uid, FirstName, LastName';
111 $fromTables = 'Members';
112 $whereClause = 'pid=0 AND cruser_id=1';
113 $groupBy = '';
114 $orderBy = '';
115
116 $remappedParameters = $this->subject->_call('map_remapSELECTQueryParts', $selectFields, $fromTables, $whereClause, $groupBy, $orderBy);
117 $result = $this->subject->_call('SELECTqueryFromArray', $remappedParameters);
118 $expected = 'SELECT "MemberID", "FirstName", "LastName" FROM "Members" WHERE 0 = 0 AND 1 = 1';
119 $this->assertEquals($expected, $this->cleanSql($result));
120 }
121
122 ///////////////////////////////////////
123 // Tests concerning advanced operators
124 ///////////////////////////////////////
125 /**
126 * @test
127 * @see http://forge.typo3.org/issues/21902
128 */
129 public function locateStatementIsProperlyQuoted() {
130 $result = $this->subject->SELECTquery('*, CASE WHEN' . ' LOCATE(' . $this->subject->fullQuoteStr('(fce)', 'tx_templavoila_tmplobj') . ', datastructure)>0 THEN 2' . ' ELSE 1' . ' END AS scope', 'tx_templavoila_tmplobj', '1=1');
131 $expected = 'SELECT *, CASE WHEN CHARINDEX(\'(fce)\', "datastructure") > 0 THEN 2 ELSE 1 END AS "scope" FROM "tx_templavoila_tmplobj" WHERE 1 = 1';
132 $this->assertEquals($expected, $this->cleanSql($result));
133 }
134
135 /**
136 * @test
137 * @see http://forge.typo3.org/issues/21902
138 */
139 public function locateStatementWithPositionIsProperlyQuoted() {
140 $result = $this->subject->SELECTquery('*, CASE WHEN' . ' LOCATE(' . $this->subject->fullQuoteStr('(fce)', 'tx_templavoila_tmplobj') . ', datastructure, 4)>0 THEN 2' . ' ELSE 1' . ' END AS scope', 'tx_templavoila_tmplobj', '1=1');
141 $expected = 'SELECT *, CASE WHEN CHARINDEX(\'(fce)\', "datastructure", 4) > 0 THEN 2 ELSE 1 END AS "scope" FROM "tx_templavoila_tmplobj" WHERE 1 = 1';
142 $this->assertEquals($expected, $this->cleanSql($result));
143 }
144
145 /**
146 * @test
147 * @see http://forge.typo3.org/issues/21902
148 */
149 public function locateStatementIsProperlyRemapped() {
150 $selectFields = '*, CASE WHEN' . ' LOCATE(' . $this->subject->fullQuoteStr('(fce)', 'tx_templavoila_tmplobj') . ', datastructure, 4)>0 THEN 2' . ' ELSE 1' . ' END AS scope';
151 $fromTables = 'tx_templavoila_tmplobj';
152 $whereClause = '1=1';
153 $groupBy = '';
154 $orderBy = '';
155 $remappedParameters = $this->subject->_call('map_remapSELECTQueryParts', $selectFields, $fromTables, $whereClause, $groupBy, $orderBy);
156
157 $result = $this->subject->_call('SELECTqueryFromArray', $remappedParameters);
158 $expected = 'SELECT *, CASE WHEN CHARINDEX(\'(fce)\', "ds", 4) > 0 THEN 2 ELSE 1 END AS "scope" FROM "tx_templavoila_tmplobj" WHERE 1 = 1';
159 $this->assertEquals($expected, $this->cleanSql($result));
160 }
161
162 /**
163 * @test
164 * @see http://forge.typo3.org/issues/21902
165 */
166 public function locateStatementWithExternalTableIsProperlyRemapped() {
167 $selectFields = '*, CASE WHEN' . ' LOCATE(' . $this->subject->fullQuoteStr('(fce)', 'tx_templavoila_tmplobj') . ', tx_templavoila_tmplobj.datastructure, 4)>0 THEN 2' . ' ELSE 1' . ' END AS scope';
168 $fromTables = 'tx_templavoila_tmplobj';
169 $whereClause = '1=1';
170 $groupBy = '';
171 $orderBy = '';
172 $remappedParameters = $this->subject->_call('map_remapSELECTQueryParts', $selectFields, $fromTables, $whereClause, $groupBy, $orderBy);
173
174 $result = $this->subject->_call('SELECTqueryFromArray', $remappedParameters);
175 $expected = 'SELECT *, CASE WHEN CHARINDEX(\'(fce)\', "tx_templavoila_tmplobj"."ds", 4) > 0 THEN 2 ELSE 1 END AS "scope" FROM "tx_templavoila_tmplobj" WHERE 1 = 1';
176 $this->assertEquals($expected, $this->cleanSql($result));
177 }
178
179 /**
180 * @test
181 * @see http://forge.typo3.org/issues/17552
182 */
183 public function IfNullIsProperlyRemapped() {
184 $result = $this->subject->SELECTquery('*', 'tt_news_cat_mm', 'IFNULL(tt_news_cat_mm.uid_foreign,0) IN (21,22)');
185 $expected = 'SELECT * FROM "tt_news_cat_mm" WHERE ISNULL("tt_news_cat_mm"."uid_foreign", 0) IN (21,22)';
186 $this->assertEquals($expected, $this->cleanSql($result));
187 }
188 }