[BUGFIX] Make sure orig file is not removed when deleting processed files 55/36855/6
authorFrans Saris <franssaris@gmail.com>
Thu, 12 Feb 2015 10:04:36 +0000 (11:04 +0100)
committerNicole Cordes <typo3@cordes.co>
Sat, 14 Feb 2015 18:33:55 +0000 (19:33 +0100)
A processed file only needs to be deleted from storage if it has its
own identifier.

Releases: master, 6.2
Resolves: #65021
Change-Id: I76c3f1afa6ba7fa5d8898f7281bef78f1ccb4a35
Reviewed-on: http://review.typo3.org/36855
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
Reviewed-by: Michael Oehlhof <typo3@oehlhof.de>
Tested-by: Michael Oehlhof <typo3@oehlhof.de>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
typo3/sysext/core/Classes/Resource/ProcessedFile.php
typo3/sysext/core/Tests/Unit/Resource/ProcessedFileTest.php [new file with mode: 0644]

index 070f679..fe2619f 100644 (file)
@@ -401,7 +401,12 @@ class ProcessedFile extends AbstractFile {
                if (!$force && $this->isUnchanged()) {
                        return FALSE;
                }
-               return parent::delete();
+               // Only delete file when original isn't used
+               if (!$this->usesOriginalFile()) {
+                       return parent::delete();
+               } else {
+                       return TRUE;
+               }
        }
 
        /**
diff --git a/typo3/sysext/core/Tests/Unit/Resource/ProcessedFileTest.php b/typo3/sysext/core/Tests/Unit/Resource/ProcessedFileTest.php
new file mode 100644 (file)
index 0000000..ac4dbf1
--- /dev/null
@@ -0,0 +1,101 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Resource;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Resource\ProcessedFile;
+use TYPO3\CMS\Core\Resource\ResourceStorage;
+use TYPO3\CMS\Core\Resource\File;
+
+/**
+ * Testcase for the ProcessedFile class of the TYPO3 FAL
+ */
+class ProcessedFileTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @var \PHPUnit_Framework_MockObject_MockObject|ResourceStorage
+        */
+       protected $storageMock;
+
+       /**
+        * @var array
+        */
+       protected $databaseRow = array();
+
+       /**
+        * @throws \PHPUnit_Framework_Exception
+        */
+       protected function setUp() {
+               $this->storageMock = $this->getMock(ResourceStorage::class, array(), array(), '', FALSE);
+               $this->storageMock->expects($this->any())->method('getUid')->will($this->returnValue(5));
+
+               $this->databaseRow = array(
+                       'uid' => '1234567',
+                       'identifier' => 'dummy.txt',
+                       'name' => $this->getUniqueId('dummy_'),
+                       'storage' => $this->storageMock->getUid(),
+               );
+       }
+
+       /**
+        * @param array $dbRow
+        * @param ResourceStorage $storageMock
+        * @return File
+        */
+       protected function getFileFixture($dbRow = NULL, $storageMock = NULL) {
+               return new File($dbRow ?: $this->databaseRow, $storageMock ?: $this->storageMock);
+       }
+
+       /**
+        * @param array $dbRow
+        * @param File $originalFile
+        * @return ProcessedFile
+        */
+       protected function getProcessedFileFixture($dbRow = NULL, $originalFile = NULL) {
+               if ($originalFile === NULL) {
+                       $originalFile = $this->getFileFixture();
+               }
+               return new ProcessedFile($originalFile, 'dummy', array(), $dbRow ?: $this->databaseRow);
+       }
+
+       /**
+        * @test
+        */
+       public function propertiesOfProcessedFileAreSetFromDatabaseRow() {
+               $processedFileObject = $this->getProcessedFileFixture();
+               $this->assertSame($this->databaseRow, $processedFileObject->getProperties());
+       }
+
+       /**
+        * @test
+        */
+       public function deletingProcessedFileRemovesFile() {
+               $this->storageMock->expects($this->once())->method('deleteFile');
+               $processedDatabaseRow = $this->databaseRow;
+               $processedDatabaseRow['identifier'] = 'processed_dummy.txt';
+               $processedFile = $this->getProcessedFileFixture($processedDatabaseRow);
+               $processedFile->delete(TRUE);
+       }
+
+       /**
+        * @test
+        */
+       public function deletingProcessedFileThatUsesOriginalFileDoesNotRemoveFile() {
+               $this->storageMock->expects($this->never())->method('deleteFile');
+               $processedDatabaseRow = $this->databaseRow;
+               $processedDatabaseRow['identifier'] = NULL;
+               $processedFile = $this->getProcessedFileFixture($processedDatabaseRow);
+               $processedFile->delete(TRUE);
+       }
+}