diff --git a/src/Shortcodes/FileShortcodeProvider.php b/src/Shortcodes/FileShortcodeProvider.php index 29cf9b62..ed77d7ae 100644 --- a/src/Shortcodes/FileShortcodeProvider.php +++ b/src/Shortcodes/FileShortcodeProvider.php @@ -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 @@ -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']; } } diff --git a/tests/php/Shortcodes/FileShortcodeProviderTest.php b/tests/php/Shortcodes/FileShortcodeProviderTest.php index 8e9505cf..16c7c4ec 100644 --- a/tests/php/Shortcodes/FileShortcodeProviderTest.php +++ b/tests/php/Shortcodes/FileShortcodeProviderTest.php @@ -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.' + ); + } }