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