]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/ApplicationUtilsTest.php
Merge pull request #1604 from ArthurHoaro/feature/server-admin-page
[github/shaarli/Shaarli.git] / tests / ApplicationUtilsTest.php
index ff4c9e17944be6de5fa9e53f9574eb1baac5c665..ac46cbf129df09479f039d44f3eeee3784b299be 100644 (file)
@@ -1,33 +1,14 @@
 <?php
-use Shaarli\Config\ConfigManager;
-
-/**
- * ApplicationUtils' tests
- */
+namespace Shaarli;
 
-require_once 'application/ApplicationUtils.php';
-
-/**
- * Fake ApplicationUtils class to avoid HTTP requests
- */
-class FakeApplicationUtils extends ApplicationUtils
-{
-    public static $VERSION_CODE = '';
-
-    /**
-     * Toggle HTTP requests, allow overriding the version code
-     */
-    public static function getVersion($url, $timeout=0)
-    {
-        return self::$VERSION_CODE;
-    }
-}
+use Shaarli\Config\ConfigManager;
 
+require_once 'tests/utils/FakeApplicationUtils.php';
 
 /**
  * Unitary tests for Shaarli utilities
  */
-class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
+class ApplicationUtilsTest extends \Shaarli\TestCase
 {
     protected static $testUpdateFile = 'sandbox/update.txt';
     protected static $testVersion = '0.5.0';
@@ -36,7 +17,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
     /**
      * Reset test data for each test
      */
-    public function setUp()
+    protected function setUp(): void
     {
         FakeApplicationUtils::$VERSION_CODE = '';
         if (file_exists(self::$testUpdateFile)) {
@@ -47,7 +28,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
     /**
      * Remove test version file if it exists
      */
-    public function tearDown()
+    protected function tearDown(): void
     {
         if (is_file('sandbox/version.php')) {
             unlink('sandbox/version.php');
@@ -67,7 +48,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
             '0.5.4',
             ApplicationUtils::getVersion(
                 'https://raw.githubusercontent.com/shaarli/Shaarli/'
-               .'v0.5.4/shaarli_version.php',
+                .'v0.5.4/shaarli_version.php',
                 $testTimeout
             )
         );
@@ -75,7 +56,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
             self::$versionPattern,
             ApplicationUtils::getVersion(
                 'https://raw.githubusercontent.com/shaarli/Shaarli/'
-               .'latest/shaarli_version.php',
+                .'latest/shaarli_version.php',
                 $testTimeout
             )
         );
@@ -163,11 +144,12 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
 
     /**
      * Test update checks - invalid Git branch
-     * @expectedException              Exception
-     * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
      */
     public function testCheckUpdateInvalidGitBranch()
     {
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessageRegExp('/Invalid branch selected for updates/');
+
         ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable');
     }
 
@@ -272,29 +254,31 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
     public function testCheckSupportedPHPVersion()
     {
         $minVersion = '5.3';
-        ApplicationUtils::checkPHPVersion($minVersion, '5.4.32');
-        ApplicationUtils::checkPHPVersion($minVersion, '5.5');
-        ApplicationUtils::checkPHPVersion($minVersion, '5.6.10');
+        $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
-     * @expectedException              Exception
-     * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
      */
     public function testCheckSupportedPHPVersion51()
     {
-        ApplicationUtils::checkPHPVersion('5.3', '5.1.0');
+        $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
-     * @expectedException              Exception
-     * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
      */
     public function testCheckSupportedPHPVersion52()
     {
-        ApplicationUtils::checkPHPVersion('5.3', '5.2');
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/');
+
+        $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.2'));
     }
 
     /**
@@ -355,6 +339,35 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
         );
     }
 
+    /**
+     * 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.
@@ -365,4 +378,37 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
             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')
+        );
+    }
 }