[TASK] Add API to retrieve the parent folder of a resource 24/26724/7
authorSteffen Ritter <info@rs-websystems.de>
Thu, 9 Jan 2014 15:52:47 +0000 (16:52 +0100)
committerErnesto Baschny <ernst@cron-it.de>
Thu, 30 Jan 2014 18:15:34 +0000 (19:15 +0100)
Currently if some code needs the parent directory or folder
object of some files or folders it either needs to query the
storage object for the identifier of the parent folder and then
create the folder object manually or needs to fiddle around with
the identifier manually, assuming that all identifiers are a
representation of a hierarchical path as it is for the local
driver.

The latter not only will fail for all storages having non-path
identifiers it is more overhead to create the folder objects
manually anyhow.

This patch adds "getParentFolder" to the ResourceInterface and
implements the method in AbstractFile and Folder as well as it
replaces the manual resolval in the file list module.

Releases: 6.2
Resolves: #54226
Change-Id: I651d62340186dd9ac57277b498f8f98ee160dfd2
Reviewed-on: https://review.typo3.org/26724
Reviewed-by: Alexander Opitz
Reviewed-by: Marcin Sągol
Reviewed-by: Sebastian Fischer
Tested-by: Sebastian Fischer
Reviewed-by: Ernesto Baschny
Tested-by: Ernesto Baschny
typo3/sysext/core/Classes/Resource/AbstractFile.php
typo3/sysext/core/Classes/Resource/Folder.php
typo3/sysext/core/Classes/Resource/ResourceInterface.php
typo3/sysext/core/Tests/Unit/Resource/AbstractFileTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Resource/FolderTest.php
typo3/sysext/filelist/Classes/FileList.php

index d99034d..5f17bfc 100644 (file)
@@ -555,4 +555,12 @@ abstract class AbstractFile implements FileInterface {
         */
        abstract public function updateProperties(array $properties);
 
+       /**
+        * Returns the parent folder.
+        *
+        * @return FolderInterface
+        */
+       public function getParentFolder() {
+               return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
+       }
 }
index e636bf4..aa6ad52 100644 (file)
@@ -455,4 +455,17 @@ class Folder implements FolderInterface {
        public function getRole() {
                return $this->storage->getRole($this);
        }
+
+       /**
+        * Returns the parent folder.
+        *
+        * In non-hierarchical storages, that always is the root folder.
+        *
+        * The parent folder of the root folder is the root folder.
+        *
+        * @return Folder
+        */
+       public function getParentFolder() {
+               return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
+       }
 }
index d673e6f..f2babd6 100644 (file)
@@ -26,10 +26,10 @@ namespace TYPO3\CMS\Core\Resource;
  *
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
+
 /**
- * FileResourceInterface
+ * ResourceInterface
  *
- * @author Ingmar Schlecht <ingmar@typo3.org>
  */
 interface ResourceInterface {
        /**
@@ -59,4 +59,9 @@ interface ResourceInterface {
         * @return string
         */
        public function getHashedIdentifier();
+
+       /**
+        * @return FolderInterface
+        */
+       public function getParentFolder();
 }
diff --git a/typo3/sysext/core/Tests/Unit/Resource/AbstractFileTest.php b/typo3/sysext/core/Tests/Unit/Resource/AbstractFileTest.php
new file mode 100644 (file)
index 0000000..b2b35db
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Resource;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2014 Steffen Müller <typo3@t3node.com>
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *  A copy is found in the textfile GPL.txt and important notices to the license
+ *  from the author is found in LICENSE.txt distributed with these scripts.
+ *
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Testcase for the abstract file class of the TYPO3 FAL
+ *
+ * @author Steffen Müller <typo3@t3node.com>
+ */
+class AbstractFileTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @test
+        */
+       public function getParentFolderGetsParentFolderFromStorage() {
+               $parentIdentifier = '/parent/';
+               $currentIdentifier = '/parent/current/';
+
+               $mockedStorageForParent = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
+
+               /** @var \TYPO3\CMS\Core\Resource\AbstractFile $parentFolderFixture */
+               $parentFolderFixture = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\AbstractFile');
+               $parentFolderFixture->setIdentifier($parentIdentifier)->setStorage($mockedStorageForParent);
+
+               $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFolderIdentifierFromFileIdentifier', 'getFolder'), array(), '', FALSE);
+               $mockedStorage->expects($this->once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->will($this->returnValue($parentIdentifier));
+               $mockedStorage->expects($this->once())->method('getFolder')->with($parentIdentifier)->will($this->returnValue($parentFolderFixture));
+
+               /** @var \TYPO3\CMS\Core\Resource\AbstractFile $currentFolderFixture */
+               $currentFolderFixture = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\AbstractFile');
+               $currentFolderFixture->setIdentifier($currentIdentifier)->setStorage($mockedStorage);
+
+               $this->assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
+       }
+}
index 8eec7f8..085d9e3 100644 (file)
@@ -69,7 +69,7 @@ class FolderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $fixture = $this->createFolderFixture($path, $name, $mockedStorage);
                $this->assertSame($mockedStorage, $fixture->getStorage());
                $this->assertStringStartsWith($path, $fixture->getIdentifier());
-               $this->assertEquals($name, $fixture->getName());
+               $this->assertSame($name, $fixture->getName());
        }
 
        /**
@@ -78,8 +78,8 @@ class FolderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        public function propertiesCanBeUpdated() {
                $fixture = $this->createFolderFixture('/somePath', 'someName');
                $fixture->updateProperties(array('identifier' => '/someOtherPath', 'name' => 'someNewName'));
-               $this->assertEquals('someNewName', $fixture->getName());
-               $this->assertEquals('/someOtherPath', $fixture->getIdentifier());
+               $this->assertSame('someNewName', $fixture->getName());
+               $this->assertSame('/someOtherPath', $fixture->getIdentifier());
        }
 
        /**
@@ -88,7 +88,7 @@ class FolderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        public function propertiesAreNotUpdatedIfNotSetInInput() {
                $fixture = $this->createFolderFixture('/somePath/someName/', 'someName');
                $fixture->updateProperties(array('identifier' => '/someOtherPath'));
-               $this->assertEquals('someName', $fixture->getName());
+               $this->assertSame('someName', $fixture->getName());
        }
 
        /**
@@ -109,7 +109,7 @@ class FolderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
                $fileList = $fixture->getFiles();
 
-               $this->assertEquals(array('somefile.png', 'somefile.jpg'), array_keys($fileList));
+               $this->assertSame(array('somefile.png', 'somefile.jpg'), array_keys($fileList));
        }
 
        /**
@@ -155,4 +155,20 @@ class FolderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $fixture->getSubfolder('someSubfolder');
        }
 
+       /**
+        * @test
+        */
+       public function getParentFolderGetsParentFolderFromStorage() {
+               $parentIdentifier = '/parent/';
+               $currentIdentifier = '/parent/current/';
+
+               $parentFolderFixture = $this->createFolderFixture($parentIdentifier, 'parent');
+               $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFolderIdentifierFromFileIdentifier', 'getFolder'), array(), '', FALSE);
+               $mockedStorage->expects($this->once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->will($this->returnValue($parentIdentifier));
+               $mockedStorage->expects($this->once())->method('getFolder')->with($parentIdentifier)->will($this->returnValue($parentFolderFixture));
+
+               $currentFolderFixture = $this->createFolderFixture($currentIdentifier, 'current', $mockedStorage);
+
+               $this->assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
+       }
 }
index 0434dcf..0ef2a27 100644 (file)
@@ -411,9 +411,7 @@ class FileList extends \TYPO3\CMS\Backend\RecordList\AbstractRecordList {
                $levelUp = '';
                try {
                        $currentStorage = $currentFolder->getStorage();
-                       $parentFolder = $currentStorage->getFolder(
-                               $currentStorage->getFolderIdentifierFromFileIdentifier($this->folderObject->getIdentifier())
-                       );
+                       $parentFolder = $currentFolder->getParentFolder();
                        if ($parentFolder->getIdentifier() !== $currentFolder->getIdentifier() && $currentStorage->isWithinFileMountBoundaries($parentFolder)) {
                                $levelUp = $this->linkWrapDir(
                                        IconUtility::getSpriteIcon(