‪TYPO3CMS  11.5
ActionControllerTest.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 Prophecy\PhpUnit\ProphecyTrait;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
29 class ‪ActionControllerTest extends UnitTestCase
30 {
31  use ProphecyTrait;
32 
36  protected ‪$fakedExtensions = [];
37 
44  protected function ‪createFakeExtension(): array
45  {
46  $extKey = strtolower(‪StringUtility::getUniqueId('testing'));
47  $absExtPath = ‪Environment::getVarPath() . '/tests/ext-' . $extKey . '/';
48  ‪GeneralUtility::mkdir($absExtPath);
49  $this->testFilesToDelete[] = ‪Environment::getVarPath() . '/tests/ext-' . $extKey;
50  return [
51  'extensionKey' => $extKey,
52  'version' => '0.0.0',
53  'packagePath' => $absExtPath,
54  ];
55  }
56 
63  {
64  // 42 second of first day in 1970 - used to have achieve stable file names
65  ‪$GLOBALS['EXEC_TIME'] = 42;
66 
67  // Create extension for testing:
68  $fakeExtension = $this->‪createFakeExtension();
69  $extKey = $fakeExtension['extensionKey'];
70  $extensionRoot = $fakeExtension['packagePath'];
71  $installUtility = $this->prophesize(InstallUtility::class);
72  $installUtility->enrichExtensionWithDetails($extKey)->willReturn($fakeExtension);
73  // Build mocked fileHandlingUtility:
74  $subject = $this->getAccessibleMock(
75  ActionController::class,
76  ['dummy'],
77  [
78  $installUtility->reveal(),
79  $this->prophesize(ExtensionManagementService::class)->reveal(),
80  ]
81  );
82 
83  // Add files and directories to extension:
84  touch($extensionRoot . 'emptyFile.txt');
85  file_put_contents($extensionRoot . 'notEmptyFile.txt', 'content');
86  touch($extensionRoot . '.hiddenFile');
87  mkdir($extensionRoot . 'emptyDir');
88  mkdir($extensionRoot . 'notEmptyDir');
89  touch($extensionRoot . 'notEmptyDir/file.txt');
90 
91  // Create zip-file from extension
92  $filename = $subject->_call('createZipFileFromExtension', $extKey);
93 
94  $expectedFilename = ‪Environment::getVarPath() . '/transient/' . $extKey . '_0.0.0_' . date('YmdHi', 42) . '.zip';
95  $this->testFilesToDelete[] = $filename;
96  self::assertEquals($expectedFilename, $filename, 'Archive file name differs from expectation');
97 
98  // File was created
99  self::assertFileExists($filename, 'Zip file not created');
100 
101  // Read archive and check its contents
102  $archive = new \ZipArchive();
103  self::assertTrue($archive->open($filename), 'Unable to open archive');
104  self::assertEquals(0, $archive->statName('emptyFile.txt')['size'], 'Empty file not in archive');
105  self::assertEquals('content', $archive->getFromName('notEmptyFile.txt'), 'Expected content not found');
106  self::assertFalse($archive->statName('.hiddenFile'), 'Hidden file not in archive');
107  self::assertIsArray($archive->statName('emptyDir/'), 'Empty directory not in archive');
108  self::assertIsArray($archive->statName('notEmptyDir/'), 'Not empty directory not in archive');
109  self::assertIsArray($archive->statName('notEmptyDir/file.txt'), 'File within directory not in archive');
110 
111  // Check that the archive has no additional content
112  self::assertEquals(5, $archive->numFiles, 'Too many or too less files in archive');
113  }
114 }
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Controller
Definition: ActionControllerTest.php:18
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility
Definition: InstallUtility.php:56
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Controller\ActionControllerTest
Definition: ActionControllerTest.php:30
‪TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService
Definition: ExtensionManagementService.php:36
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Controller\ActionControllerTest\$fakedExtensions
‪array $fakedExtensions
Definition: ActionControllerTest.php:34
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:65
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Controller\ActionControllerTest\createFakeExtension
‪array createFakeExtension()
Definition: ActionControllerTest.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir($newFolder)
Definition: GeneralUtility.php:1891
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Controller\ActionControllerTest\createZipFileFromExtensionGeneratesCorrectArchive
‪createZipFileFromExtensionGeneratesCorrectArchive()
Definition: ActionControllerTest.php:60
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:218