Passed
Push — master ( e73d62...b32499 )
by Robbie
04:11
created

testCustomThemeDefinitionsAreRespected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 10
rs 10
1
<?php
2
3
namespace SilverStripe\Subsites\Tests\Service;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Subsites\Model\Subsite;
8
use SilverStripe\Subsites\Service\ThemeResolver;
9
use SilverStripe\View\SSViewer;
10
11
class ThemeResolverTest extends SapphireTest
12
{
13
    protected $themeList = [
14
        '$public',
15
        'custom',
16
        'main',
17
        'backup',
18
        SSViewer::DEFAULT_THEME,
19
    ];
20
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
25
        // Setup known theme config
26
        Config::modify()->set(SSViewer::class, 'themes', $this->themeList);
27
    }
28
29
    public function testSubsiteWithoutThemeReturnsDefaultThemeList()
30
    {
31
        $subsite = new Subsite();
32
        $resolver = new ThemeResolver();
33
34
        $this->assertSame($this->themeList, $resolver->getThemeList($subsite));
35
    }
36
37
    public function testSubsiteWithCustomThemePrependsToList()
38
    {
39
        $subsite = new Subsite();
40
        $subsite->Theme = 'subsite';
0 ignored issues
show
Bug Best Practice introduced by
The property Theme does not exist on SilverStripe\Subsites\Model\Subsite. Since you implemented __set, consider adding a @property annotation.
Loading history...
41
42
        $resolver = new ThemeResolver();
43
44
        $expected = array_merge(['subsite'], $this->themeList);
45
46
        $this->assertSame($expected, $resolver->getThemeList($subsite));
47
    }
48
49
    public function testSubsiteWithCustomThemeDoesNotCascadeUpTheList()
50
    {
51
        $subsite = new Subsite();
52
        $subsite->Theme = 'main';
0 ignored issues
show
Bug Best Practice introduced by
The property Theme does not exist on SilverStripe\Subsites\Model\Subsite. Since you implemented __set, consider adding a @property annotation.
Loading history...
53
54
        $resolver = new ThemeResolver();
55
56
        $expected = [
57
            'main', // 'main' is moved to the top
58
            '$public', // $public is preserved
59
            // Anything above 'main' is removed
60
            'backup',
61
            SSViewer::DEFAULT_THEME,
62
        ];
63
64
        $this->assertSame($expected, $resolver->getThemeList($subsite));
65
    }
66
67
    /**
68
     * @dataProvider customThemeDefinitionsAreRespectedProvider
69
     */
70
    public function testCustomThemeDefinitionsAreRespected($themeOptions, $siteTheme, $expected)
71
    {
72
        Config::modify()->set(ThemeResolver::class, 'theme_options', $themeOptions);
73
74
        $subsite = new Subsite();
75
        $subsite->Theme = $siteTheme;
0 ignored issues
show
Bug Best Practice introduced by
The property Theme does not exist on SilverStripe\Subsites\Model\Subsite. Since you implemented __set, consider adding a @property annotation.
Loading history...
76
77
        $resolver = new ThemeResolver();
78
79
        $this->assertSame($expected, $resolver->getThemeList($subsite));
80
    }
81
82
    public function customThemeDefinitionsAreRespectedProvider()
83
    {
84
        return [
85
            // Simple
86
            [
87
                ['test' => $expected = [
88
                    'subsite',
89
                    'backup',
90
                    '$public',
91
                    SSViewer::DEFAULT_THEME,
92
                ]],
93
                'test',
94
                $expected
95
            ],
96
            // Many options
97
            [
98
                [
99
                    'aye' => [
100
                        'aye',
101
                        'thing',
102
                        SSViewer::DEFAULT_THEME,
103
                    ],
104
                    'bee' => $expected = [
105
                        'subsite',
106
                        'backup',
107
                        '$public',
108
                        SSViewer::DEFAULT_THEME,
109
                    ],
110
                    'sea' => [
111
                        'mer',
112
                        'ocean',
113
                        SSViewer::DEFAULT_THEME,
114
                    ],
115
                ],
116
                'bee',
117
                $expected
118
            ],
119
            // Conflicting with root definitions
120
            [
121
                ['main' => $expected = [
122
                    'subsite',
123
                    'backup',
124
                    '$public',
125
                    SSViewer::DEFAULT_THEME,
126
                ]],
127
                'main',
128
                $expected
129
            ],
130
            // Declaring a theme specifically should still work
131
            [
132
                ['test' => [
133
                    'subsite',
134
                    'backup',
135
                    '$public',
136
                    SSViewer::DEFAULT_THEME,
137
                ]],
138
                'other',
139
                array_merge(['other'], $this->themeList)
140
            ],
141
        ];
142
    }
143
}
144