ca4eb9ef36e99bd28828be3382d7c51558385f46
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / Service / SqlSchemaMigrationServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Install\Tests\Unit\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Install\Service\SqlSchemaMigrationService;
18
19 /**
20 * Test case
21 *
22 * @author Mario Rimann <mario.rimann@typo3.org>
23 */
24 class SqlSchemaMigrationServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
25
26 /**
27 * @test
28 */
29 public function getFieldDefinitionsFileContentHandlesMultipleWhitespacesInFieldDefinitions() {
30 $subject = new SqlSchemaMigrationService();
31 // Multiple whitespaces and tabs in field definition
32 $inputString = 'CREATE table atable (' . LF . 'aFieldName int(11)' . TAB . TAB . TAB . 'unsigned DEFAULT \'0\'' . LF . ');';
33 $result = $subject->getFieldDefinitions_fileContent($inputString);
34
35 $this->assertEquals(
36 array(
37 'atable' => array(
38 'fields' => array(
39 'aFieldName' => 'int(11) unsigned default \'0\'',
40 ),
41 'extra' => array(
42 'COLLATE' => '',
43 ),
44 ),
45 ),
46 $result
47 );
48 }
49
50 /**
51 * @test
52 */
53 public function getDatabaseExtraFindsChangedFields() {
54 $subject = new SqlSchemaMigrationService();
55 $differenceArray = $subject->getDatabaseExtra(
56 array(
57 'tx_foo' => array(
58 'fields' => array(
59 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
60 )
61 )
62 ),
63 array(
64 'tx_foo' => array(
65 'fields' => array(
66 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
67 )
68 )
69 )
70 );
71
72 $this->assertEquals(
73 $differenceArray,
74 array(
75 'extra' => array(),
76 'diff' => array(
77 'tx_foo' => array(
78 'fields' => array(
79 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
80 )
81 )
82 ),
83 'diff_currentValues' => array(
84 'tx_foo' => array(
85 'fields' => array(
86 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
87 )
88 )
89 )
90 )
91 );
92 }
93
94 /**
95 * @test
96 */
97 public function getDatabaseExtraFindsChangedFieldsIgnoreNotNull() {
98 $subject = new SqlSchemaMigrationService();
99 $differenceArray = $subject->getDatabaseExtra(
100 array(
101 'tx_foo' => array(
102 'fields' => array(
103 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
104 )
105 )
106 ),
107 array(
108 'tx_foo' => array(
109 'fields' => array(
110 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
111 )
112 )
113 ),
114 '',
115 TRUE
116 );
117
118 $this->assertEquals(
119 $differenceArray,
120 array(
121 'extra' => array(),
122 'diff' => array(
123 'tx_foo' => array(
124 'fields' => array(
125 'foo' => 'varchar(999) DEFAULT \'0\''
126 )
127 )
128 ),
129 'diff_currentValues' => array(
130 'tx_foo' => array(
131 'fields' => array(
132 'foo' => 'varchar(255) DEFAULT \'0\''
133 )
134 )
135 )
136 )
137 );
138 }
139
140 /**
141 * @test
142 */
143 public function getDatabaseExtraIgnoresCaseDifference() {
144 $subject = new SqlSchemaMigrationService();
145 $differenceArray = $subject->getDatabaseExtra(
146 array(
147 'tx_foo' => array(
148 'fields' => array(
149 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
150 )
151 )
152 ),
153 array(
154 'tx_foo' => array(
155 'fields' => array(
156 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
157 )
158 )
159 )
160 );
161
162
163 $this->assertEquals(
164 $differenceArray,
165 array(
166 'extra' => array(),
167 'diff' => array(),
168 'diff_currentValues' => NULL,
169 )
170 );
171 }
172
173 /**
174 * @test
175 */
176 public function getDatabaseExtraDoesNotLowercaseReservedWordsForTheComparison() {
177 $subject = new SqlSchemaMigrationService();
178 $differenceArray = $subject->getDatabaseExtra(
179 array(
180 'tx_foo' => array(
181 'fields' => array(
182 'PRIMARY KEY (md5hash)',
183 )
184 )
185 ),
186 array(
187 'tx_foo' => array(
188 'fields' => array(
189 'PRIMARY KEY (md5hash)'),
190 )
191 )
192 );
193
194
195 $this->assertEquals(
196 $differenceArray,
197 array(
198 'extra' => array(),
199 'diff' => array(),
200 'diff_currentValues' => NULL,
201 )
202 );
203 }
204
205 /**
206 * @test
207 */
208 public function checkColumnDefinitionIfCommentIsSupplied() {
209 $subject = new SqlSchemaMigrationService();
210 $fieldDefinition = $subject->assembleFieldDefinition(
211 array(
212 'Field' => 'uid',
213 'Type' => 'int(11)',
214 'Null' => 'NO',
215 'Key' => 'PRI',
216 'Default' => NULL,
217 'Extra' => 'auto_increment',
218 'Comment' => 'I am a comment',
219 )
220 );
221
222
223 $this->assertSame(
224 'int(11) NOT NULL auto_increment COMMENT \'I am a comment\'',
225 $fieldDefinition
226 );
227 }
228
229 /**
230 * @test
231 */
232 public function checkColumnDefinitionIfNoCommentIsSupplied() {
233 $subject = new SqlSchemaMigrationService();
234 $fieldDefinition = $subject->assembleFieldDefinition(
235 array(
236 'Field' => 'uid',
237 'Type' => 'int(11)',
238 'Null' => 'NO',
239 'Key' => 'PRI',
240 'Default' => NULL,
241 'Extra' => 'auto_increment',
242 )
243 );
244
245
246 $this->assertSame(
247 'int(11) NOT NULL auto_increment',
248 $fieldDefinition
249 );
250 }
251
252 }