Raised DBAL version from 1.1.7 to 1.1.8
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / tests / dbPostgresqlTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Xavier Perseguers <typo3@perseguers.ch>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 require_once('BaseTestCase.php');
27 require_once('FakeDbConnection.php');
28
29 /**
30 * Testcase for class ux_t3lib_db. Testing PostgreSQL database handling.
31 *
32 * $Id$
33 *
34 * @author Xavier Perseguers <typo3@perseguers.ch>
35 *
36 * @package TYPO3
37 * @subpackage dbal
38 */
39 class dbPostgresqlTest extends BaseTestCase {
40
41 /**
42 * @var t3lib_db
43 */
44 protected $db;
45
46 /**
47 * @var array
48 */
49 protected $dbalConfig;
50
51 /**
52 * Prepares the environment before running a test.
53 */
54 public function setUp() {
55 // Backup DBAL configuration
56 $this->dbalConfig = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal'];
57 // Backup database connection
58 $this->db = $GLOBALS['TYPO3_DB'];
59 // Reconfigure DBAL to use PostgreSQL
60 require('fixtures/postgresql.config.php');
61
62 $className = self::buildAccessibleProxy('ux_t3lib_db');
63 $GLOBALS['TYPO3_DB'] = new $className;
64 $parserClassName = self::buildAccessibleProxy('ux_t3lib_sqlparser');
65 $GLOBALS['TYPO3_DB']->SQLparser = new $parserClassName;
66
67 $this->assertFalse($GLOBALS['TYPO3_DB']->isConnected());
68
69 // Initialize a fake PostgreSQL connection (using 'postgres7' as 'postgres' is remapped to it in AdoDB)
70 FakeDbConnection::connect($GLOBALS['TYPO3_DB'], 'postgres7');
71
72 $this->assertTrue($GLOBALS['TYPO3_DB']->isConnected());
73 }
74
75 /**
76 * Cleans up the environment after running a test.
77 */
78 public function tearDown() {
79 // Clear DBAL-generated cache files
80 $GLOBALS['TYPO3_DB']->clearCachedFieldInfo();
81 // Restore DBAL configuration
82 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal'] = $this->dbalConfig;
83 // Restore DB connection
84 $GLOBALS['TYPO3_DB'] = $this->db;
85 }
86
87 /**
88 * Cleans a SQL query.
89 *
90 * @param mixed $sql
91 * @return mixed (string or array)
92 */
93 private function cleanSql($sql) {
94 if (!is_string($sql)) {
95 return $sql;
96 }
97
98 $sql = str_replace("\n", ' ', $sql);
99 $sql = preg_replace('/\s+/', ' ', $sql);
100 return trim($sql);
101 }
102
103 /**
104 * @test
105 */
106 public function configurationIsUsingAdodbAndDriverPostgres() {
107 $configuration = $GLOBALS['TYPO3_DB']->conf['handlerCfg'];
108 $this->assertTrue(is_array($configuration) && count($configuration) > 0, 'No configuration found');
109 $this->assertEquals('adodb', $configuration['_DEFAULT']['type']);
110 $this->assertTrue($GLOBALS['TYPO3_DB']->runningADOdbDriver('postgres') !== FALSE, 'Not using postgres driver');
111 }
112
113 /**
114 * @test
115 */
116 public function tablesWithMappingAreDetected() {
117 $tablesWithMapping = array_keys($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['mapping']);
118
119 foreach ($GLOBALS['TYPO3_DB']->cache_fieldType as $table => $fieldTypes) {
120 $tableDef = $GLOBALS['TYPO3_DB']->_call('map_needMapping', $table);
121
122 if (in_array($table, $tablesWithMapping)) {
123 self::assertTrue(is_array($tableDef), 'Table ' . $table . ' was expected to need mapping');
124 } else {
125 self::assertFalse($tableDef, 'Table ' . $table . ' was not expected to need mapping');
126 }
127 }
128 }
129
130 /**
131 * @test
132 * @see http://bugs.typo3.org/view.php?id=2367
133 */
134 public function limitIsProperlyRemapped() {
135 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
136 '*',
137 'be_users',
138 '1=1',
139 '',
140 '',
141 '20'
142 ));
143 $expected = 'SELECT * FROM "be_users" WHERE 1 = 1 LIMIT 20';
144 $this->assertEquals($expected, $query);
145 }
146
147 /**
148 * @test
149 * @see http://bugs.typo3.org/view.php?id=2367
150 */
151 public function limitWithSkipIsProperlyRemapped() {
152 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery(
153 '*',
154 'be_users',
155 '1=1',
156 '',
157 '',
158 '20,40'
159 ));
160 $expected = 'SELECT * FROM "be_users" WHERE 1 = 1 LIMIT 40 OFFSET 20';
161 $this->assertEquals($expected, $query);
162 }
163 }
164 ?>