diff --git a/src/Shortcodes/FileShortcodeProvider.php b/src/Shortcodes/FileShortcodeProvider.php index 29cf9b62..30326a71 100644 --- a/src/Shortcodes/FileShortcodeProvider.php +++ b/src/Shortcodes/FileShortcodeProvider.php @@ -139,11 +139,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 && $item['markup'] && !empty($item['filename'])) { // Initiate a protected asset grant if necessary $allowSessionGrant = static::getGrant(null, $arguments); - if ($allowSessionGrant) { - Injector::inst()->get(AssetStore::class)->grant($item['filename'], $item['hash']); + if ($allowSessionGrant && $assetStore->exists($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 90c79c5e..a671c49c 100644 --- a/tests/php/Shortcodes/FileShortcodeProviderTest.php +++ b/tests/php/Shortcodes/FileShortcodeProviderTest.php @@ -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.' + ); + } }