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