ColorpickerSiteConfigExtensionTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetThemeOptionsExcluding() 0 40 1
A testDefaultValuesAreWrittenWhenEnabled() 0 8 1
A testDefaultValuesAreNotWrittenWhenDisabled() 0 8 1
1
<?php
2
3
namespace SilverStripe\Colorpicker\Tests\Extensions;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\SiteConfig\SiteConfig;
9
10
class ColorpickerSiteConfigExtensionTest extends SapphireTest
11
{
12
    protected $usesDatabase = true;
13
14
    /**
15
     * Ensure theme options are returned in the expected format without any excluded values
16
     */
17
    public function testGetThemeOptionsExcluding()
18
    {
19
        Config::modify()->set(SiteConfig::class, 'theme_colors', [
20
            'color1' => [
21
                'Title' => 'Color 1',
22
                'CSSClass' => 'color-1',
23
                'Color' => '#111111',
24
            ],
25
            'color2' => [
26
                'Title' => 'Color 2',
27
                'CSSClass' => 'color-2',
28
                'Color' => '#222222',
29
            ],
30
        ]);
31
        $siteConfig = SiteConfig::create();
32
33
        // Returns all colors by default
34
        $themeColors = $siteConfig->getThemeOptionsExcluding();
35
        $this->assertEquals([
36
            [
37
                'Title' => 'Color 1',
38
                'CSSClass' => 'color-1',
39
                'Color' => '#111111',
40
            ],
41
            [
42
                'Title' => 'Color 2',
43
                'CSSClass' => 'color-2',
44
                'Color' => '#222222',
45
            ],
46
        ], $themeColors);
47
48
        // Returns colors without excludedColors
49
        $themeColors = $siteConfig->getThemeOptionsExcluding(['color-1']);
50
        $this->assertEquals([
51
            [
52
                'Title' => 'Color 2',
53
                'CSSClass' => 'color-2',
54
                'Color' => '#222222',
55
            ],
56
        ], $themeColors);
57
    }
58
59
    public function testDefaultValuesAreNotWrittenWhenDisabled()
60
    {
61
        SiteConfig::config()->set('enable_theme_color_picker', false);
62
63
        $siteConfig = SiteConfig::create();
64
        $siteConfig->write();
65
66
        $this->assertEmpty($siteConfig->HeaderBackground, 'Color fields should not be written when disabled');
67
    }
68
69
    public function testDefaultValuesAreWrittenWhenEnabled()
70
    {
71
        SiteConfig::config()->set('enable_theme_color_picker', true);
72
73
        $siteConfig = SiteConfig::create();
74
        $siteConfig->write();
75
76
        $this->assertNotEmpty($siteConfig->HeaderBackground, 'Color fields should be written when enabled');
77
    }
78
}
79