Completed
Push — master ( 597319...02e318 )
by Damian
15s
created

testUpdateCMSActionsInherited()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 14

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 3
nop 0
dl 27
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace TractorCow\Fluent\Tests\Extension;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\ORM\ArrayList;
12
use SilverStripe\View\ArrayData;
13
use TractorCow\Fluent\Extension\FluentDirectorExtension;
14
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
15
use TractorCow\Fluent\Model\Domain;
16
use TractorCow\Fluent\Model\Locale;
17
use TractorCow\Fluent\State\FluentState;
18
19
class FluentSiteTreeExtensionTest extends SapphireTest
20
{
21
    protected static $fixture_file = 'FluentSiteTreeExtensionTest.yml';
22
23
    protected static $required_extensions = [
24
        SiteTree::class => [
25
            FluentSiteTreeExtension::class,
26
        ],
27
    ];
28
29
    protected function setUp()
30
    {
31
        parent::setUp();
32
        Config::modify()
33
            ->set(Director::class, 'alternate_base_url', 'http://mocked')
34
            ->set(FluentDirectorExtension::class, 'disable_default_prefix', false);
35
36
        // Clear cache
37
        Locale::clearCached();
38
        Domain::clearCached();
39
        FluentState::singleton()
40
            ->setLocale('de_DE')
41
            ->setIsDomainMode(false);
42
    }
43
44
    public function testGetLocaleInformation()
45
    {
46
        /** @var Page|FluentSiteTreeExtension $page */
47
        $page = $this->objFromFixture(Page::class, 'nz-page');
48
        $result = $page->LocaleInformation('en_NZ');
49
50
        $this->assertInstanceOf(ArrayData::class, $result);
51
        $this->assertEquals([
52
            'Locale' => 'en_NZ',
53
            'LocaleRFC1766' => 'en-NZ',
54
            'Title' => 'English (New Zealand)',
55
            'LanguageNative' => 'English',
56
            'Language' => 'en',
57
            'Link' => '/newzealand/a-page/',
58
            'AbsoluteLink' => 'http://mocked/newzealand/a-page/',
59
            'LinkingMode' => 'link',
60
            'URLSegment' => 'newzealand'
61
        ], $result->toMap());
62
    }
63
64
    public function testGetLocales()
65
    {
66
        /** @var Page|FluentSiteTreeExtension $page */
67
        $page = $this->objFromFixture(Page::class, 'nz-page');
68
        $result = $page->Locales();
69
70
        $this->assertInstanceOf(ArrayList::class, $result);
71
        $this->assertCount(4, $result);
72
        $this->assertListEquals([
73
            ['Locale' => 'en_NZ'],
74
            ['Locale' => 'de_DE'],
75
            ['Locale' => 'en_US'],
76
            ['Locale' => 'es_ES'],
77
        ], $result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type array; however, parameter $list of SilverStripe\Dev\SapphireTest::assertListEquals() does only seem to accept SilverStripe\ORM\SS_List, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        ], /** @scrutinizer ignore-type */ $result);
Loading history...
78
    }
79
80
    /**
81
     * Tests for url generation
82
     *
83
     * @return array list of tests with values:
84
     *  - domain (or false for non-domain mode)
85
     *  - locale
86
     *  - disable_default_prefix flag
87
     *  - page id
88
     *  - expected link
89
     */
90
    public function provideURLTests()
91
    {
92
        return [
93
            // Non-domain tests
94
            [null, 'de_DE', false, 'home', '/'],
95
            [null, 'de_DE', false, 'about', '/german/about-us/'],
96
            [null, 'de_DE', false, 'staff', '/german/about-us/my-staff/'],
97
            // Since de_DE is the only locale on the www.example.de domain, ensure that the locale
98
            // isn't unnecessarily added to the link.
99
            // In this case disable_default_prefix is ignored
100
            // See https://github.com/tractorcow/silverstripe-fluent/issues/75
101
            ['www.example.de', 'de_DE', false, 'home', '/'],
102
            ['www.example.de', 'de_DE', false, 'about', '/about-us/'],
103
            ['www.example.de', 'de_DE', false, 'staff', '/about-us/my-staff/'],
104
105
            // Test domains with multiple locales
106
            //  - es_ES non default locale
107
            ['www.example.com', 'es_ES', false, 'home', '/es_ES/'],
108
            ['www.example.com', 'es_ES', false, 'about', '/es_ES/about-us/'],
109
            ['www.example.com', 'es_ES', false, 'staff', '/es_ES/about-us/my-staff/'],
110
            //  - en_US default locale
111
            ['www.example.com', 'en_US', false, 'home', '/'],
112
            ['www.example.com', 'en_US', false, 'about', '/usa/about-us/'],
113
            ['www.example.com', 'en_US', false, 'staff', '/usa/about-us/my-staff/'],
114
            //  - en_US default locale, but with disable_default_prefix on
115
            ['www.example.com', 'en_US', true, 'home', '/'],
116
            ['www.example.com', 'en_US', true, 'about', '/about-us/'],
117
            ['www.example.com', 'en_US', true, 'staff', '/about-us/my-staff/'],
118
119
            // Test cross-domain links include the opposing domain
120
            // - to default locale
121
            ['www.example.de', 'en_US', true, 'home', 'http://www.example.com/'],
122
            ['www.example.de', 'en_US', true, 'staff', 'http://www.example.com/about-us/my-staff/'],
123
            // - to non defalut locale
124
            ['www.example.de', 'es_ES', true, 'home', 'http://www.example.com/es_ES/'],
125
            ['www.example.de', 'es_ES', true, 'staff', 'http://www.example.com/es_ES/about-us/my-staff/'],
126
        ];
127
    }
128
129
    /**
130
     * Test that URLS for pages are generated correctly
131
     *
132
     * @dataProvider provideURLTests
133
     * @param string $domain
134
     * @param string $locale
135
     * @param bool $prefixDisabled
136
     * @param string $pageName
137
     * @param string $url
138
     */
139
    public function testFluentURLs($domain, $locale, $prefixDisabled, $pageName, $url)
140
    {
141
        // Set state
142
        FluentState::singleton()
143
            ->setLocale($locale)
144
            ->setDomain($domain)
145
            ->setIsDomainMode(!empty($domain));
146
        // Set url generation option
147
        Config::modify()
148
            ->set(FluentDirectorExtension::class, 'disable_default_prefix', $prefixDisabled);
149
150
        /** @var Page|FluentSiteTreeExtension $page */
151
        $page = $this->objFromFixture('Page', $pageName);
152
        $this->assertEquals($url, $page->Link());
0 ignored issues
show
Bug introduced by
The method Link() does not exist on TractorCow\Fluent\Extens...FluentSiteTreeExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
        $this->assertEquals($url, $page->/** @scrutinizer ignore-call */ Link());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
    }
154
155
    public function testUpdateStatusFlagsInherited()
156
    {
157
        /** @var Page|FluentSiteTreeExtension $page */
158
        $page = $this->objFromFixture('Page', 'home');
159
        $flags = $page->getStatusFlags();
0 ignored issues
show
Bug introduced by
The method getStatusFlags() does not exist on TractorCow\Fluent\Extens...FluentSiteTreeExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
        /** @scrutinizer ignore-call */ 
160
        $flags = $page->getStatusFlags();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160
161
        $this->assertTrue(array_key_exists('fluentinherited', $flags));
162
    }
163
164
    public function testUpdateStatusFlagsDrafted()
165
    {
166
        /** @var Page|FluentSiteTreeExtension $page */
167
        $page = $this->objFromFixture('Page', 'about');
168
        $flags = $page->getStatusFlags();
169
170
        $this->assertTrue(array_key_exists('modified', $flags));
171
172
        if (!array_key_exists('modified', $flags)) {
173
            return;
174
        }
175
176
        $this->assertEquals('Locale drafted', $flags['modified']['text']);
177
    }
178
179
    public function testUpdateCMSFields()
180
    {
181
        /** @var Page|FluentSiteTreeExtension $page */
182
        $page = $this->objFromFixture('Page', 'home');
183
        $fields = new FieldList();
184
185
        $page->updateCMSFields($fields);
186
187
        $this->assertNotNull($fields->fieldByName('LocaleStatusMessage'));
188
    }
189
190 View Code Duplication
    public function testUpdateCMSActionsInherited()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        /** @var Page|FluentSiteTreeExtension $page */
193
        $page = $this->objFromFixture('Page', 'home');
194
        $actions = $page->getCMSActions();
0 ignored issues
show
Bug introduced by
The method getCMSActions() does not exist on TractorCow\Fluent\Extens...FluentSiteTreeExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

194
        /** @scrutinizer ignore-call */ 
195
        $actions = $page->getCMSActions();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
195
196
        /** @var \SilverStripe\Forms\CompositeField $majorActions */
197
        $majorActions = $actions->fieldByName('MajorActions');
198
199
        $this->assertNotNull($majorActions);
200
201
        if ($majorActions === null) {
202
            return;
203
        }
204
205
        $actionSave = $majorActions->getChildren()->fieldByName('action_save');
206
        $actionPublish = $majorActions->getChildren()->fieldByName('action_publish');
207
208
        $this->assertNotNull($actionSave);
209
        $this->assertNotNull($actionPublish);
210
211
        if ($actionSave === null || $actionPublish === null) {
212
            return;
213
        }
214
215
        $this->assertEquals('Copy to draft', $actionSave->Title());
216
        $this->assertEquals('Copy & publish', $actionPublish->Title());
217
    }
218
219 View Code Duplication
    public function testUpdateCMSActionsDrafted()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
    {
221
        /** @var Page|FluentSiteTreeExtension $page */
222
        $page = $this->objFromFixture('Page', 'about');
223
        $actions = $page->getCMSActions();
224
225
        /** @var \SilverStripe\Forms\CompositeField $majorActions */
226
        $majorActions = $actions->fieldByName('MajorActions');
227
228
        $this->assertNotNull($majorActions);
229
230
        if ($majorActions === null) {
231
            return;
232
        }
233
234
        $actionSave = $majorActions->getChildren()->fieldByName('action_save');
235
        $actionPublish = $majorActions->getChildren()->fieldByName('action_publish');
236
237
        $this->assertNotNull($actionSave);
238
        $this->assertNotNull($actionPublish);
239
240
        if ($actionSave === null || $actionPublish === null) {
241
            return;
242
        }
243
244
        $this->assertEquals('Saved', $actionSave->Title());
245
        $this->assertEquals('Save & publish', $actionPublish->Title());
246
    }
247
}
248