[BUGFIX] Get folder object if a path is given
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / FactoryTest.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 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 require_once 'vfsStream/vfsStream.php';
28
29 /**
30 * Testcase for the factory of FAL
31 *
32 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
33 */
34 class FactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
35
36 /**
37 * @var array A backup of registered singleton instances
38 */
39 protected $singletonInstances = array();
40
41 /**
42 * @var \TYPO3\CMS\Core\Resource\ResourceFactory
43 */
44 private $fixture;
45
46 /**
47 * @var boolean
48 */
49 private $objectCreated = FALSE;
50
51 /**
52 * @var array
53 */
54 private $filesCreated = array();
55
56 public function setUp() {
57 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
58 $this->fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', array('dummy'));
59 }
60
61 public function tearDown() {
62 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
63 foreach ($this->filesCreated as $file) {
64 unlink($file);
65 }
66 }
67
68 /**********************************
69 * Storage Collections
70 **********************************/
71 /**
72 * @test
73 */
74 public function createStorageCollectionObjectCreatesCollectionWithCorrectArguments() {
75 $mockedMount = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
76 $path = uniqid();
77 $name = uniqid();
78 $storageCollection = $this->fixture->createFolderObject($mockedMount, $path, $name, 0);
79 $this->assertSame($mockedMount, $storageCollection->getStorage());
80 $this->assertEquals($path . '/', $storageCollection->getIdentifier());
81 $this->assertEquals($name, $storageCollection->getName());
82 }
83
84 /**********************************
85 * Drivers
86 **********************************/
87 /**
88 * @test
89 */
90 public function getDriverObjectAcceptsDriverClassName() {
91 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), array(), '', FALSE);
92 $driverFixtureClass = get_class($mockedDriver);
93 \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($driverFixtureClass, $mockedDriver);
94 $mockedMount = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
95 $mockedRegistry = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\DriverRegistry');
96 $mockedRegistry->expects($this->once())->method('getDriverClass')->with($this->equalTo($driverFixtureClass))->will($this->returnValue($driverFixtureClass));
97 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\Driver\\DriverRegistry', $mockedRegistry);
98 $obj = $this->fixture->getDriverObject($driverFixtureClass, array());
99 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', $obj);
100 }
101
102 /***********************************
103 * File Handling
104 ***********************************/
105
106 public function directoryDataProviderValidFolderValues() {
107 return array(
108 'relative path' => array('fileadmin'),
109 'path with PATH_site' => array(PATH_site . 'fileadmin')
110 );
111 }
112
113 /**
114 * @test
115 * @dataProvider directoryDataProviderValidFolderValues
116 */
117 public function retrieveFileOrFolderObjectReturnsFolderIfPathIsGiven($source) {
118 $storage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFile', 'getFolder'), array(), '', FALSE);
119 $storage->expects($this->once())
120 ->method('getFolder')
121 ->with('fileadmin');
122 $this->fixture->_set('storageInstances', array(0 => $storage));
123 $this->fixture->retrieveFileOrFolderObject($source);
124 }
125
126 /**
127 * @test
128 */
129 public function retrieveFileOrFolderObjectReturnsFileIfPathIsGiven() {
130 $filename = 'typo3temp/4711.txt';
131 $storage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFile', 'getFolder'), array(), '', FALSE);
132 $storage->expects($this->once())
133 ->method('getFile')
134 ->with($filename);
135 $this->fixture->_set('storageInstances', array(0 => $storage));
136 // Create and prepare test file
137 \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir(PATH_site . $filename, '42');
138 $this->filesCreated[] = PATH_site . $filename;
139 $this->fixture->retrieveFileOrFolderObject($filename);
140 }
141 }
142
143 ?>