Skip to content

Commit

Permalink
FIX getCachedMarkup returns NULL instead of string if file doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Jul 11, 2023
1 parent 7db8cde commit bd9ebb7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Shortcodes/FileShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,15 @@ public static function handle_shortcode($arguments, $content, $parser, $shortcod
protected static function getCachedMarkup($cache, $cacheKey, $arguments): string
{
$item = $cache->get($cacheKey);
if ($item && !empty($item['filename'])) {
$assetStore = Injector::inst()->get(AssetStore::class);
if ($item
&& !empty($item['filename'])
&& $assetStore->exists($item['filename'], $item['hash'])
&& $item['markup']) {
// Initiate a protected asset grant if necessary
$allowSessionGrant = static::getGrant(null, $arguments);
if ($allowSessionGrant) {
Injector::inst()->get(AssetStore::class)->grant($item['filename'], $item['hash']);
$assetStore->grant($item['filename'], $item['hash']);
return $item['markup'];
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/php/Shortcodes/FileShortcodeProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,37 @@ public function testOnlyGrantsAccessWhenConfiguredTo()
$parser->parse(sprintf('[file_link,id=%d]', $testFile->ID));
$this->assertFalse($assetStore->isGranted($testFile));
}

public function testMarkupHasStringValue()
{
$testFile = $this->objFromFixture(File::class, 'asdf');
$testFileID = $testFile->ID;
$tuple = $testFile->File->getValue();

$assetStore = Injector::inst()->get(AssetStore::class);

$parser = new ShortcodeParser();
$parser->register('file_link', [FileShortcodeProvider::class, 'handle_shortcode']);

FileShortcodeProvider::config()->set('allow_session_grant', true);

$fileShortcode = sprintf('[file_link,id=%d]', $testFileID);
$this->assertEquals(
$testFile->Link(),
$parser->parse(sprintf('[file_link,id=%d]', $testFileID)),
'Test that shortcode with existing file ID is parsed.'
);

$testFile->deleteFile();
$this->assertFalse(
$assetStore->exists($tuple['Filename'], $tuple['Hash']),
'Test that file was removed from Asset store.'
);

$this->assertEquals(
'',
$parser->parse(sprintf('[file_link,id=%d]', $testFileID)),
'Test that shortcode with invalid file ID is not parsed.'
);
}
}

0 comments on commit bd9ebb7

Please sign in to comment.