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