Passed
Push — master ( fe84a0...f38bd7 )
by Robbie
05:03 queued 02:33
created

testStatusMessageInherited()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 14
rs 10
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\Debug;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\Forms\FieldList;
13
use SilverStripe\Forms\LiteralField;
14
use SilverStripe\ORM\ArrayList;
15
use SilverStripe\View\ArrayData;
16
use TractorCow\Fluent\Extension\FluentDirectorExtension;
17
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
18
use TractorCow\Fluent\Model\Domain;
19
use TractorCow\Fluent\Model\Locale;
20
use TractorCow\Fluent\State\FluentState;
21
22
class FluentSiteTreeExtensionTest extends SapphireTest
23
{
24
    protected static $fixture_file = 'FluentSiteTreeExtensionTest.yml';
25
26
    protected static $required_extensions = [
27
        SiteTree::class => [
28
            FluentSiteTreeExtension::class,
29
        ],
30
    ];
31
32
    protected function setUp()
33
    {
34
        parent::setUp();
35
        Config::modify()
36
            ->set(Director::class, 'alternate_base_url', 'http://mocked')
37
            ->set(FluentDirectorExtension::class, 'disable_default_prefix', false);
38
39
        // Clear cache
40
        Locale::clearCached();
41
        Domain::clearCached();
42
        FluentState::singleton()
43
            ->setLocale('de_DE')
44
            ->setIsDomainMode(false);
45
    }
46
47
    public function testGetLocaleInformation()
48
    {
49
        /** @var Page|FluentSiteTreeExtension $page */
50
        $page = $this->objFromFixture(Page::class, 'nz-page');
51
        $result = $page->LocaleInformation('en_NZ');
52
53
        $this->assertInstanceOf(ArrayData::class, $result);
54
        $this->assertEquals([
55
            'Locale' => 'en_NZ',
56
            'LocaleRFC1766' => 'en-NZ',
57
            'Title' => 'English (New Zealand)',
58
            'LanguageNative' => 'English',
59
            'Language' => 'en',
60
            'Link' => '/newzealand/a-page/',
61
            'AbsoluteLink' => 'http://mocked/newzealand/a-page/',
62
            'LinkingMode' => 'link',
63
            'URLSegment' => 'newzealand'
64
        ], $result->toMap());
65
    }
66
67
    public function testGetLocales()
68
    {
69
        /** @var Page|FluentSiteTreeExtension $page */
70
        $page = $this->objFromFixture(Page::class, 'nz-page');
71
        $result = $page->Locales();
72
73
        $this->assertInstanceOf(ArrayList::class, $result);
74
        $this->assertCount(5, $result);
75
        $this->assertListEquals([
76
            ['Locale' => 'en_NZ'],
77
            ['Locale' => 'de_DE'],
78
            ['Locale' => 'en_US'],
79
            ['Locale' => 'es_ES'],
80
            ['Locale' => 'zh_CN'],
81
        ], $result);
82
    }
83
84
    /**
85
     * Tests for url generation
86
     *
87
     * @return array list of tests with values:
88
     *  - domain (or false for non-domain mode)
89
     *  - locale
90
     *  - disable_default_prefix flag
91
     *  - page id
92
     *  - expected link
93
     */
94
    public function provideURLTests()
95
    {
96
        return [
97
            // Non-domain tests
98
            [null, 'de_DE', false, 'home', '/'],
99
            [null, 'de_DE', false, 'about', '/german/about-us/'],
100
            [null, 'de_DE', false, 'staff', '/german/about-us/my-staff/'],
101
            // Since de_DE is the only locale on the www.example.de domain, ensure that the locale
102
            // isn't unnecessarily added to the link.
103
            // In this case disable_default_prefix is ignored
104
            // See https://github.com/tractorcow/silverstripe-fluent/issues/75
105
            ['www.example.de', 'de_DE', false, 'home', '/'],
106
            ['www.example.de', 'de_DE', false, 'about', '/about-us/'],
107
            ['www.example.de', 'de_DE', false, 'staff', '/about-us/my-staff/'],
108
109
            // Test domains with multiple locales
110
            //  - es_ES non default locale
111
            ['www.example.com', 'es_ES', false, 'home', '/es_ES/'],
112
            ['www.example.com', 'es_ES', false, 'about', '/es_ES/about-us/'],
113
            ['www.example.com', 'es_ES', false, 'staff', '/es_ES/about-us/my-staff/'],
114
            //  - en_US default locale
115
            ['www.example.com', 'en_US', false, 'home', '/'],
116
            ['www.example.com', 'en_US', false, 'about', '/usa/about-us/'],
117
            ['www.example.com', 'en_US', false, 'staff', '/usa/about-us/my-staff/'],
118
            //  - en_US default locale, but with disable_default_prefix on
119
            ['www.example.com', 'en_US', true, 'home', '/'],
120
            ['www.example.com', 'en_US', true, 'about', '/about-us/'],
121
            ['www.example.com', 'en_US', true, 'staff', '/about-us/my-staff/'],
122
123
            // Test cross-domain links include the opposing domain
124
            // - to default locale
125
            ['www.example.de', 'en_US', true, 'home', 'http://www.example.com/'],
126
            ['www.example.de', 'en_US', true, 'staff', 'http://www.example.com/about-us/my-staff/'],
127
            // - to non defalut locale
128
            ['www.example.de', 'es_ES', true, 'home', 'http://www.example.com/es_ES/'],
129
            ['www.example.de', 'es_ES', true, 'staff', 'http://www.example.com/es_ES/about-us/my-staff/'],
130
        ];
131
    }
132
133
    /**
134
     * Test that URLS for pages are generated correctly
135
     *
136
     * @dataProvider provideURLTests
137
     * @param string $domain
138
     * @param string $locale
139
     * @param bool $prefixDisabled
140
     * @param string $pageName
141
     * @param string $url
142
     */
143
    public function testFluentURLs($domain, $locale, $prefixDisabled, $pageName, $url)
144
    {
145
        // Set state
146
        FluentState::singleton()
147
            ->setLocale($locale)
148
            ->setDomain($domain)
149
            ->setIsDomainMode(!empty($domain));
150
        // Set url generation option
151
        Config::modify()
152
            ->set(FluentDirectorExtension::class, 'disable_default_prefix', $prefixDisabled);
153
154
        /** @var Page|FluentSiteTreeExtension $page */
155
        $page = $this->objFromFixture('Page', $pageName);
156
        $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

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

163
        /** @scrutinizer ignore-call */ 
164
        $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...
164
165
        $this->assertTrue(array_key_exists('fluentinvisible', $flags));
166
    }
167
168
    public function testStatusMessageNotVisibile()
169
    {
170
        /** @var Page|FluentSiteTreeExtension $page */
171
        $page = $this->objFromFixture('Page', 'home');
172
        $fields = new FieldList();
173
174
        $page->updateCMSFields($fields);
175
176
        /** @var LiteralField $statusMessage */
177
        $statusMessage = $fields->fieldByName('LocaleStatusMessage');
178
179
        $this->assertNotNull($fields->fieldByName('LocaleStatusMessage'));
180
        $this->assertContains('This page is not visible', $statusMessage->getContent());
181
    }
182
183
    public function testStatusMessageInherited()
184
    {
185
        /** @var Page|FluentSiteTreeExtension $page */
186
        $page = $this->objFromFixture('Page', 'home');
187
        $page->config()->update('frontend_publish_required', false);
0 ignored issues
show
Bug introduced by
The method config() 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

187
        $page->/** @scrutinizer ignore-call */ 
188
               config()->update('frontend_publish_required', false);

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...
188
        $fields = new FieldList();
189
190
        $page->updateCMSFields($fields);
191
192
        /** @var LiteralField $statusMessage */
193
        $statusMessage = $fields->fieldByName('LocaleStatusMessage');
194
195
        $this->assertNotNull($fields->fieldByName('LocaleStatusMessage'));
196
        $this->assertContains('Content for this page may be inherited', $statusMessage->getContent());
197
    }
198
199
    public function testStatusMessageDrafted()
200
    {
201
        FluentState::singleton()->setLocale('en_NZ');
202
203
        /** @var Page|FluentSiteTreeExtension $page */
204
        $page = $this->objFromFixture('Page', 'home');
205
        $page->config()->update('frontend_publish_required', false);
206
        $page->write();
0 ignored issues
show
Bug introduced by
The method write() 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

206
        $page->/** @scrutinizer ignore-call */ 
207
               write();

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...
207
        $fields = new FieldList();
208
209
        $page->updateCMSFields($fields);
210
211
        /** @var LiteralField $statusMessage */
212
        $statusMessage = $fields->fieldByName('LocaleStatusMessage');
213
214
        $this->assertNotNull($fields->fieldByName('LocaleStatusMessage'));
215
        $this->assertContains('A draft has been created for this locale', $statusMessage->getContent());
216
    }
217
218
    public function testUpdateCMSActionsInherited()
219
    {
220
        /** @var Page|FluentSiteTreeExtension $page */
221
        $page = $this->objFromFixture('Page', 'home');
222
        $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

222
        /** @scrutinizer ignore-call */ 
223
        $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...
223
224
        /** @var \SilverStripe\Forms\CompositeField $majorActions */
225
        $majorActions = $actions->fieldByName('MajorActions');
226
227
        $this->assertNotNull($majorActions);
228
229
        if ($majorActions === null) {
230
            return;
231
        }
232
233
        $actionSave = $majorActions->getChildren()->fieldByName('action_save');
234
        $actionPublish = $majorActions->getChildren()->fieldByName('action_publish');
235
236
        $this->assertNotNull($actionSave);
237
        $this->assertNotNull($actionPublish);
238
239
        if ($actionSave === null || $actionPublish === null) {
240
            return;
241
        }
242
243
        $this->assertEquals('Copy to draft', $actionSave->Title());
244
        $this->assertEquals('Copy & publish', $actionPublish->Title());
245
    }
246
247
    public function testUpdateCMSActionsDrafted()
248
    {
249
        /** @var Page|FluentSiteTreeExtension $page */
250
        $page = $this->objFromFixture('Page', 'about');
251
        $actions = $page->getCMSActions();
252
253
        /** @var \SilverStripe\Forms\CompositeField $majorActions */
254
        $majorActions = $actions->fieldByName('MajorActions');
255
256
        $this->assertNotNull($majorActions);
257
258
        if ($majorActions === null) {
259
            return;
260
        }
261
262
        $actionSave = $majorActions->getChildren()->fieldByName('action_save');
263
        $actionPublish = $majorActions->getChildren()->fieldByName('action_publish');
264
265
        $this->assertNotNull($actionSave);
266
        $this->assertNotNull($actionPublish);
267
268
        if ($actionSave === null || $actionPublish === null) {
269
            return;
270
        }
271
272
        $this->assertEquals('Saved', $actionSave->Title());
273
        // The default value changed between SS 4.0 and 4.1 - assert it contains Publish instead of exact matching
274
        $this->assertContains('publish', strtolower($actionPublish->Title()));
275
    }
276
277
    /**
278
     * @param string $localeCode
279
     * @param string $fixture
280
     * @param string $expected
281
     * @dataProvider localePrefixUrlProvider
282
     */
283
    public function testAddLocalePrefixToUrlSegment($localeCode, $fixture, $expected)
284
    {
285
        FluentState::singleton()
286
            ->setLocale($localeCode)
287
            ->setIsDomainMode(true);
288
289
        /** @var FieldList $fields */
290
        $fields = $this->objFromFixture(Page::class, $fixture)->getCMSFields();
291
292
        /** @var SiteTreeURLSegmentField $segmentField */
293
        $segmentField = $fields->fieldByName('Root.Main.URLSegment');
294
        $this->assertInstanceOf(SiteTreeURLSegmentField::class, $segmentField);
295
296
        $this->assertSame($expected, $segmentField->getURLPrefix());
297
    }
298
299
    /**
300
     * @return array[]
301
     */
302
    public function localePrefixUrlProvider()
303
    {
304
        return [
305
            'locale_with_domain' => ['en_US', 'about', 'http://www.example.com/usa/'],
306
            'locale_without_domain' => ['zh_CN', 'about', 'http://mocked/zh_CN/'],
307
            'locale_alone_on_domain_nested' => ['de_DE', 'staff', 'http://www.example.de/about-us/'],
308
        ];
309
    }
310
}
311