TYPO3 CMS  TYPO3_7-6
PackageManagerTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script belongs to the TYPO3 Flow framework. *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
20 
26 {
30  protected $packageManager;
31 
36  protected function setUp()
37  {
38  vfsStream::setup('Test');
39 
41  $mockCache = $this->getMock(PhpFrontend::class, ['has', 'set', 'getBackend'], [], '', false);
42  $mockCacheBackend = $this->getMock(SimpleFileBackend::class, ['has', 'set', 'getBackend'], [], '', false);
43  $mockCache->expects($this->any())->method('has')->will($this->returnValue(false));
44  $mockCache->expects($this->any())->method('set')->will($this->returnValue(true));
45  $mockCache->expects($this->any())->method('getBackend')->will($this->returnValue($mockCacheBackend));
46  $mockCacheBackend->expects($this->any())->method('getCacheDirectory')->will($this->returnValue('vfs://Test/Cache'));
47  $this->packageManager = $this->getAccessibleMock(PackageManager::class, ['sortAndSavePackageStates', 'sortAvailablePackagesByDependencies', 'registerTransientClassLoadingInformationForPackage']);
48 
49  mkdir('vfs://Test/Packages/Application', 0700, true);
50  mkdir('vfs://Test/Configuration');
51  file_put_contents('vfs://Test/Configuration/PackageStates.php', "<?php return array ('packages' => array(), 'version' => 4); ");
52 
53  $composerNameToPackageKeyMap = [
54  'typo3/flow' => 'TYPO3.Flow'
55  ];
56 
57  $this->packageManager->injectCoreCache($mockCache);
58  $this->inject($this->packageManager, 'composerNameToPackageKeyMap', $composerNameToPackageKeyMap);
59  $this->packageManager->_set('packagesBasePath', 'vfs://Test/Packages/');
60  $this->packageManager->_set('packageStatesPathAndFilename', 'vfs://Test/Configuration/PackageStates.php');
61  }
62 
67  protected function createPackage($packageKey)
68  {
69  $packagePath = 'vfs://Test/Packages/Application/' . $packageKey . '/';
70  mkdir($packagePath, 0770, true);
71  file_put_contents($packagePath . 'ext_emconf.php', '<?php' . LF . '$EM_CONF[$_EXTKEY] = [];');
72  file_put_contents($packagePath . 'composer.json', '{}');
73  $package = new Package($this->packageManager, $packageKey, $packagePath);
74  $this->packageManager->registerPackage($package);
75  $this->packageManager->activatePackage($packageKey);
76 
77  return $package;
78  }
79 
84  {
85  $this->createPackage('TYPO3.MyPackage');
86  $package = $this->packageManager->getPackage('TYPO3.MyPackage');
87 
88  $this->assertInstanceOf(Package::class, $package, 'The result of getPackage() was no valid package object.');
89  }
90 
96  {
97  $this->packageManager->getPackage('PrettyUnlikelyThatThisPackageExists');
98  }
99 
104  {
105  $expectedPackageKeys = [
106  $this->getUniqueId('TYPO3.CMS'),
107  $this->getUniqueId('TYPO3.CMS.Test'),
108  $this->getUniqueId('TYPO3.YetAnotherTestPackage'),
109  $this->getUniqueId('Lolli.Pop.NothingElse')
110  ];
111 
112  foreach ($expectedPackageKeys as $packageKey) {
113  $packagePath = 'vfs://Test/Packages/Application/' . $packageKey . '/';
114 
115  mkdir($packagePath, 0770, true);
116  file_put_contents($packagePath . 'composer.json', '{"name": "' . $packageKey . '", "type": "typo3-test"}');
117  }
118 
119  $packageManager = $this->getAccessibleMock(PackageManager::class, ['sortAndSavePackageStates']);
120  $packageManager->_set('packagesBasePath', 'vfs://Test/Packages/');
121  $packageManager->_set('packageStatesPathAndFilename', 'vfs://Test/Configuration/PackageStates.php');
122 
123  $packageManager->_set('packages', []);
124  $packageManager->_call('scanAvailablePackages');
125 
126  $packageStates = require('vfs://Test/Configuration/PackageStates.php');
127  $actualPackageKeys = array_keys($packageStates['packages']);
128  $this->assertEquals(sort($expectedPackageKeys), sort($actualPackageKeys));
129  }
130 
134  public function scanAvailablePackagesKeepsExistingPackageConfiguration()
135  {
136  $expectedPackageKeys = [
137  $this->getUniqueId('TYPO3.CMS'),
138  $this->getUniqueId('TYPO3.CMS.Test'),
139  $this->getUniqueId('TYPO3.YetAnotherTestPackage'),
140  $this->getUniqueId('Lolli.Pop.NothingElse')
141  ];
142 
143  foreach ($expectedPackageKeys as $packageKey) {
144  $packagePath = 'vfs://Test/Packages/Application/' . $packageKey . '/';
145 
146  mkdir($packagePath, 0770, true);
147  file_put_contents($packagePath . 'composer.json', '{"name": "' . $packageKey . '", "type": "typo3-cms-test"}');
148  file_put_contents($packagePath . 'ext_emconf.php', '<?php' . LF . '$EM_CONF[$_EXTKEY] = [];');
149  }
150 
152  $packageManager = $this->getAccessibleMock(PackageManager::class, ['dummy']);
153  $packageManager->_set('packagesBasePaths', ['local' => 'vfs://Test/Packages/Application']);
154  $packageManager->_set('packagesBasePath', 'vfs://Test/Packages/');
155  $packageManager->_set('packageStatesPathAndFilename', 'vfs://Test/Configuration/PackageStates.php');
156 
158  $dependencyResolver = $this->getMock(DependencyResolver::class);
159  $dependencyResolver
160  ->expects($this->any())
161  ->method('sortPackageStatesConfigurationByDependency')
162  ->willReturnArgument(0);
163 
164  $packageManager->injectDependencyResolver($dependencyResolver);
165 
166  $packageKey = $expectedPackageKeys[0];
167  $packageManager->_set('packageStatesConfiguration', [
168  'packages' => [
169  $packageKey => [
170  'state' => 'inactive',
171  'packagePath' => 'Application/' . $packageKey . '/'
172  ]
173  ],
174  'version' => 4
175  ]);
176  $packageManager->_call('scanAvailablePackages');
177  $packageManager->_call('sortAndSavePackageStates');
178 
179  $packageStates = require('vfs://Test/Configuration/PackageStates.php');
180  $this->assertEquals('inactive', $packageStates['packages'][$packageKey]['state']);
181  }
182 
186  public function packageStatesConfigurationContainsRelativePaths()
187  {
188  $packageKeys = [
189  $this->getUniqueId('Lolli.Pop.NothingElse'),
190  $this->getUniqueId('TYPO3.Package'),
191  $this->getUniqueId('TYPO3.YetAnotherTestPackage')
192  ];
193 
194  foreach ($packageKeys as $packageKey) {
195  $packagePath = 'vfs://Test/Packages/Application/' . $packageKey . '/';
196 
197  mkdir($packagePath, 0770, true);
198  file_put_contents($packagePath . 'composer.json', '{"name": "' . $packageKey . '", "type": "typo3-cms-test"}');
199  file_put_contents($packagePath . 'ext_emconf.php', '<?php' . LF . '$EM_CONF[$_EXTKEY] = [];');
200  }
201 
203  $packageManager = $this->getAccessibleMock(PackageManager::class, ['dummy']);
204  $packageManager->_set('packagesBasePaths', ['local' => 'vfs://Test/Packages/Application']);
205  $packageManager->_set('packagesBasePath', 'vfs://Test/Packages/');
206  $packageManager->_set('packageStatesPathAndFilename', 'vfs://Test/Configuration/PackageStates.php');
207 
209  $dependencyResolver = $this->getMock(DependencyResolver::class);
210  $dependencyResolver
211  ->expects($this->any())
212  ->method('sortPackageStatesConfigurationByDependency')
213  ->willReturnArgument(0);
214 
215  $packageManager->injectDependencyResolver($dependencyResolver);
216 
217  $packageManager->_set('packages', []);
218  $packageManager->_call('scanAvailablePackages');
219 
220  $expectedPackageStatesConfiguration = [];
221  foreach ($packageKeys as $packageKey) {
222  $expectedPackageStatesConfiguration[$packageKey] = [
223  'state' => 'inactive',
224  'packagePath' => 'Application/' . $packageKey . '/',
225  'composerName' => $packageKey,
226  'suggestions' => [],
227  ];
228  }
229 
230  $actualPackageStatesConfiguration = $packageManager->_get('packageStatesConfiguration');
231  $this->assertEquals($expectedPackageStatesConfiguration, $actualPackageStatesConfiguration['packages']);
232  }
233 
239  public function packageKeysAndPaths()
240  {
241  return [
242  ['TYPO3.YetAnotherTestPackage', 'vfs://Test/Packages/Application/TYPO3.YetAnotherTestPackage/'],
243  ['Lolli.Pop.NothingElse', 'vfs://Test/Packages/Application/Lolli.Pop.NothingElse/']
244  ];
245  }
246 
251  public function createPackageCreatesPackageFolderAndReturnsPackage($packageKey, $expectedPackagePath)
252  {
253  $actualPackage = $this->createPackage($packageKey);
254  $actualPackagePath = $actualPackage->getPackagePath();
255 
256  $this->assertEquals($expectedPackagePath, $actualPackagePath);
257  $this->assertTrue(is_dir($actualPackagePath), 'Package path should exist after createPackage()');
258  $this->assertEquals($packageKey, $actualPackage->getPackageKey());
259  $this->assertTrue($this->packageManager->isPackageAvailable($packageKey));
260  }
261 
266  {
267  $packageKey = 'Acme.YetAnotherTestPackage';
268 
269  $this->createPackage($packageKey);
270 
271  $this->packageManager->deactivatePackage($packageKey);
272  $this->assertFalse($this->packageManager->isPackageActive($packageKey));
273 
274  $this->packageManager->activatePackage($packageKey);
275  $this->assertTrue($this->packageManager->isPackageActive($packageKey));
276  }
277 
283  {
284  $package = $this->createPackage('Acme.YetAnotherTestPackage');
285  $package->setProtected(true);
286  $this->packageManager->deactivatePackage('Acme.YetAnotherTestPackage');
287  }
288 
294  {
295  $this->packageManager->deletePackage('PrettyUnlikelyThatThisPackageExists');
296  }
297 
303  {
304  $package = $this->createPackage('Acme.YetAnotherTestPackage');
305  $package->setProtected(true);
306  $this->packageManager->deletePackage('Acme.YetAnotherTestPackage');
307  }
308 
313  {
314  $this->createPackage('Acme.YetAnotherTestPackage');
315 
316  $this->assertTrue($this->packageManager->isPackageActive('Acme.YetAnotherTestPackage'));
317  $this->assertTrue($this->packageManager->isPackageAvailable('Acme.YetAnotherTestPackage'));
318 
319  $this->packageManager->deletePackage('Acme.YetAnotherTestPackage');
320 
321  $this->assertFalse($this->packageManager->isPackageActive('Acme.YetAnotherTestPackage'));
322  $this->assertFalse($this->packageManager->isPackageAvailable('Acme.YetAnotherTestPackage'));
323  }
324 
328  public function composerNamesAndPackageKeys()
329  {
330  return [
331  ['imagine/Imagine', 'imagine.Imagine'],
332  ['imagine/imagine', 'imagine.Imagine'],
333  ['typo3/cms', 'TYPO3.CMS'],
334  ['TYPO3/CMS', 'TYPO3.CMS']
335  ];
336  }
337 
342  public function getPackageKeyFromComposerNameIgnoresCaseDifferences($composerName, $packageKey)
343  {
344  $packageStatesConfiguration = ['packages' =>
345  [
346  'TYPO3.CMS' => [
347  'composerName' => 'typo3/cms'
348  ],
349  'imagine.Imagine' => [
350  'composerName' => 'imagine/Imagine'
351  ]
352  ]
353  ];
354 
355  $packageManager = $this->getAccessibleMock(PackageManager::class, ['resolvePackageDependencies']);
356  $packageManager->_set('packageStatesConfiguration', $packageStatesConfiguration);
357 
358  $this->assertEquals($packageKey, $packageManager->_call('getPackageKeyFromComposerName', $composerName));
359  }
360 }
inject($target, $name, $dependency)
createPackageCreatesPackageFolderAndReturnsPackage($packageKey, $expectedPackagePath)
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)
getPackageKeyFromComposerNameIgnoresCaseDifferences($composerName, $packageKey)