[TASK] Allow Folder and Storage to recursively retrieve Files from Driver
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / FolderTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 require_once 'vfsStream/vfsStream.php';
31
32 /**
33 * Testcase for the storage collection class of the TYPO3 FAL
34 *
35 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
36 */
37 class FolderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
38
39 /**
40 * @var array A backup of registered singleton instances
41 */
42 protected $singletonInstances = array();
43
44 private $basedir = 'basedir';
45
46 public function setUp() {
47 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
48 \vfsStream::setup($this->basedir);
49 }
50
51 protected function tearDown() {
52 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
53 }
54
55 protected function createFolderFixture($path, $name, $mockedStorage = NULL) {
56 if ($mockedStorage === NULL) {
57 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
58 }
59 return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $path, $name, 0);
60 }
61
62 /**
63 * @test
64 */
65 public function constructorArgumentsAreAvailableAtRuntime() {
66 $path = uniqid();
67 $name = uniqid();
68 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
69 $fixture = $this->createFolderFixture($path, $name, $mockedStorage);
70 $this->assertSame($mockedStorage, $fixture->getStorage());
71 $this->assertStringStartsWith($path, $fixture->getIdentifier());
72 $this->assertEquals($name, $fixture->getName());
73 }
74
75 /**
76 * @test
77 */
78 public function propertiesCanBeUpdated() {
79 $fixture = $this->createFolderFixture('/somePath', 'someName');
80 $fixture->updateProperties(array('identifier' => '/someOtherPath', 'name' => 'someNewName'));
81 $this->assertEquals('someNewName', $fixture->getName());
82 $this->assertEquals('/someOtherPath', $fixture->getIdentifier());
83 }
84
85 /**
86 * @test
87 */
88 public function propertiesAreNotUpdatedIfNotSetInInput() {
89 $fixture = $this->createFolderFixture('/somePath/someName/', 'someName');
90 $fixture->updateProperties(array('identifier' => '/someOtherPath'));
91 $this->assertEquals('someName', $fixture->getName());
92 }
93
94 /**
95 * @test
96 */
97 public function getFilesReturnsArrayWithFilenamesAsKeys() {
98 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
99 $mockedStorage->expects($this->once())->method('getFileList')->will($this->returnValue(array(
100 'somefile.png' => array(
101 'name' => 'somefile.png'
102 ),
103 'somefile.jpg' => array(
104 'name' => 'somefile.jpg'
105 )
106 )
107 ));
108 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
109
110 $fileList = $fixture->getFiles();
111
112 $this->assertEquals(array('somefile.png', 'somefile.jpg'), array_keys($fileList));
113 }
114
115 /**
116 * @test
117 */
118 public function getFilesHandsOverRecursiveFALSEifNotExplicitlySet() {
119 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
120 $mockedStorage
121 ->expects($this->once())
122 ->method('getFileList')
123 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
124 ->will($this->returnValue(array()));
125
126 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
127 $fixture->getFiles();
128 }
129
130 /**
131 * @test
132 */
133 public function getFilesHandsOverRecursiveTRUEifSet() {
134 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
135 $mockedStorage
136 ->expects($this->once())
137 ->method('getFileList')
138 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
139 ->will($this->returnValue(array()));
140
141 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
142 $fixture->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, TRUE);
143 }
144
145 /**
146 * @test
147 */
148 public function getSubfolderCallsFactoryWithCorrectArguments() {
149 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
150 $mockedStorage->expects($this->once())->method('hasFolderInFolder')->with($this->equalTo('someSubfolder'))->will($this->returnValue(TRUE));
151 $mockedFactory = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
152 $mockedFactory->expects($this->once())->method('createFolderObject')->with($mockedStorage, '/somePath/someFolder/someSubfolder/', 'someSubfolder');
153 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', $mockedFactory);
154 $fixture = $this->createFolderFixture('/somePath/someFolder/', 'someFolder', $mockedStorage);
155 $fixture->getSubfolder('someSubfolder');
156 }
157
158 }
159
160 ?>