TYPO3 CMS  TYPO3_6-2
LocalDriverTest.php
Go to the documentation of this file.
1 <?php
3 
18 use \org\bovigo\vfs\vfsStream;
19 use \org\bovigo\vfs\vfsStreamWrapper;
20 
27 
31  protected $localDriver = NULL;
32 
36  protected $singletonInstances = array();
37 
41  protected $testDirs = array();
42 
46  protected $iso88591GreaterThan127 = '';
47 
51  protected $utf8Latin1Supplement = '';
52 
56  protected $utf8Latin1ExtendedA = '';
57 
61  public function tearDown() {
62  foreach ($this->testDirs as $dir) {
63  chmod($dir, 0777);
65  }
66  parent::tearDown();
67  }
68 
77  protected function createRealTestdir() {
78  $basedir = PATH_site . 'typo3temp/' . $this->getUniqueId('fal-test-');
79  mkdir($basedir);
80  $this->testDirs[] = $basedir;
81  return $basedir;
82  }
83 
90  protected function prepareRealTestEnvironment() {
91  $basedir = $this->createRealTestdir();
92  $fixture = $this->createDriverFixture(array(
93  'basePath' => $basedir
94  ));
95  return array($basedir, $fixture);
96  }
97 
107  protected function createDriverFixture($driverConfiguration = array(), $mockedDriverMethods = array()) {
108  // it's important to do that here, so vfsContents could have been set before
109  if (!isset($driverConfiguration['basePath'])) {
110  $this->initializeVfs();
111  $driverConfiguration['basePath'] = $this->getMountRootUrl();
112  }
114  $mockedDriverMethods[] = 'isPathValid';
115  $driver = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', $mockedDriverMethods, array($driverConfiguration));
116  $driver->expects($this->any())
117  ->method('isPathValid')
118  ->will(
119  $this->returnValue(TRUE)
120  );
121 
122  $driver->setStorageUid(5);
123  $driver->processConfiguration();
124  $driver->initialize();
125  return $driver;
126  }
127 
132  $fixture = $this->createDriverFixture();
133 
134  // This would cause problems if you fill "/fileadmin/" into the base path field of a sys_file_storage record and select "relative" as path type
135  $relativeDriverConfiguration = array(
136  'pathType' => 'relative',
137  'basePath' => '/typo3temp/',
138  );
139  $basePath = $fixture->_call('calculateBasePath', $relativeDriverConfiguration);
140 
141  $this->assertNotContains('//', $basePath);
142  }
143 
148  $fixture = $this->createDriverFixture();
149 
150  // This test checks if "/../" are properly filtered out (i.e. from "Base path" field of sys_file_storage)
151  $relativeDriverConfiguration = array(
152  'basePath' => PATH_site . 'typo3temp/../typo3temp/',
153  );
154  $basePath = $fixture->_call('calculateBasePath', $relativeDriverConfiguration);
155 
156  $this->assertNotContains('/../', $basePath);
157  }
158 
162  public function createFolderRecursiveSanitizesFilename() {
164  $driver = $this->createDriverFixture(array(), array('sanitizeFilename'));
165  $driver->expects($this->exactly(2))
166  ->method('sanitizeFileName')
167  ->will(
168  $this->returnValue('sanitized')
169  );
170  $driver->createFolder('newFolder/andSubfolder', '/', TRUE);
171  $this->assertFileExists($this->getUrlInMount('/sanitized/sanitized/'));
172  }
173 
177  public function determineBaseUrlUrlEncodesUriParts() {
179  $driver = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('hasCapability'), array(), '', FALSE);
180  $driver->expects($this->once())
181  ->method('hasCapability')
182  ->with(\TYPO3\CMS\Core\Resource\ResourceStorage::CAPABILITY_PUBLIC)
183  ->will(
184  $this->returnValue(TRUE)
185  );
186  $driver->_set('absoluteBasePath', PATH_site . 'un encö/ded %path/');
187  $driver->_call('determineBaseUrl');
188  $baseUri = $driver->_get('baseUri');
189  $this->assertEquals(rawurlencode('un encö') . '/' . rawurlencode('ded %path') . '/', $baseUri);
190  }
191 
196  $fixture = $this->createDriverFixture();
197  $folderIdentifier = $fixture->getDefaultFolder();
198  $this->assertEquals('/user_upload/', $folderIdentifier);
199  }
200 
205  $fixture = $this->createDriverFixture();
206  $this->assertFileExists($this->getUrlInMount($fixture->getDefaultFolder()));
207  }
208 
213  $this->addToMount(array(
214  'someDir' => array(
215  'someSubdir' => array()
216  )
217  ));
218  $fixture = $this->createDriverFixture();
219  $folder = $fixture->getFolderInFolder('someSubdir', '/someDir/');
220  $this->assertEquals('/someDir/someSubdir/', $folder);
221  }
222 
227  $this->addToMount(array('some' => array('folder' => array())));
228  $fixture = $this->createDriverFixture();
229  $fixture->createFolder('path', '/some/folder/');
230  $this->assertFileExists($this->getUrlInMount('/some/folder/'));
231  $this->assertFileExists($this->getUrlInMount('/some/folder/path'));
232  }
233 
238  $this->addToMount(array('some' => array('folder' => array())));
239  $fixture = $this->createDriverFixture();
240  $createdFolder = $fixture->createFolder('path', '/some/folder/');
241  $this->assertEquals('/some/folder/path/', $createdFolder);
242  }
243 
245  return array(
246  'folder name with NULL character' => array(
247  'some' . chr(0) . 'Folder',
248  'some_Folder'
249  ),
250  'folder name with directory part' => array(
251  '../someFolder',
252  '.._someFolder'
253  )
254  );
255  }
256 
261  public function createFolderSanitizesFolderNameBeforeCreation($newFolderName, $expectedFolderName) {
262  $this->addToMount(array('some' => array('folder' => array())));
263  $fixture = $this->createDriverFixture();
264  $fixture->createFolder($newFolderName, '/some/folder/');
265  $this->assertFileExists($this->getUrlInMount('/some/folder/' . $expectedFolderName));
266  }
267 
272  $fixture = $this->createDriverFixture();
273  $this->assertEquals('/', substr($fixture->_call('getAbsoluteBasePath'), -1));
274  }
275 
280  $fixture = $this->createDriverFixture();
281  $this->assertNotEquals('/', substr($fixture->_call('getAbsoluteBasePath'), -2, 1));
282  }
283 
288  $this->addToMount(array(
289  'someFolder' => array(
290  'file1.ext' => 'asdfg'
291  )
292  ));
293  $fixture = $this->createDriverFixture();
294  $path = $fixture->_call('getAbsolutePath', '/someFolder/file1.ext');
295  $this->assertTrue(file_exists($path));
296  $this->assertEquals($this->getUrlInMount('/someFolder/file1.ext'), $path);
297  }
298 
303  $this->addToMount(array('targetFolder' => array()));
304  $this->addToVfs(array(
305  'sourceFolder' => array(
306  'file' => 'asdf'
307  )
308  ));
309  $fixture = $this->createDriverFixture(
310  array(),
311  array('getMimeTypeOfFile')
312  );
313  $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
314  $fixture->addFile($this->getUrl('sourceFolder/file'), '/targetFolder/', 'file');
315  $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/file')));
316  }
317 
321  public function addFileUsesFilenameIfGiven() {
322  $this->addToMount(array('targetFolder' => array()));
323  $this->addToVfs(array(
324  'sourceFolder' => array(
325  'file' => 'asdf'
326  )
327  ));
328  $fixture = $this->createDriverFixture(
329  array(),
330  array('getMimeTypeOfFile')
331  );
332  $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
333  $fixture->addFile($this->getUrl('sourceFolder/file'), '/targetFolder/', 'targetFile');
334  $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/targetFile')));
335  }
336 
341  $this->setExpectedException('InvalidArgumentException', '', 1314778269);
342  $this->addToMount(array(
343  'targetFolder' => array(
344  'file' => 'asdf'
345  )
346  ));
347  $fixture = $this->createDriverFixture();
348  $fixture->addFile($this->getUrlInMount('/targetFolder/file'), '/targetFolder/', 'file');
349  }
350 
354  public function addFileReturnsFileIdentifier() {
355  $this->addToMount(array('targetFolder' => array()));
356  $this->addToVfs(array(
357  'sourceFolder' => array(
358  'file' => 'asdf'
359  )
360  ));
361  $fixture = $this->createDriverFixture(
362  array(),
363  array('getMimeTypeOfFile')
364  );
365  $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
366  $fileIdentifier = $fixture->addFile($this->getUrl('sourceFolder/file'), '/targetFolder/', 'file');
367  $this->assertEquals('file', basename($fileIdentifier));
368  $this->assertEquals('/targetFolder/file', $fileIdentifier);
369  }
370 
375  $this->addToMount(array(
376  'file' => 'asdf',
377  'folder' => array()
378  ));
379  $fixture = $this->createDriverFixture();
380  // Using slashes at the beginning of paths because they will be stored in the DB this way.
381  $this->assertTrue($fixture->fileExists('/file'));
382  $this->assertTrue($fixture->folderExists('/folder/'));
383  $this->assertFalse($fixture->fileExists('/nonexistingFile'));
384  $this->assertFalse($fixture->folderExists('/nonexistingFolder/'));
385  }
386 
391  $this->addToMount(array(
392  'subfolder' => array(
393  'file' => 'asdf',
394  'folder' => array()
395  )
396  ));
397  $fixture = $this->createDriverFixture();
398  $this->assertTrue($fixture->fileExistsInFolder('file', '/subfolder/'));
399  $this->assertTrue($fixture->folderExistsInFolder('folder', '/subfolder/'));
400  $this->assertFalse($fixture->fileExistsInFolder('nonexistingFile', '/subfolder/'));
401  $this->assertFalse($fixture->folderExistsInFolder('nonexistingFolder', '/subfolder/'));
402  }
403 
408  $baseUri = 'http://example.org/foobar/' . $this->getUniqueId();
409  $this->addToMount(array(
410  'file.ext' => 'asdf',
411  'subfolder' => array(
412  'file2.ext' => 'asdf'
413  )
414  ));
415  $fixture = $this->createDriverFixture(array(
416  'baseUri' => $baseUri
417  ));
418  $this->assertEquals($baseUri . '/file.ext', $fixture->getPublicUrl('/file.ext'));
419  $this->assertEquals($baseUri . '/subfolder/file2.ext', $fixture->getPublicUrl('/subfolder/file2.ext'));
420  }
421 
428  return array(
429  array('/single file with some special chars äüö!.txt'),
430  array('/on subfolder/with special chars äüö!.ext'),
431  array('/who names a file like !"§$%&()=?*+~"#\'´`<>-.ext'),
432  array('no leading slash !"§$%&()=?*+~#\'"´`"<>-.txt')
433  );
434  }
435 
441  $baseUri = 'http://example.org/foobar/' . $this->getUniqueId();
442  $fixture = $this->createDriverFixture(array(
443  'baseUri' => $baseUri
444  ));
445  $publicUrl = $fixture->getPublicUrl($fileIdentifier);
446  $this->assertTrue(GeneralUtility::isValidUrl($publicUrl), 'getPublicUrl did not return a valid URL:' . $publicUrl);
447  }
448 
453  $fileContents = 'asdf';
454  $this->addToMount(array(
455  'file.ext' => $fileContents
456  ));
457  $fixture = $this->createDriverFixture();
458  $this->assertEquals($fileContents, $fixture->getFileContents('/file.ext'), 'File contents could not be read');
459  $newFileContents = 'asdfgh';
460  $fixture->setFileContents('/file.ext', $newFileContents);
461  $this->assertEquals($newFileContents, $fixture->getFileContents('/file.ext'), 'New file contents could not be read.');
462  }
463 
468  $fileContents = 'asdf';
469  $this->addToMount(array(
470  'file.ext' => $fileContents
471  ));
472  $fixture = $this->createDriverFixture();
473  $newFileContents = 'asdfgh';
474  $bytesWritten = $fixture->setFileContents('/file.ext', $newFileContents);
475  $this->assertEquals(strlen($newFileContents), $bytesWritten);
476  }
477 
482  public function newFilesCanBeCreated() {
483  if (version_compare(PHP_VERSION, '5.4.0', '<')) {
484  $this->markTestSkipped('touch() does not work with vfsStream in PHP 5.3 and below.');
485  }
486  $fixture = $this->createDriverFixture();
487  $fixture->createFile('testfile.txt', '/');
488  $this->assertTrue($fixture->fileExists('/testfile.txt'));
489  }
490 
495  public function createdFilesAreEmpty() {
496  if (version_compare(PHP_VERSION, '5.4.0', '<')) {
497  $this->markTestSkipped('touch() does not work with vfsStream in PHP 5.3 and below.');
498  }
499  $fixture = $this->createDriverFixture();
500  $fixture->createFile('testfile.txt', '/');
501  $this->assertTrue($fixture->fileExists('/testfile.txt'));
502  $fileData = $fixture->getFileContents('/testfile.txt');
503  $this->assertEquals(0, strlen($fileData));
504  }
505 
509  public function createFileFixesPermissionsOnCreatedFile() {
510  if (TYPO3_OS == 'WIN') {
511  $this->markTestSkipped('createdFilesHaveCorrectRights() tests not available on Windows');
512  }
513 
514  // No one will use this as his default file create mask so we hopefully don't get any false positives
515  $testpattern = '0646';
516  $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = $testpattern;
517 
518  $this->addToMount(
519  array(
520  'someDir' => array()
521  )
522  );
524  list($basedir, $fixture) = $this->prepareRealTestEnvironment();
525  mkdir($basedir . '/someDir');
526  $fixture->createFile('testfile.txt', '/someDir');
527  $this->assertEquals($testpattern, decoct(fileperms($basedir . '/someDir/testfile.txt') & 0777));
528  }
529 
530  /**********************************
531  * File and directory listing
532  **********************************/
537  $this->addToMount(array(
538  'someDir' => array(
539  'someFile' => 'asdfg'
540  ),
541  'someFileAtRootLevel' => 'foobar'
542  ));
543  $fixture = $this->createDriverFixture(
544  array(),
545  array('getMimeTypeOfFile')
546  );
547  $subdirFileInfo = $fixture->getFileInfoByIdentifier('/someDir/someFile');
548  $this->assertEquals('/someDir/someFile', $subdirFileInfo['identifier']);
549  $rootFileInfo = $fixture->getFileInfoByIdentifier('/someFileAtRootLevel');
550  $this->assertEquals('/someFileAtRootLevel', $rootFileInfo['identifier']);
551  }
552 
557  $this->setExpectedException('InvalidArgumentException', '', 1314516809);
558  $fixture = $this->createDriverFixture();
559  $fixture->getFileInfoByIdentifier('/some/file/at/a/random/path');
560  }
561 
566  $fixture = $this->createDriverFixture();
567  $fileList = $fixture->getFilesInFolder('/');
568  $this->assertEmpty($fileList);
569  }
570 
575  $dirStructure = array(
576  'aDir' => array(),
577  'file1' => 'asdfg',
578  'file2' => 'fdsa'
579  );
580  $this->addToMount($dirStructure);
581  $fixture = $this->createDriverFixture(
582  array(),
583  // Mocked because finfo() can not deal with vfs streams and throws warnings
584  array('getMimeTypeOfFile')
585  );
586  $fileList = $fixture->getFilesInFolder('/');
587  $this->assertEquals(array('/file1', '/file2'), array_keys($fileList));
588  }
589 
594  $dirStructure = array(
595  'aDir' => array(
596  'file3' => 'asdfgh',
597  'subdir' => array(
598  'file4' => 'asklfjklasjkl'
599  )
600  ),
601  'file1' => 'asdfg',
602  'file2' => 'fdsa'
603  );
604  $this->addToMount($dirStructure);
605  $fixture = $this->createDriverFixture(
606  array(),
607  // Mocked because finfo() can not deal with vfs streams and throws warnings
608  array('getMimeTypeOfFile')
609  );
610  $fileList = $fixture->getFilesInFolder('/', 0, 0, TRUE);
611  $this->assertEquals(array('/aDir/subdir/file4', '/aDir/file3', '/file1', '/file2'), array_keys($fileList));
612  }
613 
618  $this->setExpectedException('InvalidArgumentException', '', 1314349666);
619  $this->addToMount(array('somefile' => ''));
620  $fixture = $this->createDriverFixture();
621  $fixture->getFilesInFolder('somedir/');
622  }
623 
628  $dirStructure = array(
629  'file2' => 'fdsa'
630  );
631  // register static callback to self
632  $callback = array(
633  array(
634  get_class($this),
635  'callbackStaticTestFunction'
636  )
637  );
638  $this->addToMount($dirStructure);
639  $fixture = $this->createDriverFixture();
640  // the callback function will throw an exception used to check if it was called with correct $itemName
641  $this->setExpectedException('InvalidArgumentException', '$itemName', 1336159604);
642  $fixture->getFilesInFolder('/', 0, 0, FALSE, $callback);
643  }
644 
653  static public function callbackStaticTestFunction() {
654  list($itemName) = func_get_args();
655  if ($itemName === 'file2') {
656  throw new \InvalidArgumentException('$itemName', 1336159604);
657  }
658  }
659 
664  $dirStructure = array(
665  'fileA' => 'asdfg',
666  'fileB' => 'fdsa'
667  );
668  $this->addToMount($dirStructure);
669  $fixture = $this->createDriverFixture(
670  array(),
671  // Mocked because finfo() can not deal with vfs streams and throws warnings
672  array('getMimeTypeOfFile')
673  );
674  $filterCallbacks = array(
675  array(
676  'TYPO3\CMS\Core\Tests\Unit\Resource\Driver\Fixtures\LocalDriverFilenameFilter',
677  'filterFilename',
678  ),
679  );
680  $fileList = $fixture->getFilesInFolder('/', 0, 0, FALSE, $filterCallbacks);
681  $this->assertNotContains('/fileA', array_keys($fileList));
682  }
683 
688  $dirStructure = array(
689  'dir1' => array(),
690  'dir2' => array(),
691  'file' => 'asdfg'
692  );
693  $this->addToMount($dirStructure);
694  $fixture = $this->createDriverFixture();
695  $fileList = $fixture->getFoldersInFolder('/');
696  $this->assertEquals(array('/dir1/', '/dir2/'), array_keys($fileList));
697  }
698 
703  $dirStructure = array(
704  '.someHiddenDir' => array(),
705  'aDir' => array(),
706  'file1' => ''
707  );
708  $this->addToMount($dirStructure);
709  $fixture = $this->createDriverFixture();
710 
711  $fileList = $fixture->getFoldersInFolder('/');
712 
713  $this->assertEquals(array('/.someHiddenDir/', '/aDir/'), array_keys($fileList));
714  }
715 
722  // we have to add .. and . manually, as these are not included in vfsStream directory listings (as opposed
723  // to normal file listings)
724  $this->addToMount(array(
725  '..' => array(),
726  '.' => array()
727  ));
728  $fixture = $this->createDriverFixture();
729  $fileList = $fixture->getFoldersInFolder('/');
730  $this->assertEmpty($fileList);
731  }
732 
737  $dirStructure = array(
738  'folderA' => array(),
739  'folderB' => array()
740  );
741  $this->addToMount($dirStructure);
742  $fixture = $this->createDriverFixture();
743  $filterCallbacks = array(
744  array(
745  'TYPO3\CMS\Core\Tests\Unit\Resource\Driver\Fixtures\LocalDriverFilenameFilter',
746  'filterFilename',
747  ),
748  );
749  $folderList = $fixture->getFoldersInFolder('/', 0, 0, $filterCallbacks);
750  $this->assertNotContains('folderA', array_keys($folderList));
751  }
752 
757  $this->setExpectedException('InvalidArgumentException', '', 1314349666);
758  $fixture = $this->createDriverFixture();
759  vfsStream::create(array($this->basedir => array('somefile' => '')));
760  $fixture->getFoldersInFolder('somedir/');
761  }
762 
766  public function hashReturnsCorrectHashes() {
767  $contents = '68b329da9893e34099c7d8ad5cb9c940';
768  $expectedMd5Hash = '8c67dbaf0ba22f2e7fbc26413b86051b';
769  $expectedSha1Hash = 'a60cd808ba7a0bcfa37fa7f3fb5998e1b8dbcd9d';
770  $this->addToMount(array('hashFile' => $contents));
771  $fixture = $this->createDriverFixture();
772  $this->assertEquals($expectedSha1Hash, $fixture->hash('/hashFile', 'sha1'));
773  $this->assertEquals($expectedMd5Hash, $fixture->hash('/hashFile', 'md5'));
774  }
775 
780  $this->setExpectedException('InvalidArgumentException', '', 1304964032);
781  $fixture = $this->createDriverFixture();
782  $fixture->hash('/hashFile', $this->getUniqueId());
783  }
784 
790  $fileContents = 'asdfgh';
791  $this->addToMount(array(
792  'someDir' => array(
793  'someFile' => $fileContents
794  )
795  ));
796  $fixture = $this->createDriverFixture(array(), array('copyFileToTemporaryPath'));
797  $fixture->expects($this->once())->method('copyFileToTemporaryPath');
798  $fixture->getFileForLocalProcessing('/someDir/someFile');
799  }
800 
805  $fileContents = 'asdfgh';
806  $this->addToMount(array(
807  'someDir' => array(
808  'someFile' => $fileContents
809  )
810  ));
811  $fixture = $this->createDriverFixture();
812  $filePath = $fixture->getFileForLocalProcessing('/someDir/someFile', FALSE);
813  $this->assertEquals($filePath, $this->getUrlInMount('someDir/someFile'));
814  }
815 
820  $fileContents = 'asdfgh';
821  $this->addToMount(array(
822  'someDir' => array(
823  'someFile' => $fileContents
824  )
825  ));
826  $fixture = $this->createDriverFixture();
827  $filePath = GeneralUtility::fixWindowsFilePath($fixture->_call('copyFileToTemporaryPath', '/someDir/someFile'));
828  $this->assertContains('/typo3temp/', $filePath);
829  $this->assertEquals($fileContents, file_get_contents($filePath));
830  }
831 
835  public function permissionsAreCorrectlyRetrievedForAllowedFile() {
837  list($basedir, $fixture) = $this->prepareRealTestEnvironment();
838  touch($basedir . '/someFile');
839  chmod($basedir . '/someFile', 448);
840  clearstatcache();
841  $this->assertEquals(array('r' => TRUE, 'w' => TRUE), $fixture->getPermissions('/someFile'));
842  }
843 
847  public function permissionsAreCorrectlyRetrievedForForbiddenFile() {
848  if (function_exists('posix_getegid') && posix_getegid() === 0) {
849  $this->markTestSkipped('Test skipped if run on linux as root');
850  } elseif (TYPO3_OS === 'WIN') {
851  $this->markTestSkipped('Test skipped if run on Windows system');
852  }
854  list($basedir, $fixture) = $this->prepareRealTestEnvironment();
855  touch($basedir . '/someForbiddenFile');
856  chmod($basedir . '/someForbiddenFile', 0);
857  clearstatcache();
858  $this->assertEquals(array('r' => FALSE, 'w' => FALSE), $fixture->getPermissions('/someForbiddenFile'));
859  }
860 
864  public function permissionsAreCorrectlyRetrievedForAllowedFolder() {
866  list($basedir, $fixture) = $this->prepareRealTestEnvironment();
867  mkdir($basedir . '/someFolder');
868  chmod($basedir . '/someFolder', 448);
869  clearstatcache();
870  $this->assertEquals(array('r' => TRUE, 'w' => TRUE), $fixture->getPermissions('/someFolder'));
871  }
872 
876  public function permissionsAreCorrectlyRetrievedForForbiddenFolder() {
877  if (function_exists('posix_getegid') && posix_getegid() === 0) {
878  $this->markTestSkipped('Test skipped if run on linux as root');
879  } elseif (TYPO3_OS === 'WIN') {
880  $this->markTestSkipped('Test skipped if run on Windows system');
881  }
883  list($basedir, $fixture) = $this->prepareRealTestEnvironment();
884  mkdir($basedir . '/someForbiddenFolder');
885  chmod($basedir . '/someForbiddenFolder', 0);
886  clearstatcache();
887  $result = $fixture->getPermissions('/someForbiddenFolder');
888  // Change permissions back to writable, so the sub-folder can be removed in tearDown
889  chmod($basedir . '/someForbiddenFolder', 0777);
890  $this->assertEquals(array('r' => FALSE, 'w' => FALSE), $result);
891  }
892 
899  $data = array();
900  // On some OS, the posix_* functions do not exits
901  if (function_exists('posix_getgid')) {
902  $data = array(
903  'current group, readable/writable' => array(
904  posix_getgid(),
905  48,
906  array('r' => TRUE, 'w' => TRUE)
907  ),
908  'current group, readable/not writable' => array(
909  posix_getgid(),
910  32,
911  array('r' => TRUE, 'w' => FALSE)
912  ),
913  'current group, not readable/not writable' => array(
914  posix_getgid(),
915  0,
916  array('r' => FALSE, 'w' => FALSE)
917  )
918  );
919  }
920  $data = array_merge_recursive($data, array(
921  'arbitrary group, readable/writable' => array(
922  vfsStream::GROUP_USER_1,
923  6,
924  array('r' => TRUE, 'w' => TRUE)
925  ),
926  'arbitrary group, readable/not writable' => array(
927  vfsStream::GROUP_USER_1,
928  436,
929  array('r' => TRUE, 'w' => FALSE)
930  ),
931  'arbitrary group, not readable/not writable' => array(
932  vfsStream::GROUP_USER_1,
933  432,
934  array('r' => FALSE, 'w' => FALSE)
935  )
936  ));
937  return $data;
938  }
939 
944  public function getFilePermissionsReturnsCorrectPermissionsForFilesNotOwnedByCurrentUser($group, $permissions, $expectedResult) {
945  if (TYPO3_OS === 'WIN') {
946  $this->markTestSkipped('Test skipped if run on Windows system');
947  }
948  $this->addToMount(array(
949  'testfile' => 'asdfg'
950  ));
951  $fixture = $this->createDriverFixture();
953  $fileObject = vfsStreamWrapper::getRoot()->getChild($this->mountDir)->getChild('testfile');
954  // just use an "arbitrary" user here - it is only important that
955  $fileObject->chown(vfsStream::OWNER_USER_1);
956  $fileObject->chgrp($group);
957  $fileObject->chmod($permissions);
958  $this->assertEquals($expectedResult, $fixture->getPermissions('/testfile'));
959  }
960 
965  $fixture = $this->createDriverFixture();
966  $this->assertTrue($fixture->isWithin('/someFolder/', '/someFolder/test.jpg'));
967  $this->assertTrue($fixture->isWithin('/someFolder/', '/someFolder/subFolder/test.jpg'));
968  $this->assertFalse($fixture->isWithin('/someFolder/', '/someFolderWithALongName/test.jpg'));
969  }
970 
975  $fixture = $this->createDriverFixture();
976  $this->assertTrue($fixture->isWithin('/someFolder/', '/someFolder/test.jpg'));
977  $this->assertTrue($fixture->isWithin('/someFolder/', '/someFolder/subfolder/'));
978  }
979 
980  /**********************************
981  * Copy/move file
982  **********************************/
983 
987  public function filesCanBeCopiedWithinStorage() {
988  $fileContents = $this->getUniqueId();
989  $this->addToMount(array(
990  'someFile' => $fileContents,
991  'targetFolder' => array()
992  ));
993  $fixture = $this->createDriverFixture(
994  array(),
995  array('getMimeTypeOfFile')
996  );
997  $fixture->copyFileWithinStorage('/someFile', '/targetFolder/', 'someFile');
998  $this->assertFileEquals($this->getUrlInMount('/someFile'), $this->getUrlInMount('/targetFolder/someFile'));
999  }
1000 
1004  public function filesCanBeMovedWithinStorage() {
1005  $fileContents = $this->getUniqueId();
1006  $this->addToMount(array(
1007  'targetFolder' => array(),
1008  'someFile' => $fileContents
1009  ));
1010  $fixture = $this->createDriverFixture();
1011  $newIdentifier = $fixture->moveFileWithinStorage('/someFile', '/targetFolder/', 'file');
1012  $this->assertEquals($fileContents, file_get_contents($this->getUrlInMount('/targetFolder/file')));
1013  $this->assertFileNotExists($this->getUrlInMount('/someFile'));
1014  $this->assertEquals('/targetFolder/file', $newIdentifier);
1015  }
1016 
1021  $fileContents = $this->getUniqueId();
1022  $this->addToMount(array(
1023  'targetFolder' => array(),
1024  'someFile' => $fileContents
1025  ));
1026  $fixture = $this->createDriverFixture(
1027  array(),
1028  // Mocked because finfo() can not deal with vfs streams and throws warnings
1029  array('getMimeTypeOfFile')
1030  );
1031  $newIdentifier = $fixture->moveFileWithinStorage('/someFile', '/targetFolder/', 'file');
1032  $fileMetadata = $fixture->getFileInfoByIdentifier($newIdentifier);
1033  $this->assertEquals($newIdentifier, $fileMetadata['identifier']);
1034  }
1035 
1036  public function renamingFiles_dataProvider() {
1037  return array(
1038  'file in subfolder' => array(
1039  array(
1040  'targetFolder' => array('file' => '')
1041  ),
1042  '/targetFolder/file',
1043  'newFile',
1044  '/targetFolder/newFile'
1045  ),
1046  'file in rootfolder' => array(
1047  array(
1048  'fileInRoot' => ''
1049  ),
1050  '/fileInRoot',
1051  'newFile',
1052  '/newFile'
1053  )
1054  );
1055  }
1056 
1061  public function renamingFilesChangesFilenameOnDisk(array $filesystemStructure, $oldFileIdentifier, $newFileName, $expectedNewIdentifier) {
1062  $this->addToMount($filesystemStructure);
1063  $fixture = $this->createDriverFixture();
1064  $newIdentifier = $fixture->renameFile($oldFileIdentifier, $newFileName);
1065  $this->assertFalse($fixture->fileExists($oldFileIdentifier));
1066  $this->assertTrue($fixture->fileExists($newIdentifier));
1067  $this->assertEquals($expectedNewIdentifier, $newIdentifier);
1068  }
1069 
1074  $this->setExpectedException('TYPO3\\CMS\\Core\\Resource\\Exception\\ExistingTargetFileNameException', '', 1320291063);
1075  $this->addToMount(array(
1076  'targetFolder' => array('file' => '', 'newFile' => '')
1077  ));
1078  $fixture = $this->createDriverFixture();
1079  $fixture->renameFile('/targetFolder/file', 'newFile');
1080  }
1081 
1087  public function renamingFolders_dataProvider() {
1088  return array(
1089  'folder in root folder' => array(
1090  array(
1091  'someFolder' => array()
1092  ),
1093  '/someFolder/',
1094  'newFolder',
1095  '/newFolder/'
1096  ),
1097  'file in subfolder' => array(
1098  array(
1099  'subfolder' => array(
1100  'someFolder' => array()
1101  )
1102  ),
1103  '/subfolder/someFolder/',
1104  'newFolder',
1105  '/subfolder/newFolder/'
1106  )
1107  );
1108  }
1109 
1114  public function renamingFoldersChangesFolderNameOnDisk(array $filesystemStructure, $oldFolderIdentifier, $newFolderName, $expectedNewIdentifier) {
1115  $this->addToMount($filesystemStructure);
1116  $fixture = $this->createDriverFixture();
1117  $mapping = $fixture->renameFolder($oldFolderIdentifier, $newFolderName);
1118  $this->assertFalse($fixture->folderExists($oldFolderIdentifier));
1119  $this->assertTrue($fixture->folderExists($expectedNewIdentifier));
1120  $this->assertEquals($expectedNewIdentifier, $mapping[$oldFolderIdentifier]);
1121  }
1122 
1127  $fileContents = 'asdfg';
1128  $this->addToMount(array(
1129  'sourceFolder' => array(
1130  'subFolder' => array('file' => $fileContents),
1131  'file2' => 'asdfg'
1132  )
1133  ));
1134  $fixture = $this->createDriverFixture();
1135  $mappingInformation = $fixture->renameFolder('/sourceFolder/', 'newFolder');
1136  $this->isTrue(is_array($mappingInformation));
1137  $this->assertEquals('/newFolder/', $mappingInformation['/sourceFolder/']);
1138  $this->assertEquals('/newFolder/file2', $mappingInformation['/sourceFolder/file2']);
1139  $this->assertEquals('/newFolder/subFolder/file', $mappingInformation['/sourceFolder/subFolder/file']);
1140  $this->assertEquals('/newFolder/subFolder/', $mappingInformation['/sourceFolder/subFolder/']);
1141  }
1142 
1147  $this->setExpectedException('\\RuntimeException', '', 1334160746);
1148  $this->addToMount(array(
1149  'sourceFolder' => array(
1150  'file' => 'asdfg'
1151  )
1152  ));
1153  $fixture = $this->createDriverFixture(array(), array('createIdentifierMap'));
1154  $fixture->expects($this->atLeastOnce())->method('createIdentifierMap')->will($this->throwException(new \TYPO3\CMS\Core\Resource\Exception\FileOperationErrorException()));
1155  $fixture->renameFolder('/sourceFolder/', 'newFolder');
1156  $this->assertFileExists($this->getUrlInMount('/sourceFolder/file'));
1157  }
1158 
1163  // This also prepares the next few tests, so add more info than required for this test
1164  $this->addToMount(array(
1165  'emptyFolder' => array()
1166  ));
1167  $fixture = $this->createDriverFixture();
1168  $this->assertTrue($fixture->isFolderEmpty('/emptyFolder/'));
1169  return $fixture;
1170  }
1171 
1176  $this->addToMount(array(
1177  'folderWithFile' => array(
1178  'someFile' => ''
1179  )
1180  ));
1181  $fixture = $this->createDriverFixture();
1182  $this->assertFalse($fixture->isFolderEmpty('/folderWithFile/'));
1183  }
1184 
1189  $this->addToMount(array(
1190  'folderWithSubfolder' => array(
1191  'someFolder' => array()
1192  )
1193  ));
1194  $fixture = $this->createDriverFixture();
1195  $this->assertFalse($fixture->isFolderEmpty('/folderWithSubfolder/'));
1196  }
1197 
1198  /**********************************
1199  * Copy/move folder
1200  **********************************/
1204  public function foldersCanBeMovedWithinStorage() {
1205  $fileContents = $this->getUniqueId();
1206  $this->addToMount(array(
1207  'sourceFolder' => array(
1208  'file' => $fileContents,
1209  ),
1210  'targetFolder' => array(),
1211  ));
1212  $fixture = $this->createDriverFixture();
1214  $fixture->moveFolderWithinStorage('/sourceFolder/', '/targetFolder/', 'someFolder');
1215  $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/someFolder/')));
1216  $this->assertEquals($fileContents, file_get_contents($this->getUrlInMount('/targetFolder/someFolder/file')));
1217  $this->assertFileNotExists($this->getUrlInMount('/sourceFolder'));
1218  }
1219 
1224  $fileContents = 'asdfg';
1225  $this->addToMount(array(
1226  'targetFolder' => array(),
1227  'sourceFolder' => array(
1228  'subFolder' => array('file' => $fileContents),
1229  'file' => 'asdfg'
1230  )
1231  ));
1232  $fixture = $this->createDriverFixture();
1233  $mappingInformation = $fixture->moveFolderWithinStorage('/sourceFolder/', '/targetFolder/', 'sourceFolder');
1234  $this->assertEquals('/targetFolder/sourceFolder/file', $mappingInformation['/sourceFolder/file']);
1235  $this->assertEquals('/targetFolder/sourceFolder/subFolder/file', $mappingInformation['/sourceFolder/subFolder/file']);
1236  $this->assertEquals('/targetFolder/sourceFolder/subFolder/', $mappingInformation['/sourceFolder/subFolder/']);
1237  }
1238 
1242  public function folderCanBeRenamedWhenMoving() {
1243  $this->addToMount(array(
1244  'sourceFolder' => array(
1245  'file' => $this->getUniqueId(),
1246  ),
1247  'targetFolder' => array(),
1248  ));
1249  $fixture = $this->createDriverFixture();
1250  $fixture->moveFolderWithinStorage('/sourceFolder/', '/targetFolder/', 'newFolder');
1251  $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/newFolder/')));
1252  }
1253 
1258  $this->addToMount(array(
1259  'sourceFolder' => array(
1260  'file' => $this->getUniqueId(),
1261  ),
1262  'targetFolder' => array(),
1263  ));
1264  $fixture = $this->createDriverFixture();
1265  $fixture->copyFolderWithinStorage('/sourceFolder/', '/targetFolder/', 'newFolderName');
1266  $this->assertTrue(is_file($this->getUrlInMount('/targetFolder/newFolderName/file')));
1267  }
1268 
1273  list($basePath, $fixture) = $this->prepareRealTestEnvironment();
1274  GeneralUtility::mkdir_deep($basePath, '/sourceFolder/subFolder');
1275  GeneralUtility::mkdir_deep($basePath, '/targetFolder');
1276 
1277  $fixture->copyFolderWithinStorage('/sourceFolder/', '/targetFolder/', 'newFolderName');
1278  $this->isTrue(is_dir($basePath . '/targetFolder/newFolderName/subFolder'));
1279  }
1280 
1285  list($basePath, $fixture) = $this->prepareRealTestEnvironment();
1286  GeneralUtility::mkdir_deep($basePath, '/sourceFolder/subFolder');
1287  GeneralUtility::mkdir_deep($basePath, '/targetFolder');
1288  file_put_contents($basePath . '/sourceFolder/subFolder/file', $this->getUniqueId());
1289  GeneralUtility::fixPermissions($basePath . '/sourceFolder/subFolder/file');
1290 
1291  $fixture->copyFolderWithinStorage('/sourceFolder/', '/targetFolder/', 'newFolderName');
1292  $this->assertTrue(is_file($basePath . '/targetFolder/newFolderName/subFolder/file'));
1293  }
1294 
1296  // Tests concerning sanitizeFileName
1298 
1302  public function setUpCharacterStrings() {
1303  // Generate string containing all characters for the iso8859-1 charset, charcode greater than 127
1304  $this->iso88591GreaterThan127 = '';
1305  for ($i = 0xA0; $i <= 0xFF; $i++) {
1306  $this->iso88591GreaterThan127 .= chr($i);
1307  }
1308 
1309  // Generate string containing all characters for the utf-8 Latin-1 Supplement (U+0080 to U+00FF)
1310  // without U+0080 to U+009F: control characters
1311  // Based on http://www.utf8-chartable.de/unicode-utf8-table.pl
1312  $this->utf8Latin1Supplement = '';
1313  for ($i = 0xA0; $i <= 0xBF; $i++) {
1314  $this->utf8Latin1Supplement .= chr(0xC2) . chr($i);
1315  }
1316  for ($i = 0x80; $i <= 0xBF; $i++) {
1317  $this->utf8Latin1Supplement .= chr(0xC3) . chr($i);
1318  }
1319 
1320  // Generate string containing all characters for the utf-8 Latin-1 Extended-A (U+0100 to U+017F)
1321  $this->utf8Latin1ExtendedA = '';
1322  for ($i = 0x80; $i <= 0xBF; $i++) {
1323  $this->utf8Latin1ExtendedA .= chr(0xC4) . chr($i);
1324  }
1325  for ($i = 0x80; $i <= 0xBF; $i++) {
1326  $this->utf8Latin1ExtendedA .= chr(0xC5) . chr($i);
1327  }
1328  }
1329 
1341  $this->setUpCharacterStrings();
1342  return array(
1343  // Characters ordered by ASCII table
1344  'allowed characters utf-8 (ASCII part)' => array(
1345  '-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz',
1346  '-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
1347  ),
1348  // Characters ordered by ASCII table (except for space-character, because space-character ist trimmed)
1349  'replace special characters with _ (not allowed characters) utf-8 (ASCII part)' => array(
1350  '! "#$%&\'()*+,/:;<=>?[\\]^`{|}~',
1351  '_____________________________'
1352  ),
1353  'utf-8 (Latin-1 Supplement)' => array(
1354  $this->utf8Latin1Supplement,
1355  '________________________________ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
1356  ),
1357  'trim leading and tailing spaces utf-8' => array(
1358  ' test.txt ',
1359  'test.txt'
1360  ),
1361  'remove tailing dot' => array(
1362  'test.txt.',
1363  'test.txt'
1364  ),
1365  );
1366  }
1367 
1372  public function sanitizeFileNameUTF8Filesystem($fileName, $expectedResult) {
1373  $GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem'] = 1;
1374  $this->assertEquals(
1375  $expectedResult,
1376  $this->createDriverFixture()->sanitizeFileName($fileName)
1377  );
1378  }
1379 
1380 
1392  $this->setUpCharacterStrings();
1393  return array(
1394  // Characters ordered by ASCII table
1395  'allowed characters iso-8859-1' => array(
1396  '-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz',
1397  'iso-8859-1',
1398  '-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
1399  ),
1400  // Characters ordered by ASCII table
1401  'allowed characters utf-8' => array(
1402  '-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz',
1403  'utf-8',
1404  '-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
1405  ),
1406  // Characters ordered by ASCII table (except for space-character, because space-character ist trimmed)
1407  'replace special characters with _ (not allowed characters) iso-8859-1' => array(
1408  '! "#$%&\'()*+,/:;<=>?[\\]^`{|}~',
1409  'iso-8859-1',
1410  '_____________________________'
1411  ),
1412  // Characters ordered by ASCII table (except for space-character, because space-character ist trimmed)
1413  'replace special characters with _ (not allowed characters) utf-8' => array(
1414  '! "#$%&\'()*+,/:;<=>?[\\]^`{|}~',
1415  'utf-8',
1416  '_____________________________'
1417  ),
1418  'iso-8859-1 (code > 127)' => array(
1419  // http://de.wikipedia.org/wiki/ISO_8859-1
1420  // chr(0xA0) = NBSP (no-break space) => gets trimmed
1421  $this->iso88591GreaterThan127,
1422  'iso-8859-1',
1423  '_centpound_yen____c_a_____R_____-23_u___1o__1_41_23_4_AAAAAEAAAECEEEEIIIIDNOOOOOExOEUUUUEYTHssaaaaaeaaaeceeeeiiiidnoooooe_oeuuuueythy'
1424  ),
1425  'utf-8 (Latin-1 Supplement)' => array(
1426  // chr(0xC2) . chr(0x0A) = NBSP (no-break space) => gets trimmed
1427  $this->utf8Latin1Supplement,
1428  'utf-8',
1429  '_centpound__yen______c_a_______R_______-23__u_____1o__1_41_23_4_AAAAAEAAAECEEEEIIIIDNOOOOOExOEUUUUEYTHssaaaaaeaaaeceeeeiiiidnoooooe_oeuuuueythy'
1430  ),
1431  'utf-8 (Latin-1 Extended A)' => array(
1432  $this->utf8Latin1ExtendedA,
1433  'utf-8',
1434  'AaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIiIJijJjKk__LlLlLlL_l_LlNnNnNn_n____OOooOoOoOEoeRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzs'
1435  ),
1436  'trim leading and tailing spaces iso-8859-1' => array(
1437  ' test.txt ',
1438  'iso-8859-1',
1439  'test.txt'
1440  ),
1441  'trim leading and tailing spaces utf-8' => array(
1442  ' test.txt ',
1443  'utf-8',
1444  'test.txt'
1445  ),
1446  'remove tailing dot iso-8859-1' => array(
1447  'test.txt.',
1448  'iso-8859-1',
1449  'test.txt'
1450  ),
1451  'remove tailing dot utf-8' => array(
1452  'test.txt.',
1453  'utf-8',
1454  'test.txt'
1455  ),
1456  );
1457  }
1458 
1463  public function sanitizeFileNameNonUTF8Filesystem($fileName, $charset, $expectedResult) {
1464  $GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem'] = 0;
1465  $this->assertEquals(
1466  $expectedResult,
1467  $this->createDriverFixture()->sanitizeFileName($fileName, $charset)
1468  );
1469  }
1470 
1476  $GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem'] = 1;
1477  $this->createDriverFixture()->sanitizeFileName('');
1478  }
1479 
1484  $this->setExpectedException('Exception', 'I was called!');
1485  $closure = function() {
1486  throw new \Exception('I was called!');
1487  };
1488 
1489  $filterMethods = array(
1490  $closure,
1491  );
1492 
1493  $this->createDriverFixture()->_call('applyFilterMethodsToDirectoryItem', $filterMethods, '', '', '');
1494  }
1495 
1500  $dummyObject = $this
1501  ->getMockBuilder('\TYPO3\CMS\Core\Resource\Driver\LocalDriver')
1502  ->setMethods(array('dummy'))
1503  ->disableOriginalConstructor()
1504  ->getMock();
1505  $method = array(
1506  $dummyObject,
1507  'dummy',
1508  );
1509  $dummyObject->expects($this->once())->method('dummy');
1510  $filterMethods = array(
1511  $method,
1512  );
1513  $this->createDriverFixture()->_call('applyFilterMethodsToDirectoryItem', $filterMethods, '', '', '');
1514  }
1515 
1516 }
static mkdir_deep($directory, $deepDirectory='')
$driver
Definition: server.php:34
static rmdir($path, $removeNonEmpty=FALSE)
sanitizeFileNameNonUTF8Filesystem($fileName, $charset, $expectedResult)
static fixPermissions($path, $recursive=FALSE)
renamingFilesChangesFilenameOnDisk(array $filesystemStructure, $oldFileIdentifier, $newFileName, $expectedNewIdentifier)
renamingFoldersChangesFolderNameOnDisk(array $filesystemStructure, $oldFolderIdentifier, $newFolderName, $expectedNewIdentifier)
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
createFolderSanitizesFolderNameBeforeCreation($newFolderName, $expectedFolderName)