From c2cd15dac2bfaebe6d32f7649fbdedc07400fa08 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 16 Oct 2020 13:34:59 +0200 Subject: Move utils classes to Shaarli\Helper namespace and folder --- tests/ApplicationUtilsTest.php | 414 --------------------------------- tests/FileUtilsTest.php | 196 ---------------- tests/helper/ApplicationUtilsTest.php | 415 ++++++++++++++++++++++++++++++++++ tests/helper/FileUtilsTest.php | 196 ++++++++++++++++ tests/security/BanManagerTest.php | 2 +- tests/utils/FakeApplicationUtils.php | 2 + tests/utils/ReferenceHistory.php | 2 +- 7 files changed, 615 insertions(+), 612 deletions(-) delete mode 100644 tests/ApplicationUtilsTest.php delete mode 100644 tests/FileUtilsTest.php create mode 100644 tests/helper/ApplicationUtilsTest.php create mode 100644 tests/helper/FileUtilsTest.php (limited to 'tests') diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php deleted file mode 100644 index ac46cbf1..00000000 --- a/tests/ApplicationUtilsTest.php +++ /dev/null @@ -1,414 +0,0 @@ -assertEquals( - '0.5.4', - ApplicationUtils::getVersion( - 'https://raw.githubusercontent.com/shaarli/Shaarli/' - .'v0.5.4/shaarli_version.php', - $testTimeout - ) - ); - $this->assertRegExp( - self::$versionPattern, - ApplicationUtils::getVersion( - 'https://raw.githubusercontent.com/shaarli/Shaarli/' - .'latest/shaarli_version.php', - $testTimeout - ) - ); - } - - /** - * Attempt to retrieve the latest version from an invalid File - */ - public function testGetVersionCodeFromFile() - { - file_put_contents('sandbox/version.php', ''. PHP_EOL); - $this->assertEquals( - '1.2.3', - ApplicationUtils::getVersion('sandbox/version.php', 1) - ); - } - - /** - * Attempt to retrieve the latest version from an invalid File - */ - public function testGetVersionCodeInvalidFile() - { - $oldlog = ini_get('error_log'); - ini_set('error_log', '/dev/null'); - $this->assertFalse( - ApplicationUtils::getVersion('idontexist', 1) - ); - ini_set('error_log', $oldlog); - } - - /** - * Test update checks - the user is logged off - */ - public function testCheckUpdateLoggedOff() - { - $this->assertFalse( - ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, false) - ); - } - - /** - * Test update checks - the user has disabled updates - */ - public function testCheckUpdateUserDisabled() - { - $this->assertFalse( - ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, true) - ); - } - - /** - * A newer version is available - */ - public function testCheckUpdateNewVersionAvailable() - { - $newVersion = '1.8.3'; - FakeApplicationUtils::$VERSION_CODE = $newVersion; - - $version = FakeApplicationUtils::checkUpdate( - self::$testVersion, - self::$testUpdateFile, - 100, - true, - true - ); - - $this->assertEquals($newVersion, $version); - } - - /** - * No available information about versions - */ - public function testCheckUpdateNewVersionUnavailable() - { - $version = FakeApplicationUtils::checkUpdate( - self::$testVersion, - self::$testUpdateFile, - 100, - true, - true - ); - - $this->assertFalse($version); - } - - /** - * Test update checks - invalid Git branch - */ - public function testCheckUpdateInvalidGitBranch() - { - $this->expectException(\Exception::class); - $this->expectExceptionMessageRegExp('/Invalid branch selected for updates/'); - - ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable'); - } - - /** - * Shaarli is up-to-date - */ - public function testCheckUpdateNewVersionUpToDate() - { - FakeApplicationUtils::$VERSION_CODE = self::$testVersion; - - $version = FakeApplicationUtils::checkUpdate( - self::$testVersion, - self::$testUpdateFile, - 100, - true, - true - ); - - $this->assertFalse($version); - } - - /** - * Time-traveller's Shaarli - */ - public function testCheckUpdateNewVersionMaartiMcFly() - { - FakeApplicationUtils::$VERSION_CODE = '0.4.1'; - - $version = FakeApplicationUtils::checkUpdate( - self::$testVersion, - self::$testUpdateFile, - 100, - true, - true - ); - - $this->assertFalse($version); - } - - /** - * The version has been checked recently and Shaarli is up-to-date - */ - public function testCheckUpdateNewVersionTwiceUpToDate() - { - FakeApplicationUtils::$VERSION_CODE = self::$testVersion; - - // Create the update file - $version = FakeApplicationUtils::checkUpdate( - self::$testVersion, - self::$testUpdateFile, - 100, - true, - true - ); - - $this->assertFalse($version); - - // Reuse the update file - $version = FakeApplicationUtils::checkUpdate( - self::$testVersion, - self::$testUpdateFile, - 100, - true, - true - ); - - $this->assertFalse($version); - } - - /** - * The version has been checked recently and Shaarli is outdated - */ - public function testCheckUpdateNewVersionTwiceOutdated() - { - $newVersion = '1.8.3'; - FakeApplicationUtils::$VERSION_CODE = $newVersion; - - // Create the update file - $version = FakeApplicationUtils::checkUpdate( - self::$testVersion, - self::$testUpdateFile, - 100, - true, - true - ); - $this->assertEquals($newVersion, $version); - - // Reuse the update file - $version = FakeApplicationUtils::checkUpdate( - self::$testVersion, - self::$testUpdateFile, - 100, - true, - true - ); - $this->assertEquals($newVersion, $version); - } - - /** - * Check supported PHP versions - */ - public function testCheckSupportedPHPVersion() - { - $minVersion = '5.3'; - $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.4.32')); - $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.5')); - $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.6.10')); - } - - /** - * Check a unsupported PHP version - */ - public function testCheckSupportedPHPVersion51() - { - $this->expectException(\Exception::class); - $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/'); - - $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.1.0')); - } - - /** - * Check another unsupported PHP version - */ - public function testCheckSupportedPHPVersion52() - { - $this->expectException(\Exception::class); - $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/'); - - $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.2')); - } - - /** - * Checks resource permissions for the current Shaarli installation - */ - public function testCheckCurrentResourcePermissions() - { - $conf = new ConfigManager(''); - $conf->set('resource.thumbnails_cache', 'cache'); - $conf->set('resource.config', 'data/config.php'); - $conf->set('resource.data_dir', 'data'); - $conf->set('resource.datastore', 'data/datastore.php'); - $conf->set('resource.ban_file', 'data/ipbans.php'); - $conf->set('resource.log', 'data/log.txt'); - $conf->set('resource.page_cache', 'pagecache'); - $conf->set('resource.raintpl_tmp', 'tmp'); - $conf->set('resource.raintpl_tpl', 'tpl'); - $conf->set('resource.theme', 'default'); - $conf->set('resource.update_check', 'data/lastupdatecheck.txt'); - - $this->assertEquals( - array(), - ApplicationUtils::checkResourcePermissions($conf) - ); - } - - /** - * Checks resource permissions for a non-existent Shaarli installation - */ - public function testCheckCurrentResourcePermissionsErrors() - { - $conf = new ConfigManager(''); - $conf->set('resource.thumbnails_cache', 'null/cache'); - $conf->set('resource.config', 'null/data/config.php'); - $conf->set('resource.data_dir', 'null/data'); - $conf->set('resource.datastore', 'null/data/store.php'); - $conf->set('resource.ban_file', 'null/data/ipbans.php'); - $conf->set('resource.log', 'null/data/log.txt'); - $conf->set('resource.page_cache', 'null/pagecache'); - $conf->set('resource.raintpl_tmp', 'null/tmp'); - $conf->set('resource.raintpl_tpl', 'null/tpl'); - $conf->set('resource.raintpl_theme', 'null/tpl/default'); - $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt'); - $this->assertEquals( - array( - '"null/tpl" directory is not readable', - '"null/tpl/default" directory is not readable', - '"null/cache" directory is not readable', - '"null/cache" directory is not writable', - '"null/data" directory is not readable', - '"null/data" directory is not writable', - '"null/pagecache" directory is not readable', - '"null/pagecache" directory is not writable', - '"null/tmp" directory is not readable', - '"null/tmp" directory is not writable' - ), - ApplicationUtils::checkResourcePermissions($conf) - ); - } - - /** - * Checks resource permissions in minimal mode. - */ - public function testCheckCurrentResourcePermissionsErrorsMinimalMode(): void - { - $conf = new ConfigManager(''); - $conf->set('resource.thumbnails_cache', 'null/cache'); - $conf->set('resource.config', 'null/data/config.php'); - $conf->set('resource.data_dir', 'null/data'); - $conf->set('resource.datastore', 'null/data/store.php'); - $conf->set('resource.ban_file', 'null/data/ipbans.php'); - $conf->set('resource.log', 'null/data/log.txt'); - $conf->set('resource.page_cache', 'null/pagecache'); - $conf->set('resource.raintpl_tmp', 'null/tmp'); - $conf->set('resource.raintpl_tpl', 'null/tpl'); - $conf->set('resource.raintpl_theme', 'null/tpl/default'); - $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt'); - - static::assertSame( - [ - '"null/tpl" directory is not readable', - '"null/tpl/default" directory is not readable', - '"null/tmp" directory is not readable', - '"null/tmp" directory is not writable' - ], - ApplicationUtils::checkResourcePermissions($conf, true) - ); - } - - /** - * Check update with 'dev' as curent version (master branch). - * It should always return false. - */ - public function testCheckUpdateDev() - { - $this->assertFalse( - ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true) - ); - } - - /** - * Basic test of getPhpExtensionsRequirement() - */ - public function testGetPhpExtensionsRequirementSimple(): void - { - static::assertCount(8, ApplicationUtils::getPhpExtensionsRequirement()); - static::assertSame([ - 'name' => 'json', - 'required' => true, - 'desc' => 'Configuration parsing', - 'loaded' => true, - ], ApplicationUtils::getPhpExtensionsRequirement()[0]); - } - - /** - * Test getPhpEol with a known version: 7.4 -> 2022 - */ - public function testGetKnownPhpEol(): void - { - static::assertSame('2022-11-28', ApplicationUtils::getPhpEol('7.4.7')); - } - - /** - * Test getPhpEol with an unknown version: 7.4 -> 2022 - */ - public function testGetUnknownPhpEol(): void - { - static::assertSame( - (((int) (new \DateTime())->format('Y')) + 2) . (new \DateTime())->format('-m-d'), - ApplicationUtils::getPhpEol('7.51.34') - ); - } -} diff --git a/tests/FileUtilsTest.php b/tests/FileUtilsTest.php deleted file mode 100644 index 3384504a..00000000 --- a/tests/FileUtilsTest.php +++ /dev/null @@ -1,196 +0,0 @@ -assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0); - $this->assertTrue(startsWith(file_get_contents(self::$file), '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() - { - $this->expectException(\Shaarli\Exceptions\IOException::class); - $this->expectExceptionMessage('Error accessing "sandbox/flat.db"'); - - touch(self::$file); - chmod(self::$file, 0440); - FileUtils::writeFlatDB(self::$file, null); - } - - /** - * Folder non existent: raise an exception. - */ - public function testWriteFolderDoesNotExist() - { - $this->expectException(\Shaarli\Exceptions\IOException::class); - $this->expectExceptionMessage('Error accessing "nopefolder"'); - - FileUtils::writeFlatDB('nopefolder/file', null); - } - - /** - * Folder non writable: raise an exception. - */ - public function testWriteFolderPermission() - { - $this->expectException(\Shaarli\Exceptions\IOException::class); - $this->expectExceptionMessage('Error accessing "sandbox"'); - - 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'])); - } - - /** - * 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); - } -} diff --git a/tests/helper/ApplicationUtilsTest.php b/tests/helper/ApplicationUtilsTest.php new file mode 100644 index 00000000..654857b9 --- /dev/null +++ b/tests/helper/ApplicationUtilsTest.php @@ -0,0 +1,415 @@ +assertEquals( + '0.5.4', + ApplicationUtils::getVersion( + 'https://raw.githubusercontent.com/shaarli/Shaarli/' + .'v0.5.4/shaarli_version.php', + $testTimeout + ) + ); + $this->assertRegExp( + self::$versionPattern, + ApplicationUtils::getVersion( + 'https://raw.githubusercontent.com/shaarli/Shaarli/' + .'latest/shaarli_version.php', + $testTimeout + ) + ); + } + + /** + * Attempt to retrieve the latest version from an invalid File + */ + public function testGetVersionCodeFromFile() + { + file_put_contents('sandbox/version.php', ''. PHP_EOL); + $this->assertEquals( + '1.2.3', + ApplicationUtils::getVersion('sandbox/version.php', 1) + ); + } + + /** + * Attempt to retrieve the latest version from an invalid File + */ + public function testGetVersionCodeInvalidFile() + { + $oldlog = ini_get('error_log'); + ini_set('error_log', '/dev/null'); + $this->assertFalse( + ApplicationUtils::getVersion('idontexist', 1) + ); + ini_set('error_log', $oldlog); + } + + /** + * Test update checks - the user is logged off + */ + public function testCheckUpdateLoggedOff() + { + $this->assertFalse( + ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, false) + ); + } + + /** + * Test update checks - the user has disabled updates + */ + public function testCheckUpdateUserDisabled() + { + $this->assertFalse( + ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, true) + ); + } + + /** + * A newer version is available + */ + public function testCheckUpdateNewVersionAvailable() + { + $newVersion = '1.8.3'; + FakeApplicationUtils::$VERSION_CODE = $newVersion; + + $version = FakeApplicationUtils::checkUpdate( + self::$testVersion, + self::$testUpdateFile, + 100, + true, + true + ); + + $this->assertEquals($newVersion, $version); + } + + /** + * No available information about versions + */ + public function testCheckUpdateNewVersionUnavailable() + { + $version = FakeApplicationUtils::checkUpdate( + self::$testVersion, + self::$testUpdateFile, + 100, + true, + true + ); + + $this->assertFalse($version); + } + + /** + * Test update checks - invalid Git branch + */ + public function testCheckUpdateInvalidGitBranch() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessageRegExp('/Invalid branch selected for updates/'); + + ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable'); + } + + /** + * Shaarli is up-to-date + */ + public function testCheckUpdateNewVersionUpToDate() + { + FakeApplicationUtils::$VERSION_CODE = self::$testVersion; + + $version = FakeApplicationUtils::checkUpdate( + self::$testVersion, + self::$testUpdateFile, + 100, + true, + true + ); + + $this->assertFalse($version); + } + + /** + * Time-traveller's Shaarli + */ + public function testCheckUpdateNewVersionMaartiMcFly() + { + FakeApplicationUtils::$VERSION_CODE = '0.4.1'; + + $version = FakeApplicationUtils::checkUpdate( + self::$testVersion, + self::$testUpdateFile, + 100, + true, + true + ); + + $this->assertFalse($version); + } + + /** + * The version has been checked recently and Shaarli is up-to-date + */ + public function testCheckUpdateNewVersionTwiceUpToDate() + { + FakeApplicationUtils::$VERSION_CODE = self::$testVersion; + + // Create the update file + $version = FakeApplicationUtils::checkUpdate( + self::$testVersion, + self::$testUpdateFile, + 100, + true, + true + ); + + $this->assertFalse($version); + + // Reuse the update file + $version = FakeApplicationUtils::checkUpdate( + self::$testVersion, + self::$testUpdateFile, + 100, + true, + true + ); + + $this->assertFalse($version); + } + + /** + * The version has been checked recently and Shaarli is outdated + */ + public function testCheckUpdateNewVersionTwiceOutdated() + { + $newVersion = '1.8.3'; + FakeApplicationUtils::$VERSION_CODE = $newVersion; + + // Create the update file + $version = FakeApplicationUtils::checkUpdate( + self::$testVersion, + self::$testUpdateFile, + 100, + true, + true + ); + $this->assertEquals($newVersion, $version); + + // Reuse the update file + $version = FakeApplicationUtils::checkUpdate( + self::$testVersion, + self::$testUpdateFile, + 100, + true, + true + ); + $this->assertEquals($newVersion, $version); + } + + /** + * Check supported PHP versions + */ + public function testCheckSupportedPHPVersion() + { + $minVersion = '5.3'; + $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.4.32')); + $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.5')); + $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.6.10')); + } + + /** + * Check a unsupported PHP version + */ + public function testCheckSupportedPHPVersion51() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/'); + + $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.1.0')); + } + + /** + * Check another unsupported PHP version + */ + public function testCheckSupportedPHPVersion52() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/'); + + $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.2')); + } + + /** + * Checks resource permissions for the current Shaarli installation + */ + public function testCheckCurrentResourcePermissions() + { + $conf = new ConfigManager(''); + $conf->set('resource.thumbnails_cache', 'cache'); + $conf->set('resource.config', 'data/config.php'); + $conf->set('resource.data_dir', 'data'); + $conf->set('resource.datastore', 'data/datastore.php'); + $conf->set('resource.ban_file', 'data/ipbans.php'); + $conf->set('resource.log', 'data/log.txt'); + $conf->set('resource.page_cache', 'pagecache'); + $conf->set('resource.raintpl_tmp', 'tmp'); + $conf->set('resource.raintpl_tpl', 'tpl'); + $conf->set('resource.theme', 'default'); + $conf->set('resource.update_check', 'data/lastupdatecheck.txt'); + + $this->assertEquals( + array(), + ApplicationUtils::checkResourcePermissions($conf) + ); + } + + /** + * Checks resource permissions for a non-existent Shaarli installation + */ + public function testCheckCurrentResourcePermissionsErrors() + { + $conf = new ConfigManager(''); + $conf->set('resource.thumbnails_cache', 'null/cache'); + $conf->set('resource.config', 'null/data/config.php'); + $conf->set('resource.data_dir', 'null/data'); + $conf->set('resource.datastore', 'null/data/store.php'); + $conf->set('resource.ban_file', 'null/data/ipbans.php'); + $conf->set('resource.log', 'null/data/log.txt'); + $conf->set('resource.page_cache', 'null/pagecache'); + $conf->set('resource.raintpl_tmp', 'null/tmp'); + $conf->set('resource.raintpl_tpl', 'null/tpl'); + $conf->set('resource.raintpl_theme', 'null/tpl/default'); + $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt'); + $this->assertEquals( + array( + '"null/tpl" directory is not readable', + '"null/tpl/default" directory is not readable', + '"null/cache" directory is not readable', + '"null/cache" directory is not writable', + '"null/data" directory is not readable', + '"null/data" directory is not writable', + '"null/pagecache" directory is not readable', + '"null/pagecache" directory is not writable', + '"null/tmp" directory is not readable', + '"null/tmp" directory is not writable' + ), + ApplicationUtils::checkResourcePermissions($conf) + ); + } + + /** + * Checks resource permissions in minimal mode. + */ + public function testCheckCurrentResourcePermissionsErrorsMinimalMode(): void + { + $conf = new ConfigManager(''); + $conf->set('resource.thumbnails_cache', 'null/cache'); + $conf->set('resource.config', 'null/data/config.php'); + $conf->set('resource.data_dir', 'null/data'); + $conf->set('resource.datastore', 'null/data/store.php'); + $conf->set('resource.ban_file', 'null/data/ipbans.php'); + $conf->set('resource.log', 'null/data/log.txt'); + $conf->set('resource.page_cache', 'null/pagecache'); + $conf->set('resource.raintpl_tmp', 'null/tmp'); + $conf->set('resource.raintpl_tpl', 'null/tpl'); + $conf->set('resource.raintpl_theme', 'null/tpl/default'); + $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt'); + + static::assertSame( + [ + '"null/tpl" directory is not readable', + '"null/tpl/default" directory is not readable', + '"null/tmp" directory is not readable', + '"null/tmp" directory is not writable' + ], + ApplicationUtils::checkResourcePermissions($conf, true) + ); + } + + /** + * Check update with 'dev' as curent version (master branch). + * It should always return false. + */ + public function testCheckUpdateDev() + { + $this->assertFalse( + ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true) + ); + } + + /** + * Basic test of getPhpExtensionsRequirement() + */ + public function testGetPhpExtensionsRequirementSimple(): void + { + static::assertCount(8, ApplicationUtils::getPhpExtensionsRequirement()); + static::assertSame([ + 'name' => 'json', + 'required' => true, + 'desc' => 'Configuration parsing', + 'loaded' => true, + ], ApplicationUtils::getPhpExtensionsRequirement()[0]); + } + + /** + * Test getPhpEol with a known version: 7.4 -> 2022 + */ + public function testGetKnownPhpEol(): void + { + static::assertSame('2022-11-28', ApplicationUtils::getPhpEol('7.4.7')); + } + + /** + * Test getPhpEol with an unknown version: 7.4 -> 2022 + */ + public function testGetUnknownPhpEol(): void + { + static::assertSame( + (((int) (new \DateTime())->format('Y')) + 2) . (new \DateTime())->format('-m-d'), + ApplicationUtils::getPhpEol('7.51.34') + ); + } +} diff --git a/tests/helper/FileUtilsTest.php b/tests/helper/FileUtilsTest.php new file mode 100644 index 00000000..948e46d1 --- /dev/null +++ b/tests/helper/FileUtilsTest.php @@ -0,0 +1,196 @@ +assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0); + $this->assertTrue(startsWith(file_get_contents(self::$file), '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() + { + $this->expectException(\Shaarli\Exceptions\IOException::class); + $this->expectExceptionMessage('Error accessing "sandbox/flat.db"'); + + touch(self::$file); + chmod(self::$file, 0440); + FileUtils::writeFlatDB(self::$file, null); + } + + /** + * Folder non existent: raise an exception. + */ + public function testWriteFolderDoesNotExist() + { + $this->expectException(\Shaarli\Exceptions\IOException::class); + $this->expectExceptionMessage('Error accessing "nopefolder"'); + + FileUtils::writeFlatDB('nopefolder/file', null); + } + + /** + * Folder non writable: raise an exception. + */ + public function testWriteFolderPermission() + { + $this->expectException(\Shaarli\Exceptions\IOException::class); + $this->expectExceptionMessage('Error accessing "sandbox"'); + + 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'])); + } + + /** + * 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); + } +} diff --git a/tests/security/BanManagerTest.php b/tests/security/BanManagerTest.php index 22aa8666..29d2791b 100644 --- a/tests/security/BanManagerTest.php +++ b/tests/security/BanManagerTest.php @@ -4,7 +4,7 @@ namespace Shaarli\Security; use Psr\Log\LoggerInterface; -use Shaarli\FileUtils; +use Shaarli\Helper\FileUtils; use Shaarli\TestCase; /** diff --git a/tests/utils/FakeApplicationUtils.php b/tests/utils/FakeApplicationUtils.php index de83d598..d5289ede 100644 --- a/tests/utils/FakeApplicationUtils.php +++ b/tests/utils/FakeApplicationUtils.php @@ -2,6 +2,8 @@ namespace Shaarli; +use Shaarli\Helper\ApplicationUtils; + /** * Fake ApplicationUtils class to avoid HTTP requests */ diff --git a/tests/utils/ReferenceHistory.php b/tests/utils/ReferenceHistory.php index 516c9f51..aed5d2cf 100644 --- a/tests/utils/ReferenceHistory.php +++ b/tests/utils/ReferenceHistory.php @@ -1,6 +1,6 @@