2017-01-16 12:30:18 +01:00
|
|
|
<?php
|
|
|
|
|
2020-10-16 13:34:59 +02:00
|
|
|
namespace Shaarli\Helper;
|
2018-12-02 23:31:40 +01:00
|
|
|
|
2018-12-03 00:59:21 +01:00
|
|
|
use Exception;
|
2020-10-21 13:12:15 +02:00
|
|
|
use Shaarli\Exceptions\IOException;
|
2020-10-27 19:55:29 +01:00
|
|
|
use Shaarli\TestCase;
|
2017-01-16 12:30:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FileUtilsTest
|
|
|
|
*
|
|
|
|
* Test file utility class.
|
|
|
|
*/
|
2020-10-21 13:12:15 +02:00
|
|
|
class FileUtilsTest extends TestCase
|
2017-01-16 12:30:18 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string Test file path.
|
|
|
|
*/
|
|
|
|
protected static $file = 'sandbox/flat.db';
|
|
|
|
|
2020-10-21 13:12:15 +02:00
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
@mkdir('sandbox');
|
|
|
|
mkdir('sandbox/folder2');
|
|
|
|
touch('sandbox/file1');
|
|
|
|
touch('sandbox/file2');
|
|
|
|
mkdir('sandbox/folder1');
|
|
|
|
touch('sandbox/folder1/file1');
|
|
|
|
touch('sandbox/folder1/file2');
|
|
|
|
mkdir('sandbox/folder3');
|
|
|
|
mkdir('/tmp/shaarli-to-delete');
|
|
|
|
}
|
|
|
|
|
2017-01-16 12:30:18 +01:00
|
|
|
/**
|
|
|
|
* Delete test file after every test.
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function tearDown(): void
|
2017-01-16 12:30:18 +01:00
|
|
|
{
|
|
|
|
@unlink(self::$file);
|
2020-10-21 13:12:15 +02:00
|
|
|
|
|
|
|
@unlink('sandbox/folder1/file1');
|
|
|
|
@unlink('sandbox/folder1/file2');
|
|
|
|
@rmdir('sandbox/folder1');
|
|
|
|
@unlink('sandbox/file1');
|
|
|
|
@unlink('sandbox/file2');
|
|
|
|
@rmdir('sandbox/folder2');
|
|
|
|
@rmdir('sandbox/folder3');
|
|
|
|
@rmdir('/tmp/shaarli-to-delete');
|
2017-01-16 12:30:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test writeDB, then readDB with different data.
|
|
|
|
*/
|
|
|
|
public function testSimpleWriteRead()
|
|
|
|
{
|
|
|
|
$data = ['blue', 'red'];
|
|
|
|
$this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
|
|
|
|
$this->assertTrue(startsWith(file_get_contents(self::$file), '<?php /*'));
|
|
|
|
$this->assertEquals($data, FileUtils::readFlatDB(self::$file));
|
|
|
|
|
|
|
|
$data = 0;
|
|
|
|
$this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
|
|
|
|
$this->assertEquals($data, FileUtils::readFlatDB(self::$file));
|
|
|
|
|
|
|
|
$data = null;
|
|
|
|
$this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
|
|
|
|
$this->assertEquals($data, FileUtils::readFlatDB(self::$file));
|
|
|
|
|
|
|
|
$data = false;
|
|
|
|
$this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
|
|
|
|
$this->assertEquals($data, FileUtils::readFlatDB(self::$file));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File not writable: raise an exception.
|
|
|
|
*/
|
|
|
|
public function testWriteWithoutPermission()
|
|
|
|
{
|
2020-09-27 14:07:08 +02:00
|
|
|
$this->expectException(\Shaarli\Exceptions\IOException::class);
|
|
|
|
$this->expectExceptionMessage('Error accessing "sandbox/flat.db"');
|
|
|
|
|
2017-01-16 12:30:18 +01:00
|
|
|
touch(self::$file);
|
|
|
|
chmod(self::$file, 0440);
|
|
|
|
FileUtils::writeFlatDB(self::$file, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Folder non existent: raise an exception.
|
|
|
|
*/
|
|
|
|
public function testWriteFolderDoesNotExist()
|
|
|
|
{
|
2020-09-27 14:07:08 +02:00
|
|
|
$this->expectException(\Shaarli\Exceptions\IOException::class);
|
|
|
|
$this->expectExceptionMessage('Error accessing "nopefolder"');
|
|
|
|
|
2017-01-16 12:30:18 +01:00
|
|
|
FileUtils::writeFlatDB('nopefolder/file', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Folder non writable: raise an exception.
|
|
|
|
*/
|
|
|
|
public function testWriteFolderPermission()
|
|
|
|
{
|
2020-09-27 14:07:08 +02:00
|
|
|
$this->expectException(\Shaarli\Exceptions\IOException::class);
|
|
|
|
$this->expectExceptionMessage('Error accessing "sandbox"');
|
|
|
|
|
2017-01-16 12:30:18 +01:00
|
|
|
chmod(dirname(self::$file), 0555);
|
|
|
|
try {
|
|
|
|
FileUtils::writeFlatDB(self::$file, null);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
chmod(dirname(self::$file), 0755);
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read non existent file, use default parameter.
|
|
|
|
*/
|
|
|
|
public function testReadNotExistentFile()
|
|
|
|
{
|
|
|
|
$this->assertEquals(null, FileUtils::readFlatDB(self::$file));
|
|
|
|
$this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read non readable file, use default parameter.
|
|
|
|
*/
|
|
|
|
public function testReadNotReadable()
|
|
|
|
{
|
|
|
|
touch(self::$file);
|
|
|
|
chmod(self::$file, 0220);
|
|
|
|
$this->assertEquals(null, FileUtils::readFlatDB(self::$file));
|
|
|
|
$this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
|
|
|
|
}
|
2020-10-21 13:12:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test clearFolder with self delete and excluded files
|
|
|
|
*/
|
|
|
|
public function testClearFolderSelfDeleteWithExclusion(): void
|
|
|
|
{
|
|
|
|
FileUtils::clearFolder('sandbox', true, ['file2']);
|
|
|
|
|
|
|
|
static::assertFileExists('sandbox/folder1/file2');
|
|
|
|
static::assertFileExists('sandbox/folder1');
|
|
|
|
static::assertFileExists('sandbox/file2');
|
|
|
|
static::assertFileExists('sandbox');
|
|
|
|
|
|
|
|
static::assertFileNotExists('sandbox/folder1/file1');
|
|
|
|
static::assertFileNotExists('sandbox/file1');
|
|
|
|
static::assertFileNotExists('sandbox/folder3');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test clearFolder with self delete and excluded files
|
|
|
|
*/
|
|
|
|
public function testClearFolderSelfDeleteWithoutExclusion(): void
|
|
|
|
{
|
|
|
|
FileUtils::clearFolder('sandbox', true);
|
|
|
|
|
|
|
|
static::assertFileNotExists('sandbox');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test clearFolder with self delete and excluded files
|
|
|
|
*/
|
|
|
|
public function testClearFolderNoSelfDeleteWithoutExclusion(): void
|
|
|
|
{
|
|
|
|
FileUtils::clearFolder('sandbox', false);
|
|
|
|
|
|
|
|
static::assertFileExists('sandbox');
|
|
|
|
|
|
|
|
// 2 because '.' and '..'
|
|
|
|
static::assertCount(2, new \DirectoryIterator('sandbox'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test clearFolder on a file instead of a folder
|
|
|
|
*/
|
|
|
|
public function testClearFolderOnANonDirectory(): void
|
|
|
|
{
|
|
|
|
$this->expectException(IOException::class);
|
|
|
|
$this->expectExceptionMessage('Provided path is not a directory.');
|
|
|
|
|
|
|
|
FileUtils::clearFolder('sandbox/file1', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test clearFolder on a file instead of a folder
|
|
|
|
*/
|
|
|
|
public function testClearFolderOutsideOfShaarliDirectory(): void
|
|
|
|
{
|
|
|
|
$this->expectException(IOException::class);
|
|
|
|
$this->expectExceptionMessage('Trying to delete a folder outside of Shaarli path.');
|
|
|
|
|
|
|
|
|
|
|
|
FileUtils::clearFolder('/tmp/shaarli-to-delete', true);
|
|
|
|
}
|
2017-01-16 12:30:18 +01:00
|
|
|
}
|