TYPO3 CMS  TYPO3_6-2
DatabaseConnectionTest.php
Go to the documentation of this file.
1 <?php
3 
18 
23 
27  protected $subject;
28 
32  protected $temporaryFiles = array();
33 
37  public function setUp() {
38  $GLOBALS['TYPO3_LOADED_EXT'] = array();
39 
41  $subject = $this->getAccessibleMock('TYPO3\\CMS\\Dbal\\Database\\DatabaseConnection', array('getFieldInfoCache'), array(), '', FALSE);
42 
43  // Disable caching
44  $mockCacheFrontend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\PhpFrontend', array(), array(), '', FALSE);
45  $subject->expects($this->any())->method('getFieldInfoCache')->will($this->returnValue($mockCacheFrontend));
46 
47  // Inject SqlParser - Its logic is tested with the tests, too.
48  $sqlParser = $this->getAccessibleMock('TYPO3\\CMS\\Dbal\\Database\\SqlParser', array('dummy'), array(), '', FALSE);
49  $sqlParser->_set('databaseConnection', $subject);
50  $subject->SQLparser = $sqlParser;
51 
52  // Mock away schema migration service from install tool
53  $installerSqlMock = $this->getMock('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService', array('getFieldDefinitions_fileContent'), array(), '', FALSE);
54  $installerSqlMock->expects($this->any())->method('getFieldDefinitions_fileContent')->will($this->returnValue(array()));
55  $subject->_set('installerSql', $installerSqlMock);
56 
57  $subject->initialize();
58  $subject->lastHandlerKey = '_DEFAULT';
59 
60  $this->subject = $subject;
61  }
62 
66  public function tearDown() {
67  // Delete temporary files
68  foreach ($this->temporaryFiles as $filename) {
69  unlink($filename);
70  }
71  parent::tearDown();
72  }
73 
81  protected function createFakeExtension($tableDefinition) {
82  // Prepare a fake extension configuration
83  $ext_tables = GeneralUtility::tempnam('ext_tables');
84  if (!GeneralUtility::writeFile($ext_tables, $tableDefinition)) {
85  throw new \RuntimeException('Can\'t write temporary ext_tables file.');
86  }
87  $this->temporaryFiles[] = $ext_tables;
88  $GLOBALS['TYPO3_LOADED_EXT'] = array(
89  'test_dbal' => array(
90  'ext_tables.sql' => $ext_tables
91  )
92  );
93  // Append our test table to the list of existing tables
94  $this->subject->initialize();
95  }
96 
100  public function tableWithMappingIsDetected() {
101  $dbalConfiguration = array(
102  'mapping' => array(
103  'cf_cache_hash' => array(),
104  ),
105  );
106 
108  $subject = $this->getAccessibleMock('TYPO3\\CMS\\Dbal\\Database\\DatabaseConnection', array('getFieldInfoCache'), array(), '', FALSE);
109 
110  $mockCacheFrontend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\PhpFrontend', array(), array(), '', FALSE);
111  $subject->expects($this->any())->method('getFieldInfoCache')->will($this->returnValue($mockCacheFrontend));
112 
113  $sqlParser = $this->getAccessibleMock('TYPO3\\CMS\\Dbal\\Database\\SqlParser', array('dummy'), array(), '', FALSE);
114  $sqlParser->_set('databaseConnection', $subject);
115  $subject->SQLparser = $sqlParser;
116 
117  $installerSqlMock = $this->getMock('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService', array(), array(), '', FALSE);
118  $subject->_set('installerSql', $installerSqlMock);
119  $schemaMigrationResult = array(
120  'cf_cache_pages' => array(),
121  );
122  $installerSqlMock->expects($this->once())->method('getFieldDefinitions_fileContent')->will($this->returnValue($schemaMigrationResult));
123 
124  $subject->conf = $dbalConfiguration;
125  $subject->initialize();
126  $subject->lastHandlerKey = '_DEFAULT';
127 
128  $this->assertFalse($subject->_call('map_needMapping', 'cf_cache_pages'));
129  $cfCacheHashNeedsMapping = $subject->_call('map_needMapping', 'cf_cache_hash');
130  $this->assertEquals('cf_cache_hash', $cfCacheHashNeedsMapping[0]['table']);
131  }
132 
138  $handlerMock = $this->getMock('\ADODB_mock', array('MetaTables'), array(), '', FALSE);
139  $handlerMock->expects($this->any())->method('MetaTables')->will($this->returnValue(array('cf_cache_hash')));
140  $this->subject->handlerCfg['_DEFAULT']['type'] = 'adodb';
141  $this->subject->handlerInstance['_DEFAULT'] = $handlerMock;
142 
143  $actual = $this->subject->admin_get_tables();
144  $expected = array('cf_cache_hash' => array('Name' => 'cf_cache_hash'));
145  $this->assertSame($expected, $actual);
146  }
147 
153  $result = $this->subject->SELECTquery('*', 'sys_refindex, tx_dam_file_tracking', 'sys_refindex.tablename = \'tx_dam_file_tracking\'' . ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path, tx_dam_file_tracking.file_name)');
154  $expected = 'SELECT * FROM sys_refindex, tx_dam_file_tracking WHERE sys_refindex.tablename = \'tx_dam_file_tracking\'';
155  $expected .= ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path, tx_dam_file_tracking.file_name)';
156  $this->assertEquals($expected, $this->cleanSql($result));
157  }
158 
164  $this->createFakeExtension('
165  CREATE TABLE tx_test_dbal (
166  foo double default \'0\',
167  foobar int default \'0\'
168  );
169  ');
170  $data = array(
171  'foo' => 99.12,
172  'foobar' => -120
173  );
174  $result = $this->subject->INSERTquery('tx_test_dbal', $data);
175  $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'99.12\', \'-120\' )';
176  $this->assertEquals($expected, $this->cleanSql($result));
177  }
178 
184  if (!is_int(9223372036854775806)) {
185  $this->markTestSkipped('Test skipped because running on 32 bit system.');
186  }
187  $this->createFakeExtension('
188  CREATE TABLE tx_test_dbal (
189  foo int default \'0\',
190  foobar bigint default \'0\'
191  );
192  ');
193  $data = array(
194  'foo' => 9223372036854775807,
195  'foobar' => 9223372036854775807
196  );
197  $result = $this->subject->INSERTquery('tx_test_dbal', $data);
198  $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'9223372036854775807\', \'9223372036854775807\' )';
199  $this->assertEquals($expected, $this->cleanSql($result));
200  }
201 
206  $fields = array('uid', 'pid', 'title', 'body');
207  $rows = array(
208  array('1', '2', 'Title #1', 'Content #1'),
209  array('3', '4', 'Title #2', 'Content #2'),
210  array('5', '6', 'Title #3', 'Content #3')
211  );
212  $result = $this->subject->INSERTmultipleRows('tt_content', $fields, $rows);
213  $expected = 'INSERT INTO tt_content (uid, pid, title, body) VALUES ';
214  $expected .= '(\'1\', \'2\', \'Title #1\', \'Content #1\'), ';
215  $expected .= '(\'3\', \'4\', \'Title #2\', \'Content #2\'), ';
216  $expected .= '(\'5\', \'6\', \'Title #3\', \'Content #3\')';
217  $this->assertEquals($expected, $this->cleanSql($result));
218  }
219 
225  $result = $this->subject->SELECTquery('*', 'pages', 'MIN(uid) IN (1,2,3,4)');
226  $expected = 'SELECT * FROM pages WHERE MIN(uid) IN (1,2,3,4)';
227  $this->assertEquals($expected, $this->cleanSql($result));
228  }
229 
235  $result = $this->subject->SELECTquery('*', 'pages', 'MAX(uid) IN (1,2,3,4)');
236  $expected = 'SELECT * FROM pages WHERE MAX(uid) IN (1,2,3,4)';
237  $this->assertEquals($expected, $this->cleanSql($result));
238  }
239 
244  public function likeBinaryOperatorIsKept() {
245  $result = $this->cleanSql($this->subject->SELECTquery('*', 'tt_content', 'bodytext LIKE BINARY \'test\''));
246  $expected = 'SELECT * FROM tt_content WHERE bodytext LIKE BINARY \'test\'';
247  $this->assertEquals($expected, $this->cleanSql($result));
248  }
249 
254  public function notLikeBinaryOperatorIsKept() {
255  $result = $this->cleanSql($this->subject->SELECTquery('*', 'tt_content', 'bodytext NOT LIKE BINARY \'test\''));
256  $expected = 'SELECT * FROM tt_content WHERE bodytext NOT LIKE BINARY \'test\'';
257  $this->assertEquals($expected, $this->cleanSql($result));
258  }
259 
261  // Tests concerning prepared queries
263 
268  $sql = 'SELECT * FROM cache WHERE tag = :tag1 OR tag = :tag10 OR tag = :tag100';
269  $parameterValues = array(
270  ':tag1' => 'tag-one',
271  ':tag10' => 'tag-two',
272  ':tag100' => 'tag-three'
273  );
274  $className = self::buildAccessibleProxy('TYPO3\\CMS\\Core\\Database\\PreparedStatement');
275  $query = $sql;
276  $precompiledQueryParts = array();
277  $statement = new $className($sql, 'cache');
278  $statement->bindValues($parameterValues);
279  $parameters = $statement->_get('parameters');
280  $statement->_callRef('convertNamedPlaceholdersToQuestionMarks', $query, $parameters, $precompiledQueryParts);
281  $expectedQuery = 'SELECT * FROM cache WHERE tag = ? OR tag = ? OR tag = ?';
282  $expectedParameterValues = array(
283  0 => array(
284  'type' => \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_STR,
285  'value' => 'tag-one',
286  ),
287  1 => array(
288  'type' => \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_STR,
289  'value' => 'tag-two',
290  ),
291  2 => array(
292  'type' => \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_STR,
293  'value' => 'tag-three',
294  ),
295  );
296  $this->assertEquals($expectedQuery, $query);
297  $this->assertEquals($expectedParameterValues, $parameters);
298  }
299 
300 }
$sql
Definition: server.php:82
static writeFile($file, $content, $changePermissions=FALSE)
$parameters
Definition: FileDumpEID.php:15
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
static tempnam($filePrefix, $fileSuffix='')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]