‪TYPO3CMS  ‪main
FileHandlingUtilityTest.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 
27 use TYPO3\CMS\Extensionmanager\Utility\EmConfUtility;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 class ‪FileHandlingUtilityTest extends UnitTestCase
32 {
36  protected array ‪$fakedExtensions = [];
37 
38  protected string ‪$testRoot;
39 
40  protected function ‪setUp(): void
41  {
42  $this->testRoot = ‪Environment::getVarPath() . '/tests/';
43  ‪GeneralUtility::mkdir_deep($this->testRoot);
44  $this->testFilesToDelete[] = ‪$this->testRoot;
45  parent::setUp();
46  }
47 
54  protected function ‪createFakeExtension(): string
55  {
56  $extKey = strtolower(‪StringUtility::getUniqueId('testing'));
57  $absExtPath = $this->testRoot . 'ext-' . $extKey . '/';
58  ‪GeneralUtility::mkdir_deep($absExtPath);
59  $this->fakedExtensions[$extKey] = [
60  'packagePath' => $absExtPath,
61  ];
62  return $extKey;
63  }
64 
69  {
70  $extKey = $this->‪createFakeExtension();
71  $path = $this->fakedExtensions[$extKey]['packagePath'];
72  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, ['removeDirectory', 'addDirectory', 'getExtensionDir'], [], '', false);
73  $fileHandlerMock->expects(self::once())
74  ->method('removeDirectory')
75  ->with($path);
76  $fileHandlerMock
77  ->method('getExtensionDir')
78  ->willReturn($path);
79  $fileHandlerMock->_call('makeAndClearExtensionDir', $extKey);
80  }
81 
85  public function ‪makeAndClearExtensionDirAddsDir(): void
86  {
87  $extKey = $this->‪createFakeExtension();
88  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, ['removeDirectory', 'addDirectory', 'getExtensionDir']);
89  $fileHandlerMock->expects(self::once())
90  ->method('addDirectory')
91  ->with($this->testRoot . 'ext-' . $extKey . '/');
92  $fileHandlerMock
93  ->method('getExtensionDir')
94  ->willReturn($this->testRoot . 'ext-' . $extKey . '/');
95  $fileHandlerMock->_call('makeAndClearExtensionDir', $extKey);
96  }
97 
102  {
103  $this->expectException(ExtensionManagerException::class);
104  $this->expectExceptionCode(1337280417);
105  ‪$GLOBALS['BE_USER'] = $this->getMockBuilder(BackendUserAuthentication::class)->getMock();
106  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, ['removeDirectory', 'addDirectory']);
107  $languageServiceMock = $this->createMock(LanguageService::class);
108  $languageServiceMock->method('includeLLFile')->with(self::anything())->willReturn([]);
109  $languageServiceMock->method('getLL')->with(self::anything())->willReturn('');
110  $languageServiceFactoryMock = $this->createMock(LanguageServiceFactory::class);
111  $languageServiceFactoryMock->method('createFromUserPreferences')->with(self::anything())->willReturn($languageServiceMock);
112  $fileHandlerMock->injectLanguageServiceFactory($languageServiceFactoryMock);
113  $fileHandlerMock->initializeObject();
114  $fileHandlerMock->_call('makeAndClearExtensionDir', 'testing123', 'fakepath');
115  }
116 
120  public function ‪addDirectoryAddsDirectory(): void
121  {
122  $extDirPath = $this->testRoot . ‪StringUtility::getUniqueId('test-extensions-');
123  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, null);
124  $fileHandlerMock->_call('addDirectory', $extDirPath);
125  self::assertDirectoryExists($extDirPath);
126  }
127 
131  public function ‪removeDirectoryRemovesDirectory(): void
132  {
133  $extDirPath = $this->testRoot . ‪StringUtility::getUniqueId('test-extensions-');
134  @mkdir($extDirPath);
135  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, null);
136  $fileHandlerMock->_call('removeDirectory', $extDirPath);
137  self::assertDirectoryDoesNotExist($extDirPath);
138  }
139 
143  public function ‪removeDirectoryRemovesSymlink(): void
144  {
145  $absoluteSymlinkPath = $this->testRoot . ‪StringUtility::getUniqueId('test_symlink_');
146  $absoluteFilePath = $this->testRoot . ‪StringUtility::getUniqueId('test_file_');
147  touch($absoluteFilePath);
148  symlink($absoluteFilePath, $absoluteSymlinkPath);
149  $fileHandler = new ‪FileHandlingUtility();
150  $fileHandler->removeDirectory($absoluteSymlinkPath);
151  self::assertFalse(is_link($absoluteSymlinkPath));
152  }
153 
158  {
159  $absoluteSymlinkPath = $this->testRoot . ‪StringUtility::getUniqueId('test_symlink_');
160  $absoluteDirectoryPath = $this->testRoot . ‪StringUtility::getUniqueId('test_dir_') . '/';
161  $relativeFilePath = ‪StringUtility::getUniqueId('test_file_');
162 
163  ‪GeneralUtility::mkdir_deep($absoluteDirectoryPath);
164  touch($absoluteDirectoryPath . $relativeFilePath);
165 
166  symlink($absoluteDirectoryPath, $absoluteSymlinkPath);
167 
168  $fileHandler = new ‪FileHandlingUtility();
169  $fileHandler->removeDirectory($absoluteSymlinkPath);
170  self::assertTrue(is_file($absoluteDirectoryPath . $relativeFilePath));
171  }
172 
177  {
178  $extensionKey = 'test';
179  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, [
180  'makeAndClearExtensionDir',
181  'writeEmConfToFile',
182  'extractDirectoriesFromExtensionData',
183  'createDirectoriesForExtensionFiles',
184  'writeExtensionFiles',
185  'reloadPackageInformation',
186  ]);
187  $fileHandlerMock->expects(self::once())->method('extractDirectoriesFromExtensionData')->willReturn([]);
188  $fileHandlerMock->expects(self::once())->method('makeAndClearExtensionDir')->with($extensionKey)->willReturn('my_path');
189  $fileHandlerMock->unpackExtensionFromExtensionDataArray($extensionKey, []);
190  }
191 
196  {
197  $extensionData = [
198  'extKey' => 'test',
199  'FILES' => [
200  'ChangeLog' => [
201  'name' => 'ChangeLog',
202  'size' => 4559,
203  'mtime' => 1219448527,
204  'is_executable' => false,
205  'content' => 'some content to write',
206  ],
207  'doc/' => [
208  'name' => 'doc/',
209  'size' => 0,
210  'mtime' => 1219448527,
211  'is_executable' => false,
212  'content' => '',
213  ],
214  'doc/ChangeLog' => [
215  'name' => 'ChangeLog',
216  'size' => 4559,
217  'mtime' => 1219448527,
218  'is_executable' => false,
219  'content' => 'some content to write',
220  ],
221  ],
222  ];
223  $cleanedFiles = [
224  'ChangeLog' => [
225  'name' => 'ChangeLog',
226  'size' => 4559,
227  'mtime' => 1219448527,
228  'is_executable' => false,
229  'content' => 'some content to write',
230  ],
231  'doc/ChangeLog' => [
232  'name' => 'ChangeLog',
233  'size' => 4559,
234  'mtime' => 1219448527,
235  'is_executable' => false,
236  'content' => 'some content to write',
237  ],
238  ];
239  $directories = [
240  'doc/',
241  'mod/doc/',
242  ];
243 
244  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, [
245  'makeAndClearExtensionDir',
246  'writeEmConfToFile',
247  'extractDirectoriesFromExtensionData',
248  'createDirectoriesForExtensionFiles',
249  'writeExtensionFiles',
250  'reloadPackageInformation',
251  ]);
252  $fileHandlerMock->expects(self::once())->method('extractDirectoriesFromExtensionData')->willReturn($directories);
253  $fileHandlerMock->expects(self::once())->method('createDirectoriesForExtensionFiles')->with($directories);
254  $fileHandlerMock->expects(self::once())->method('makeAndClearExtensionDir')->with($extensionData['extKey'])->willReturn('my_path');
255  $fileHandlerMock->expects(self::once())->method('writeExtensionFiles')->with($cleanedFiles);
256  $fileHandlerMock->expects(self::once())->method('reloadPackageInformation')->with('test');
257  $fileHandlerMock->unpackExtensionFromExtensionDataArray('test', $extensionData);
258  }
259 
263  public function ‪writeExtensionFilesWritesFiles(): void
264  {
265  $files = [
266  'ChangeLog' => [
267  'name' => 'ChangeLog',
268  'size' => 4559,
269  'mtime' => 1219448527,
270  'is_executable' => false,
271  'content' => 'some content to write',
272  ],
273  'README' => [
274  'name' => 'README',
275  'size' => 4566,
276  'mtime' => 1219448533,
277  'is_executable' => false,
278  'content' => 'FEEL FREE TO ADD SOME DOCUMENTATION HERE',
279  ],
280  ];
281  $rootPath = $this->fakedExtensions[$this->‪createFakeExtension()]['packagePath'];
282  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, ['makeAndClearExtensionDir']);
283  $fileHandlerMock->_call('writeExtensionFiles', $files, $rootPath);
284  self::assertFileExists($rootPath . 'ChangeLog');
285  }
286 
291  {
292  $files = [
293  'ChangeLog' => [
294  'name' => 'ChangeLog',
295  'size' => 4559,
296  'mtime' => 1219448527,
297  'is_executable' => false,
298  'content' => 'some content to write',
299  ],
300  'doc/' => [
301  'name' => 'doc/',
302  'size' => 0,
303  'mtime' => 1219448527,
304  'is_executable' => false,
305  'content' => '',
306  ],
307  'doc/ChangeLog' => [
308  'name' => 'ChangeLog',
309  'size' => 4559,
310  'mtime' => 1219448527,
311  'is_executable' => false,
312  'content' => 'some content to write',
313  ],
314  'doc/README' => [
315  'name' => 'README',
316  'size' => 4566,
317  'mtime' => 1219448533,
318  'is_executable' => false,
319  'content' => 'FEEL FREE TO ADD SOME DOCUMENTATION HERE',
320  ],
321  'mod/doc/README' => [
322  'name' => 'README',
323  'size' => 4566,
324  'mtime' => 1219448533,
325  'is_executable' => false,
326  'content' => 'FEEL FREE TO ADD SOME DOCUMENTATION HERE',
327  ],
328  ];
329  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, ['makeAndClearExtensionDir']);
330  $extractedDirectories = $fileHandlerMock->_call('extractDirectoriesFromExtensionData', $files);
331  $expected = [
332  'doc/',
333  'mod/doc/',
334  ];
335  self::assertSame($expected, array_values($extractedDirectories));
336  }
337 
342  {
343  $rootPath = $this->fakedExtensions[$this->‪createFakeExtension()]['packagePath'];
344  $directories = [
345  'doc/',
346  'mod/doc/',
347  ];
348  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, ['makeAndClearExtensionDir']);
349  self::assertDirectoryDoesNotExist($rootPath . 'doc/');
350  self::assertDirectoryDoesNotExist($rootPath . 'mod/doc/');
351  $fileHandlerMock->_call('createDirectoriesForExtensionFiles', $directories, $rootPath);
352  self::assertDirectoryExists($rootPath . 'doc/');
353  self::assertDirectoryExists($rootPath . 'mod/doc/');
354  }
355 
359  public function ‪writeEmConfWritesEmConfFile(): void
360  {
361  $extKey = $this->‪createFakeExtension();
362  $emConfData = [
363  'title' => 'Plugin cache engine',
364  'description' => 'Provides an interface to cache plugin content elements based on 4.3 caching framework',
365  'category' => 'Frontend',
366  ];
367  $rootPath = $this->fakedExtensions[$extKey]['packagePath'];
368 
369  $fileHandlerMock = $this->getAccessibleMock(FileHandlingUtility::class, ['makeAndClearExtensionDir']);
370  $fileHandlerMock->injectEmConfUtility(new EmConfUtility());
371  $fileHandlerMock->_call('writeEmConfToFile', $extKey, $emConfData, $rootPath);
372  self::assertFileExists($rootPath . 'ext_emconf.php');
373  }
374 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\makeAndClearExtensionDirThrowsExceptionOnInvalidPath
‪makeAndClearExtensionDirThrowsExceptionOnInvalidPath()
Definition: FileHandlingUtilityTest.php:101
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\createFakeExtension
‪string createFakeExtension()
Definition: FileHandlingUtilityTest.php:54
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\removeDirectoryDoesNotRemoveContentOfSymlinkedTargetDirectory
‪removeDirectoryDoesNotRemoveContentOfSymlinkedTargetDirectory()
Definition: FileHandlingUtilityTest.php:157
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility
Definition: DependencyUtilityTest.php:18
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\makeAndClearExtensionDirAddsDir
‪makeAndClearExtensionDirAddsDir()
Definition: FileHandlingUtilityTest.php:85
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\removeDirectoryRemovesDirectory
‪removeDirectoryRemovesDirectory()
Definition: FileHandlingUtilityTest.php:131
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\removeDirectoryRemovesSymlink
‪removeDirectoryRemovesSymlink()
Definition: FileHandlingUtilityTest.php:143
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
Definition: FileHandlingUtility.php:36
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\extractDirectoriesFromExtensionDataExtractsDirectories
‪extractDirectoriesFromExtensionDataExtractsDirectories()
Definition: FileHandlingUtilityTest.php:290
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest
Definition: FileHandlingUtilityTest.php:32
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\addDirectoryAddsDirectory
‪addDirectoryAddsDirectory()
Definition: FileHandlingUtilityTest.php:120
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\unpackExtensionFromExtensionDataArrayCreatesTheExtensionDirectory
‪unpackExtensionFromExtensionDataArrayCreatesTheExtensionDirectory()
Definition: FileHandlingUtilityTest.php:176
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:1753
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\setUp
‪setUp()
Definition: FileHandlingUtilityTest.php:40
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\writeExtensionFilesWritesFiles
‪writeExtensionFilesWritesFiles()
Definition: FileHandlingUtilityTest.php:263
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:24
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:66
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\unpackExtensionFromExtensionDataArrayStripsDirectoriesFromFilesArray
‪unpackExtensionFromExtensionDataArrayStripsDirectoriesFromFilesArray()
Definition: FileHandlingUtilityTest.php:195
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\writeEmConfWritesEmConfFile
‪writeEmConfWritesEmConfFile()
Definition: FileHandlingUtilityTest.php:359
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\makeAndClearExtensionDirRemovesExtensionDirIfAlreadyExists
‪makeAndClearExtensionDirRemovesExtensionDirIfAlreadyExists()
Definition: FileHandlingUtilityTest.php:68
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\createDirectoriesForExtensionFilesCreatesDirectories
‪createDirectoriesForExtensionFilesCreatesDirectories()
Definition: FileHandlingUtilityTest.php:341
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\$fakedExtensions
‪array $fakedExtensions
Definition: FileHandlingUtilityTest.php:36
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\FileHandlingUtilityTest\$testRoot
‪string $testRoot
Definition: FileHandlingUtilityTest.php:38
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:29