From 06efd2ac12d92bdfb08d23bcc317a83e9c369cd3 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Thu, 3 Aug 2017 13:20:39 +1200 Subject: [PATCH] ENHANCEMENT Ensure flush destroys temp tinymce files --- src/Forms/HTMLEditor/HTMLEditorConfig.php | 12 ++++--- .../HTMLEditor/TinyMCECombinedGenerator.php | 18 +++++++++- .../TinyMCECombinedGeneratorTest.php | 33 ++++++++++++++++++- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/Forms/HTMLEditor/HTMLEditorConfig.php b/src/Forms/HTMLEditor/HTMLEditorConfig.php index 10b59959a..50a5124a8 100644 --- a/src/Forms/HTMLEditor/HTMLEditorConfig.php +++ b/src/Forms/HTMLEditor/HTMLEditorConfig.php @@ -80,15 +80,19 @@ abstract class HTMLEditorConfig } /** - * Assign a new config for the given identifier + * Assign a new config, or clear existing, for the given identifier * * @param string $identifier A specific identifier - * @param HTMLEditorConfig $config + * @param HTMLEditorConfig $config Config to set, or null to clear * @return HTMLEditorConfig The assigned config */ - public static function set_config($identifier, HTMLEditorConfig $config) + public static function set_config($identifier, HTMLEditorConfig $config = null) { - self::$configs[$identifier] = $config; + if ($config) { + self::$configs[$identifier] = $config; + } else { + unset(self::$configs[$identifier]); + } return $config; } diff --git a/src/Forms/HTMLEditor/TinyMCECombinedGenerator.php b/src/Forms/HTMLEditor/TinyMCECombinedGenerator.php index ace718319..90fb5c116 100644 --- a/src/Forms/HTMLEditor/TinyMCECombinedGenerator.php +++ b/src/Forms/HTMLEditor/TinyMCECombinedGenerator.php @@ -6,14 +6,17 @@ use SilverStripe\Assets\Storage\GeneratedAssetHandler; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Convert; +use SilverStripe\Core\Flushable; +use SilverStripe\Core\Injector\Injectable; /** * Generates tinymce config using a combined file generated via a standard * SilverStripe {@link GeneratedAssetHandler} */ -class TinyMCECombinedGenerator implements TinyMCEScriptGenerator +class TinyMCECombinedGenerator implements TinyMCEScriptGenerator, Flushable { use Configurable; + use Injectable; /** * Named config @@ -201,4 +204,17 @@ class TinyMCECombinedGenerator implements TinyMCEScriptGenerator ); return $url; } + + /** + * This function is triggered early in the request if the "flush" query + * parameter has been set. Each class that implements Flushable implements + * this function which looks after it's own specific flushing functionality. + * + * @see FlushMiddleware + */ + public static function flush() + { + $dir = dirname(static::config()->get('filename_base')); + static::singleton()->getAssetHandler()->removeContent($dir); + } } diff --git a/tests/php/Forms/HTMLEditor/TinyMCECombinedGeneratorTest.php b/tests/php/Forms/HTMLEditor/TinyMCECombinedGeneratorTest.php index 46ceed868..ff47b8514 100644 --- a/tests/php/Forms/HTMLEditor/TinyMCECombinedGeneratorTest.php +++ b/tests/php/Forms/HTMLEditor/TinyMCECombinedGeneratorTest.php @@ -19,10 +19,17 @@ class TinyMCECombinedGeneratorTest extends SapphireTest // Set custom base_path for tinymce Director::config()->set('alternate_base_folder', __DIR__ . '/TinyMCECombinedGeneratorTest'); Director::config()->set('alternate_base_url', 'http://www.mysite.com/basedir/'); - SSViewer::config()->set('themes', [ SSViewer::DEFAULT_THEME ]); + SSViewer::config()->set('themes', [SSViewer::DEFAULT_THEME]); TinyMCEConfig::config()->set('base_dir', 'tinymce'); } + protected function tearDown() + { + parent::tearDown(); + // Flush test configs + HTMLEditorConfig::set_config('testconfig', null); + } + public function testConfig() { // Disable nonces @@ -83,4 +90,28 @@ EOS $content ); } + + public function testFlush() + { + // Disable nonces + $c = new TinyMCEConfig(); + $c->setTheme('testtheme'); + $c->setOption('language', 'en'); + $c->disablePlugins('table', 'emoticons', 'paste', 'code', 'link', 'importcss'); + $c->enablePlugins(['plugin1' => 'mycode/plugin1.js']); + HTMLEditorConfig::set_config('testconfig', $c); + + // Generate file for this + /** @var TinyMCECombinedGenerator $generator */ + $generator = Injector::inst()->create(TinyMCECombinedGenerator::class); + $generator->getScriptURL($c); + $filename = $generator->generateFilename($c); + + // Ensure content exists + $this->assertNotEmpty($generator->getAssetHandler()->getContent($filename)); + + // Flush should destroy this + TinyMCECombinedGenerator::flush(); + $this->assertEmpty($generator->getAssetHandler()->getContent($filename)); + } }