[BUGFIX] Fatal error thrown in install tool
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / tests / DatabasePostgreesqlTest.php
1 <?php
2 namespace TYPO3\CMS\Dbal\Tests;
3
4 /**
5 * Testcase for class ux_t3lib_db. Testing PostgreSQL database handling.
6 *
7 * @author Xavier Perseguers <xavier@typo3.org>
8 * @package TYPO3
9 * @subpackage dbal
10 */
11 class DatabasePostgreesqlTest extends \TYPO3\CMS\Dbal\Tests\BaseTestCase {
12
13 /**
14 * @var t3lib_db
15 */
16 protected $db;
17
18 /**
19 * @var array
20 */
21 protected $dbalConfig;
22
23 /**
24 * Prepares the environment before running a test.
25 */
26 public function setUp() {
27 // Backup DBAL configuration
28 $this->dbalConfig = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal'];
29 // Backup database connection
30 $this->db = $GLOBALS['TYPO3_DB'];
31 // Reconfigure DBAL to use PostgreSQL
32 require 'fixtures/postgresql.config.php';
33 $className = self::buildAccessibleProxy('ux_t3lib_db');
34 $GLOBALS['TYPO3_DB'] = new $className();
35 $parserClassName = self::buildAccessibleProxy('ux_TYPO3\\CMS\\Core\\Database\\SqlParser');
36 $GLOBALS['TYPO3_DB']->SQLparser = new $parserClassName();
37 $this->assertFalse($GLOBALS['TYPO3_DB']->isConnected());
38 // Initialize a fake PostgreSQL connection (using 'postgres7' as 'postgres' is remapped to it in AdoDB)
39 \TYPO3\CMS\Dbal\Tests\FakeDatabaseConnection::connect($GLOBALS['TYPO3_DB'], 'postgres7');
40 $this->assertTrue($GLOBALS['TYPO3_DB']->isConnected());
41 }
42
43 /**
44 * Cleans up the environment after running a test.
45 */
46 public function tearDown() {
47 // Clear DBAL-generated cache files
48 $GLOBALS['TYPO3_DB']->clearCachedFieldInfo();
49 // Restore DBAL configuration
50 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal'] = $this->dbalConfig;
51 // Restore DB connection
52 $GLOBALS['TYPO3_DB'] = $this->db;
53 }
54
55 /**
56 * Cleans a SQL query.
57 *
58 * @param mixed $sql
59 * @return mixed (string or array)
60 */
61 private function cleanSql($sql) {
62 if (!is_string($sql)) {
63 return $sql;
64 }
65 $sql = str_replace('
66 ', ' ', $sql);
67 $sql = preg_replace('/\\s+/', ' ', $sql);
68 return trim($sql);
69 }
70
71 /**
72 * @test
73 */
74 public function configurationIsUsingAdodbAndDriverPostgres() {
75 $configuration = $GLOBALS['TYPO3_DB']->conf['handlerCfg'];
76 $this->assertTrue(is_array($configuration) && count($configuration) > 0, 'No configuration found');
77 $this->assertEquals('adodb', $configuration['_DEFAULT']['type']);
78 $this->assertTrue($GLOBALS['TYPO3_DB']->runningADOdbDriver('postgres') !== FALSE, 'Not using postgres driver');
79 }
80
81 /**
82 * @test
83 */
84 public function tablesWithMappingAreDetected() {
85 $tablesWithMapping = array_keys($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['mapping']);
86 foreach ($GLOBALS['TYPO3_DB']->cache_fieldType as $table => $fieldTypes) {
87 $tableDef = $GLOBALS['TYPO3_DB']->_call('map_needMapping', $table);
88 if (in_array($table, $tablesWithMapping)) {
89 self::assertTrue(is_array($tableDef), ('Table ' . $table) . ' was expected to need mapping');
90 } else {
91 self::assertFalse($tableDef, ('Table ' . $table) . ' was not expected to need mapping');
92 }
93 }
94 }
95
96 /**
97 * @test
98 * @see http://bugs.typo3.org/view.php?id=2367
99 */
100 public function limitIsProperlyRemapped() {
101 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'be_users', '1=1', '', '', '20'));
102 $expected = 'SELECT * FROM "be_users" WHERE 1 = 1 LIMIT 20';
103 $this->assertEquals($expected, $query);
104 }
105
106 /**
107 * @test
108 * @see http://bugs.typo3.org/view.php?id=2367
109 */
110 public function limitWithSkipIsProperlyRemapped() {
111 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'be_users', '1=1', '', '', '20,40'));
112 $expected = 'SELECT * FROM "be_users" WHERE 1 = 1 LIMIT 40 OFFSET 20';
113 $this->assertEquals($expected, $query);
114 }
115
116 /**
117 * @test
118 * @see http://bugs.typo3.org/view.php?id=14985
119 */
120 public function findInSetIsProperlyRemapped() {
121 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'fe_users', 'FIND_IN_SET(10, usergroup)'));
122 $expected = 'SELECT * FROM "fe_users" WHERE FIND_IN_SET(10, "usergroup") != 0';
123 $this->assertEquals($expected, $query);
124 }
125
126 /**
127 * @test
128 * @see http://bugs.typo3.org/view.php?id=12535
129 */
130 public function likeBinaryOperatorIsRemappedToLike() {
131 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'tt_content', 'bodytext LIKE BINARY \'test\''));
132 $expected = 'SELECT * FROM "tt_content" WHERE "bodytext" LIKE \'test\'';
133 $this->assertEquals($expected, $query);
134 }
135
136 /**
137 * @test
138 * @see http://bugs.typo3.org/view.php?id=12535
139 */
140 public function notLikeBinaryOperatorIsRemappedToNotLike() {
141 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'tt_content', 'bodytext NOT LIKE BINARY \'test\''));
142 $expected = 'SELECT * FROM "tt_content" WHERE "bodytext" NOT LIKE \'test\'';
143 $this->assertEquals($expected, $query);
144 }
145
146 /**
147 * @test
148 * @see http://bugs.typo3.org/view.php?id=12535
149 */
150 public function likeOperatorIsRemappedToIlike() {
151 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'tt_content', 'bodytext LIKE \'test\''));
152 $expected = 'SELECT * FROM "tt_content" WHERE "bodytext" ILIKE \'test\'';
153 $this->assertEquals($expected, $query);
154 }
155
156 /**
157 * @test
158 * @see http://bugs.typo3.org/view.php?id=12535
159 */
160 public function notLikeOperatorIsRemappedToNotIlike() {
161 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'tt_content', 'bodytext NOT LIKE \'test\''));
162 $expected = 'SELECT * FROM "tt_content" WHERE "bodytext" NOT ILIKE \'test\'';
163 $this->assertEquals($expected, $query);
164 }
165
166 /**
167 * @test
168 * @see http://forge.typo3.org/issues/32626
169 */
170 public function notEqualAnsiOperatorCanBeParsed() {
171 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'pages', 'pid<>3'));
172 $expected = 'SELECT * FROM "pages" WHERE "pid" <> 3';
173 $this->assertEquals($expected, $query);
174 }
175
176 }
177
178
179 ?>