Skip to content

Commit

Permalink
Finish fix
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jun 2, 2022
1 parent 43beea2 commit 42563c2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
21 changes: 17 additions & 4 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -1015,12 +1015,25 @@ function slash_item(string $item): ?string
{
$config = config(App::class);

if (property_exists($config, $item)) {
$configItem = $config->{$item};
if (! property_exists($config, $item)) {
return null;
}

if (! isset($configItem) || empty(trim($configItem))) {
return null;
$configItem = $config->{$item};

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;
}

return rtrim($configItem, '/') . '/';
Expand Down
26 changes: 23 additions & 3 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Config\Logger;
use Config\Modules;
use Kint;
use RuntimeException;
use stdClass;
use Tests\Support\Models\JobModel;

Expand Down Expand Up @@ -390,9 +391,28 @@ public function testReallyWritable()

public function testSlashItem()
{
$this->assertSame('/', slash_item('cookiePath')); // slash already there
$this->assertNull(null, 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()
Expand Down

0 comments on commit 42563c2

Please sign in to comment.