TYPO3 API  SVNRelease
dbPostgresqlTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010 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 PostgreSQL database handling.
00031  *
00032  * $Id$
00033  *
00034  * @author Xavier Perseguers <typo3@perseguers.ch>
00035  *
00036  * @package TYPO3
00037  * @subpackage dbal
00038  */
00039 class dbPostgresqlTest 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 PostgreSQL
00060         require('fixtures/postgresql.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 PostgreSQL connection (using 'postgres7' as 'postgres' is remapped to it in AdoDB)
00070         FakeDbConnection::connect($GLOBALS['TYPO3_DB'], 'postgres7');
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 configurationIsUsingAdodbAndDriverPostgres() {
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('postgres') !== FALSE, 'Not using postgres 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=2367
00133      */
00134     public function limitIsProperlyRemapped() {
00135         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00136             '*',
00137             'be_users',
00138             '1=1',
00139             '',
00140             '',
00141             '20'
00142         ));
00143         $expected = 'SELECT * FROM "be_users" WHERE 1 = 1 LIMIT 20';
00144         $this->assertEquals($expected, $query);
00145     }
00146 
00147     /**
00148      * @test
00149      * @see http://bugs.typo3.org/view.php?id=2367
00150      */
00151     public function limitWithSkipIsProperlyRemapped() {
00152         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00153             '*',
00154             'be_users',
00155             '1=1',
00156             '',
00157             '',
00158             '20,40'
00159         ));
00160         $expected = 'SELECT * FROM "be_users" WHERE 1 = 1 LIMIT 40 OFFSET 20';
00161         $this->assertEquals($expected, $query);
00162     }
00163 
00164     /**
00165      * @test
00166      * @see http://bugs.typo3.org/view.php?id=14985
00167      */
00168     public function findInSetIsProperlyRemapped() {
00169         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00170             '*',
00171             'fe_users',
00172             'FIND_IN_SET(10, usergroup)'
00173         ));
00174         $expected = 'SELECT * FROM "fe_users" WHERE FIND_IN_SET(10, "usergroup") != 0';
00175         $this->assertEquals($expected, $query);
00176     }
00177 
00178     /**
00179      * @test
00180      * @see http://bugs.typo3.org/view.php?id=12535
00181      */
00182     public function likeBinaryOperatorIsRemappedToLike() {
00183         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00184             '*',
00185             'tt_content',
00186             'bodytext LIKE BINARY \'test\''
00187         ));
00188         $expected = 'SELECT * FROM "tt_content" WHERE "bodytext" LIKE \'test\'';
00189         $this->assertEquals($expected, $query);
00190     }
00191 
00192     /**
00193      * @test
00194      * @see http://bugs.typo3.org/view.php?id=12535
00195      */
00196     public function notLikeBinaryOperatorIsRemappedToNotLike() {
00197         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00198             '*',
00199             'tt_content',
00200             'bodytext NOT LIKE BINARY \'test\''
00201         ));
00202         $expected = 'SELECT * FROM "tt_content" WHERE "bodytext" NOT LIKE \'test\'';
00203         $this->assertEquals($expected, $query);
00204     }
00205 
00206     /**
00207      * @test
00208      * @see http://bugs.typo3.org/view.php?id=12535
00209      */
00210     public function likeOperatorIsRemappedToIlike() {
00211         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00212             '*',
00213             'tt_content',
00214             'bodytext LIKE \'test\''
00215         ));
00216         $expected = 'SELECT * FROM "tt_content" WHERE "bodytext" ILIKE \'test\'';
00217         $this->assertEquals($expected, $query);
00218     }
00219 
00220     /**
00221      * @test
00222      * @see http://bugs.typo3.org/view.php?id=12535
00223      */
00224     public function notLikeOperatorIsRemappedToNotIlike() {
00225         $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
00226             '*',
00227             'tt_content',
00228             'bodytext NOT LIKE \'test\''
00229         ));
00230         $expected = 'SELECT * FROM "tt_content" WHERE "bodytext" NOT ILIKE \'test\'';
00231         $this->assertEquals($expected, $query);
00232     }
00233 }
00234 
00235 ?>