TYPO3 CMS  TYPO3_6-2
ResourceFactoryTest.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $singletonInstances = array();
28 
32  protected $subject;
33 
37  protected $filesCreated = array();
38 
39  public function setUp() {
41  $this->subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', array('dummy'), array(), '', FALSE);
42  }
43 
44  public function tearDown() {
46  foreach ($this->filesCreated as $file) {
47  unlink($file);
48  }
49  parent::tearDown();
50  }
51 
52  /**********************************
53  * Storage Collections
54  **********************************/
59  $mockedMount = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
60  $path = $this->getUniqueId();
61  $name = $this->getUniqueId();
62  $storageCollection = $this->subject->createFolderObject($mockedMount, $path, $name, 0);
63  $this->assertSame($mockedMount, $storageCollection->getStorage());
64  $this->assertEquals($path . '/', $storageCollection->getIdentifier());
65  $this->assertEquals($name, $storageCollection->getName());
66  }
67 
68  /**********************************
69  * Drivers
70  **********************************/
75  $mockedDriver = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver');
76  $driverFixtureClass = get_class($mockedDriver);
77  \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($driverFixtureClass, $mockedDriver);
78  $mockedMount = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
79  $mockedRegistry = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\DriverRegistry');
80  $mockedRegistry->expects($this->once())->method('getDriverClass')->with($this->equalTo($driverFixtureClass))->will($this->returnValue($driverFixtureClass));
81  \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\Driver\\DriverRegistry', $mockedRegistry);
82  $obj = $this->subject->getDriverObject($driverFixtureClass, array());
83  $this->assertInstanceOf('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', $obj);
84  }
85 
86  /***********************************
87  * File Handling
88  ***********************************/
89 
93  public function retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithRelativePath() {
95  $subject = $this->getAccessibleMock(
96  'TYPO3\\CMS\\Core\\Resource\\ResourceFactory',
97  array('getFolderObjectFromCombinedIdentifier'),
98  array(),
99  '',
100  FALSE
101  );
102  $subject
103  ->expects($this->once())
104  ->method('getFolderObjectFromCombinedIdentifier')
105  ->with('typo3');
106  $subject->retrieveFileOrFolderObject('typo3');
107  }
108 
112  public function retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithAbsolutePath() {
114  $subject = $this->getAccessibleMock(
115  'TYPO3\\CMS\\Core\\Resource\\ResourceFactory',
116  array('getFolderObjectFromCombinedIdentifier'),
117  array(),
118  '',
119  FALSE
120  );
121  $subject
122  ->expects($this->once())
123  ->method('getFolderObjectFromCombinedIdentifier')
124  ->with('typo3');
125  $subject->retrieveFileOrFolderObject(PATH_site . 'typo3');
126  }
127 
132  $this->subject = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', array('getFileObjectFromCombinedIdentifier'), array(), '', FALSE);
133  $filename = 'typo3temp/4711.txt';
134  $this->subject->expects($this->once())
135  ->method('getFileObjectFromCombinedIdentifier')
136  ->with($filename);
137  // Create and prepare test file
139  $this->filesCreated[] = PATH_site . $filename;
140  $this->subject->retrieveFileOrFolderObject($filename);
141  }
142 
143  /***********************************
144  * Storage AutoDetection
145  ***********************************/
146 
155  public function findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId) {
156  $this->subject->_set('localDriverStorageCache', $storageConfiguration);
157  $this->assertSame($expectedStorageId, $this->subject->_callRef('findBestMatchingStorageByLocalPath', $path));
158  }
159 
160 
161 
165  public function storageDetectionDataProvider() {
166  return array(
167  'NoLocalStoragesReturnDefaultStorage' => array(
168  array(),
169  'my/dummy/Image.png',
170  0
171  ),
172  'NoMatchReturnsDefaultStorage' => array(
173  array(1 => 'fileadmin/', 2 => 'fileadmin2/public/'),
174  'my/dummy/Image.png',
175  0
176  ),
177  'MatchReturnsTheMatch' => array(
178  array(1 => 'fileadmin/', 2 => 'other/public/'),
179  'fileadmin/dummy/Image.png',
180  1
181  ),
182  'TwoFoldersWithSameStartReturnsCorrect' => array(
183  array(1 => 'fileadmin/', 2 => 'fileadmin/public/'),
184  'fileadmin/dummy/Image.png',
185  1
186  ),
187  'NestedStorageReallyReturnsTheBestMatching' => array(
188  array(1 => 'fileadmin/', 2 => 'fileadmin/public/'),
189  'fileadmin/public/Image.png',
190  2
191  ),
192  'CommonPrefixButWrongPath' => array(
193  array(1 => 'fileadmin/', 2 => 'uploads/test/'),
194  'uploads/bogus/dummy.png',
195  0
196  ),
197  'CommonPrefixRightPath' => array(
198  array(1 => 'fileadmin/', 2 => 'uploads/test/'),
199  'uploads/test/dummy.png',
200  2
201  ),
202  'FindStorageFromWindowsPath' => array(
203  array(1 => 'fileadmin/', 2 => 'uploads/test/'),
204  'uploads\\test\\dummy.png',
205  2
206  ),
207  );
208  }
209 }
static setSingletonInstance($className, \TYPO3\CMS\Core\SingletonInterface $instance)
static addInstance($className, $instance)
static writeFileToTypo3tempDir($filepath, $content)
static resetSingletonInstances(array $newSingletonInstances)
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)