33317c69f3f7ec0370795927a12ee621588d8019
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / Controller / Action / Ajax / ExtensionCompatibilityTesterTest.php
1 <?php
2 namespace TYPO3\CMS\Install\Tests\Unit\Controller\Action\Ajax;
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\Core\Utility;
18
19 /**
20 * Test case
21 */
22 class ExtensionCompatibilityTesterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 public function setUp() {
25 $this->markTestIncomplete('FIXME: rework to match package management change');
26 }
27 /**
28 * @var \TYPO3\CMS\Core\Package\UnitTestPackageManager A backup of unit test package manager
29 */
30 protected $backupPackageManager = NULL;
31
32 /**
33 * Set up
34 *
35 * @return void
36 */
37 public function setUp() {
38 // Package manager is mocked in some tests. Backup the original one here to re-inject it to
39 // ExtensionManagementUtility in tearDown() again. makeInstance() is allowed to be used here
40 // since the PackageManager is registered as singleton by bootstrap.
41 $this->backupPackageManager = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Package\\PackageManager');
42 }
43
44 /**
45 * Tear down
46 *
47 * @return void
48 */
49 public function tearDown() {
50 Utility\ExtensionManagementUtility::setPackageManager($this->backupPackageManager);
51 if (file_exists(PATH_site . 'typo3temp/ExtensionCompatibilityTester.txt')) {
52 unlink(PATH_site . 'typo3temp/ExtensionCompatibilityTester.txt');
53 }
54 parent::tearDown();
55 }
56
57 /**
58 * @test
59 */
60 public function executeActionReturnsStringOkIfAllIsWell() {
61 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('getExtensionsToLoad', 'tryToLoadExtLocalconfAndExtTablesOfExtensions', 'deleteProtocolFile'), array());
62 $extensionCompatibilityTesterMock->expects($this->once())->method('getExtensionsToLoad')->will($this->returnValue(array()));
63 $result = $extensionCompatibilityTesterMock->_call('executeAction');
64 $this->assertEquals('OK', $result);
65 }
66
67 /**
68 * @test
69 */
70 public function executeActionCallsGetExtensionsToLoad() {
71 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('getExtensionsToLoad', 'deleteProtocolFile', 'tryToLoadExtLocalconfAndExtTablesOfExtensions'), array());
72 $extensionCompatibilityTesterMock->expects($this->once())->method('getExtensionsToLoad')->will($this->returnValue(array()));
73 $extensionCompatibilityTesterMock->expects($this->once())->method('getExtensionsToLoad');
74 $extensionCompatibilityTesterMock->_call('executeAction');
75 }
76
77 /**
78 * @test
79 */
80 public function executeActionCallsLoadExtensions() {
81 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('tryToLoadExtLocalconfAndExtTablesOfExtensions', 'getExtensionsToLoad', 'deleteProtocolFile'), array());
82 $extensionCompatibilityTesterMock->expects($this->once())->method('getExtensionsToLoad')->will($this->returnValue(array()));
83 $extensionCompatibilityTesterMock->expects($this->once())->method('tryToLoadExtLocalconfAndExtTablesOfExtensions');
84 $extensionCompatibilityTesterMock->_call('executeAction');
85 }
86
87 /**
88 * @test
89 */
90 public function executeActionCallsDeleteProtocolFileIfForceCheckIsSet() {
91 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('tryToLoadExtLocalconfAndExtTablesOfExtensions', 'getExtensionsToLoad', 'deleteProtocolFile'), array());
92 $extensionCompatibilityTesterMock->expects($this->once())->method('getExtensionsToLoad')->will($this->returnValue(array()));
93 $_GET['install']['extensionCompatibilityTester']['forceCheck'] = 1;
94 $extensionCompatibilityTesterMock->expects($this->once())->method('deleteProtocolFile');
95 $extensionCompatibilityTesterMock->_call('executeAction');
96 unset($_GET['install']['extensionCompatibilityTester']['forceCheck']);
97 }
98
99 /**
100 * @test
101 */
102 public function deleteProtocolFileDeletesFile() {
103 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('dummy'), array());
104 Utility\GeneralUtility::writeFile(PATH_site . 'typo3temp/ExtensionCompatibilityTester.txt', 'foobar');
105 $extensionCompatibilityTesterMock->_call('deleteProtocolFile');
106 $this->assertFalse(file_exists(PATH_site . 'typo3temp/ExtensionCompatibilityTester.txt'));
107 }
108
109 /**
110 * @test
111 */
112 public function getLoadedExtensionsReturnsArray() {
113 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('dummy'), array());
114 $result = $extensionCompatibilityTesterMock->_call('getExtensionsToLoad');
115 $this->assertInternalType('array', $result);
116 }
117
118 /**
119 * @test
120 */
121 public function loadExtTablesForExtensionIncludesExtTablesPhp() {
122 $extension = array(
123 'demo1' => array(
124 'type' => 'L',
125 'ext_tables.php' => PATH_typo3 . 'sysext/install/Tests/Unit/Controller/Action/Ajax/Fixtures/demo1/ext_tables.php'
126 )
127 );
128 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('dummy'), array());
129 $extensionCompatibilityTesterMock->_call('loadExtTablesForExtension', 'demo1', $extension['demo1']);
130 $this->assertArrayHasKey('demo1_executed', $GLOBALS);
131 $this->assertEquals('foobar', $GLOBALS['demo1_executed']);
132 unset($GLOBALS['demo1_executed']);
133 }
134
135 /**
136 * @test
137 */
138 public function tryToLoadExtLocalconfAndExtTablesOfExtensionsCallsLoadExtTablesForExtension() {
139 // tryToLoadExtLocalconfAndExtTablesOfExtensions() triggers a call to ExtensionManagementUtility::loadBaseTca
140 // that works on our UnitTestPackageManager to do things. This package manager needs to be mocked here to
141 // not trigger unwanted side effects.
142 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array(), array(), '', FALSE);
143 $packageManager->expects($this->any())->method('getActivePackages')->will($this->returnValue(array()));
144 Utility\ExtensionManagementUtility::setPackageManager($packageManager);
145
146 $extension = array(
147 'demo1' => array(
148 'type' => 'L',
149 'ext_tables.php' => PATH_typo3 . 'sysext/install/Tests/Unit/Controller/Action/Ajax/Fixtures/demo1/ext_tables.php'
150 )
151 );
152 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('loadExtLocalconfForExtension', 'writeCurrentExtensionToFile', 'loadExtTablesForExtension', 'removeCurrentExtensionFromFile'), array());
153 $extensionCompatibilityTesterMock->expects($this->atLeastOnce())->method('loadExtTablesForExtension');
154 $extensionCompatibilityTesterMock->_call('tryToLoadExtLocalconfAndExtTablesOfExtensions', $extension);
155 }
156
157 /**
158 * @test
159 */
160 public function tryToLoadExtLocalconfAndExtTablesOfExtensionsCallsLoadExtLocalconfForExtension() {
161 // tryToLoadExtLocalconfAndExtTablesOfExtensions() triggers a call to ExtensionManagementUtility::loadBaseTca
162 // that works on our UnitTestPackageManager to do things. This package manager needs to be mocked here to
163 // not trigger unwanted side effects.
164 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array(), array(), '', FALSE);
165 $packageManager->expects($this->any())->method('getActivePackages')->will($this->returnValue(array()));
166 Utility\ExtensionManagementUtility::setPackageManager($packageManager);
167
168 $extension = array(
169 'demo1' => array(
170 'type' => 'L',
171 'ext_localconf.php' => PATH_typo3 . 'sysext/install/Tests/Unit/Controller/Action/Ajax/Fixtures/demo1/ext_localconf.php'
172 )
173 );
174 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('loadExtLocalconfForExtension', 'writeCurrentExtensionToFile', 'loadExtTablesForExtension', 'removeCurrentExtensionFromFile'), array());
175 $extensionCompatibilityTesterMock->expects($this->atLeastOnce())->method('loadExtLocalconfForExtension');
176 $extensionCompatibilityTesterMock->_call('tryToLoadExtLocalconfAndExtTablesOfExtensions', $extension);
177 }
178
179 /**
180 * @test
181 */
182 public function loadExtLocalconfForExtensionIncludesExtLocalconfPhp() {
183 $extension = array(
184 'demo1' => array(
185 'type' => 'L',
186 'ext_localconf.php' => PATH_typo3 . 'sysext/install/Tests/Unit/Controller/Action/Ajax/Fixtures/demo1/ext_localconf.php'
187 )
188 );
189 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('dummy'), array());
190 $extensionCompatibilityTesterMock->_call('loadExtLocalconfForExtension', 'demo1', $extension['demo1']);
191 $this->assertArrayHasKey('demo1_executed', $GLOBALS);
192 $this->assertEquals('foobaz', $GLOBALS['demo1_executed']);
193 unset($GLOBALS['demo1_executed']);
194 }
195
196 /**
197 * @test
198 */
199 public function tryToLoadExtLocalconfAndExtTablesOfExtensionsCallsWriteCurrentExtensionToFile() {
200 // tryToLoadExtLocalconfAndExtTablesOfExtensions() triggers a call to ExtensionManagementUtility::loadBaseTca
201 // that works on our UnitTestPackageManager to do things. This package manager needs to be mocked here to
202 // not trigger unwanted side effects.
203 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array(), array(), '', FALSE);
204 $packageManager->expects($this->any())->method('getActivePackages')->will($this->returnValue(array()));
205 Utility\ExtensionManagementUtility::setPackageManager($packageManager);
206
207 $extension = array(
208 'demo1' => array(
209 'type' => 'L',
210 'ext_tables.php' => PATH_typo3 . 'sysext/install/Tests/Unit/Controller/Action/Ajax/Fixtures/demo1/ext_tables.php'
211 )
212 );
213 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('loadExtLocalconfForExtension', 'writeCurrentExtensionToFile', 'loadExtTablesForExtension', 'removeCurrentExtensionFromFile'), array());
214 $extensionCompatibilityTesterMock->expects($this->atLeastOnce())->method('writeCurrentExtensionToFile')->with('demo1');
215 $extensionCompatibilityTesterMock->_call('tryToLoadExtLocalconfAndExtTablesOfExtensions', $extension);
216 }
217
218
219 /**
220 * @test
221 */
222 public function writeCurrentExtensionToFileWritesExtensionKeyToFile() {
223 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('dummy'), array());
224 $extensionCompatibilityTesterMock->_call('writeCurrentExtensionToFile', 'demo1');
225 $fileContent = file_get_contents($extensionCompatibilityTesterMock->_get('protocolFile'));
226 $this->assertEquals('demo1', $fileContent);
227 }
228
229 /**
230 * @test
231 */
232 public function getExtensionsToLoadCallsGetExtensionsToExclude() {
233 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('getExtensionsToExclude'), array());
234 $extensionCompatibilityTesterMock
235 ->expects($this->once())
236 ->method('getExtensionsToExclude')
237 ->will($this->returnValue(array()));
238 $extensionCompatibilityTesterMock->_call('getExtensionsToLoad');
239 }
240
241 /**
242 * @test
243 */
244 public function getExtensionsToExcludeReturnsArray() {
245 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('dummy'), array());
246 $returnValue = $extensionCompatibilityTesterMock->_call('getExtensionsToExclude');
247 $this->assertInternalType('array', $returnValue);
248 }
249
250 /**
251 * removeCurrentExtensionFromFileRemovesGivenExtensionDataProvider
252 *
253 * @return array
254 */
255 public function removeCurrentExtensionFromFileRemovesGivenExtensionDataProvider() {
256 return array(
257 'first' => array(
258 'demo1',
259 'demo1, demo2, demo3',
260 'demo2, demo3'
261 ),
262 'second' => array(
263 'demo2',
264 'demo1, demo2, demo3',
265 'demo1, demo3'
266 ),
267 'third' => array(
268 'demo3',
269 'demo1, demo2, demo3',
270 'demo1, demo2'
271 )
272 );
273 }
274
275 /**
276 * @test
277 * @dataProvider removeCurrentExtensionFromFileRemovesGivenExtensionDataProvider
278 */
279 public function removeCurrentExtensionFromFileRemovesGivenExtension($extensionToRemove, $extensions, $expectedExtensions) {
280 $extensionCompatibilityTesterMock = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Controller\\Action\\Ajax\\ExtensionCompatibilityTester', array('dummy'), array());
281 Utility\GeneralUtility::writeFile($extensionCompatibilityTesterMock->_get('protocolFile'), $extensions);
282 $extensionCompatibilityTesterMock->_call('removeCurrentExtensionFromFile', $extensionToRemove);
283
284 $fileContent = file_get_contents($extensionCompatibilityTesterMock->_get('protocolFile'));
285 $this->assertEquals($expectedExtensions, $fileContent);
286 }
287 }