717c88a3d7f4750874ece040c3932b26feac144a
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / DatabaseConnectionTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Database;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Ernesto Baschny (ernst@cron-it.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 /**
28 * Testcase for TYPO3\CMS\Core\Database\DatabaseConnection
29 *
30 * @author Ernesto Baschny <ernst@cron-it.de>
31 */
32 class DatabaseConnectionTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
36 */
37 private $fixture = NULL;
38
39 private $testTable;
40
41 public function setUp() {
42 $this->fixture = $GLOBALS['TYPO3_DB'];
43 $this->testTable = 'test_t3lib_dbtest';
44 $this->fixture->sql_query('CREATE TABLE ' . $this->testTable . ' (
45 id int(11) unsigned NOT NULL auto_increment,
46 fieldblob mediumblob,
47 PRIMARY KEY (id)
48 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
49 ');
50 }
51
52 public function tearDown() {
53 $this->fixture->sql_query('DROP TABLE ' . $this->testTable . ';');
54 unset($this->fixture);
55 }
56
57 //////////////////////////////////////////////////
58 // Write/Read tests for charsets and binaries
59 //////////////////////////////////////////////////
60 /**
61 * @test
62 */
63 public function storedFullAsciiRangeReturnsSameData() {
64 $binaryString = '';
65 for ($i = 0; $i < 256; $i++) {
66 $binaryString .= chr($i);
67 }
68 $this->fixture->exec_INSERTquery($this->testTable, array('fieldblob' => $binaryString));
69 $id = $this->fixture->sql_insert_id();
70 $entry = $this->fixture->exec_SELECTgetRows('fieldblob', $this->testTable, 'id = ' . $id);
71 $this->assertEquals($binaryString, $entry[0]['fieldblob']);
72 }
73
74 /**
75 * @test
76 */
77 public function storedGzipCompressedDataReturnsSameData() {
78 $testStringWithBinary = @gzcompress('sdfkljer4587');
79 $this->fixture->exec_INSERTquery($this->testTable, array('fieldblob' => $testStringWithBinary));
80 $id = $this->fixture->sql_insert_id();
81 $entry = $this->fixture->exec_SELECTgetRows('fieldblob', $this->testTable, 'id = ' . $id);
82 $this->assertEquals($testStringWithBinary, $entry[0]['fieldblob']);
83 }
84
85 ////////////////////////////////
86 // Tests concerning listQuery
87 ////////////////////////////////
88 /**
89 * @test
90 * @see http://bugs.typo3.org/view.php?id=15211
91 */
92 public function listQueryWithIntegerCommaAsValue() {
93 // Note: 44 = ord(',')
94 $this->assertEquals($this->fixture->listQuery('dummy', 44, 'table'), $this->fixture->listQuery('dummy', '44', 'table'));
95 }
96
97 /////////////////////////////////////////////////
98 // Tests concerning escapeStringForLikeComparison
99 /////////////////////////////////////////////////
100 /**
101 * @test
102 */
103 public function escapeStringForLikeComparison() {
104 $this->assertEquals('foo\\_bar\\%', $this->fixture->escapeStrForLike('foo_bar%', 'table'));
105 }
106
107 /////////////////////////////////////////////////
108 // Tests concerning stripOrderByForOrderByKeyword
109 /////////////////////////////////////////////////
110
111
112 /**
113 * Data Provider for stripGroupByForGroupByKeyword()
114 *
115 * @see stripOrderByForOrderByKeyword()
116 * @return array
117 */
118 public function stripOrderByForOrderByKeywordDataProvider() {
119 return array(
120 'single ORDER BY' => array('ORDER BY name, tstamp', 'name, tstamp'),
121 'single ORDER BY in lower case' => array('order by name, tstamp', 'name, tstamp'),
122 'ORDER BY with additional space behind' => array('ORDER BY name, tstamp', 'name, tstamp'),
123 'ORDER BY without space between the words' => array('ORDERBY name, tstamp', 'name, tstamp'),
124 'ORDER BY added twice' => array('ORDER BY ORDER BY name, tstamp', 'name, tstamp'),
125 'ORDER BY added twice without spaces in the first occurrence' => array('ORDERBY ORDER BY name, tstamp', 'name, tstamp'),
126 'ORDER BY added twice without spaces in the second occurrence' => array('ORDER BYORDERBY name, tstamp', 'name, tstamp'),
127 'ORDER BY added twice without spaces' => array('ORDERBYORDERBY name, tstamp', 'name, tstamp'),
128 'ORDER BY added twice without spaces afterwards' => array('ORDERBYORDERBYname, tstamp', 'name, tstamp'),
129 );
130 }
131
132 /**
133 * @test
134 * @dataProvider stripOrderByForOrderByKeywordDataProvider
135 * @param string $orderByClause The clause to test
136 * @param string $expectedResult The expected result
137 * @return void
138 */
139 public function stripOrderByForOrderByKeyword($orderByClause, $expectedResult) {
140 $strippedQuery = $this->fixture->stripOrderBy($orderByClause);
141 $this->assertEquals($expectedResult, $strippedQuery);
142 }
143
144 /////////////////////////////////////////////////
145 // Tests concerning stripGroupByForGroupByKeyword
146 /////////////////////////////////////////////////
147
148 /**
149 * Data Provider for stripGroupByForGroupByKeyword()
150 *
151 * @see stripGroupByForGroupByKeyword()
152 * @return array
153 */
154 public function stripGroupByForGroupByKeywordDataProvider() {
155 return array(
156 'single GROUP BY' => array('GROUP BY name, tstamp', 'name, tstamp'),
157 'single GROUP BY in lower case' => array('group by name, tstamp', 'name, tstamp'),
158 'GROUP BY with additional space behind' => array('GROUP BY name, tstamp', 'name, tstamp'),
159 'GROUP BY without space between the words' => array('GROUPBY name, tstamp', 'name, tstamp'),
160 'GROUP BY added twice' => array('GROUP BY GROUP BY name, tstamp', 'name, tstamp'),
161 'GROUP BY added twice without spaces in the first occurrence' => array('GROUPBY GROUP BY name, tstamp', 'name, tstamp'),
162 'GROUP BY added twice without spaces in the second occurrence' => array('GROUP BYGROUPBY name, tstamp', 'name, tstamp'),
163 'GROUP BY added twice without spaces' => array('GROUPBYGROUPBY name, tstamp', 'name, tstamp'),
164 'GROUP BY added twice without spaces afterwards' => array('GROUPBYGROUPBYname, tstamp', 'name, tstamp'),
165 );
166 }
167
168 /**
169 * @test
170 * @dataProvider stripGroupByForGroupByKeywordDataProvider
171 * @param string $groupByClause The clause to test
172 * @param string $expectedResult The expected result
173 * @return void
174 */
175 public function stripGroupByForGroupByKeyword($groupByClause, $expectedResult) {
176 $strippedQuery = $this->fixture->stripGroupBy($groupByClause);
177 $this->assertEquals($expectedResult, $strippedQuery);
178 }
179
180 }
181
182 ?>