TYPO3 CMS  TYPO3_8-7
FolderTest.php
Go to the documentation of this file.
1 <?php
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 
18 
22 class FolderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
27  protected $singletonInstances = [];
28 
29  protected $basedir = 'basedir';
30 
31  protected function setUp()
32  {
34  vfsStream::setup($this->basedir);
35  }
36 
37  protected function tearDown()
38  {
40  parent::tearDown();
41  }
42 
43  protected function createFolderFixture($path, $name, $mockedStorage = null)
44  {
45  if ($mockedStorage === null) {
46  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
47  }
48  return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $path, $name);
49  }
50 
55  {
56  $path = $this->getUniqueId();
57  $name = $this->getUniqueId();
58  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
59  $fixture = $this->createFolderFixture($path, $name, $mockedStorage);
60  $this->assertSame($mockedStorage, $fixture->getStorage());
61  $this->assertStringStartsWith($path, $fixture->getIdentifier());
62  $this->assertSame($name, $fixture->getName());
63  }
64 
68  public function propertiesCanBeUpdated()
69  {
70  $fixture = $this->createFolderFixture('/somePath', 'someName');
71  $fixture->updateProperties(['identifier' => '/someOtherPath', 'name' => 'someNewName']);
72  $this->assertSame('someNewName', $fixture->getName());
73  $this->assertSame('/someOtherPath', $fixture->getIdentifier());
74  }
75 
80  {
81  $fixture = $this->createFolderFixture('/somePath/someName/', 'someName');
82  $fixture->updateProperties(['identifier' => '/someOtherPath']);
83  $this->assertSame('someName', $fixture->getName());
84  }
85 
90  {
91  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
92  $mockedStorage->expects($this->once())->method('getFilesInFolder')->will($this->returnValue(
93  [
94  'somefile.png' => [
95  'name' => 'somefile.png'
96  ],
97  'somefile.jpg' => [
98  'name' => 'somefile.jpg'
99  ]
100  ]
101  ));
102  $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
103 
104  $fileList = $fixture->getFiles();
105 
106  $this->assertSame(['somefile.png', 'somefile.jpg'], array_keys($fileList));
107  }
108 
113  {
114  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
115  $mockedStorage
116  ->expects($this->once())
117  ->method('getFilesInFolder')
118  ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), false)
119  ->will($this->returnValue([]));
120 
121  $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
122  $fixture->getFiles();
123  }
124 
129  {
130  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
131  $mockedStorage
132  ->expects($this->once())
133  ->method('getFilesInFolder')
134  ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), true)
135  ->will($this->returnValue([]));
136 
137  $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
138  $fixture->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true);
139  }
140 
144  public function getSubfolderCallsFactoryWithCorrectArguments()
145  {
146  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
147  $mockedStorage->expects($this->once())->method('hasFolderInFolder')->with($this->equalTo('someSubfolder'))->will($this->returnValue(true));
149  $mockedFactory = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
150  $folderFixture = $this->createFolderFixture(
151  '/somePath/someFolder/',
152  'someFolder',
153  $mockedStorage
154  );
155  $subfolderFixture = $this->createFolderFixture(
156  '/somePath/someSubfolder/',
157  'someSubfolder',
158  $mockedStorage
159  );
160  $mockedStorage->expects($this->once())->method('getFolderInFolder')->will($this->returnValue($subfolderFixture));
162  \TYPO3\CMS\Core\Resource\ResourceFactory::class,
163  $mockedFactory
164  );
165  $this->assertEquals($subfolderFixture, $folderFixture->getSubfolder('someSubfolder'));
166  }
167 
172  {
173  $parentIdentifier = '/parent/';
174  $currentIdentifier = '/parent/current/';
175 
176  $parentFolderFixture = $this->createFolderFixture($parentIdentifier, 'parent');
177  $mockedStorage = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\ResourceStorage::class)
178  ->setMethods(['getFolderIdentifierFromFileIdentifier', 'getFolder'])
179  ->disableOriginalConstructor()
180  ->getMock();
181  $mockedStorage->expects($this->once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->will($this->returnValue($parentIdentifier));
182  $mockedStorage->expects($this->once())->method('getFolder')->with($parentIdentifier)->will($this->returnValue($parentFolderFixture));
183 
184  $currentFolderFixture = $this->createFolderFixture($currentIdentifier, 'current', $mockedStorage);
185 
186  $this->assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
187  }
188 }
static setSingletonInstance($className, SingletonInterface $instance)
static resetSingletonInstances(array $newSingletonInstances)
const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:68
createFolderFixture($path, $name, $mockedStorage=null)
Definition: FolderTest.php:43