diff --git a/system/Common.php b/system/Common.php index 257c238d2b42..30e28a1dc840 100644 --- a/system/Common.php +++ b/system/Common.php @@ -1013,10 +1013,26 @@ function single_service(string $name, ...$params) */ function slash_item(string $item): ?string { - $config = config(App::class); + $config = config(App::class); + + if (! property_exists($config, $item)) { + return null; + } + $configItem = $config->{$item}; - if (! isset($configItem) || empty(trim($configItem))) { + if (! is_scalar($configItem)) { + throw new RuntimeException(sprintf( + 'Cannot convert "%s::$%s" of type "%s" to type "string".', + App::class, + $item, + gettype($configItem) + )); + } + + $configItem = trim((string) $configItem); + + if ($configItem === '') { return $configItem; } diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index 9410b13322c1..4f27a8ab2f2c 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -30,6 +30,7 @@ use Config\Modules; use Config\Services; use Kint; +use RuntimeException; use stdClass; use Tests\Support\Models\JobModel; @@ -392,9 +393,28 @@ public function testReallyWritable() public function testSlashItem() { - $this->assertSame('/', slash_item('cookiePath')); // slash already there - $this->assertSame('', slash_item('cookieDomain')); // empty, so untouched - $this->assertSame('en/', slash_item('defaultLocale')); // slash appended + $this->assertSame('/', slash_item('cookiePath')); // / + $this->assertSame('', slash_item('cookieDomain')); // '' + $this->assertSame('en/', slash_item('defaultLocale')); // en + $this->assertSame('7200/', slash_item('sessionExpiration')); // int 7200 + $this->assertSame('', slash_item('negotiateLocale')); // false + $this->assertSame('1/', slash_item('cookieHTTPOnly')); // true + } + + public function testSlashItemOnInexistentItem() + { + $this->assertNull(slash_item('foo')); + $this->assertNull(slash_item('bar')); + $this->assertNull(slash_item('cookieDomains')); + $this->assertNull(slash_item('indices')); + } + + public function testSlashItemThrowsErrorOnNonStringableItem() + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Cannot convert "Config\\App::$supportedLocales" of type "array" to type "string".'); + + slash_item('supportedLocales'); } protected function injectSessionMock()