Passed
Pull Request — master (#301)
by
unknown
01:42
created

testUpdateStatusFlagsDrafted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 13
rs 9.4285
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\CompositeField;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\FormAction;
13
use SilverStripe\ORM\ArrayList;
14
use SilverStripe\View\ArrayData;
15
use TractorCow\Fluent\Extension\FluentDirectorExtension;
16
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
17
use TractorCow\Fluent\Model\Domain;
18
use TractorCow\Fluent\Model\Locale;
19
use TractorCow\Fluent\State\FluentState;
20
21
class FluentSiteTreeExtensionTest extends SapphireTest
22
{
23
    protected static $fixture_file = 'FluentSiteTreeExtensionTest.yml';
24
25
    protected static $required_extensions = [
26
        SiteTree::class => [
27
            FluentSiteTreeExtension::class,
28
        ],
29
    ];
30
31
    protected function setUp()
32
    {
33
        parent::setUp();
34
        Config::modify()
35
            ->set(Director::class, 'alternate_base_url', 'http://mocked')
36
            ->set(FluentDirectorExtension::class, 'disable_default_prefix', false);
37
38
        // Clear cache
39
        Locale::clearCached();
40
        Domain::clearCached();
41
        FluentState::singleton()
42
            ->setLocale('de_DE')
43
            ->setIsDomainMode(false);
44
    }
45
46
    public function testGetLocaleInformation()
47
    {
48
        /** @var Page|FluentSiteTreeExtension $page */
49
        $page = $this->objFromFixture(Page::class, 'nz-page');
50
        $result = $page->LocaleInformation('en_NZ');
51
52
        $this->assertInstanceOf(ArrayData::class, $result);
53
        $this->assertEquals([
54
            'Locale' => 'en_NZ',
55
            'LocaleRFC1766' => 'en-NZ',
56
            'Title' => 'English (New Zealand)',
57
            'LanguageNative' => 'English',
58
            'Language' => 'en',
59
            'Link' => '/newzealand/a-page/',
60
            'AbsoluteLink' => 'http://mocked/newzealand/a-page/',
61
            'LinkingMode' => 'link',
62
            'URLSegment' => 'newzealand'
63
        ], $result->toMap());
64
    }
65
66
    public function testGetLocales()
67
    {
68
        /** @var Page|FluentSiteTreeExtension $page */
69
        $page = $this->objFromFixture(Page::class, 'nz-page');
70
        $result = $page->Locales();
71
72
        $this->assertInstanceOf(ArrayList::class, $result);
73
        $this->assertCount(4, $result);
74
        $this->assertListEquals([
75
            ['Locale' => 'en_NZ'],
76
            ['Locale' => 'de_DE'],
77
            ['Locale' => 'en_US'],
78
            ['Locale' => 'es_ES'],
79
        ], $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

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

154
        $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...
155
    }
156
157
    public function testUpdateStatusFlagsInherited()
158
    {
159
        /** @var Page|FluentSiteTreeExtension $page */
160
        $page = $this->objFromFixture('Page', 'home');
161
        $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

161
        /** @scrutinizer ignore-call */ 
162
        $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...
162
163
        $this->assertTrue(array_key_exists('fluentinherited', $flags));
164
    }
165
166
    public function testUpdateStatusFlagsDrafted()
167
    {
168
        /** @var Page|FluentSiteTreeExtension $page */
169
        $page = $this->objFromFixture('Page', 'about');
170
        $flags = $page->getStatusFlags();
171
172
        $this->assertTrue(array_key_exists('modified', $flags));
173
174
        if (!array_key_exists('modified', $flags)) {
175
            return;
176
        }
177
178
        $this->assertEquals('Locale drafted', $flags['modified']['text']);
179
    }
180
181
    public function testUpdateCMSFields()
182
    {
183
        /** @var Page|FluentSiteTreeExtension $page */
184
        $page = $this->objFromFixture('Page', 'home');
185
        $fields = new FieldList();
186
187
        $page->updateCMSFields($fields);
188
189
        $this->assertNotNull($fields->fieldByName('LocaleStatusMessage'));
190
    }
191
192 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...
193
    {
194
        /** @var Page|FluentSiteTreeExtension $page */
195
        $page = $this->objFromFixture('Page', 'home');
196
        $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

196
        /** @scrutinizer ignore-call */ 
197
        $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...
197
198
        /** @var \SilverStripe\Forms\CompositeField $majorActions */
199
        $majorActions = $actions->fieldByName('MajorActions');
200
201
        $this->assertNotNull($majorActions);
202
203
        if ($majorActions === null) {
204
            return;
205
        }
206
207
        $actionSave = $majorActions->getChildren()->fieldByName('action_save');
208
        $actionPublish = $majorActions->getChildren()->fieldByName('action_publish');
209
210
        $this->assertNotNull($actionSave);
211
        $this->assertNotNull($actionPublish);
212
213
        if ($actionSave === null || $actionPublish === null) {
214
            return;
215
        }
216
217
        $this->assertEquals('Copy to draft', $actionSave->Title());
218
        $this->assertEquals('Copy & publish', $actionPublish->Title());
219
    }
220
221 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...
222
    {
223
        /** @var Page|FluentSiteTreeExtension $page */
224
        $page = $this->objFromFixture('Page', 'about');
225
        $actions = $page->getCMSActions();
226
227
        /** @var \SilverStripe\Forms\CompositeField $majorActions */
228
        $majorActions = $actions->fieldByName('MajorActions');
229
230
        $this->assertNotNull($majorActions);
231
232
        if ($majorActions === null) {
233
            return;
234
        }
235
236
        $actionSave = $majorActions->getChildren()->fieldByName('action_save');
237
        $actionPublish = $majorActions->getChildren()->fieldByName('action_publish');
238
239
        $this->assertNotNull($actionSave);
240
        $this->assertNotNull($actionPublish);
241
242
        if ($actionSave === null || $actionPublish === null) {
243
            return;
244
        }
245
246
        $this->assertEquals('Saved', $actionSave->Title());
247
        $this->assertEquals('Save & publish', $actionPublish->Title());
248
    }
249
}
250