Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX getCachedMarkup returns NULL instead of string if file doesn't exist #561

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Shortcodes/FileShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public static function handle_shortcode($arguments, $content, $parser, $shortcod
$url = $record->Link();
}

$url = $url ?? '';

// build the HTML tag
if ($content) {
// build some useful meta-data (file type and size) as data attributes
Expand Down Expand Up @@ -139,11 +141,12 @@ 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'])) {
// 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 @@ -146,4 +146,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.'
);
}
}