TYPO3 CMS  TYPO3_6-2
DatabaseConnectionTest.php
Go to the documentation of this file.
1 <?php
3 
22 
24  // Write/Read tests for charsets and binaries
26 
30  public function storedFullAsciiRangeCallsLinkObjectWithGivenData() {
31  $binaryString = '';
32  for ($i = 0; $i < 256; $i++) {
33  $binaryString .= chr($i);
34  }
35 
37  $subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('fullQuoteStr'), array(), '', FALSE);
38  $subject->_set('isConnected', TRUE);
39  $subject
40  ->expects($this->any())
41  ->method('fullQuoteStr')
42  ->will($this->returnCallback(function ($data) {
43  return $data;
44  }));
45  $mysqliMock = $this->getMock('mysqli');
46  $mysqliMock
47  ->expects($this->once())
48  ->method('query')
49  ->with('INSERT INTO aTable (fieldblob) VALUES (' . $binaryString . ')');
50  $subject->_set('link', $mysqliMock);
51 
52  $subject->exec_INSERTquery('aTable', array('fieldblob' => $binaryString));
53  }
54 
58  public function storedGzipCompressedDataReturnsSameData() {
59  $testStringWithBinary = @gzcompress('sdfkljer4587');
60 
62  $subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('fullQuoteStr'), array(), '', FALSE);
63  $subject->_set('isConnected', TRUE);
64  $subject
65  ->expects($this->any())
66  ->method('fullQuoteStr')
67  ->will($this->returnCallback(function ($data) {
68  return $data;
69  }));
70  $mysqliMock = $this->getMock('mysqli');
71  $mysqliMock
72  ->expects($this->once())
73  ->method('query')
74  ->with('INSERT INTO aTable (fieldblob) VALUES (' . $testStringWithBinary . ')');
75  $subject->_set('link', $mysqliMock);
76 
77  $subject->exec_INSERTquery('aTable', array('fieldblob' => $testStringWithBinary));
78  }
79 
80 
82  // Tests concerning listQuery
84 
89  public function listQueryWithIntegerCommaAsValue() {
91  $subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('quoteStr'), array(), '', FALSE);
92  $subject->_set('isConnected', TRUE);
93  $subject
94  ->expects($this->any())
95  ->method('quoteStr')
96  ->will($this->returnCallback(function ($data) {
97  return $data;
98  }));
99  // Note: 44 = ord(',')
100  $this->assertEquals($subject->listQuery('dummy', 44, 'table'), $subject->listQuery('dummy', '44', 'table'));
101  }
102 
107  public function listQueryThrowsExceptionIfValueContainsComma() {
109  $subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('quoteStr'), array(), '', FALSE);
110  $subject->_set('isConnected', TRUE);
111  $subject->listQuery('aField', 'foo,bar', 'aTable');
112  }
113 
114 
116  // Tests concerning searchQuery
118 
124  public function searchQueryDataProvider() {
125  return array(
126  'One search word in one field' => array(
127  '(pages.title LIKE \'%TYPO3%\')',
128  array('TYPO3'),
129  array('title'),
130  'pages',
131  'AND'
132  ),
133 
134  'One search word in multiple fields' => array(
135  '(pages.title LIKE \'%TYPO3%\' OR pages.keyword LIKE \'%TYPO3%\' OR pages.description LIKE \'%TYPO3%\')',
136  array('TYPO3'),
137  array('title', 'keyword', 'description'),
138  'pages',
139  'AND'
140  ),
141 
142  'Multiple search words in one field with AND constraint' => array(
143  '(pages.title LIKE \'%TYPO3%\') AND (pages.title LIKE \'%is%\') AND (pages.title LIKE \'%great%\')',
144  array('TYPO3', 'is', 'great'),
145  array('title'),
146  'pages',
147  'AND'
148  ),
149 
150  'Multiple search words in one field with OR constraint' => array(
151  '(pages.title LIKE \'%TYPO3%\') OR (pages.title LIKE \'%is%\') OR (pages.title LIKE \'%great%\')',
152  array('TYPO3', 'is', 'great'),
153  array('title'),
154  'pages',
155  'OR'
156  ),
157 
158  'Multiple search words in multiple fields with AND constraint' => array(
159  '(pages.title LIKE \'%TYPO3%\' OR pages.keywords LIKE \'%TYPO3%\' OR pages.description LIKE \'%TYPO3%\') AND ' .
160  '(pages.title LIKE \'%is%\' OR pages.keywords LIKE \'%is%\' OR pages.description LIKE \'%is%\') AND ' .
161  '(pages.title LIKE \'%great%\' OR pages.keywords LIKE \'%great%\' OR pages.description LIKE \'%great%\')',
162  array('TYPO3', 'is', 'great'),
163  array('title', 'keywords', 'description'),
164  'pages',
165  'AND'
166  ),
167 
168  'Multiple search words in multiple fields with OR constraint' => array(
169  '(pages.title LIKE \'%TYPO3%\' OR pages.keywords LIKE \'%TYPO3%\' OR pages.description LIKE \'%TYPO3%\') OR ' .
170  '(pages.title LIKE \'%is%\' OR pages.keywords LIKE \'%is%\' OR pages.description LIKE \'%is%\') OR ' .
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'),
174  'pages',
175  'OR'
176  ),
177  );
178  }
179 
184  public function searchQueryCreatesQuery($expectedResult, $searchWords, $fields, $table, $constraint) {
186  $subject = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('quoteStr'), array(), '', FALSE);
187  $subject
188  ->expects($this->any())
189  ->method('quoteStr')
190  ->will($this->returnCallback(function ($data) {
191  return $data;
192  }));
193 
194  $this->assertSame($expectedResult, $subject->searchQuery($searchWords, $fields, $table, $constraint));
195  }
196 
197 
199  // Tests concerning escapeStringForLikeComparison
201 
205  public function escapeStringForLikeComparison() {
207  $subject = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('dummy'), array(), '', FALSE);
208  $this->assertEquals('foo\\_bar\\%', $subject->escapeStrForLike('foo_bar%', 'table'));
209  }
210 
211 
213  // Tests concerning stripOrderByForOrderByKeyword
215 
223  return array(
224  'single ORDER BY' => array('ORDER BY name, tstamp', 'name, tstamp'),
225  'single ORDER BY in lower case' => array('order by name, tstamp', 'name, tstamp'),
226  'ORDER BY with additional space behind' => array('ORDER BY name, tstamp', 'name, tstamp'),
227  'ORDER BY without space between the words' => array('ORDERBY name, tstamp', 'name, tstamp'),
228  'ORDER BY added twice' => array('ORDER BY ORDER BY name, tstamp', 'name, tstamp'),
229  'ORDER BY added twice without spaces in the first occurrence' => array('ORDERBY ORDER BY name, tstamp', 'name, tstamp'),
230  'ORDER BY added twice without spaces in the second occurrence' => array('ORDER BYORDERBY name, tstamp', 'name, tstamp'),
231  'ORDER BY added twice without spaces' => array('ORDERBYORDERBY name, tstamp', 'name, tstamp'),
232  'ORDER BY added twice without spaces afterwards' => array('ORDERBYORDERBYname, tstamp', 'name, tstamp'),
233  );
234  }
235 
243  public function stripOrderByForOrderByKeyword($orderByClause, $expectedResult) {
245  $subject = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('dummy'), array(), '', FALSE);
246  $strippedQuery = $subject->stripOrderBy($orderByClause);
247  $this->assertEquals($expectedResult, $strippedQuery);
248  }
249 
250 
252  // Tests concerning stripGroupByForGroupByKeyword
254 
262  return array(
263  'single GROUP BY' => array('GROUP BY name, tstamp', 'name, tstamp'),
264  'single GROUP BY in lower case' => array('group by name, tstamp', 'name, tstamp'),
265  'GROUP BY with additional space behind' => array('GROUP BY name, tstamp', 'name, tstamp'),
266  'GROUP BY without space between the words' => array('GROUPBY name, tstamp', 'name, tstamp'),
267  'GROUP BY added twice' => array('GROUP BY GROUP BY name, tstamp', 'name, tstamp'),
268  'GROUP BY added twice without spaces in the first occurrence' => array('GROUPBY GROUP BY name, tstamp', 'name, tstamp'),
269  'GROUP BY added twice without spaces in the second occurrence' => array('GROUP BYGROUPBY name, tstamp', 'name, tstamp'),
270  'GROUP BY added twice without spaces' => array('GROUPBYGROUPBY name, tstamp', 'name, tstamp'),
271  'GROUP BY added twice without spaces afterwards' => array('GROUPBYGROUPBYname, tstamp', 'name, tstamp'),
272  );
273  }
274 
282  public function stripGroupByForGroupByKeyword($groupByClause, $expectedResult) {
284  $subject = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('dummy'), array(), '', FALSE);
285  $strippedQuery = $subject->stripGroupBy($groupByClause);
286  $this->assertEquals($expectedResult, $strippedQuery);
287  }
288 
289 
291  // Tests concerning stripOrderByForOrderByKeyword
293 
300  public function cleanIntArrayDataProvider() {
301  return array(
302  'simple array' => array(
303  array(1, 2, 3),
304  array(1, 2, 3)
305  ),
306  'string array' => array(
307  array('2', '4', '8'),
308  array(2, 4, 8)
309  ),
310  'string array with letters #1' => array(
311  array('3', '6letters', '12'),
312  array(3, 6, 12)
313  ),
314  'string array with letters #2' => array(
315  array('3', 'letters6', '12'),
316  array(3, 0, 12)
317  ),
318  'string array with letters #3' => array(
319  array('3', '6letters4', '12'),
320  array(3, 6, 12)
321  ),
322  'associative array' => array(
323  array('apples' => 3, 'bananas' => 4, 'kiwis' => 9),
324  array('apples' => 3, 'bananas' => 4, 'kiwis' => 9)
325  ),
326  'associative string array' => array(
327  array('apples' => '1', 'bananas' => '5', 'kiwis' => '7'),
328  array('apples' => 1, 'bananas' => 5, 'kiwis' => 7)
329  ),
330  'associative string array with letters #1' => array(
331  array('apples' => '1', 'bananas' => 'no5', 'kiwis' => '7'),
332  array('apples' => 1, 'bananas' => 0, 'kiwis' => 7)
333  ),
334  'associative string array with letters #2' => array(
335  array('apples' => '1', 'bananas' => '5yes', 'kiwis' => '7'),
336  array('apples' => 1, 'bananas' => 5, 'kiwis' => 7)
337  ),
338  'associative string array with letters #3' => array(
339  array('apples' => '1', 'bananas' => '5yes9', 'kiwis' => '7'),
340  array('apples' => 1, 'bananas' => 5, 'kiwis' => 7)
341  ),
342  'multidimensional associative array' => array(
343  array('apples' => '1', 'bananas' => array(3, 4), 'kiwis' => '7'),
344  // intval(array(...)) is 1
345  // But by specification "cleanIntArray" should only get used on one-dimensional arrays
346  array('apples' => 1, 'bananas' => 1, 'kiwis' => 7)
347  ),
348  );
349  }
350 
358  public function cleanIntArray($exampleData, $expectedResult) {
360  $subject = new \TYPO3\CMS\Core\Database\DatabaseConnection();
361  $sanitizedArray = $subject->cleanIntArray($exampleData);
362  $this->assertEquals($expectedResult, $sanitizedArray);
363  }
364 
365 }
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)