From bd9ebb72a0fb91ed2ca3d977f5b88efd6d0f7740 Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Tue, 11 Jul 2023 12:37:36 +1200 Subject: [PATCH 1/3] FIX getCachedMarkup returns NULL instead of string if file doesn't exist --- src/Shortcodes/FileShortcodeProvider.php | 8 +++-- .../Shortcodes/FileShortcodeProviderTest.php | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Shortcodes/FileShortcodeProvider.php b/src/Shortcodes/FileShortcodeProvider.php index 29cf9b62..42feb332 100644 --- a/src/Shortcodes/FileShortcodeProvider.php +++ b/src/Shortcodes/FileShortcodeProvider.php @@ -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']; } } 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.' + ); + } } From 69cc7edae15165ed5051d7587ac5219f84ec8e58 Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Tue, 11 Jul 2023 14:28:18 +1200 Subject: [PATCH 2/3] FIX --- src/Shortcodes/FileShortcodeProvider.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Shortcodes/FileShortcodeProvider.php b/src/Shortcodes/FileShortcodeProvider.php index 42feb332..60af995a 100644 --- a/src/Shortcodes/FileShortcodeProvider.php +++ b/src/Shortcodes/FileShortcodeProvider.php @@ -140,13 +140,10 @@ protected static function getCachedMarkup($cache, $cacheKey, $arguments): string { $item = $cache->get($cacheKey); $assetStore = Injector::inst()->get(AssetStore::class); - if ($item - && !empty($item['filename']) - && $assetStore->exists($item['filename'], $item['hash']) - && $item['markup']) { + if ($item && !empty($item['filename']) && $item['markup']) { // Initiate a protected asset grant if necessary $allowSessionGrant = static::getGrant(null, $arguments); - if ($allowSessionGrant) { + if ($allowSessionGrant && $assetStore->exists($item['filename'], $item['hash'])) { $assetStore->grant($item['filename'], $item['hash']); return $item['markup']; } From 7f8057ea9af1a11fc8e4dbc3e24fdae84f97dc8b Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Tue, 11 Jul 2023 14:29:48 +1200 Subject: [PATCH 3/3] FIX --- src/Shortcodes/FileShortcodeProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shortcodes/FileShortcodeProvider.php b/src/Shortcodes/FileShortcodeProvider.php index 60af995a..30326a71 100644 --- a/src/Shortcodes/FileShortcodeProvider.php +++ b/src/Shortcodes/FileShortcodeProvider.php @@ -140,7 +140,7 @@ protected static function getCachedMarkup($cache, $cacheKey, $arguments): string { $item = $cache->get($cacheKey); $assetStore = Injector::inst()->get(AssetStore::class); - if ($item && !empty($item['filename']) && $item['markup']) { + if ($item && $item['markup'] && !empty($item['filename'])) { // Initiate a protected asset grant if necessary $allowSessionGrant = static::getGrant(null, $arguments); if ($allowSessionGrant && $assetStore->exists($item['filename'], $item['hash'])) {