‪TYPO3CMS  ‪main
ExtensionManagementServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
27 use TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
33 final class ‪ExtensionManagementServiceTest extends UnitTestCase
34 {
35  protected bool ‪$resetSingletonInstances = true;
36 
37  protected ExtensionManagementService ‪$managementService;
43 
44  public function ‪setUp(): void
45  {
46  parent::setUp();
47  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['extensionmanager']['offlineMode'] = false;
48  $this->remoteMock = $this->createMock(ExtensionDownloaderRemoteInterface::class);
49  $remoteRegistryMock = $this->createMock(RemoteRegistry::class);
50  $remoteRegistryMock->method('hasRemote')->with(self::anything())->willReturn(true);
51  $remoteRegistryMock->method('getRemote')->with(self::anything())->willReturn($this->remoteMock);
52  $this->fileHandlingUtilityMock = $this->createMock(FileHandlingUtility::class);
53 
54  $this->downloadQueue = new ‪DownloadQueue();
55  $this->managementService = new ExtensionManagementService(
56  $remoteRegistryMock,
57  $this->fileHandlingUtilityMock,
58  $this->downloadQueue,
60  );
61 
62  $this->dependencyUtilityMock = $this->createMock(DependencyUtility::class);
63  $this->installUtilityMock = $this->createMock(InstallUtility::class);
64 
65  $this->managementService->injectDependencyUtility($this->dependencyUtilityMock);
66  $this->managementService->injectInstallUtility($this->installUtilityMock);
67  }
68 
69  #[Test]
71  {
72  $extension = new ‪Extension();
73  $extension->setExtensionKey('foobar');
74  $extension->setVersion('1.0.0');
75  // an extension with a uid means it needs to be downloaded
76  $extension->_setProperty('uid', 123);
77  $extension->_setProperty('remote', 'ter');
78 
79  $this->remoteMock->expects(self::once())->method('downloadExtension')->with(
80  $extension->getExtensionKey(),
81  $extension->getVersion(),
82  $this->fileHandlingUtilityMock,
83  $extension->getMd5hash(),
84  'Local'
85  );
86  $this->managementService->installExtension($extension);
87  }
88 
89  #[Test]
91  {
92  $extension = new ‪Extension();
93  $this->dependencyUtilityMock->expects(self::atLeastOnce())->method('setSkipDependencyCheck')->with(false);
94  $this->dependencyUtilityMock->expects(self::atLeastOnce())->method('checkDependencies')->with($extension);
95  $this->dependencyUtilityMock->method('hasDependencyErrors')->willReturn(true);
96 
97  $result = $this->managementService->installExtension($extension);
98  self::assertFalse($result);
99  }
100 
101  #[Test]
103  {
104  $extension = new ‪Extension();
105  $extension->setExtensionKey('foo');
106 
107  $result = $this->managementService->installExtension($extension);
108  self::assertSame(['installed' => ['foo' => 'foo']], $result);
109  }
110 
111  #[Test]
113  {
114  $extension = new ‪Extension();
115  $extension->setExtensionKey('foo');
116  $extension->_setProperty('remote', 'ter');
117  $this->downloadQueue->addExtensionToQueue($extension);
118  $this->installUtilityMock->method('enrichExtensionWithDetails')->with('foo')->willReturn([
119  'key' => 'foo',
120  'remote' => 'ter',
121  ]);
122  $this->installUtilityMock->expects(self::atLeastOnce())->method('reloadAvailableExtensions');
123  $this->installUtilityMock->expects(self::atLeastOnce())->method('install')->with('foo');
124 
125  $result = $this->managementService->installExtension($extension);
126  self::assertSame(['downloaded' => ['foo' => $extension], 'installed' => ['foo' => 'foo']], $result);
127  }
128 
129  #[Test]
131  {
132  $extension = new ‪Extension();
133  $extension->setExtensionKey('foo');
134  $extension->_setProperty('remote', 'ter');
135  $this->downloadQueue->addExtensionToQueue($extension, 'update');
136  $this->installUtilityMock->method('enrichExtensionWithDetails')->with('foo')->willReturn([
137  'key' => 'foo',
138  'remote' => 'ter',
139  ]);
140  $this->installUtilityMock->expects(self::atLeastOnce())->method('reloadAvailableExtensions');
141 
142  // an extension update will uninstall the extension and install it again
143  $this->installUtilityMock->expects(self::atLeastOnce())->method('uninstall')->with('foo');
144  $this->installUtilityMock->expects(self::atLeastOnce())->method('install')->with('foo');
145 
146  $result = $this->managementService->installExtension($extension);
147 
148  self::assertSame(['updated' => ['foo' => $extension], 'installed' => ['foo' => 'foo']], $result);
149  }
150 
151  #[Test]
153  {
154  $extension = new ‪Extension();
155  $extension->setExtensionKey('foo');
156  $this->dependencyUtilityMock->method('hasDependencyErrors')->willReturn(false);
157  $this->dependencyUtilityMock->expects(self::once())->method('checkDependencies')->with($extension);
158 
159  $this->managementService->markExtensionForDownload($extension);
160 
161  self::assertSame(['download' => ['foo' => $extension]], $this->downloadQueue->getExtensionQueue());
162  }
163 
164  #[Test]
166  {
167  $extension = new ‪Extension();
168  $extension->setExtensionKey('foo');
169  $this->dependencyUtilityMock->method('hasDependencyErrors')->willReturn(false);
170  $this->dependencyUtilityMock->expects(self::once())->method('checkDependencies')->with($extension);
171 
172  $this->managementService->markExtensionForUpdate($extension);
173 
174  self::assertSame(['update' => ['foo' => $extension]], $this->downloadQueue->getExtensionQueue());
175  }
176 }
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\$installUtilityMock
‪InstallUtility &MockObject $installUtilityMock
Definition: ExtensionManagementServiceTest.php:39
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\$managementService
‪ExtensionManagementService $managementService
Definition: ExtensionManagementServiceTest.php:37
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\installExtensionWillReturnDownloadedExtensions
‪installExtensionWillReturnDownloadedExtensions()
Definition: ExtensionManagementServiceTest.php:112
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension
Definition: Extension.php:30
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\$downloadQueue
‪DownloadQueue $downloadQueue
Definition: ExtensionManagementServiceTest.php:40
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\installExtensionWillReturnUpdatedExtensions
‪installExtensionWillReturnUpdatedExtensions()
Definition: ExtensionManagementServiceTest.php:130
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\installExtensionWillReturnInstalledExtensions
‪installExtensionWillReturnInstalledExtensions()
Definition: ExtensionManagementServiceTest.php:102
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest
Definition: ExtensionManagementServiceTest.php:34
‪TYPO3\CMS\Extensionmanager\Domain\Model\DownloadQueue
Definition: DownloadQueue.php:28
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\installDownloadsExtensionIfNecessary
‪installDownloadsExtensionIfNecessary()
Definition: ExtensionManagementServiceTest.php:70
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\$fileHandlingUtilityMock
‪FileHandlingUtility &MockObject $fileHandlingUtilityMock
Definition: ExtensionManagementServiceTest.php:42
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
Definition: FileHandlingUtility.php:40
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ExtensionManagementServiceTest.php:35
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility
Definition: InstallUtility.php:40
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\$remoteMock
‪ExtensionDownloaderRemoteInterface &MockObject $remoteMock
Definition: ExtensionManagementServiceTest.php:41
‪TYPO3\CMS\Extensionmanager\Utility\DependencyUtility
Definition: DependencyUtility.php:37
‪TYPO3\CMS\Extensionmanager\Remote\ExtensionDownloaderRemoteInterface
Definition: ExtensionDownloaderRemoteInterface.php:28
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\setUp
‪setUp()
Definition: ExtensionManagementServiceTest.php:44
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\markExtensionForUpdateAddsExtensionToUpdateQueueAndChecksDependencies
‪markExtensionForUpdateAddsExtensionToUpdateQueueAndChecksDependencies()
Definition: ExtensionManagementServiceTest.php:165
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\installExtensionReturnsFalseIfDependenciesCannotBeResolved
‪installExtensionReturnsFalseIfDependenciesCannotBeResolved()
Definition: ExtensionManagementServiceTest.php:90
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service
Definition: ExtensionManagementServiceTest.php:18
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\markExtensionForDownloadAddsExtensionToDownloadQueueAndChecksDependencies
‪markExtensionForDownloadAddsExtensionToDownloadQueueAndChecksDependencies()
Definition: ExtensionManagementServiceTest.php:152
‪TYPO3\CMS\Extensionmanager\Remote\RemoteRegistry
Definition: RemoteRegistry.php:26
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Service\ExtensionManagementServiceTest\$dependencyUtilityMock
‪DependencyUtility &MockObject $dependencyUtilityMock
Definition: ExtensionManagementServiceTest.php:38