Passed
Push — 4 ( 33a283...e59625 )
by Maxime
07:11
created

TinyMCEConfigTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 82
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testLanguagesValid() 0 21 4
A testGetContentCSS() 0 21 1
A testEditorIdentifier() 0 4 1
A testProvideI18nEntities() 0 20 1
1
<?php
2
3
namespace SilverStripe\Forms\Tests\HTMLEditor;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Core\Manifest\ModuleResourceLoader;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
9
10
class TinyMCEConfigTest extends SapphireTest
11
{
12
13
    public function testEditorIdentifier()
14
    {
15
        $config = TinyMCEConfig::get('myconfig');
16
        $this->assertEquals('myconfig', $config->getOption('editorIdentifier'));
17
    }
18
19
    /**
20
     * Ensure that all TinyMCEConfig.tinymce_lang are valid
21
     */
22
    public function testLanguagesValid()
23
    {
24
        $configDir = TinyMCEConfig::config()->get('base_dir');
25
        if (!$configDir) {
26
            $this->markTestSkipped("Test skipped without TinyMCE resources folder being installed");
27
        }
28
29
        $langs = Director::baseFolder() . '/' . ModuleResourceLoader::resourcePath($configDir) . '/langs';
30
31
        // Test all langs exist as real files
32
        foreach (TinyMCEConfig::config()->get('tinymce_lang') as $locale => $resource) {
33
            // Check valid
34
            $this->assertFileExists(
35
                "{$langs}/{$resource}.js",
36
                "Locale code {$locale} maps to {$resource}.js which exists"
37
            );
38
            // Check we don't simplify to locale when a specific version exists
39
            if (strpos($resource, '_') === false) {
40
                $this->assertFileNotExists(
41
                    "{$langs}/{$locale}.js",
42
                    "Locale code {$locale} doesn't map to simple {$resource}.js when a better {$locale}.js is available"
43
                );
44
            }
45
        }
46
    }
47
48
    public function testGetContentCSS()
49
    {
50
        TinyMCEConfig::config()->set('editor_css', [
51
            'silverstripe/framework:tests/php/Forms/HTMLEditor.css'
52
        ]);
53
54
        // Test default config
55
        $config = new TinyMCEConfig();
56
        $this->assertContains('silverstripe/framework:tests/php/Forms/HTMLEditor.css', $config->getContentCSS());
57
58
        // Test manual disable
59
        $config->setContentCSS([]);
60
        $this->assertEmpty($config->getContentCSS());
61
62
        // Test replacement config
63
        $config->setContentCSS([
64
            'silverstripe/framework:tests/php/Forms/HTMLEditor_another.css'
65
        ]);
66
        $this->assertEquals(
67
            [ 'silverstripe/framework:tests/php/Forms/HTMLEditor_another.css'],
68
            $config->getContentCSS()
69
        );
70
    }
71
72
    public function testProvideI18nEntities()
73
    {
74
        TinyMCEConfig::config()->set('image_size_presets', [
75
            ['i18n' => TinyMCEConfig::class . '.TEST', 'text' => 'Foo bar'],
76
            ['text' => 'No translation key'],
77
            ['i18n' => TinyMCEConfig::class . '.NO_TRANSLATION_TEXT'],
78
            ['i18n' => TinyMCEConfig::class . '.TEST_TWO', 'text' => 'Bar foo'],
79
        ]);
80
81
        $config = TinyMCEConfig::create();
82
        $translations = $config->provideI18nEntities();
83
84
        $this->assertEquals(
85
            3,
86
            sizeof($translations),
87
            'Only two presets have valid translation + the generic PIXEL_WIDTH one'
88
        );
89
        $this->assertEquals('Foo bar', $translations[TinyMCEConfig::class . '.TEST']);
90
        $this->assertEquals('Bar foo', $translations[TinyMCEConfig::class . '.TEST_TWO']);
91
        $this->assertEquals('{width} pixels', $translations[TinyMCEConfig::class . '.PIXEL_WIDTH']);
92
    }
93
}
94