|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2009 Xavier Perseguers <typo3@perseguers.ch> 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * 00017 * This script is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU General Public License for more details. 00021 * 00022 * This copyright notice MUST APPEAR in all copies of the script! 00023 ***************************************************************/ 00024 00025 00026 require_once('BaseTestCase.php'); 00027 00028 /** 00029 * Testcase for class ux_t3lib_db. 00030 * 00031 * $Id$ 00032 * 00033 * @author Xavier Perseguers <typo3@perseguers.ch> 00034 * 00035 * @package TYPO3 00036 * @subpackage dbal 00037 */ 00038 class dbGeneralTest extends BaseTestCase { 00039 00040 /** 00041 * @var t3lib_db 00042 */ 00043 protected $db; 00044 00045 /** 00046 * @var array 00047 */ 00048 protected $loadedExtensions; 00049 00050 /** 00051 * @var array 00052 */ 00053 protected $temporaryFiles; 00054 00055 /** 00056 * Prepares the environment before running a test. 00057 */ 00058 public function setUp() { 00059 // Backup list of loaded extensions 00060 $this->loadedExtensions = $GLOBALS['TYPO3_LOADED_EXT']; 00061 // Backup database connection 00062 $this->db = $GLOBALS['TYPO3_DB']; 00063 $this->temporaryFiles = array(); 00064 00065 $className = self::buildAccessibleProxy('ux_t3lib_db'); 00066 $GLOBALS['TYPO3_DB'] = new $className; 00067 $GLOBALS['TYPO3_DB']->lastHandlerKey = '_DEFAULT'; 00068 } 00069 00070 /** 00071 * Cleans up the environment after running a test. 00072 */ 00073 public function tearDown() { 00074 // Clear DBAL-generated cache files 00075 $GLOBALS['TYPO3_DB']->clearCachedFieldInfo(); 00076 // Delete temporary files 00077 foreach ($this->temporaryFiles as $filename) unlink($filename); 00078 // Restore DB connection 00079 $GLOBALS['TYPO3_DB'] = $this->db; 00080 // Restore list of loaded extensions 00081 $GLOBALS['TYPO3_LOADED_EXT'] = $this->loadedExtensions; 00082 } 00083 00084 /** 00085 * Cleans a SQL query. 00086 * 00087 * @param mixed $sql 00088 * @return mixed (string or array) 00089 */ 00090 private function cleanSql($sql) { 00091 if (!is_string($sql)) { 00092 return $sql; 00093 } 00094 00095 $sql = str_replace("\n", ' ', $sql); 00096 $sql = preg_replace('/\s+/', ' ', $sql); 00097 return trim($sql); 00098 } 00099 00100 /** 00101 * Creates a fake extension with a given table definition. 00102 * 00103 * @param string $tableDefinition SQL script to create the extension's tables 00104 * @return void 00105 */ 00106 protected function createFakeExtension($tableDefinition) { 00107 // Prepare a fake extension configuration 00108 $ext_tables = t3lib_div::tempnam('ext_tables'); 00109 t3lib_div::writeFile($ext_tables, $tableDefinition); 00110 $this->temporaryFiles[] = $ext_tables; 00111 00112 $GLOBALS['TYPO3_LOADED_EXT']['test_dbal'] = array( 00113 'ext_tables.sql' => $ext_tables 00114 ); 00115 00116 // Append our test table to the list of existing tables 00117 $GLOBALS['TYPO3_DB']->clearCachedFieldInfo(); 00118 $GLOBALS['TYPO3_DB']->_call('initInternalVariables'); 00119 } 00120 00121 /** 00122 * @test 00123 * @see http://bugs.typo3.org/view.php?id=12515 00124 */ 00125 public function concatCanBeParsedAfterLikeOperator() { 00126 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery( 00127 '*', 00128 'sys_refindex, tx_dam_file_tracking', 00129 'sys_refindex.tablename = \'tx_dam_file_tracking\'' 00130 . ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path, tx_dam_file_tracking.file_name)' 00131 )); 00132 $expected = 'SELECT * FROM sys_refindex, tx_dam_file_tracking WHERE sys_refindex.tablename = \'tx_dam_file_tracking\''; 00133 $expected .= ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path, tx_dam_file_tracking.file_name)'; 00134 $this->assertEquals($expected, $query); 00135 } 00136 00137 /** 00138 * @test 00139 * @see http://bugs.typo3.org/view.php?id=10965 00140 */ 00141 public function floatNumberCanBeStoredInDatabase() { 00142 $this->createFakeExtension(' 00143 CREATE TABLE tx_test_dbal ( 00144 foo double default \'0\', 00145 foobar integer default \'0\' 00146 ); 00147 '); 00148 $data = array( 00149 'foo' => 99.12, 00150 'foobar' => -120, 00151 ); 00152 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTquery('tx_test_dbal', $data)); 00153 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'99.12\', \'-120\' )'; 00154 $this->assertEquals($expected, $query); 00155 } 00156 00157 /** 00158 * @test 00159 * @see http://bugs.typo3.org/view.php?id=11093 00160 */ 00161 public function positive64BitIntegerIsSupported() { 00162 $this->createFakeExtension(' 00163 CREATE TABLE tx_test_dbal ( 00164 foo int default \'0\', 00165 foobar bigint default \'0\' 00166 ); 00167 '); 00168 $data = array( 00169 'foo' => 9223372036854775807, 00170 'foobar' => 9223372036854775807, 00171 ); 00172 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTquery('tx_test_dbal', $data)); 00173 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'9223372036854775807\', \'9223372036854775807\' )'; 00174 $this->assertEquals($expected, $query); 00175 } 00176 00177 /** 00178 * @test 00179 * @see http://bugs.typo3.org/view.php?id=11093 00180 */ 00181 public function negative64BitIntegerIsSupported() { 00182 $this->createFakeExtension(' 00183 CREATE TABLE tx_test_dbal ( 00184 foo int default \'0\', 00185 foobar bigint default \'0\' 00186 ); 00187 '); 00188 $data = array( 00189 'foo' => -9223372036854775808, 00190 'foobar' => -9223372036854775808, 00191 ); 00192 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTquery('tx_test_dbal', $data)); 00193 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'-9223372036854775808\', \'-9223372036854775808\' )'; 00194 $this->assertEquals($expected, $query); 00195 } 00196 00197 /** 00198 * @test 00199 * http://bugs.typo3.org/view.php?id=12858 00200 */ 00201 public function sqlForInsertWithMultipleRowsIsValid() { 00202 $fields = array('uid', 'pid', 'title', 'body'); 00203 $rows = array( 00204 array('1', '2', 'Title #1', 'Content #1'), 00205 array('3', '4', 'Title #2', 'Content #2'), 00206 array('5', '6', 'Title #3', 'Content #3'), 00207 ); 00208 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTmultipleRows('tt_content', $fields, $rows)); 00209 00210 $expected = 'INSERT INTO tt_content (uid, pid, title, body) VALUES '; 00211 $expected .= "('1', '2', 'Title #1', 'Content #1'), "; 00212 $expected .= "('3', '4', 'Title #2', 'Content #2'), "; 00213 $expected .= "('5', '6', 'Title #3', 'Content #3')"; 00214 00215 $this->assertEquals($expected, $query); 00216 } 00217 00218 /** 00219 * @test 00220 * @see http://bugs.typo3.org/view.php?id=4493 00221 */ 00222 public function minFunctionAndInOperatorCanBeParsed() { 00223 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery( 00224 '*', 00225 'pages', 00226 'MIN(uid) IN (1,2,3,4)' 00227 )); 00228 $expected = 'SELECT * FROM pages WHERE MIN(uid) IN (1,2,3,4)'; 00229 $this->assertEquals($expected, $query); 00230 } 00231 00232 /** 00233 * @test 00234 * @see http://bugs.typo3.org/view.php?id=4493 00235 */ 00236 public function maxFunctionAndInOperatorCanBeParsed() { 00237 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery( 00238 '*', 00239 'pages', 00240 'MAX(uid) IN (1,2,3,4)' 00241 )); 00242 $expected = 'SELECT * FROM pages WHERE MAX(uid) IN (1,2,3,4)'; 00243 $this->assertEquals($expected, $query); 00244 } 00245 00246 /** 00247 * @test 00248 * @see http://bugs.typo3.org/view.php?id=12535 00249 */ 00250 public function likeBinaryOperatorIsKept() { 00251 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery( 00252 '*', 00253 'tt_content', 00254 'bodytext LIKE BINARY \'test\'' 00255 )); 00256 $expected = 'SELECT * FROM tt_content WHERE bodytext LIKE BINARY \'test\''; 00257 $this->assertEquals($expected, $query); 00258 } 00259 00260 /** 00261 * @test 00262 * @see http://bugs.typo3.org/view.php?id=12535 00263 */ 00264 public function notLikeBinaryOperatorIsKept() { 00265 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery( 00266 '*', 00267 'tt_content', 00268 'bodytext NOT LIKE BINARY \'test\'' 00269 )); 00270 $expected = 'SELECT * FROM tt_content WHERE bodytext NOT LIKE BINARY \'test\''; 00271 $this->assertEquals($expected, $query); 00272 } 00273 00274 /////////////////////////////////////// 00275 // Tests concerning prepared queries 00276 /////////////////////////////////////// 00277 00278 /** 00279 * @test 00280 * @see http://bugs.typo3.org/view.php?id=15457 00281 */ 00282 public function similarNamedParametersAreProperlyReplaced() { 00283 $sql = 'SELECT * FROM cache WHERE tag = :tag1 OR tag = :tag10 OR tag = :tag100'; 00284 $parameterValues = array( 00285 ':tag1' => 'tag-one', 00286 ':tag10' => 'tag-two', 00287 ':tag100' => 'tag-three', 00288 ); 00289 00290 $className = self::buildAccessibleProxy('t3lib_db_PreparedStatement'); 00291 $query = $sql; 00292 $precompiledQueryParts = array(); 00293 $statement = new $className($sql, 'cache'); 00294 $statement->bindValues($parameterValues); 00295 $parameters = $statement->_get('parameters'); 00296 00297 $statement->_callRef('replaceValuesInQuery', $query, $precompiledQueryParts, $parameters); 00298 $expected = 'SELECT * FROM cache WHERE tag = \'tag-one\' OR tag = \'tag-two\' OR tag = \'tag-three\''; 00299 $this->assertEquals($expected, $query); 00300 } 00301 } 00302 00303 ?>
1.8.0