[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/tests/install/ -> ConfigTest.php (source)

   1  <?php
   2  
   3  /**
   4   * Checks config check functions
   5   */
   6  #[\PHPUnit\Framework\Attributes\Group('install')]
   7  class ConfigTest extends PHPUnit\Framework\TestCase {
   8  
   9      /**
  10       * Test (sort of) defining constants
  11       */
  12      public function test_correct_config() {
  13          $test = new \YOURLS\Config\Config(YOURLS_CONFIGFILE);
  14  
  15          // This should return a readable file
  16          $readable = is_readable($test->find_config());
  17          $this->assertTrue($readable);
  18          // For the record, $this->assertFileIsReadable() was introduced around PHPUnit 5.6
  19  
  20          // redefining YOURLS_ constants should not throw any error ("constant already defined...")
  21          // or define any new constants
  22          $consts = get_defined_constants(true);
  23          $before = $consts['user'];
  24          $test->define_core_constants();
  25          $consts = get_defined_constants(true);
  26          $after = $consts['user'];
  27          $this->assertSame($before,$after);
  28      }
  29  
  30      /**
  31       * Test incorrect config provided
  32       */
  33      public function test_incorrect_config() {
  34          $this->expectException(YOURLS\Exceptions\ConfigException::class);
  35          $this->expectExceptionMessageMatches('/User defined config not found at \'[0-9a-z]+\'/');
  36  
  37          $test = new \YOURLS\Config\Config(rand_str());
  38          $test->find_config();
  39      }
  40  
  41      /**
  42       * Test config not found
  43       */
  44      public function test_not_found_config() {
  45          $this->expectException(YOURLS\Exceptions\ConfigException::class);
  46          $this->expectExceptionMessage('Cannot find config.php. Please read the readme.html to learn how to install YOURLS');
  47  
  48          $test = new \YOURLS\Config\Config();
  49          $test->set_root(rand_str());
  50          $test->find_config();
  51      }
  52  
  53      /**
  54       * Missing constant triggers an exception naming it
  55       */
  56      public function test_missing_mandatory_constant() {
  57          $this->expectException(YOURLS\Exceptions\ConfigException::class);
  58          $this->expectExceptionMessageMatches('/YOURLS_NOT_DEFINED_1337/');
  59          (new \YOURLS\Config\Config())->check_mandatory_constants(['YOURLS_DB_USER', 'YOURLS_NOT_DEFINED_1337']);
  60      }
  61  
  62      /**
  63       * Defined constants pass the check
  64       */
  65      public function test_defined_mandatory_constants() {
  66          $this->expectNotToPerformAssertions();
  67          (new \YOURLS\Config\Config())->check_mandatory_constants(['YOURLS_DB_USER', 'YOURLS_SITE']);
  68      }
  69  
  70      /**
  71       * Unacceptable cookie key values
  72       *
  73       * @return array
  74       */
  75      public static function bad_cookie_keys(): array {
  76          return [
  77              'undefined'  => [null],
  78              'empty'      => [''],
  79              'sample'     => ['modify this text with something random'],
  80              'doc'        => ['qQ4KhL_pu|s@Zm7n#%:b^{A[vhm'],
  81              'not string' => [1337],
  82          ];
  83      }
  84  
  85      /**
  86       * @param mixed $key
  87       * @return void
  88       */
  89      #[\PHPUnit\Framework\Attributes\DataProvider('bad_cookie_keys')]
  90      public function test_bad_cookie_key(mixed $key): void {
  91          $this->expectException(YOURLS\Exceptions\ConfigException::class);
  92          (new \YOURLS\Config\Config())->check_cookie_key($key);
  93      }
  94  
  95      /**
  96       * A random long string is a valid cookie key
  97       */
  98      public function test_good_cookie_key() {
  99          $this->expectNotToPerformAssertions();
 100          (new \YOURLS\Config\Config())->check_cookie_key(bin2hex(random_bytes(16)));
 101      }
 102  
 103      /**
 104       * Test Init actions. Not sure this is a good idea, might become cumbersome to maintain?
 105       */
 106      public function test_init_defaults() {
 107          $test = new \YOURLS\Config\InitDefaults();
 108  
 109          $expected = array (
 110              'include_core_funcs' => true,
 111              'default_timezone' => true,
 112              'load_default_textdomain' => true,
 113              'check_maintenance_mode' => true,
 114              'fix_request_uri' => true,
 115              'redirect_ssl' => true,
 116              'include_db' => true,
 117              'include_cache' => true,
 118              'return_if_fast_init' => true,
 119              'get_all_options' => true,
 120              'register_shutdown' => true,
 121              'core_loaded' => true,
 122              'redirect_to_install' => true,
 123              'check_if_upgrade_needed' => true,
 124              'load_plugins' => true,
 125              'plugins_loaded_action' => true,
 126              'check_new_version' => true,
 127              'init_admin' => true,
 128          );
 129  
 130          $actual = get_class_vars(get_class($test));
 131  
 132          $this->assertSame($expected, $actual);
 133      }
 134  
 135  }


Generated: Sat Sep 5 05:13:00 2026 Cross-referenced by PHPXref 0.7.1