2012-02-12 12:06:32 -05:00
< ? php
2025-06-30 09:04:05 -04:00
2012-02-12 12:06:32 -05:00
/**
2024-05-10 09:09:14 -04:00
* SPDX - FileCopyrightText : 2016 - 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - or - later
2012-10-11 13:30:27 -04:00
*/
2012-02-12 12:06:32 -05:00
2012-09-22 08:51:15 -04:00
namespace Test\Files\Storage ;
2015-04-27 12:03:04 -04:00
use OC\Files\Cache\Watcher ;
2025-06-12 12:31:58 -04:00
use OC\Files\Storage\Wrapper\Wrapper ;
2024-09-15 09:38:25 -04:00
use OCP\Files\Storage\IStorage ;
2018-10-31 14:41:55 -04:00
use OCP\Files\Storage\IWriteStreamStorage ;
2015-04-27 12:03:04 -04:00
2014-11-10 16:28:12 -05:00
abstract class Storage extends \Test\TestCase {
2012-02-12 12:06:32 -05:00
/**
2012-09-07 12:30:48 -04:00
* @ var \OC\Files\Storage\Storage instance
2012-02-12 12:06:32 -05:00
*/
protected $instance ;
2014-01-31 10:06:11 -05:00
protected $waitDelay = 0 ;
/**
* Sleep for the number of seconds specified in the
* $waitDelay attribute
*/
protected function wait () {
if ( $this -> waitDelay > 0 ) {
sleep ( $this -> waitDelay );
}
}
2012-02-12 12:06:32 -05:00
/**
* the root folder of the storage should always exist , be readable and be recognized as a directory
*/
2024-09-15 16:32:31 -04:00
public function testRoot () : void {
2012-10-11 13:30:27 -04:00
$this -> assertTrue ( $this -> instance -> file_exists ( '/' ), 'Root folder does not exist' );
$this -> assertTrue ( $this -> instance -> isReadable ( '/' ), 'Root folder is not readable' );
$this -> assertTrue ( $this -> instance -> is_dir ( '/' ), 'Root folder is not a directory' );
$this -> assertFalse ( $this -> instance -> is_file ( '/' ), 'Root folder is a file' );
2013-01-24 10:47:17 -05:00
$this -> assertEquals ( 'dir' , $this -> instance -> filetype ( '/' ));
2012-10-11 13:30:27 -04:00
2012-10-11 16:54:39 -04:00
//without this, any further testing would be useless, not an actual requirement for filestorage though
2012-10-11 13:30:27 -04:00
$this -> assertTrue ( $this -> instance -> isUpdatable ( '/' ), 'Root folder is not writable' );
2012-02-12 12:06:32 -05:00
}
2012-10-11 13:30:27 -04:00
2013-12-03 08:35:53 -05:00
/**
* Check that the test () function works
*/
2024-09-15 16:32:31 -04:00
public function testTestFunction () : void {
2013-12-03 08:35:53 -05:00
$this -> assertTrue ( $this -> instance -> test ());
}
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('directoryProvider')]
2024-09-15 16:32:31 -04:00
public function testDirectories ( $directory ) : void {
2014-03-20 10:32:12 -04:00
$this -> assertFalse ( $this -> instance -> file_exists ( '/' . $directory ));
2012-10-11 13:30:27 -04:00
2014-03-20 10:32:12 -04:00
$this -> assertTrue ( $this -> instance -> mkdir ( '/' . $directory ));
2012-10-11 13:30:27 -04:00
2014-03-20 10:32:12 -04:00
$this -> assertTrue ( $this -> instance -> file_exists ( '/' . $directory ));
$this -> assertTrue ( $this -> instance -> is_dir ( '/' . $directory ));
$this -> assertFalse ( $this -> instance -> is_file ( '/' . $directory ));
$this -> assertEquals ( 'dir' , $this -> instance -> filetype ( '/' . $directory ));
$this -> assertEquals ( 0 , $this -> instance -> filesize ( '/' . $directory ));
$this -> assertTrue ( $this -> instance -> isReadable ( '/' . $directory ));
$this -> assertTrue ( $this -> instance -> isUpdatable ( '/' . $directory ));
2012-10-11 13:30:27 -04:00
$dh = $this -> instance -> opendir ( '/' );
2020-03-26 04:30:18 -04:00
$content = [];
2023-06-03 08:57:38 -04:00
while (( $file = readdir ( $dh )) !== false ) {
2025-09-27 16:56:38 -04:00
if ( $file !== '.' && $file !== '..' ) {
2012-10-11 13:30:27 -04:00
$content [] = $file ;
2012-02-13 04:25:45 -05:00
}
}
2020-03-26 04:30:18 -04:00
$this -> assertEquals ([ $directory ], $content );
2012-10-11 13:30:27 -04:00
2020-04-29 10:34:41 -04:00
$content = iterator_to_array ( $this -> instance -> getDirectoryContent ( '/' ));
$this -> assertCount ( 1 , $content );
$dirEntry = $content [ 0 ];
unset ( $dirEntry [ 'scan_permissions' ]);
unset ( $dirEntry [ 'etag' ]);
2024-02-06 11:44:23 -05:00
$this -> assertLessThanOrEqual ( 1 , abs ( $dirEntry [ 'mtime' ] - $this -> instance -> filemtime ( $directory )));
unset ( $dirEntry [ 'mtime' ]);
unset ( $dirEntry [ 'storage_mtime' ]);
2020-04-29 10:34:41 -04:00
$this -> assertEquals ([
'name' => $directory ,
'mimetype' => $this -> instance -> getMimeType ( $directory ),
'size' => - 1 ,
'permissions' => $this -> instance -> getPermissions ( $directory ),
], $dirEntry );
2016-04-06 06:14:52 -04:00
$this -> assertFalse ( $this -> instance -> mkdir ( '/' . $directory )); //can't create existing folders
2014-03-20 10:32:12 -04:00
$this -> assertTrue ( $this -> instance -> rmdir ( '/' . $directory ));
2012-10-11 13:30:27 -04:00
2014-01-31 10:06:11 -05:00
$this -> wait ();
2014-03-20 10:32:12 -04:00
$this -> assertFalse ( $this -> instance -> file_exists ( '/' . $directory ));
2012-10-11 13:30:27 -04:00
2016-04-06 06:14:52 -04:00
$this -> assertFalse ( $this -> instance -> rmdir ( '/' . $directory )); //can't remove non existing folders
2012-10-11 13:30:27 -04:00
$dh = $this -> instance -> opendir ( '/' );
2020-03-26 04:30:18 -04:00
$content = [];
2023-06-03 08:57:38 -04:00
while (( $file = readdir ( $dh )) !== false ) {
2025-09-27 16:56:38 -04:00
if ( $file != '.' && $file != '..' ) {
2012-10-11 13:30:27 -04:00
$content [] = $file ;
2012-04-18 14:46:00 -04:00
}
}
2020-03-26 04:30:18 -04:00
$this -> assertEquals ([], $content );
2012-02-13 04:25:45 -05:00
}
2012-02-12 12:06:32 -05:00
2025-05-12 10:17:59 -04:00
public static function fileNameProvider () : array {
2016-07-04 11:49:44 -04:00
return [
[ 'file.txt' ],
[ ' file.txt' ],
[ 'folder .txt' ],
[ 'file with space.txt' ],
[ 'spéciäl fäile' ],
[ 'test single\'quote.txt' ],
];
}
2025-05-12 10:17:59 -04:00
public static function directoryProvider () : array {
2015-03-26 07:15:02 -04:00
return [
[ 'folder' ],
[ ' folder' ],
[ 'folder ' ],
[ 'folder with space' ],
[ 'spéciäl földer' ],
[ 'test single\'quote' ],
];
2013-10-31 06:55:58 -04:00
}
2014-03-20 10:32:12 -04:00
2025-05-12 10:17:59 -04:00
public static function loremFileProvider () : array {
2014-03-31 12:36:52 -04:00
$root = \OC :: $SERVERROOT . '/tests/data/' ;
2020-03-26 04:30:18 -04:00
return [
2014-03-31 12:36:52 -04:00
// small file
2020-03-26 04:30:18 -04:00
[ $root . 'lorem.txt' ],
2014-03-31 12:36:52 -04:00
// bigger file (> 8 KB which is the standard PHP block size)
2020-03-26 04:30:18 -04:00
[ $root . 'lorem-big.txt' ]
];
2014-03-31 12:36:52 -04:00
}
2012-02-12 12:06:32 -05:00
/**
* test the various uses of file_get_contents and file_put_contents
*/
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('loremFileProvider')]
2024-09-15 16:32:31 -04:00
public function testGetPutContents ( $sourceFile ) : void {
2012-10-11 13:30:27 -04:00
$sourceText = file_get_contents ( $sourceFile );
2012-02-12 12:06:32 -05:00
//fill a file with string data
2012-10-11 13:30:27 -04:00
$this -> instance -> file_put_contents ( '/lorem.txt' , $sourceText );
2012-05-17 19:54:02 -04:00
$this -> assertFalse ( $this -> instance -> is_dir ( '/lorem.txt' ));
2013-01-24 10:47:17 -05:00
$this -> assertEquals ( $sourceText , $this -> instance -> file_get_contents ( '/lorem.txt' ), 'data returned from file_get_contents is not equal to the source data' );
2012-02-12 12:06:32 -05:00
//empty the file
2012-10-11 13:30:27 -04:00
$this -> instance -> file_put_contents ( '/lorem.txt' , '' );
2013-01-24 10:47:17 -05:00
$this -> assertEquals ( '' , $this -> instance -> file_get_contents ( '/lorem.txt' ), 'file not emptied' );
2012-02-12 12:06:32 -05:00
}
2012-10-11 13:30:27 -04:00
2012-02-13 04:25:45 -05:00
/**
* test various known mimetypes
*/
2024-09-15 16:32:31 -04:00
public function testMimeType () : void {
2013-01-24 10:47:17 -05:00
$this -> assertEquals ( 'httpd/unix-directory' , $this -> instance -> getMimeType ( '/' ));
$this -> assertEquals ( false , $this -> instance -> getMimeType ( '/non/existing/file' ));
2012-10-11 13:30:27 -04:00
2012-10-11 16:54:39 -04:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
2012-10-11 13:30:27 -04:00
$this -> instance -> file_put_contents ( '/lorem.txt' , file_get_contents ( $textFile , 'r' ));
2013-01-24 10:47:17 -05:00
$this -> assertEquals ( 'text/plain' , $this -> instance -> getMimeType ( '/lorem.txt' ));
2012-10-11 13:30:27 -04:00
2015-05-21 16:40:26 -04:00
$pngFile = \OC :: $SERVERROOT . '/tests/data/desktopapp.png' ;
$this -> instance -> file_put_contents ( '/desktopapp.png' , file_get_contents ( $pngFile , 'r' ));
$this -> assertEquals ( 'image/png' , $this -> instance -> getMimeType ( '/desktopapp.png' ));
2012-10-11 13:30:27 -04:00
2015-05-21 16:40:26 -04:00
$svgFile = \OC :: $SERVERROOT . '/tests/data/desktopapp.svg' ;
$this -> instance -> file_put_contents ( '/desktopapp.svg' , file_get_contents ( $svgFile , 'r' ));
$this -> assertEquals ( 'image/svg+xml' , $this -> instance -> getMimeType ( '/desktopapp.svg' ));
2012-02-13 04:25:45 -05:00
}
2012-10-11 13:30:27 -04:00
2014-08-14 09:46:14 -04:00
2025-05-12 10:17:59 -04:00
public static function copyAndMoveProvider () : array {
2015-03-26 07:15:02 -04:00
return [
[ '/source.txt' , '/target.txt' ],
[ '/source.txt' , '/target with space.txt' ],
[ '/source with space.txt' , '/target.txt' ],
[ '/source with space.txt' , '/target with space.txt' ],
[ '/source.txt' , '/tärgét.txt' ],
[ '/sòurcē.txt' , '/target.txt' ],
[ '/sòurcē.txt' , '/tärgét.txt' ],
[ '/single \' quote.txt' , '/tar\'get.txt' ],
];
2014-08-14 09:46:14 -04:00
}
2015-05-19 11:30:32 -04:00
public function initSourceAndTarget ( $source , $target = null ) {
2014-08-14 09:46:14 -04:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
$this -> instance -> file_put_contents ( $source , file_get_contents ( $textFile ));
if ( $target ) {
$testContents = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
$this -> instance -> file_put_contents ( $target , $testContents );
}
}
2015-05-19 11:30:32 -04:00
public function assertSameAsLorem ( $file ) {
2012-10-11 16:54:39 -04:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
2014-08-14 09:46:14 -04:00
$this -> assertEquals (
file_get_contents ( $textFile ),
$this -> instance -> file_get_contents ( $file ),
2015-05-19 11:30:32 -04:00
'Expected ' . $file . ' to be a copy of ' . $textFile
2014-08-14 09:46:14 -04:00
);
}
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('copyAndMoveProvider')]
2024-09-15 16:32:31 -04:00
public function testCopy ( $source , $target ) : void {
2014-08-14 09:46:14 -04:00
$this -> initSourceAndTarget ( $source );
$this -> instance -> copy ( $source , $target );
2015-05-19 11:30:32 -04:00
$this -> assertTrue ( $this -> instance -> file_exists ( $target ), $target . ' was not created' );
2014-08-14 09:46:14 -04:00
$this -> assertSameAsLorem ( $target );
2015-05-19 11:30:32 -04:00
$this -> assertTrue ( $this -> instance -> file_exists ( $source ), $source . ' was deleted' );
2014-08-14 09:46:14 -04:00
}
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('copyAndMoveProvider')]
2024-09-15 16:32:31 -04:00
public function testMove ( $source , $target ) : void {
2014-08-14 09:46:14 -04:00
$this -> initSourceAndTarget ( $source );
$this -> instance -> rename ( $source , $target );
2012-10-11 13:30:27 -04:00
2014-01-31 10:06:11 -05:00
$this -> wait ();
2015-05-19 11:30:32 -04:00
$this -> assertTrue ( $this -> instance -> file_exists ( $target ), $target . ' was not created' );
$this -> assertFalse ( $this -> instance -> file_exists ( $source ), $source . ' still exists' );
2014-08-14 09:46:14 -04:00
$this -> assertSameAsLorem ( $target );
}
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('copyAndMoveProvider')]
2024-09-15 16:32:31 -04:00
public function testCopyOverwrite ( $source , $target ) : void {
2015-05-19 11:30:32 -04:00
$this -> initSourceAndTarget ( $source , $target );
2014-08-14 09:46:14 -04:00
$this -> instance -> copy ( $source , $target );
2015-05-19 11:30:32 -04:00
$this -> assertTrue ( $this -> instance -> file_exists ( $target ), $target . ' was not created' );
$this -> assertTrue ( $this -> instance -> file_exists ( $source ), $source . ' was deleted' );
2014-08-14 09:46:14 -04:00
$this -> assertSameAsLorem ( $target );
$this -> assertSameAsLorem ( $source );
}
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('copyAndMoveProvider')]
2024-09-15 16:32:31 -04:00
public function testMoveOverwrite ( $source , $target ) : void {
2014-08-14 09:46:14 -04:00
$this -> initSourceAndTarget ( $source , $target );
$this -> instance -> rename ( $source , $target );
2015-05-19 11:30:32 -04:00
$this -> assertTrue ( $this -> instance -> file_exists ( $target ), $target . ' was not created' );
$this -> assertFalse ( $this -> instance -> file_exists ( $source ), $source . ' still exists' );
2014-08-14 09:46:14 -04:00
$this -> assertSameAsLorem ( $target );
2012-02-27 06:20:47 -05:00
}
2012-10-11 13:30:27 -04:00
2024-09-15 16:32:31 -04:00
public function testLocal () : void {
2012-10-11 16:54:39 -04:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
2012-10-11 13:30:27 -04:00
$this -> instance -> file_put_contents ( '/lorem.txt' , file_get_contents ( $textFile ));
$localFile = $this -> instance -> getLocalFile ( '/lorem.txt' );
2012-02-28 06:06:34 -05:00
$this -> assertTrue ( file_exists ( $localFile ));
2015-04-02 08:44:58 -04:00
$this -> assertEquals ( file_get_contents ( $textFile ), file_get_contents ( $localFile ));
2012-10-11 13:30:27 -04:00
2012-08-18 20:30:33 -04:00
$this -> instance -> mkdir ( '/folder' );
2012-10-11 13:30:27 -04:00
$this -> instance -> file_put_contents ( '/folder/lorem.txt' , file_get_contents ( $textFile ));
$this -> instance -> file_put_contents ( '/folder/bar.txt' , 'asd' );
2012-08-18 20:30:33 -04:00
$this -> instance -> mkdir ( '/folder/recursive' );
2012-10-11 13:30:27 -04:00
$this -> instance -> file_put_contents ( '/folder/recursive/file.txt' , 'foo' );
2013-02-06 17:36:38 -05:00
// test below require to use instance->getLocalFile because the physical storage might be different
$localFile = $this -> instance -> getLocalFile ( '/folder/lorem.txt' );
$this -> assertTrue ( file_exists ( $localFile ));
$this -> assertEquals ( file_get_contents ( $localFile ), file_get_contents ( $textFile ));
$localFile = $this -> instance -> getLocalFile ( '/folder/bar.txt' );
$this -> assertTrue ( file_exists ( $localFile ));
$this -> assertEquals ( file_get_contents ( $localFile ), 'asd' );
$localFile = $this -> instance -> getLocalFile ( '/folder/recursive/file.txt' );
$this -> assertTrue ( file_exists ( $localFile ));
$this -> assertEquals ( file_get_contents ( $localFile ), 'foo' );
2012-02-28 06:06:34 -05:00
}
2012-02-29 17:47:53 -05:00
2024-09-15 16:32:31 -04:00
public function testStat () : void {
2012-10-11 16:54:39 -04:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
2012-10-11 13:30:27 -04:00
$ctimeStart = time ();
$this -> instance -> file_put_contents ( '/lorem.txt' , file_get_contents ( $textFile ));
2012-08-15 11:55:54 -04:00
$this -> assertTrue ( $this -> instance -> isReadable ( '/lorem.txt' ));
2012-10-11 13:30:27 -04:00
$ctimeEnd = time ();
$mTime = $this -> instance -> filemtime ( '/lorem.txt' );
2013-07-08 09:03:55 -04:00
$this -> assertTrue ( $this -> instance -> hasUpdated ( '/lorem.txt' , $ctimeStart - 5 ));
$this -> assertTrue ( $this -> instance -> hasUpdated ( '/' , $ctimeStart - 5 ));
2012-10-11 13:30:27 -04:00
2013-11-14 12:39:39 -05:00
// check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1)
$this -> assertGreaterThanOrEqual (( $ctimeStart - 5 ), $mTime );
$this -> assertLessThanOrEqual (( $ctimeEnd + 1 ), $mTime );
2013-01-24 10:47:17 -05:00
$this -> assertEquals ( filesize ( $textFile ), $this -> instance -> filesize ( '/lorem.txt' ));
2012-10-11 13:30:27 -04:00
$stat = $this -> instance -> stat ( '/lorem.txt' );
2013-07-16 17:04:07 -04:00
//only size and mtime are required in the result
2013-01-24 10:47:17 -05:00
$this -> assertEquals ( $stat [ 'size' ], $this -> instance -> filesize ( '/lorem.txt' ));
$this -> assertEquals ( $stat [ 'mtime' ], $mTime );
2012-10-11 13:30:27 -04:00
2013-07-16 17:07:35 -04:00
if ( $this -> instance -> touch ( '/lorem.txt' , 100 ) !== false ) {
2012-10-11 13:30:27 -04:00
$mTime = $this -> instance -> filemtime ( '/lorem.txt' );
2013-07-16 17:07:35 -04:00
$this -> assertEquals ( $mTime , 100 );
2012-03-02 12:42:04 -05:00
}
2012-10-06 07:45:46 -04:00
2012-10-11 13:30:27 -04:00
$mtimeStart = time ();
2012-06-15 10:43:24 -04:00
$this -> instance -> unlink ( '/lorem.txt' );
2013-07-08 09:03:55 -04:00
$this -> assertTrue ( $this -> instance -> hasUpdated ( '/' , $mtimeStart - 5 ));
2012-02-29 17:47:53 -05:00
}
2012-03-02 12:42:04 -05:00
2015-04-27 12:03:04 -04:00
/**
* Test whether checkUpdate properly returns false when there was
* no change .
*/
2024-09-15 16:32:31 -04:00
public function testCheckUpdate () : void {
2025-06-12 12:31:58 -04:00
if ( $this -> instance instanceof Wrapper ) {
2015-06-02 09:14:37 -04:00
$this -> markTestSkipped ( 'Cannot test update check on wrappers' );
}
2015-04-27 12:03:04 -04:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
$watcher = $this -> instance -> getWatcher ();
$watcher -> setPolicy ( Watcher :: CHECK_ALWAYS );
$this -> instance -> file_put_contents ( '/lorem.txt' , file_get_contents ( $textFile ));
$this -> assertTrue ( $watcher -> checkUpdate ( '/lorem.txt' ), 'Update detected' );
$this -> assertFalse ( $watcher -> checkUpdate ( '/lorem.txt' ), 'No update' );
}
2024-09-15 16:32:31 -04:00
public function testUnlink () : void {
2013-11-14 12:39:39 -05:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
$this -> instance -> file_put_contents ( '/lorem.txt' , file_get_contents ( $textFile ));
$this -> assertTrue ( $this -> instance -> file_exists ( '/lorem.txt' ));
$this -> assertTrue ( $this -> instance -> unlink ( '/lorem.txt' ));
2014-01-31 10:06:11 -05:00
$this -> wait ();
2013-11-14 12:39:39 -05:00
$this -> assertFalse ( $this -> instance -> file_exists ( '/lorem.txt' ));
}
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('fileNameProvider')]
2024-09-15 16:32:31 -04:00
public function testFOpen ( $fileName ) : void {
2012-10-11 16:54:39 -04:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
2012-10-11 13:38:32 -04:00
2016-07-04 11:49:44 -04:00
$fh = @ $this -> instance -> fopen ( $fileName , 'r' );
2012-10-11 13:38:32 -04:00
if ( $fh ) {
fclose ( $fh );
}
$this -> assertFalse ( $fh );
2016-07-04 11:49:44 -04:00
$this -> assertFalse ( $this -> instance -> file_exists ( $fileName ));
2012-10-11 13:38:32 -04:00
2016-07-04 11:49:44 -04:00
$fh = $this -> instance -> fopen ( $fileName , 'w' );
2012-10-11 13:38:32 -04:00
fwrite ( $fh , file_get_contents ( $textFile ));
fclose ( $fh );
2016-07-04 11:49:44 -04:00
$this -> assertTrue ( $this -> instance -> file_exists ( $fileName ));
2012-10-11 13:38:32 -04:00
2016-07-04 11:49:44 -04:00
$fh = $this -> instance -> fopen ( $fileName , 'r' );
2025-09-10 04:51:41 -04:00
$this -> assertTrue ( is_resource ( $fh ));
2012-10-11 13:38:32 -04:00
$content = stream_get_contents ( $fh );
2013-01-24 10:47:17 -05:00
$this -> assertEquals ( file_get_contents ( $textFile ), $content );
2012-03-02 12:42:04 -05:00
}
2013-04-10 07:45:36 -04:00
2024-09-15 16:32:31 -04:00
public function testTouchCreateFile () : void {
2014-11-10 20:49:35 -05:00
$this -> assertFalse ( $this -> instance -> file_exists ( 'touch' ));
2013-11-26 06:53:03 -05:00
// returns true on success
2014-11-10 20:49:35 -05:00
$this -> assertTrue ( $this -> instance -> touch ( 'touch' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'touch' ));
2013-04-10 07:45:36 -04:00
}
2013-06-06 14:47:20 -04:00
2024-09-15 16:32:31 -04:00
public function testRecursiveRmdir () : void {
2013-06-06 14:47:20 -04:00
$this -> instance -> mkdir ( 'folder' );
$this -> instance -> mkdir ( 'folder/bar' );
2014-01-31 10:06:11 -05:00
$this -> wait ();
2013-06-06 14:47:20 -04:00
$this -> instance -> file_put_contents ( 'folder/asd.txt' , 'foobar' );
$this -> instance -> file_put_contents ( 'folder/bar/foo.txt' , 'asd' );
2013-11-29 06:58:57 -05:00
$this -> assertTrue ( $this -> instance -> rmdir ( 'folder' ));
2014-01-31 10:06:11 -05:00
$this -> wait ();
2013-11-29 06:58:57 -05:00
$this -> assertFalse ( $this -> instance -> file_exists ( 'folder/asd.txt' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'folder/bar/foo.txt' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'folder/bar' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'folder' ));
}
2024-09-15 16:32:31 -04:00
public function testRmdirEmptyFolder () : void {
2015-06-18 11:40:38 -04:00
$this -> assertTrue ( $this -> instance -> mkdir ( 'empty' ));
$this -> wait ();
$this -> assertTrue ( $this -> instance -> rmdir ( 'empty' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'empty' ));
}
2024-09-15 16:32:31 -04:00
public function testRecursiveUnlink () : void {
2013-11-29 06:58:57 -05:00
$this -> instance -> mkdir ( 'folder' );
$this -> instance -> mkdir ( 'folder/bar' );
$this -> instance -> file_put_contents ( 'folder/asd.txt' , 'foobar' );
$this -> instance -> file_put_contents ( 'folder/bar/foo.txt' , 'asd' );
$this -> assertTrue ( $this -> instance -> unlink ( 'folder' ));
2014-01-31 10:06:11 -05:00
$this -> wait ();
2013-06-06 14:47:20 -04:00
$this -> assertFalse ( $this -> instance -> file_exists ( 'folder/asd.txt' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'folder/bar/foo.txt' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'folder/bar' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'folder' ));
}
2014-03-20 10:32:12 -04:00
2025-05-12 10:17:59 -04:00
public static function hashProvider () : array {
2020-03-26 04:30:18 -04:00
return [
[ 'Foobar' , 'md5' ],
[ 'Foobar' , 'sha1' ],
[ 'Foobar' , 'sha256' ],
];
2014-03-20 10:32:12 -04:00
}
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('hashProvider')]
2024-09-15 16:32:31 -04:00
public function testHash ( $data , $type ) : void {
2014-03-20 10:32:12 -04:00
$this -> instance -> file_put_contents ( 'hash.txt' , $data );
$this -> assertEquals ( hash ( $type , $data ), $this -> instance -> hash ( $type , 'hash.txt' ));
$this -> assertEquals ( hash ( $type , $data , true ), $this -> instance -> hash ( $type , 'hash.txt' , true ));
}
2014-03-31 11:00:32 -04:00
2024-09-15 16:32:31 -04:00
public function testHashInFileName () : void {
2014-03-31 11:00:32 -04:00
$this -> instance -> file_put_contents ( '#test.txt' , 'data' );
$this -> assertEquals ( 'data' , $this -> instance -> file_get_contents ( '#test.txt' ));
$this -> instance -> mkdir ( '#foo' );
$this -> instance -> file_put_contents ( '#foo/test.txt' , 'data' );
$this -> assertEquals ( 'data' , $this -> instance -> file_get_contents ( '#foo/test.txt' ));
$dh = $this -> instance -> opendir ( '#foo' );
2020-03-26 04:30:18 -04:00
$content = [];
2014-03-31 11:00:32 -04:00
while ( $file = readdir ( $dh )) {
2025-09-27 16:56:38 -04:00
if ( $file != '.' && $file != '..' ) {
2014-03-31 11:00:32 -04:00
$content [] = $file ;
}
}
2020-03-26 04:30:18 -04:00
$this -> assertEquals ([ 'test.txt' ], $content );
2014-03-31 11:00:32 -04:00
}
2013-07-01 11:40:19 -04:00
2024-09-15 16:32:31 -04:00
public function testCopyOverWriteFile () : void {
2013-07-01 11:40:19 -04:00
$this -> instance -> file_put_contents ( 'target.txt' , 'foo' );
$this -> instance -> file_put_contents ( 'source.txt' , 'bar' );
$this -> instance -> copy ( 'source.txt' , 'target.txt' );
$this -> assertEquals ( 'bar' , $this -> instance -> file_get_contents ( 'target.txt' ));
}
2024-09-15 16:32:31 -04:00
public function testRenameOverWriteFile () : void {
2013-07-01 11:40:19 -04:00
$this -> instance -> file_put_contents ( 'target.txt' , 'foo' );
$this -> instance -> file_put_contents ( 'source.txt' , 'bar' );
$this -> instance -> rename ( 'source.txt' , 'target.txt' );
$this -> assertEquals ( 'bar' , $this -> instance -> file_get_contents ( 'target.txt' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'source.txt' ));
}
2024-09-15 16:32:31 -04:00
public function testRenameDirectory () : void {
2013-07-01 11:40:19 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> instance -> file_put_contents ( 'source/test1.txt' , 'foo' );
$this -> instance -> file_put_contents ( 'source/test2.txt' , 'qwerty' );
$this -> instance -> mkdir ( 'source/subfolder' );
$this -> instance -> file_put_contents ( 'source/subfolder/test.txt' , 'bar' );
$this -> instance -> rename ( 'source' , 'target' );
$this -> assertFalse ( $this -> instance -> file_exists ( 'source' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'source/test1.txt' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'source/test2.txt' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'source/subfolder' ));
2013-07-01 11:57:40 -04:00
$this -> assertFalse ( $this -> instance -> file_exists ( 'source/subfolder/test.txt' ));
2013-07-01 11:40:19 -04:00
$this -> assertTrue ( $this -> instance -> file_exists ( 'target' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'target/test1.txt' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'target/test2.txt' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'target/subfolder' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'target/subfolder/test.txt' ));
2021-10-14 11:28:32 -04:00
$contents = iterator_to_array ( $this -> instance -> getDirectoryContent ( '' ));
$this -> assertCount ( 1 , $contents );
2013-07-01 11:40:19 -04:00
$this -> assertEquals ( 'foo' , $this -> instance -> file_get_contents ( 'target/test1.txt' ));
$this -> assertEquals ( 'qwerty' , $this -> instance -> file_get_contents ( 'target/test2.txt' ));
$this -> assertEquals ( 'bar' , $this -> instance -> file_get_contents ( 'target/subfolder/test.txt' ));
}
2013-07-01 11:45:01 -04:00
2024-09-15 16:32:31 -04:00
public function testRenameOverWriteDirectory () : void {
2013-07-01 11:45:01 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> instance -> file_put_contents ( 'source/test1.txt' , 'foo' );
$this -> instance -> mkdir ( 'target' );
$this -> instance -> file_put_contents ( 'target/test1.txt' , 'bar' );
$this -> instance -> file_put_contents ( 'target/test2.txt' , 'bar' );
2014-10-01 07:12:41 -04:00
$this -> assertTrue ( $this -> instance -> rename ( 'source' , 'target' ), 'rename must return true on success' );
2013-07-01 11:45:01 -04:00
2014-10-01 07:12:41 -04:00
$this -> assertFalse ( $this -> instance -> file_exists ( 'source' ), 'source has not been removed' );
$this -> assertFalse ( $this -> instance -> file_exists ( 'source/test1.txt' ), 'source/test1.txt has not been removed' );
$this -> assertFalse ( $this -> instance -> file_exists ( 'target/test2.txt' ), 'target/test2.txt has not been removed' );
$this -> assertEquals ( 'foo' , $this -> instance -> file_get_contents ( 'target/test1.txt' ), 'target/test1.txt has not been overwritten' );
2013-07-01 11:45:01 -04:00
}
2013-07-01 11:57:40 -04:00
2024-09-15 16:32:31 -04:00
public function testRenameOverWriteDirectoryOverFile () : void {
2013-07-01 11:57:40 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> instance -> file_put_contents ( 'source/test1.txt' , 'foo' );
$this -> instance -> file_put_contents ( 'target' , 'bar' );
2014-10-01 07:12:41 -04:00
$this -> assertTrue ( $this -> instance -> rename ( 'source' , 'target' ), 'rename must return true on success' );
2013-07-01 11:57:40 -04:00
$this -> assertFalse ( $this -> instance -> file_exists ( 'source' ));
$this -> assertFalse ( $this -> instance -> file_exists ( 'source/test1.txt' ));
$this -> assertEquals ( 'foo' , $this -> instance -> file_get_contents ( 'target/test1.txt' ));
}
2024-09-15 16:32:31 -04:00
public function testCopyDirectory () : void {
2013-07-01 11:57:40 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> instance -> file_put_contents ( 'source/test1.txt' , 'foo' );
$this -> instance -> file_put_contents ( 'source/test2.txt' , 'qwerty' );
$this -> instance -> mkdir ( 'source/subfolder' );
$this -> instance -> file_put_contents ( 'source/subfolder/test.txt' , 'bar' );
$this -> instance -> copy ( 'source' , 'target' );
$this -> assertTrue ( $this -> instance -> file_exists ( 'source' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'source/test1.txt' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'source/test2.txt' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'source/subfolder' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'source/subfolder/test.txt' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'target' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'target/test1.txt' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'target/test2.txt' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'target/subfolder' ));
$this -> assertTrue ( $this -> instance -> file_exists ( 'target/subfolder/test.txt' ));
$this -> assertEquals ( 'foo' , $this -> instance -> file_get_contents ( 'target/test1.txt' ));
$this -> assertEquals ( 'qwerty' , $this -> instance -> file_get_contents ( 'target/test2.txt' ));
$this -> assertEquals ( 'bar' , $this -> instance -> file_get_contents ( 'target/subfolder/test.txt' ));
}
2024-09-15 16:32:31 -04:00
public function testCopyOverWriteDirectory () : void {
2013-07-01 11:57:40 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> instance -> file_put_contents ( 'source/test1.txt' , 'foo' );
$this -> instance -> mkdir ( 'target' );
$this -> instance -> file_put_contents ( 'target/test1.txt' , 'bar' );
$this -> instance -> file_put_contents ( 'target/test2.txt' , 'bar' );
$this -> instance -> copy ( 'source' , 'target' );
2025-06-06 06:18:11 -04:00
$this -> assertFalse ( $this -> instance -> file_exists ( 'target/test2.txt' ), 'File target/test2.txt should no longer exist, but does' );
2013-07-01 11:57:40 -04:00
$this -> assertEquals ( 'foo' , $this -> instance -> file_get_contents ( 'target/test1.txt' ));
}
2024-09-15 16:32:31 -04:00
public function testCopyOverWriteDirectoryOverFile () : void {
2013-07-01 11:57:40 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> instance -> file_put_contents ( 'source/test1.txt' , 'foo' );
$this -> instance -> file_put_contents ( 'target' , 'bar' );
$this -> instance -> copy ( 'source' , 'target' );
$this -> assertEquals ( 'foo' , $this -> instance -> file_get_contents ( 'target/test1.txt' ));
}
2014-05-29 07:45:50 -04:00
2024-09-15 16:32:31 -04:00
public function testInstanceOfStorage () : void {
2024-09-15 09:38:25 -04:00
$this -> assertTrue ( $this -> instance -> instanceOfStorage ( IStorage :: class ));
2014-05-29 07:45:50 -04:00
$this -> assertTrue ( $this -> instance -> instanceOfStorage ( get_class ( $this -> instance )));
$this -> assertFalse ( $this -> instance -> instanceOfStorage ( '\OC' ));
}
2015-05-19 11:30:32 -04:00
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('copyAndMoveProvider')]
2024-09-15 16:32:31 -04:00
public function testCopyFromSameStorage ( $source , $target ) : void {
2015-05-19 11:30:32 -04:00
$this -> initSourceAndTarget ( $source );
$this -> instance -> copyFromStorage ( $this -> instance , $source , $target );
$this -> assertTrue ( $this -> instance -> file_exists ( $target ), $target . ' was not created' );
$this -> assertSameAsLorem ( $target );
$this -> assertTrue ( $this -> instance -> file_exists ( $source ), $source . ' was deleted' );
}
2015-09-23 08:26:55 -04:00
2024-09-15 16:32:31 -04:00
public function testIsCreatable () : void {
2015-09-23 08:26:55 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> assertTrue ( $this -> instance -> isCreatable ( 'source' ));
}
2024-09-15 16:32:31 -04:00
public function testIsReadable () : void {
2015-09-23 08:26:55 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> assertTrue ( $this -> instance -> isReadable ( 'source' ));
}
2024-09-15 16:32:31 -04:00
public function testIsUpdatable () : void {
2015-09-23 08:26:55 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> assertTrue ( $this -> instance -> isUpdatable ( 'source' ));
}
2024-09-15 16:32:31 -04:00
public function testIsDeletable () : void {
2015-09-23 08:26:55 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> assertTrue ( $this -> instance -> isDeletable ( 'source' ));
}
2024-09-15 16:32:31 -04:00
public function testIsShareable () : void {
2015-09-23 08:26:55 -04:00
$this -> instance -> mkdir ( 'source' );
$this -> assertTrue ( $this -> instance -> isSharable ( 'source' ));
}
2015-12-15 07:48:17 -05:00
2024-09-15 16:32:31 -04:00
public function testStatAfterWrite () : void {
2015-12-15 07:48:17 -05:00
$this -> instance -> file_put_contents ( 'foo.txt' , 'bar' );
$stat = $this -> instance -> stat ( 'foo.txt' );
$this -> assertEquals ( 3 , $stat [ 'size' ]);
$fh = $this -> instance -> fopen ( 'foo.txt' , 'w' );
fwrite ( $fh , 'qwerty' );
fclose ( $fh );
$stat = $this -> instance -> stat ( 'foo.txt' );
$this -> assertEquals ( 6 , $stat [ 'size' ]);
}
2016-02-11 11:22:40 -05:00
2024-09-15 16:32:31 -04:00
public function testPartFile () : void {
2016-02-11 11:22:40 -05:00
$this -> instance -> file_put_contents ( 'bar.txt.part' , 'bar' );
$this -> instance -> rename ( 'bar.txt.part' , 'bar.txt' );
$this -> assertEquals ( 'bar' , $this -> instance -> file_get_contents ( 'bar.txt' ));
}
2018-10-31 14:41:55 -04:00
2024-09-15 16:32:31 -04:00
public function testWriteStream () : void {
2018-10-31 14:41:55 -04:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
if ( ! $this -> instance -> instanceOfStorage ( IWriteStreamStorage :: class )) {
$this -> markTestSkipped ( 'Not a WriteSteamStorage' );
}
/** @var IWriteStreamStorage $storage */
$storage = $this -> instance ;
$source = fopen ( $textFile , 'r' );
$storage -> writeStream ( 'test.txt' , $source );
$this -> assertTrue ( $storage -> file_exists ( 'test.txt' ));
2021-09-20 13:20:51 -04:00
$this -> assertStringEqualsFile ( $textFile , $storage -> file_get_contents ( 'test.txt' ));
$this -> assertEquals ( 'resource (closed)' , gettype ( $source ));
2018-10-31 14:41:55 -04:00
}
2022-08-26 09:59:29 -04:00
2024-09-15 16:32:31 -04:00
public function testFseekSize () : void {
2022-08-26 09:59:29 -04:00
$textFile = \OC :: $SERVERROOT . '/tests/data/lorem.txt' ;
$this -> instance -> file_put_contents ( 'bar.txt' , file_get_contents ( $textFile ));
$size = $this -> instance -> filesize ( 'bar.txt' );
$this -> assertEquals ( filesize ( $textFile ), $size );
$fh = $this -> instance -> fopen ( 'bar.txt' , 'r' );
fseek ( $fh , 0 , SEEK_END );
$pos = ftell ( $fh );
$this -> assertEquals ( $size , $pos );
}
2012-02-12 12:06:32 -05:00
}