Skip to content

Commit 42563c2

Browse files
committed
Finish fix
1 parent 43beea2 commit 42563c2

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

system/Common.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,12 +1015,25 @@ function slash_item(string $item): ?string
10151015
{
10161016
$config = config(App::class);
10171017

1018-
if (property_exists($config, $item)) {
1019-
$configItem = $config->{$item};
1018+
if (! property_exists($config, $item)) {
1019+
return null;
10201020
}
10211021

1022-
if (! isset($configItem) || empty(trim($configItem))) {
1023-
return null;
1022+
$configItem = $config->{$item};
1023+
1024+
if (! is_scalar($configItem)) {
1025+
throw new RuntimeException(sprintf(
1026+
'Cannot convert "%s::$%s" of type "%s" to type "string".',
1027+
App::class,
1028+
$item,
1029+
gettype($configItem)
1030+
));
1031+
}
1032+
1033+
$configItem = trim((string) $configItem);
1034+
1035+
if ($configItem === '') {
1036+
return $configItem;
10241037
}
10251038

10261039
return rtrim($configItem, '/') . '/';

tests/system/CommonFunctionsTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Config\Logger;
3131
use Config\Modules;
3232
use Kint;
33+
use RuntimeException;
3334
use stdClass;
3435
use Tests\Support\Models\JobModel;
3536

@@ -390,9 +391,28 @@ public function testReallyWritable()
390391

391392
public function testSlashItem()
392393
{
393-
$this->assertSame('/', slash_item('cookiePath')); // slash already there
394-
$this->assertNull(null, slash_item('cookieDomain')); // empty, so untouched
395-
$this->assertSame('en/', slash_item('defaultLocale')); // slash appended
394+
$this->assertSame('/', slash_item('cookiePath')); // /
395+
$this->assertSame('', slash_item('cookieDomain')); // ''
396+
$this->assertSame('en/', slash_item('defaultLocale')); // en
397+
$this->assertSame('7200/', slash_item('sessionExpiration')); // int 7200
398+
$this->assertSame('', slash_item('negotiateLocale')); // false
399+
$this->assertSame('1/', slash_item('cookieHTTPOnly')); // true
400+
}
401+
402+
public function testSlashItemOnInexistentItem()
403+
{
404+
$this->assertNull(slash_item('foo'));
405+
$this->assertNull(slash_item('bar'));
406+
$this->assertNull(slash_item('cookieDomains'));
407+
$this->assertNull(slash_item('indices'));
408+
}
409+
410+
public function testSlashItemThrowsErrorOnNonStringableItem()
411+
{
412+
$this->expectException(RuntimeException::class);
413+
$this->expectExceptionMessage('Cannot convert "Config\\App::$supportedLocales" of type "array" to type "string".');
414+
415+
slash_item('supportedLocales');
396416
}
397417

398418
protected function injectSessionMock()

0 commit comments

Comments
 (0)