2017-07-31 08:04:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\Tests\HTMLEditor;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-10-03 03:08:24 +02:00
|
|
|
use SilverStripe\Core\Manifest\Module;
|
2017-07-31 08:04:20 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\TinyMCECombinedGenerator;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
|
2017-08-01 03:59:35 +02:00
|
|
|
use SilverStripe\View\SSViewer;
|
2017-07-31 08:04:20 +02:00
|
|
|
|
|
|
|
class TinyMCECombinedGeneratorTest extends SapphireTest
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2017-07-31 08:04:20 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// 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/');
|
2018-01-12 04:25:02 +01:00
|
|
|
Director::config()->set('alternate_public_dir', ''); // Disable public dir
|
2017-08-03 03:20:39 +02:00
|
|
|
SSViewer::config()->set('themes', [SSViewer::DEFAULT_THEME]);
|
2017-10-03 03:08:24 +02:00
|
|
|
TinyMCEConfig::config()
|
|
|
|
->set('base_dir', 'tinymce')
|
|
|
|
->set('editor_css', [ 'mycode/editor.css' ]);
|
2017-07-31 08:04:20 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function tearDown(): void
|
2017-08-03 03:20:39 +02:00
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
// Flush test configs
|
|
|
|
HTMLEditorConfig::set_config('testconfig', null);
|
|
|
|
}
|
|
|
|
|
2017-07-31 08:04:20 +02:00
|
|
|
public function testConfig()
|
|
|
|
{
|
2018-01-16 19:39:30 +01:00
|
|
|
$module = new Module(Director::baseFolder() . '/mycode', Director::baseFolder());
|
2017-07-31 08:04:20 +02:00
|
|
|
// Disable nonces
|
|
|
|
$c = new TinyMCEConfig();
|
|
|
|
$c->setTheme('testtheme');
|
|
|
|
$c->setOption('language', 'en');
|
2017-09-01 02:20:10 +02:00
|
|
|
$c->disablePlugins('table', 'emoticons', 'paste', 'code', 'link', 'importcss', 'lists');
|
2017-07-31 08:04:20 +02:00
|
|
|
$c->enablePlugins(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-07-31 08:04:20 +02:00
|
|
|
'plugin1' => 'mycode/plugin1.js', //
|
|
|
|
'plugin2' => '/anotherbase/mycode/plugin2.js',
|
|
|
|
'plugin3' => 'https://www.google.com/mycode/plugin3.js',
|
|
|
|
'plugin4' => null,
|
|
|
|
'plugin5' => null,
|
|
|
|
'plugin6' => '/basedir/mycode/plugin6.js',
|
|
|
|
'plugin7' => '/basedir/mycode/plugin7.js',
|
2017-10-03 03:08:24 +02:00
|
|
|
'plugin8' => $module->getResource('plugin8.js'),
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-07-31 08:04:20 +02:00
|
|
|
);
|
|
|
|
HTMLEditorConfig::set_config('testconfig', $c);
|
|
|
|
|
|
|
|
// Get config for this
|
|
|
|
/** @var TinyMCECombinedGenerator $generator */
|
|
|
|
$generator = Injector::inst()->create(TinyMCECombinedGenerator::class);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertMatchesRegularExpression('#_tinymce/tinymce-testconfig-[0-9a-z]{10,10}#', $generator->generateFilename($c));
|
2017-07-31 08:04:20 +02:00
|
|
|
$content = $generator->generateContent($c);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-08-16 05:56:20 +02:00
|
|
|
"var baseURL = baseTag.length ? baseTag[0].baseURI : 'http://www.mysite.com/basedir/';\n",
|
|
|
|
$content
|
|
|
|
);
|
2017-07-31 08:04:20 +02:00
|
|
|
// Main script file
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString("/* tinymce.js */\n", $content);
|
2017-07-31 08:04:20 +02:00
|
|
|
// Locale file
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString("/* en.js */\n", $content);
|
2017-07-31 08:04:20 +02:00
|
|
|
// Local plugins
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString("/* plugin1.js */\n", $content);
|
|
|
|
$this->assertStringContainsString("/* plugin4.min.js */\n", $content);
|
|
|
|
$this->assertStringContainsString("/* plugin4/langs/en.js */\n", $content);
|
|
|
|
$this->assertStringContainsString("/* plugin5.js */\n", $content);
|
|
|
|
$this->assertStringContainsString("/* plugin6.js */\n", $content);
|
2017-10-03 03:08:24 +02:00
|
|
|
// module-resource plugin
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString("/* plugin8.js */\n", $content);
|
2017-07-31 08:04:20 +02:00
|
|
|
// Exclude non-local plugins
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('plugin2.js', $content);
|
|
|
|
$this->assertStringNotContainsString('plugin3.js', $content);
|
2017-07-31 08:04:20 +02:00
|
|
|
// Exclude missing file
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('plugin7.js', $content);
|
2017-07-31 08:04:20 +02:00
|
|
|
|
|
|
|
// Check themes
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString("/* theme.js */\n", $content);
|
|
|
|
$this->assertStringContainsString("/* testtheme/langs/en.js */\n", $content);
|
2017-07-31 08:04:20 +02:00
|
|
|
|
2017-10-03 03:08:24 +02:00
|
|
|
// Check plugin links included
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-07-31 08:04:20 +02:00
|
|
|
<<<EOS
|
2022-12-14 04:08:47 +01:00
|
|
|
tinymce.each('tinymce/langs/en.js,mycode/plugin1.js,tinymce/plugins/plugin4/plugin.min.js,tinymce/plugins/plugin4/langs/en.js,tinymce/plugins/plugin5/plugin.js,mycode/plugin6.js,_resources/mycode/plugin8.js?m=
|
2017-10-03 03:08:24 +02:00
|
|
|
EOS
|
|
|
|
,
|
|
|
|
$content
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check theme links included
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-10-03 03:08:24 +02:00
|
|
|
<<<EOS
|
|
|
|
tinymce/themes/testtheme/theme.js,tinymce/themes/testtheme/langs/en.js'.split(','),function(f){tinymce.ScriptLoader.markDone(baseURL+f);});
|
2017-07-31 08:04:20 +02:00
|
|
|
EOS
|
|
|
|
,
|
|
|
|
$content
|
|
|
|
);
|
|
|
|
}
|
2017-08-03 03:20:39 +02:00
|
|
|
|
|
|
|
public function testFlush()
|
|
|
|
{
|
|
|
|
// Disable nonces
|
|
|
|
$c = new TinyMCEConfig();
|
|
|
|
$c->setTheme('testtheme');
|
|
|
|
$c->setOption('language', 'en');
|
2017-09-01 02:20:10 +02:00
|
|
|
$c->disablePlugins('table', 'emoticons', 'paste', 'code', 'link', 'importcss', 'lists');
|
2017-08-03 03:20:39 +02:00
|
|
|
$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));
|
|
|
|
}
|
2017-07-31 08:04:20 +02:00
|
|
|
}
|