[TASK] Re-work/simplify copyright header in PHP files - Part 4
[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 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use \org\bovigo\vfs\vfsStream;
18
19 /**
20 * Testcase for the storage collection class of the TYPO3 FAL
21 *
22 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
23 */
24 class FolderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
25
26 /**
27 * @var array A backup of registered singleton instances
28 */
29 protected $singletonInstances = array();
30
31 protected $basedir = 'basedir';
32
33 public function setUp() {
34 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
35 vfsStream::setup($this->basedir);
36 }
37
38 protected function tearDown() {
39 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
40 parent::tearDown();
41 }
42
43 protected function createFolderFixture($path, $name, $mockedStorage = NULL) {
44 if ($mockedStorage === NULL) {
45 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
46 }
47 return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $path, $name, 0);
48 }
49
50 /**
51 * @test
52 */
53 public function constructorArgumentsAreAvailableAtRuntime() {
54 $path = uniqid();
55 $name = uniqid();
56 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
57 $fixture = $this->createFolderFixture($path, $name, $mockedStorage);
58 $this->assertSame($mockedStorage, $fixture->getStorage());
59 $this->assertStringStartsWith($path, $fixture->getIdentifier());
60 $this->assertSame($name, $fixture->getName());
61 }
62
63 /**
64 * @test
65 */
66 public function propertiesCanBeUpdated() {
67 $fixture = $this->createFolderFixture('/somePath', 'someName');
68 $fixture->updateProperties(array('identifier' => '/someOtherPath', 'name' => 'someNewName'));
69 $this->assertSame('someNewName', $fixture->getName());
70 $this->assertSame('/someOtherPath', $fixture->getIdentifier());
71 }
72
73 /**
74 * @test
75 */
76 public function propertiesAreNotUpdatedIfNotSetInInput() {
77 $fixture = $this->createFolderFixture('/somePath/someName/', 'someName');
78 $fixture->updateProperties(array('identifier' => '/someOtherPath'));
79 $this->assertSame('someName', $fixture->getName());
80 }
81
82 /**
83 * @test
84 */
85 public function getFilesReturnsArrayWithFilenamesAsKeys() {
86 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
87 $mockedStorage->expects($this->once())->method('getFilesInFolder')->will($this->returnValue(array(
88 'somefile.png' => array(
89 'name' => 'somefile.png'
90 ),
91 'somefile.jpg' => array(
92 'name' => 'somefile.jpg'
93 )
94 )
95 ));
96 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
97
98 $fileList = $fixture->getFiles();
99
100 $this->assertSame(array('somefile.png', 'somefile.jpg'), array_keys($fileList));
101 }
102
103 /**
104 * @test
105 */
106 public function getFilesHandsOverRecursiveFALSEifNotExplicitlySet() {
107 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
108 $mockedStorage
109 ->expects($this->once())
110 ->method('getFilesInFolder')
111 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
112 ->will($this->returnValue(array()));
113
114 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
115 $fixture->getFiles();
116 }
117
118 /**
119 * @test
120 */
121 public function getFilesHandsOverRecursiveTRUEifSet() {
122 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
123 $mockedStorage
124 ->expects($this->once())
125 ->method('getFilesInFolder')
126 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
127 ->will($this->returnValue(array()));
128
129 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
130 $fixture->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, TRUE);
131 }
132
133 /**
134 * @test
135 */
136 public function getSubfolderCallsFactoryWithCorrectArguments() {
137 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
138 $mockedStorage->expects($this->once())->method('hasFolderInFolder')->with($this->equalTo('someSubfolder'))->will($this->returnValue(TRUE));
139 $mockedFactory = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
140 $mockedFactory->expects($this->once())->method('createFolderObject')->with($mockedStorage, '/somePath/someFolder/someSubfolder/', 'someSubfolder');
141 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', $mockedFactory);
142 $fixture = $this->createFolderFixture('/somePath/someFolder/', 'someFolder', $mockedStorage);
143 $fixture->getSubfolder('someSubfolder');
144 }
145
146 /**
147 * @test
148 */
149 public function getParentFolderGetsParentFolderFromStorage() {
150 $parentIdentifier = '/parent/';
151 $currentIdentifier = '/parent/current/';
152
153 $parentFolderFixture = $this->createFolderFixture($parentIdentifier, 'parent');
154 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFolderIdentifierFromFileIdentifier', 'getFolder'), array(), '', FALSE);
155 $mockedStorage->expects($this->once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->will($this->returnValue($parentIdentifier));
156 $mockedStorage->expects($this->once())->method('getFolder')->with($parentIdentifier)->will($this->returnValue($parentFolderFixture));
157
158 $currentFolderFixture = $this->createFolderFixture($currentIdentifier, 'current', $mockedStorage);
159
160 $this->assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
161 }
162 }