Passed
Pull Request — master (#358)
by
unknown
02:35
created

testUpdateStatusFlagsFluentInivisible()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
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\Forms\SiteTreeURLSegmentField;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\ORM\ArrayList;
13
use SilverStripe\View\ArrayData;
14
use TractorCow\Fluent\Extension\FluentDirectorExtension;
15
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
16
use TractorCow\Fluent\Model\Domain;
17
use TractorCow\Fluent\Model\Locale;
18
use TractorCow\Fluent\State\FluentState;
19
20
class FluentSiteTreeExtensionTest extends SapphireTest
21
{
22
    protected static $fixture_file = 'FluentSiteTreeExtensionTest.yml';
23
24
    protected static $required_extensions = [
25
        SiteTree::class => [
26
            FluentSiteTreeExtension::class,
27
        ],
28
    ];
29
30
    protected function setUp()
31
    {
32
        parent::setUp();
33
        Config::modify()
34
            ->set(Director::class, 'alternate_base_url', 'http://mocked')
35
            ->set(FluentDirectorExtension::class, 'disable_default_prefix', false);
36
37
        // Clear cache
38
        Locale::clearCached();
39
        Domain::clearCached();
40
        FluentState::singleton()
41
            ->setLocale('de_DE')
42
            ->setIsDomainMode(false);
43
    }
44
45
    public function testGetLocaleInformation()
46
    {
47
        /** @var Page|FluentSiteTreeExtension $page */
48
        $page = $this->objFromFixture(Page::class, 'nz-page');
49
        $result = $page->LocaleInformation('en_NZ');
50
51
        $this->assertInstanceOf(ArrayData::class, $result);
52
        $this->assertEquals([
53
            'Locale' => 'en_NZ',
54
            'LocaleRFC1766' => 'en-NZ',
55
            'Title' => 'English (New Zealand)',
56
            'LanguageNative' => 'English',
57
            'Language' => 'en',
58
            'Link' => '/newzealand/a-page/',
59
            'AbsoluteLink' => 'http://mocked/newzealand/a-page/',
60
            'LinkingMode' => 'link',
61
            'URLSegment' => 'newzealand'
62
        ], $result->toMap());
63
    }
64
65
    public function testGetLocales()
66
    {
67
        /** @var Page|FluentSiteTreeExtension $page */
68
        $page = $this->objFromFixture(Page::class, 'nz-page');
69
        $result = $page->Locales();
70
71
        $this->assertInstanceOf(ArrayList::class, $result);
72
        $this->assertCount(5, $result);
73
        $this->assertListEquals([
74
            ['Locale' => 'en_NZ'],
75
            ['Locale' => 'de_DE'],
76
            ['Locale' => 'en_US'],
77
            ['Locale' => 'es_ES'],
78
            ['Locale' => 'zh_CN'],
79
        ], $result);
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 testUpdateStatusFlagsFluentInivisible()
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('fluentinvisible', $flags));
164
    }
165
166
    public function testUpdateCMSFields()
167
    {
168
        /** @var Page|FluentSiteTreeExtension $page */
169
        $page = $this->objFromFixture('Page', 'home');
170
        $fields = new FieldList();
171
172
        $page->updateCMSFields($fields);
173
174
        $this->assertNotNull($fields->fieldByName('LocaleStatusMessage'));
175
    }
176
177
    public function testUpdateCMSActionsInherited()
178
    {
179
        /** @var Page|FluentSiteTreeExtension $page */
180
        $page = $this->objFromFixture('Page', 'home');
181
        $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

181
        /** @scrutinizer ignore-call */ 
182
        $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...
182
183
        /** @var \SilverStripe\Forms\CompositeField $majorActions */
184
        $majorActions = $actions->fieldByName('MajorActions');
185
186
        $this->assertNotNull($majorActions);
187
188
        if ($majorActions === null) {
189
            return;
190
        }
191
192
        $actionSave = $majorActions->getChildren()->fieldByName('action_save');
193
        $actionPublish = $majorActions->getChildren()->fieldByName('action_publish');
194
195
        $this->assertNotNull($actionSave);
196
        $this->assertNotNull($actionPublish);
197
198
        if ($actionSave === null || $actionPublish === null) {
199
            return;
200
        }
201
202
        $this->assertEquals('Copy to draft', $actionSave->Title());
203
        $this->assertEquals('Copy & publish', $actionPublish->Title());
204
    }
205
206
    public function testUpdateCMSActionsDrafted()
207
    {
208
        /** @var Page|FluentSiteTreeExtension $page */
209
        $page = $this->objFromFixture('Page', 'about');
210
        $actions = $page->getCMSActions();
211
212
        /** @var \SilverStripe\Forms\CompositeField $majorActions */
213
        $majorActions = $actions->fieldByName('MajorActions');
214
215
        $this->assertNotNull($majorActions);
216
217
        if ($majorActions === null) {
218
            return;
219
        }
220
221
        $actionSave = $majorActions->getChildren()->fieldByName('action_save');
222
        $actionPublish = $majorActions->getChildren()->fieldByName('action_publish');
223
224
        $this->assertNotNull($actionSave);
225
        $this->assertNotNull($actionPublish);
226
227
        if ($actionSave === null || $actionPublish === null) {
228
            return;
229
        }
230
231
        $this->assertEquals('Saved', $actionSave->Title());
232
        $this->assertEquals('Save & publish', $actionPublish->Title());
233
    }
234
235
    /**
236
     * @param string $localeCode
237
     * @param string $fixture
238
     * @param string $expected
239
     * @dataProvider localePrefixUrlProvider
240
     */
241
    public function testAddLocalePrefixToUrlSegment($localeCode, $fixture, $expected)
242
    {
243
        FluentState::singleton()
244
            ->setLocale($localeCode)
245
            ->setIsDomainMode(true);
246
247
        /** @var FieldList $fields */
248
        $fields = $this->objFromFixture(Page::class, $fixture)->getCMSFields();
249
250
        /** @var SiteTreeURLSegmentField $segmentField */
251
        $segmentField = $fields->fieldByName('Root.Main.URLSegment');
252
        $this->assertInstanceOf(SiteTreeURLSegmentField::class, $segmentField);
253
254
        $this->assertSame($expected, $segmentField->getURLPrefix());
255
    }
256
257
    /**
258
     * @return array[]
259
     */
260
    public function localePrefixUrlProvider()
261
    {
262
        return [
263
            'locale_with_domain' => ['en_US', 'about', 'http://www.example.com/usa/'],
264
            'locale_without_domain' => ['zh_CN', 'about', 'http://mocked/zh_CN/'],
265
            'locale_withalias_and_parent_page' => ['de_DE', 'staff', 'http://www.example.de/german/about-us/'],
266
        ];
267
    }
268
}
269