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

BUG Make sure the cache shortcode value is used in FileShortcodeProvider #598

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/Shortcodes/FileShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,32 @@ public static function handle_shortcode($arguments, $content, $parser, $shortcod
*/
protected static function getCachedMarkup($cache, $cacheKey, $arguments): string
{
// Retrieve the cache markup
$item = $cache->get($cacheKey);
if (empty($item)) {
// This is a cache miss
return '';
}

// Destructure our cache entry
$markup = $item['markup'] ?? '';
$filename = $item['filename'] ?? '';
$hash = $item['hash'] ?? '';
$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 && $assetStore->exists($item['filename'], $item['hash'])) {
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
$assetStore->grant($item['filename'], $item['hash']);
return $item['markup'];
}

// Validate the cache entry
if (empty($markup) || empty($filename) || empty($hash) || !$assetStore->exists($filename, $hash)) {
// Cache entry is wrong, delete it
$cache->delete($cacheKey);
return '';
}
return '';

// Shortcode can be configured to allow session grant to files even if the file is protected
if (static::getGrant(null, $arguments)) {
$assetStore->grant($filename, $hash);
}

return $markup;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use SilverStripe\Assets\Image;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\View\HTML;
use SilverStripe\View\Parsers\ShortcodeHandler;
use SilverStripe\View\Parsers\ShortcodeParser;

Expand Down
28 changes: 28 additions & 0 deletions tests/php/Shortcodes/FileShortcodeProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ protected function setUp(): void
Config::inst()->set(SiteTree::class, 'create_default_pages', true);
ErrorPage::singleton()->requireDefaultRecords();
}

FileShortcodeProvider::getCache()->clear();
}

protected function tearDown(): void
Expand Down Expand Up @@ -179,4 +181,30 @@ public function testMarkupHasStringValue()
'Test that shortcode with invalid file ID is not parsed.'
);
}

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

// Prewarm cache with nonsense value
$markup = '<img src="cacheHit">';
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
$key = FileShortcodeProvider::getCacheKey(['id' => (string)$testFileID], '');
FileShortcodeProvider::getCache()->set($key, [
'markup' => $markup,
'filename' => $tuple['Filename'],
'hash' => $tuple['Hash']
]);

// Try to retrieve short code ... which should hit the cache
$parser = new ShortcodeParser();
$parser->register('file_link', [FileShortcodeProvider::class, 'handle_shortcode']);
$fileShortcode = sprintf('[file_link,id=%d]', $testFileID);
$this->assertEquals(
$markup,
$parser->parse(sprintf('[file_link,id=%d]', $testFileID)),
'Value Stored in the FileShortcodeProvider cache is returned by the shortcode.'
);
}
}