‪TYPO3CMS  ‪main
CleanUpLocalProcessedFilesTest.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 Symfony\Component\Console\Helper\HelperSet;
22 use Symfony\Component\Console\Helper\QuestionHelper;
23 use Symfony\Component\Console\Output\OutputInterface;
24 use Symfony\Component\Console\Tester\CommandTester;
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 use TYPO3\TestingFramework\Core\Testbase;
30 
31 final class ‪CleanUpLocalProcessedFilesTest extends FunctionalTestCase
32 {
34 
35  protected ?CommandTester ‪$commandTester = null;
36 
37  protected array ‪$coreExtensionsToLoad = ['lowlevel'];
38 
40  'typo3/sysext/lowlevel/Tests/Functional/Fixtures/DataSet/image.png' => 'fileadmin/image.png',
41  'typo3/sysext/lowlevel/Tests/Functional/Fixtures/DataSet/NotReferencedImage.png' => 'fileadmin/_processed_/0/a/NotReferencedImage.png',
42  'typo3/sysext/lowlevel/Tests/Functional/Fixtures/DataSet/FileWithoutProcessedFileRecord.png' => 'fileadmin/_processed_/1/b/FileWithoutProcessedFileRecord.png',
43  'typo3/sysext/lowlevel/Tests/Functional/Fixtures/DataSet/NotReferencedImage2.png' => 'local-storage/_processed_/0/a/NotReferencedImage2.png',
44  'typo3/sysext/lowlevel/Tests/Functional/Fixtures/DataSet/FileWithoutProcessedFileRecord2.png' => 'local-storage/_processed_/1/b/FileWithoutProcessedFileRecord2.png',
45  'typo3/sysext/lowlevel/Tests/Functional/Fixtures/DataSet/ReferencedImage.png' => 'local-storage/_processed_/1/a/ReferencedImage.png',
46  'typo3/sysext/lowlevel/Tests/Functional/Fixtures/DataSet/ReferencedImage2.png' => 'local-storage/_processed_/1/a/ReferencedImage2.png',
47  ];
48 
49  protected function ‪setUp(): void
50  {
51  parent::setUp();
52 
53  $this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv');
54  $this->setUpBackendUser(1);
55 
56  $this->importCSVDataSet(__DIR__ . '/../Fixtures/DataSet/sys_file_processedfile.csv');
57  $this->subject = GeneralUtility::makeInstance(CleanUpLocalProcessedFilesCommand::class);
58 
59  $helperSet = new HelperSet();
60  $helperSet->set(new QuestionHelper(), 'question');
61 
62  $this->subject->setHelperSet($helperSet);
63  $this->commandTester = new CommandTester($this->subject);
64  $this->setUpBackendUser(1);
65 
66  // create fileadmin (1) and an additional absolute local storage (2)
67  ‪$subject = GeneralUtility::makeInstance(StorageRepository::class);
68  ‪$subject->createLocalStorage(
69  'fileadmin',
70  'fileadmin/',
71  'relative'
72  );
73  ‪$subject->createLocalStorage(
74  'another-storage',
75  $this->instancePath . '/local-storage/',
76  'absolute'
77  );
78 
79  // check for existing files to ensure setup is complete for this test
80  foreach ($this->pathsToProvideInTestInstance as $instanceFilePath) {
81  self::assertFileExists(GeneralUtility::getFileAbsFileName($instanceFilePath), $instanceFilePath . ' must exists in testcase instance.');
82  }
83  }
84 
85  protected function ‪tearDown(): void
86  {
87  // Some tests in this testcase deletes provided files. To avoid false-positive with changed orders we need to
88  // ensure that they are re-provided. We are doing this on a test case basis, to avoid unneded disk io if not
89  // really needed.
90  $testbase = new Testbase();
91  $testbase->providePathsInTestInstance($this->instancePath, $this->pathsToProvideInTestInstance);
92 
93  parent::tearDown();
94  }
95 
96  #[Test]
98  {
99  $this->commandTester->execute(['--force' => true]);
100 
101  $this->assertCSVDataSet(__DIR__ . '/../Fixtures/Modify/oneDeleted.csv');
102  }
103 
104  #[Test]
105  public function ‪fileForMissingReferenceIsDeleted(): void
106  {
107  $this->commandTester->execute(['--force' => true]);
108 
109  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('fileadmin/_processed_/0/a/NotReferencedImage.png'));
110  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('fileadmin/_processed_/1/b/FileWithoutProcessedFileRecord.png'));
111  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('local-storage/_processed_/0/a/NotReferencedImage2.png'));
112  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('local-storage/_processed_/1/b/FileWithoutProcessedFileRecord2.png'));
113  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('local-storage/_processed_/1/a/ReferencedImage.png'));
114  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('local-storage/_processed_/1/a/ReferencedImage2.png'));
115  self::assertFileExists(GeneralUtility::getFileAbsFileName('fileadmin/image.png'));
116  }
117 
118  #[Test]
119  public function ‪dryRunReallyDoesNothing(): void
120  {
121  $this->commandTester->execute(
122  [
123  '--dry-run' => true,
124  ]
125  );
126  $this->assertCSVDataSet(__DIR__ . '/../Fixtures/DataSet/sys_file_processedfile.csv');
127 
128  // `dry-run` should not remove files, therefore we need to test if `_processed_`file still exists.
129  self::assertFileExists(GeneralUtility::getFileAbsFileName('fileadmin/_processed_/0/a/NotReferencedImage.png'));
130  self::assertFileExists(GeneralUtility::getFileAbsFileName('fileadmin/_processed_/1/b/FileWithoutProcessedFileRecord.png'));
131  self::assertFileExists(GeneralUtility::getFileAbsFileName('local-storage/_processed_/0/a/NotReferencedImage2.png'));
132  self::assertFileExists(GeneralUtility::getFileAbsFileName('local-storage/_processed_/1/b/FileWithoutProcessedFileRecord2.png'));
133  self::assertFileExists(GeneralUtility::getFileAbsFileName('fileadmin/image.png'));
134  self::assertFileExists(GeneralUtility::getFileAbsFileName('local-storage/_processed_/1/a/ReferencedImage.png'));
135  self::assertFileExists(GeneralUtility::getFileAbsFileName('local-storage/_processed_/1/a/ReferencedImage2.png'));
136  }
137 
138  #[Test]
139  public function ‪confirmDeleteYes(): void
140  {
141  $this->commandTester->setInputs(['yes']);
142  // Set -v option, because the command does not need provide this option due to the use of isVerbose().
143  $this->commandTester->execute([], [
144  'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
145  ]);
146 
147  ‪$output = $this->commandTester->getDisplay();
148 
149  self::assertStringContainsString('[RECORD] Would delete /_processed_/a/SomeMissingFile.png', ‪$output);
150  self::assertStringContainsString('Are you sure you want to delete these processed files and records', ‪$output);
151  self::assertStringContainsString('Deleted 3 processed records', ‪$output);
152  self::assertStringContainsString('Deleted 6 processed files', ‪$output);
153  }
154 
155  #[Test]
156  public function ‪confirmDeleteYesForAll(): void
157  {
158  $this->commandTester->setInputs(['yes']);
159  // Set -v option, because the command does not need provide this option due to the use of isVerbose().
160  $this->commandTester->execute(['--all' => true], [
161  'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
162  ]);
163 
164  ‪$output = $this->commandTester->getDisplay();
165 
166  self::assertStringContainsString('[RECORD] Would delete /_processed_/a/SomeMissingFile.png', ‪$output);
167  self::assertStringContainsString('Are you sure you want to delete these processed files and records', ‪$output);
168  self::assertStringContainsString('Deleted 5 processed records', ‪$output);
169  self::assertStringContainsString('Failed to delete 5 records', ‪$output);
170  self::assertStringContainsString('Deleted 6 processed files', ‪$output);
171  }
172 
173  #[Test]
174  public function ‪confirmDeleteNo(): void
175  {
176  $this->commandTester->setInputs(['no']);
177  $this->commandTester->execute([]);
178  ‪$output = $this->commandTester->getDisplay();
179 
180  self::assertStringContainsString('Are you sure you want to delete these processed files and records', ‪$output);
181  self::assertStringNotContainsString('Deleted', ‪$output);
182  }
183 
184  #[Test]
185  public function ‪confirmDeleteNoWithAllOption(): void
186  {
187  $this->commandTester->setInputs(['no']);
188  $this->commandTester->execute(['--all' => true]);
189  ‪$output = $this->commandTester->getDisplay();
190 
191  self::assertStringContainsString('Are you sure you want to delete these processed files and records', ‪$output);
192  self::assertStringNotContainsString('Deleted', ‪$output);
193  }
194 
195  #[Test]
196  public function ‪allFilesAreDeleted(): void
197  {
198  $this->commandTester->execute(['--force' => true, '--all' => true]);
199 
200  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('fileadmin/_processed_/0/a/NotReferencedImage.png'));
201  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('fileadmin/_processed_/1/b/FileWithoutProcessedFileRecord.png'));
202  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('local-storage/_processed_/0/a/NotReferencedImage2.png'));
203  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('local-storage/_processed_/1/b/FileWithoutProcessedFileRecord2.png'));
204  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('local-storage/_processed_/1/a/ReferencedImage.png'));
205  self::assertFileDoesNotExist(GeneralUtility::getFileAbsFileName('local-storage/_processed_/1/a/ReferencedImage2.png'));
206  self::assertFileExists(GeneralUtility::getFileAbsFileName('fileadmin/image.png'));
207  }
208 
209  #[Test]
210  public function ‪allDatabaseRecordsAreDeleted(): void
211  {
212  $this->commandTester->execute(['--force' => true, '--all' => true]);
213 
214  $this->assertCSVDataSet(__DIR__ . '/../Fixtures/Modify/allDeleted.csv');
215  }
216 
217 }
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\allDatabaseRecordsAreDeleted
‪allDatabaseRecordsAreDeleted()
Definition: CleanUpLocalProcessedFilesTest.php:210
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean
Definition: CleanUpLocalProcessedFilesTest.php:18
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\databaseRecordForMissingFileIsDeleted
‪databaseRecordForMissingFileIsDeleted()
Definition: CleanUpLocalProcessedFilesTest.php:97
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\$commandTester
‪CommandTester $commandTester
Definition: CleanUpLocalProcessedFilesTest.php:35
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\allFilesAreDeleted
‪allFilesAreDeleted()
Definition: CleanUpLocalProcessedFilesTest.php:196
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\$subject
‪CleanUpLocalProcessedFilesCommand $subject
Definition: CleanUpLocalProcessedFilesTest.php:33
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\dryRunReallyDoesNothing
‪dryRunReallyDoesNothing()
Definition: CleanUpLocalProcessedFilesTest.php:119
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\tearDown
‪tearDown()
Definition: CleanUpLocalProcessedFilesTest.php:85
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\confirmDeleteNoWithAllOption
‪confirmDeleteNoWithAllOption()
Definition: CleanUpLocalProcessedFilesTest.php:185
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\confirmDeleteYesForAll
‪confirmDeleteYesForAll()
Definition: CleanUpLocalProcessedFilesTest.php:156
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\setUp
‪setUp()
Definition: CleanUpLocalProcessedFilesTest.php:49
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: CleanUpLocalProcessedFilesTest.php:37
‪TYPO3\CMS\Lowlevel\Command\CleanUpLocalProcessedFilesCommand
Definition: CleanUpLocalProcessedFilesCommand.php:33
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest
Definition: CleanUpLocalProcessedFilesTest.php:32
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\$pathsToProvideInTestInstance
‪array $pathsToProvideInTestInstance
Definition: CleanUpLocalProcessedFilesTest.php:39
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\confirmDeleteYes
‪confirmDeleteYes()
Definition: CleanUpLocalProcessedFilesTest.php:139
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\confirmDeleteNo
‪confirmDeleteNo()
Definition: CleanUpLocalProcessedFilesTest.php:174
‪TYPO3\CMS\Lowlevel\Tests\Functional\Clean\CleanUpLocalProcessedFilesTest\fileForMissingReferenceIsDeleted
‪fileForMissingReferenceIsDeleted()
Definition: CleanUpLocalProcessedFilesTest.php:105