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