From 0cf76ccb4736473a958d9fd36ed914e2d25d594a Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 21 Oct 2020 13:12:15 +0200 Subject: Feature: add a Server administration page It contains mostly read only information about the current Shaarli instance, PHP version, extensions, file and folder permissions, etc. Also action buttons to clear the cache or sync thumbnails. Part of the content of this page is also displayed on the install page, to check server requirement before installing Shaarli config file. Fixes #40 Fixes #185 --- tests/FileUtilsTest.php | 88 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) (limited to 'tests/FileUtilsTest.php') diff --git a/tests/FileUtilsTest.php b/tests/FileUtilsTest.php index 9163bdf1..3384504a 100644 --- a/tests/FileUtilsTest.php +++ b/tests/FileUtilsTest.php @@ -3,25 +3,48 @@ namespace Shaarli; use Exception; +use Shaarli\Exceptions\IOException; /** * Class FileUtilsTest * * Test file utility class. */ -class FileUtilsTest extends \Shaarli\TestCase +class FileUtilsTest extends TestCase { /** * @var string Test file path. */ protected static $file = 'sandbox/flat.db'; + 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'); + } + /** * Delete test file after every test. */ protected function tearDown(): void { @unlink(self::$file); + + @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'); } /** @@ -107,4 +130,67 @@ class FileUtilsTest extends \Shaarli\TestCase $this->assertEquals(null, FileUtils::readFlatDB(self::$file)); $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test'])); } + + /** + * 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); + } } -- cgit v1.2.3