PageRenderTime 26ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/public/typo3/sysext/extensionmanager/Tests/Unit/Utility/InstallUtilityTest.php

https://bitbucket.org/followupcio/website
PHP | 365 lines | 252 code | 25 blank | 88 comment | 0 complexity | 3da3e3b2297e8dd448cb95398277f8a3 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause
  1. <?php
  2. namespace TYPO3\CMS\Extensionmanager\Tests\Unit\Utility;
  3. /*
  4. * This file is part of the TYPO3 CMS project.
  5. *
  6. * It is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License, either version 2
  8. * of the License, or any later version.
  9. *
  10. * For the full copyright and license information, please read the
  11. * LICENSE.txt file that was distributed with this source code.
  12. *
  13. * The TYPO3 project - inspiring people to share!
  14. */
  15. /**
  16. * Test case
  17. */
  18. class InstallUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
  19. {
  20. /**
  21. * @var string
  22. */
  23. protected $extensionKey;
  24. /**
  25. * @var array
  26. */
  27. protected $extensionData = [];
  28. /**
  29. * @var array List of created fake extensions to be deleted in tearDown() again
  30. */
  31. protected $fakedExtensions = [];
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Extensionmanager\Utility\InstallUtility|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
  34. */
  35. protected $installMock;
  36. /**
  37. */
  38. protected function setUp()
  39. {
  40. $this->extensionKey = 'dummy';
  41. $this->extensionData = [
  42. 'key' => $this->extensionKey
  43. ];
  44. $this->installMock = $this->getAccessibleMock(
  45. \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
  46. [
  47. 'isLoaded',
  48. 'loadExtension',
  49. 'unloadExtension',
  50. 'processDatabaseUpdates',
  51. 'processRuntimeDatabaseUpdates',
  52. 'reloadCaches',
  53. 'processCachingFrameworkUpdates',
  54. 'saveDefaultConfiguration',
  55. 'getExtensionArray',
  56. 'enrichExtensionWithDetails',
  57. 'ensureConfiguredDirectoriesExist',
  58. 'importInitialFiles',
  59. 'emitAfterExtensionInstallSignal'
  60. ],
  61. [],
  62. '',
  63. false
  64. );
  65. $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
  66. $this->installMock->_set('dependencyUtility', $dependencyUtility);
  67. $this->installMock->expects($this->any())
  68. ->method('getExtensionArray')
  69. ->with($this->extensionKey)
  70. ->will($this->returnCallback([$this, 'getExtensionData']));
  71. $this->installMock->expects($this->any())
  72. ->method('enrichExtensionWithDetails')
  73. ->with($this->extensionKey)
  74. ->will($this->returnCallback([$this, 'getExtensionData']));
  75. }
  76. /**
  77. * @return array
  78. */
  79. public function getExtensionData()
  80. {
  81. return $this->extensionData;
  82. }
  83. /**
  84. */
  85. protected function tearDown()
  86. {
  87. foreach ($this->fakedExtensions as $fakeExtkey => $fakeExtension) {
  88. $this->testFilesToDelete[] = PATH_site . 'typo3temp/var/tests/' . $fakeExtkey;
  89. }
  90. parent::tearDown();
  91. }
  92. /**
  93. * Creates a fake extension inside typo3temp/. No configuration is created,
  94. * just the folder
  95. *
  96. * @return string The extension key
  97. */
  98. protected function createFakeExtension()
  99. {
  100. $extKey = strtolower($this->getUniqueId('testing'));
  101. $absExtPath = PATH_site . 'typo3temp/var/tests/' . $extKey;
  102. $relPath = 'typo3temp/var/tests/' . $extKey . '/';
  103. \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absExtPath);
  104. $this->fakedExtensions[$extKey] = [
  105. 'siteRelPath' => $relPath
  106. ];
  107. return $extKey;
  108. }
  109. /**
  110. * @test
  111. */
  112. public function installCallsProcessRuntimeDatabaseUpdates()
  113. {
  114. $this->installMock->expects($this->once())
  115. ->method('processRuntimeDatabaseUpdates')
  116. ->with($this->extensionKey);
  117. $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
  118. $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
  119. $this->installMock->_set('cacheManager', $cacheManagerMock);
  120. $this->installMock->install($this->extensionKey);
  121. }
  122. /**
  123. * @test
  124. */
  125. public function installCallsLoadExtension()
  126. {
  127. $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
  128. $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
  129. $this->installMock->_set('cacheManager', $cacheManagerMock);
  130. $this->installMock->expects($this->once())->method('loadExtension');
  131. $this->installMock->install($this->extensionKey);
  132. }
  133. /**
  134. * @test
  135. */
  136. public function installCallsFlushCachesIfClearCacheOnLoadIsSet()
  137. {
  138. $this->extensionData['clearcacheonload'] = true;
  139. $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
  140. $cacheManagerMock->expects($this->once())->method('flushCaches');
  141. $this->installMock->_set('cacheManager', $cacheManagerMock);
  142. $this->installMock->install($this->extensionKey);
  143. }
  144. /**
  145. * @test
  146. */
  147. public function installCallsFlushCachesIfClearCacheOnLoadCamelCasedIsSet()
  148. {
  149. $this->extensionData['clearCacheOnLoad'] = true;
  150. $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
  151. $cacheManagerMock->expects($this->once())->method('flushCaches');
  152. $this->installMock->_set('cacheManager', $cacheManagerMock);
  153. $this->installMock->install($this->extensionKey);
  154. }
  155. /**
  156. * @test
  157. */
  158. public function installationOfAnExtensionWillCallEnsureThatDirectoriesExist()
  159. {
  160. $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
  161. $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
  162. $this->installMock->_set('cacheManager', $cacheManagerMock);
  163. $this->installMock->expects($this->once())->method('ensureConfiguredDirectoriesExist');
  164. $this->installMock->install($this->extensionKey);
  165. }
  166. /**
  167. * @test
  168. */
  169. public function installCallsReloadCaches()
  170. {
  171. $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
  172. $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
  173. $this->installMock->_set('cacheManager', $cacheManagerMock);
  174. $this->installMock->expects($this->once())->method('reloadCaches');
  175. $this->installMock->install('dummy');
  176. }
  177. /**
  178. * @test
  179. */
  180. public function installCallsSaveDefaultConfigurationWithExtensionKey()
  181. {
  182. $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
  183. $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
  184. $this->installMock->_set('cacheManager', $cacheManagerMock);
  185. $this->installMock->expects($this->once())->method('saveDefaultConfiguration')->with('dummy');
  186. $this->installMock->install('dummy');
  187. }
  188. /**
  189. * @test
  190. */
  191. public function uninstallCallsUnloadExtension()
  192. {
  193. $this->installMock->expects($this->once())->method('unloadExtension');
  194. $this->installMock->uninstall($this->extensionKey);
  195. }
  196. /**
  197. * @test
  198. */
  199. public function processDatabaseUpdatesCallsUpdateDbWithExtTablesSql()
  200. {
  201. $extKey = $this->createFakeExtension();
  202. $extPath = PATH_site . 'typo3temp/var/tests/' . $extKey . '/';
  203. $extTablesFile = $extPath . 'ext_tables.sql';
  204. $fileContent = 'DUMMY TEXT TO COMPARE';
  205. file_put_contents($extTablesFile, $fileContent);
  206. $installMock = $this->getAccessibleMock(
  207. \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
  208. ['updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'],
  209. [],
  210. '',
  211. false
  212. );
  213. $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
  214. $installMock->_set('dependencyUtility', $dependencyUtility);
  215. $installMock->expects($this->once())->method('updateDbWithExtTablesSql')->with($this->stringStartsWith($fileContent));
  216. $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
  217. }
  218. /**
  219. * @test
  220. */
  221. public function processDatabaseUpdatesCallsImportStaticSqlFile()
  222. {
  223. $extKey = $this->createFakeExtension();
  224. $extRelPath = 'typo3temp/var/tests/' . $extKey . '/';
  225. $installMock = $this->getAccessibleMock(
  226. \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
  227. ['importStaticSqlFile', 'updateDbWithExtTablesSql', 'importT3DFile'],
  228. [],
  229. '',
  230. false
  231. );
  232. $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
  233. $installMock->_set('dependencyUtility', $dependencyUtility);
  234. $installMock->expects($this->once())->method('importStaticSqlFile')->with($extRelPath);
  235. $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
  236. }
  237. /**
  238. * @return array
  239. */
  240. public function processDatabaseUpdatesCallsImportFileDataProvider()
  241. {
  242. return [
  243. 'T3D file' => [
  244. 'data.t3d'
  245. ],
  246. 'XML file' => [
  247. 'data.xml'
  248. ]
  249. ];
  250. }
  251. /**
  252. * @param string $fileName
  253. * @test
  254. * @dataProvider processDatabaseUpdatesCallsImportFileDataProvider
  255. */
  256. public function processDatabaseUpdatesCallsImportFile($fileName)
  257. {
  258. $extKey = $this->createFakeExtension();
  259. $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
  260. \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . '/Initialisation');
  261. file_put_contents($absPath . '/Initialisation/' . $fileName, 'DUMMY');
  262. $installMock = $this->getAccessibleMock(
  263. \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
  264. ['updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'],
  265. [],
  266. '',
  267. false
  268. );
  269. $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
  270. $installMock->_set('dependencyUtility', $dependencyUtility);
  271. $installMock->expects($this->once())->method('importT3DFile')->with($this->fakedExtensions[$extKey]['siteRelPath']);
  272. $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
  273. }
  274. /**
  275. * @return array
  276. */
  277. public function importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider()
  278. {
  279. return [
  280. 'Import T3D file when T3D was imported before extension to XML' => [
  281. 'data.t3d',
  282. 'dataImported',
  283. 'data.t3d',
  284. ],
  285. 'Import T3D file when a file was imported after extension to XML' => [
  286. 'data.t3d',
  287. 'data.t3d',
  288. 'dataImported'
  289. ],
  290. 'Import XML file when T3D was imported before extension to XML' => [
  291. 'data.xml',
  292. 'dataImported',
  293. 'data.t3d'
  294. ],
  295. 'Import XML file when a file was imported after extension to XML' => [
  296. 'data.xml',
  297. 'data.t3d',
  298. 'dataImported'
  299. ]
  300. ];
  301. }
  302. /**
  303. * @param string $fileName
  304. * @param string $registryNameReturnsFalse
  305. * @param string $registryNameReturnsTrue
  306. * @test
  307. * @dataProvider importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider
  308. */
  309. public function importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue)
  310. {
  311. $extKey = $this->createFakeExtension();
  312. $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
  313. \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . 'Initialisation');
  314. file_put_contents($absPath . 'Initialisation/' . $fileName, 'DUMMY');
  315. $registryMock = $this->getMockBuilder(\TYPO3\CMS\Core\Registry::class)
  316. ->setMethods(['get', 'set'])
  317. ->getMock();
  318. $registryMock
  319. ->expects($this->any())
  320. ->method('get')
  321. ->will($this->returnValueMap(
  322. [
  323. ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsFalse, null, false],
  324. ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsTrue, null, true],
  325. ]
  326. ));
  327. $installMock = $this->getAccessibleMock(
  328. \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
  329. ['getRegistry', 'getImportExportUtility'],
  330. [],
  331. '',
  332. false
  333. );
  334. $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
  335. $installMock->_set('dependencyUtility', $dependencyUtility);
  336. $installMock->_set('registry', $registryMock);
  337. $installMock->expects($this->never())->method('getImportExportUtility');
  338. $installMock->_call('importT3DFile', $this->fakedExtensions[$extKey]['siteRelPath']);
  339. }
  340. }