TYPO3 API  SVNRelease
dbMssqlTest.php
Go to the documentation of this file.
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 require_once('FakeDbConnection.php');
00028 
00029 /**
00030  * Testcase for class ux_t3lib_db. Testing MS SQL database handling.
00031  *
00032  * $Id$
00033  *
00034  * @author Xavier Perseguers <typo3@perseguers.ch>
00035  *
00036  * @package TYPO3
00037  * @subpackage dbal
00038  */
00039 class dbMssqlTest extends BaseTestCase {
00040 
00041     /**
00042      * @var t3lib_db
00043      */
00044     protected $db;
00045 
00046     /**
00047      * @var array
00048      */
00049     protected $dbalConfig;
00050 
00051     /**
00052      * Prepares the environment before running a test.
00053      */
00054     public function setUp() {
00055         // Backup DBAL configuration
00056         $this->dbalConfig = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal'];
00057         // Backup database connection
00058         $this->db = $GLOBALS['TYPO3_DB'];
00059         // Reconfigure DBAL to use MS SQL
00060         require('fixtures/mssql.config.php');
00061 
00062         $className = self::buildAccessibleProxy('ux_t3lib_db');
00063         $GLOBALS['TYPO3_DB'] = new $className;
00064         $parserClassName = self::buildAccessibleProxy('ux_t3lib_sqlparser');
00065         $GLOBALS['TYPO3_DB']->SQLparser = new $parserClassName;
00066 
00067         $this->assertFalse($GLOBALS['TYPO3_DB']->isConnected());
00068 
00069         // Initialize a fake MS SQL connection
00070         FakeDbConnection::connect($GLOBALS['TYPO3_DB'], 'mssql');
00071 
00072         $this->assertTrue($GLOBALS['TYPO3_DB']->isConnected());
00073     }
00074 
00075     /**
00076      * Cleans up the environment after running a test.
00077      */
00078     public function tearDown() {
00079         // Clear DBAL-generated cache files
00080         $GLOBALS['TYPO3_DB']->clearCachedFieldInfo();
00081         // Restore DBAL configuration
00082         $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal'] = $this->dbalConfig;
00083         // Restore DB connection
00084         $GLOBALS['TYPO3_DB'] = $this->db;
00085     }
00086 
00087     /**
00088      * Cleans a SQL query.
00089      *
00090      * @param mixed $sql
00091      * @return mixed (string or array)
00092      */
00093     private function cleanSql($sql) {
00094         if (!is_string($sql)) {
00095             return $sql;
00096         }
00097 
00098         $sql = str_replace("\n", ' ', $sql);
00099         $sql = preg_replace('/\s+/', ' ', $sql);
00100         return trim($sql);
00101     }
00102 
00103     /**
00104      * @test
00105      */
00106     public function configurationIsUsingAdodbAndDriverMssql() {
00107         $configuration = $GLOBALS['TYPO3_DB']->conf['handlerCfg'];
00108         $this->assertTrue(is_array($configuration) && count($configuration) > 0, 'No configuration found');
00109         $this->assertEquals('adodb', $configuration['_DEFAULT']['type']);
00110         $this->assertTrue($GLOBALS['TYPO3_DB']->runningADOdbDriver('mssql') !== FALSE, 'Not using mssql driver');
00111     }
00112 
00113     /**
00114      * @test
00115      */
00116     public function tablesWithMappingAreDetected() {
00117         $tablesWithMapping = array_keys($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['mapping']);
00118 
00119         foreach ($GLOBALS['TYPO3_DB']->cache_fieldType as $table => $fieldTypes) {
00120             $tableDef = $GLOBALS['TYPO3_DB']->_call('map_needMapping', $table);
00121 
00122             if (in_array($table, $tablesWithMapping)) {
00123                 self::assertTrue(is_array($tableDef), 'Table ' . $table . ' was expected to need mapping');
00124             } else {
00125                 self::assertFalse($tableDef, 'Table ' . $table . ' was not expected to need mapping');
00126             }
00127         }
00128     }
00129 
00130     /**
00131      * @test
00132      * @see http://bugs.typo3.org/view.php?id=14985
00133      */
00134     public function findInSetIsProperlyRemapped() {
00135         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00136             '*',
00137             'fe_users',
00138             'FIND_IN_SET(10, usergroup)'
00139         ));
00140         $expected = 'SELECT * FROM "fe_users" WHERE \',\'+"usergroup"+\',\' LIKE \'%,10,%\'';
00141         $this->assertEquals($expected, $query);
00142     }
00143 
00144     ///////////////////////////////////////
00145     // Tests concerning remapping with
00146     // external (non-TYPO3) databases
00147     ///////////////////////////////////////
00148 
00149     /**
00150      * @test
00151      * @see http://bugs.typo3.org/view.php?id=13490
00152      */
00153     public function canRemapPidToZero() {
00154         $selectFields = 'uid, FirstName, LastName';
00155         $fromTables = 'Members';
00156         $whereClause = 'pid=0 AND cruser_id=1';
00157         $groupBy = '';
00158         $orderBy = '';
00159 
00160         $remappedParameters = $GLOBALS['TYPO3_DB']->_call('map_remapSELECTQueryParts', $selectFields, $fromTables, $whereClause, $groupBy, $orderBy);
00161         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->_call('SELECTqueryFromArray', $remappedParameters));
00162 
00163         $expected = 'SELECT "MemberID", "FirstName", "LastName" FROM "Members" WHERE 0 = 0 AND 1 = 1';
00164         $this->assertEquals($expected, $query);
00165     }
00166 
00167     ///////////////////////////////////////
00168     // Tests concerning advanced operators
00169     ///////////////////////////////////////
00170 
00171     /**
00172      * @test
00173      * @see http://bugs.typo3.org/view.php?id=13134
00174      */
00175     public function locateStatementIsProperlyQuoted() {
00176         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00177             '*, CASE WHEN' .
00178                     ' LOCATE(' . $GLOBALS['TYPO3_DB']->fullQuoteStr('(fce)', 'tx_templavoila_tmplobj') . ', datastructure)>0 THEN 2' .
00179                     ' ELSE 1' .
00180                     ' END AS scope',
00181             'tx_templavoila_tmplobj',
00182             '1=1'
00183         ));
00184         $expected = 'SELECT *, CASE WHEN CHARINDEX(\'(fce)\', "datastructure") > 0 THEN 2 ELSE 1 END AS "scope" FROM "tx_templavoila_tmplobj" WHERE 1 = 1';
00185         $this->assertEquals($expected, $query);
00186     }
00187 
00188     /**
00189      * @test
00190      * @see http://bugs.typo3.org/view.php?id=13134
00191      */
00192     public function locateStatementWithPositionIsProperlyQuoted() {
00193         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00194             '*, CASE WHEN' .
00195                     ' LOCATE(' . $GLOBALS['TYPO3_DB']->fullQuoteStr('(fce)', 'tx_templavoila_tmplobj') . ', datastructure, 4)>0 THEN 2' .
00196                     ' ELSE 1' .
00197                     ' END AS scope',
00198             'tx_templavoila_tmplobj',
00199             '1=1'
00200         ));
00201         $expected = 'SELECT *, CASE WHEN CHARINDEX(\'(fce)\', "datastructure", 4) > 0 THEN 2 ELSE 1 END AS "scope" FROM "tx_templavoila_tmplobj" WHERE 1 = 1';
00202         $this->assertEquals($expected, $query);
00203     }
00204 
00205     /**
00206      * @test
00207      * @see http://bugs.typo3.org/view.php?id=13134
00208      */
00209     public function locateStatementIsProperlyRemapped() {
00210         $selectFields = '*, CASE WHEN' .
00211                 ' LOCATE(' . $GLOBALS['TYPO3_DB']->fullQuoteStr('(fce)', 'tx_templavoila_tmplobj') . ', datastructure, 4)>0 THEN 2' .
00212                 ' ELSE 1' .
00213                 ' END AS scope';
00214         $fromTables = 'tx_templavoila_tmplobj';
00215         $whereClause = '1=1';
00216         $groupBy = '';
00217         $orderBy = '';
00218 
00219         $remappedParameters = $GLOBALS['TYPO3_DB']->_call('map_remapSELECTQueryParts', $selectFields, $fromTables, $whereClause, $groupBy, $orderBy);
00220         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->_call('SELECTqueryFromArray', $remappedParameters));
00221 
00222         $expected = 'SELECT *, CASE WHEN CHARINDEX(\'(fce)\', "ds", 4) > 0 THEN 2 ELSE 1 END AS "scope" FROM "tx_templavoila_tmplobj" WHERE 1 = 1';
00223         $this->assertEquals($expected, $query);
00224     }
00225 
00226     /**
00227      * @test
00228      * @see http://bugs.typo3.org/view.php?id=13134
00229      */
00230     public function locateStatementWithExternalTableIsProperlyRemapped() {
00231         $selectFields = '*, CASE WHEN' .
00232                 ' LOCATE(' . $GLOBALS['TYPO3_DB']->fullQuoteStr('(fce)', 'tx_templavoila_tmplobj') . ', tx_templavoila_tmplobj.datastructure, 4)>0 THEN 2' .
00233                 ' ELSE 1' .
00234                 ' END AS scope';
00235         $fromTables = 'tx_templavoila_tmplobj';
00236         $whereClause = '1=1';
00237         $groupBy = '';
00238         $orderBy = '';
00239 
00240         $remappedParameters = $GLOBALS['TYPO3_DB']->_call('map_remapSELECTQueryParts', $selectFields, $fromTables, $whereClause, $groupBy, $orderBy);
00241         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->_call('SELECTqueryFromArray', $remappedParameters));
00242 
00243         $expected = 'SELECT *, CASE WHEN CHARINDEX(\'(fce)\', "tx_templavoila_tmplobj"."ds", 4) > 0 THEN 2 ELSE 1 END AS "scope" FROM "tx_templavoila_tmplobj" WHERE 1 = 1';
00244         $this->assertEquals($expected, $query);
00245     }
00246 
00247     /**
00248      * @test
00249      * @see http://bugs.typo3.org/view.php?id=6196
00250      */
00251     public function IfNullIsProperlyRemapped() {
00252         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00253             '*',
00254             'tt_news_cat_mm',
00255             'IFNULL(tt_news_cat_mm.uid_foreign,0) IN (21,22)'
00256         ));
00257         $expected = 'SELECT * FROM "tt_news_cat_mm" WHERE ISNULL("tt_news_cat_mm"."uid_foreign", 0) IN (21,22)';
00258         $this->assertEquals($expected, $query);
00259     }
00260 }
00261 
00262 ?>