2 namespace TYPO3\CMS\Core\Tests\Unit\Database
;
4 /***************************************************************
7 * (c) 2010-2013 Ernesto Baschny (ernst@cron-it.de)
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.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
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.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
31 class DatabaseConnectionTest
extends \TYPO3\CMS\Core\Tests\UnitTestCase
{
33 //////////////////////////////////////////////////
34 // Write/Read tests for charsets and binaries
35 //////////////////////////////////////////////////
40 public function storedFullAsciiRangeCallsLinkObjectWithGivenData() {
42 for ($i = 0; $i < 256; $i++
) {
43 $binaryString .= chr($i);
46 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
47 $subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('fullQuoteStr'), array(), '', FALSE);
48 $subject->_set('isConnected', TRUE);
50 ->expects($this->any())
51 ->method('fullQuoteStr')
52 ->will($this->returnCallback(function ($data) {
55 $mysqliMock = $this->getMock('mysqli');
57 ->expects($this->once())
59 ->with('INSERT INTO aTable (fieldblob) VALUES (' . $binaryString . ')');
60 $subject->_set('link', $mysqliMock);
62 $subject->exec_INSERTquery('aTable', array('fieldblob' => $binaryString));
68 public function storedGzipCompressedDataReturnsSameData() {
69 $testStringWithBinary = @gzcompress
('sdfkljer4587');
71 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
72 $subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('fullQuoteStr'), array(), '', FALSE);
73 $subject->_set('isConnected', TRUE);
75 ->expects($this->any())
76 ->method('fullQuoteStr')
77 ->will($this->returnCallback(function ($data) {
80 $mysqliMock = $this->getMock('mysqli');
82 ->expects($this->once())
84 ->with('INSERT INTO aTable (fieldblob) VALUES (' . $testStringWithBinary . ')');
85 $subject->_set('link', $mysqliMock);
87 $subject->exec_INSERTquery('aTable', array('fieldblob' => $testStringWithBinary));
91 ////////////////////////////////
92 // Tests concerning listQuery
93 ////////////////////////////////
97 * @see http://forge.typo3.org/issues/23253
99 public function listQueryWithIntegerCommaAsValue() {
100 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
101 $subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('quoteStr'), array(), '', FALSE);
102 $subject->_set('isConnected', TRUE);
104 ->expects($this->any())
106 ->will($this->returnCallback(function ($data) {
109 // Note: 44 = ord(',')
110 $this->assertEquals($subject->listQuery('dummy', 44, 'table'), $subject->listQuery('dummy', '44', 'table'));
115 * @expectedException \InvalidArgumentException
117 public function listQueryThrowsExceptionIfValueContainsComma() {
118 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
119 $subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('quoteStr'), array(), '', FALSE);
120 $subject->_set('isConnected', TRUE);
121 $subject->listQuery('aField', 'foo,bar', 'aTable');
125 ////////////////////////////////
126 // Tests concerning searchQuery
127 ////////////////////////////////
130 * Data provider for searchQueryCreatesQuery
134 public function searchQueryDataProvider() {
136 'One search word in one field' => array(
137 '(pages.title LIKE \'%TYPO3%\')',
144 'One search word in multiple fields' => array(
145 '(pages.title LIKE \'%TYPO3%\' OR pages.keyword LIKE \'%TYPO3%\' OR pages.description LIKE \'%TYPO3%\')',
147 array('title', 'keyword', 'description'),
152 'Multiple search words in one field with AND constraint' => array(
153 '(pages.title LIKE \'%TYPO3%\') AND (pages.title LIKE \'%is%\') AND (pages.title LIKE \'%great%\')',
154 array('TYPO3', 'is', 'great'),
160 'Multiple search words in one field with OR constraint' => array(
161 '(pages.title LIKE \'%TYPO3%\') OR (pages.title LIKE \'%is%\') OR (pages.title LIKE \'%great%\')',
162 array('TYPO3', 'is', 'great'),
168 'Multiple search words in multiple fields with AND constraint' => array(
169 '(pages.title LIKE \'%TYPO3%\' OR pages.keywords LIKE \'%TYPO3%\' OR pages.description LIKE \'%TYPO3%\') AND ' .
170 '(pages.title LIKE \'%is%\' OR pages.keywords LIKE \'%is%\' OR pages.description LIKE \'%is%\') AND ' .
171 '(pages.title LIKE \'%great%\' OR pages.keywords LIKE \'%great%\' OR pages.description LIKE \'%great%\')',
172 array('TYPO3', 'is', 'great'),
173 array('title', 'keywords', 'description'),
178 'Multiple search words in multiple fields with OR constraint' => array(
179 '(pages.title LIKE \'%TYPO3%\' OR pages.keywords LIKE \'%TYPO3%\' OR pages.description LIKE \'%TYPO3%\') OR ' .
180 '(pages.title LIKE \'%is%\' OR pages.keywords LIKE \'%is%\' OR pages.description LIKE \'%is%\') OR ' .
181 '(pages.title LIKE \'%great%\' OR pages.keywords LIKE \'%great%\' OR pages.description LIKE \'%great%\')',
182 array('TYPO3', 'is', 'great'),
183 array('title', 'keywords', 'description'),
192 * @dataProvider searchQueryDataProvider
194 public function searchQueryCreatesQuery($expectedResult, $searchWords, $fields, $table, $constraint) {
195 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject $subject */
196 $subject = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('quoteStr'), array(), '', FALSE);
198 ->expects($this->any())
200 ->will($this->returnCallback(function ($data) {
204 $this->assertSame($expectedResult, $subject->searchQuery($searchWords, $fields, $table, $constraint));
208 /////////////////////////////////////////////////
209 // Tests concerning escapeStringForLikeComparison
210 /////////////////////////////////////////////////
215 public function escapeStringForLikeComparison() {
216 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject $subject */
217 $subject = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('dummy'), array(), '', FALSE);
218 $this->assertEquals('foo\\_bar\\%', $subject->escapeStrForLike('foo_bar%', 'table'));
222 /////////////////////////////////////////////////
223 // Tests concerning stripOrderByForOrderByKeyword
224 /////////////////////////////////////////////////
227 * Data Provider for stripGroupByForGroupByKeyword()
229 * @see stripOrderByForOrderByKeyword()
232 public function stripOrderByForOrderByKeywordDataProvider() {
234 'single ORDER BY' => array('ORDER BY name, tstamp', 'name, tstamp'),
235 'single ORDER BY in lower case' => array('order by name, tstamp', 'name, tstamp'),
236 'ORDER BY with additional space behind' => array('ORDER BY name, tstamp', 'name, tstamp'),
237 'ORDER BY without space between the words' => array('ORDERBY name, tstamp', 'name, tstamp'),
238 'ORDER BY added twice' => array('ORDER BY ORDER BY name, tstamp', 'name, tstamp'),
239 'ORDER BY added twice without spaces in the first occurrence' => array('ORDERBY ORDER BY name, tstamp', 'name, tstamp'),
240 'ORDER BY added twice without spaces in the second occurrence' => array('ORDER BYORDERBY name, tstamp', 'name, tstamp'),
241 'ORDER BY added twice without spaces' => array('ORDERBYORDERBY name, tstamp', 'name, tstamp'),
242 'ORDER BY added twice without spaces afterwards' => array('ORDERBYORDERBYname, tstamp', 'name, tstamp'),
248 * @dataProvider stripOrderByForOrderByKeywordDataProvider
249 * @param string $orderByClause The clause to test
250 * @param string $expectedResult The expected result
253 public function stripOrderByForOrderByKeyword($orderByClause, $expectedResult) {
254 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject $subject */
255 $subject = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('dummy'), array(), '', FALSE);
256 $strippedQuery = $subject->stripOrderBy($orderByClause);
257 $this->assertEquals($expectedResult, $strippedQuery);
261 /////////////////////////////////////////////////
262 // Tests concerning stripGroupByForGroupByKeyword
263 /////////////////////////////////////////////////
266 * Data Provider for stripGroupByForGroupByKeyword()
268 * @see stripGroupByForGroupByKeyword()
271 public function stripGroupByForGroupByKeywordDataProvider() {
273 'single GROUP BY' => array('GROUP BY name, tstamp', 'name, tstamp'),
274 'single GROUP BY in lower case' => array('group by name, tstamp', 'name, tstamp'),
275 'GROUP BY with additional space behind' => array('GROUP BY name, tstamp', 'name, tstamp'),
276 'GROUP BY without space between the words' => array('GROUPBY name, tstamp', 'name, tstamp'),
277 'GROUP BY added twice' => array('GROUP BY GROUP BY name, tstamp', 'name, tstamp'),
278 'GROUP BY added twice without spaces in the first occurrence' => array('GROUPBY GROUP BY name, tstamp', 'name, tstamp'),
279 'GROUP BY added twice without spaces in the second occurrence' => array('GROUP BYGROUPBY name, tstamp', 'name, tstamp'),
280 'GROUP BY added twice without spaces' => array('GROUPBYGROUPBY name, tstamp', 'name, tstamp'),
281 'GROUP BY added twice without spaces afterwards' => array('GROUPBYGROUPBYname, tstamp', 'name, tstamp'),
287 * @dataProvider stripGroupByForGroupByKeywordDataProvider
288 * @param string $groupByClause The clause to test
289 * @param string $expectedResult The expected result
292 public function stripGroupByForGroupByKeyword($groupByClause, $expectedResult) {
293 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject $subject */
294 $subject = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('dummy'), array(), '', FALSE);
295 $strippedQuery = $subject->stripGroupBy($groupByClause);
296 $this->assertEquals($expectedResult, $strippedQuery);