Passed
Pull Request — master (#348)
by Robbie
02:28
created

testUpdateCMSActionsDrafted()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 27
rs 8.5806
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 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
    public function testUpdateCMSActionsInherited()
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
    public function testUpdateCMSActionsDrafted()
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
    /**
251
     * @param string $localeCode
252
     * @param string $fixture
253
     * @param string $expected
254
     * @dataProvider localePrefixUrlProvider
255
     */
256
    public function testAddLocalePrefixToUrlSegment($localeCode, $fixture, $expected)
257
    {
258
        FluentState::singleton()
259
            ->setLocale($localeCode)
260
            ->setIsDomainMode(true);
261
262
        /** @var FieldList $fields */
263
        $fields = $this->objFromFixture(Page::class, $fixture)->getCMSFields();
264
265
        /** @var SiteTreeURLSegmentField $segmentField */
266
        $segmentField = $fields->fieldByName('Root.Main.URLSegment');
267
        $this->assertInstanceOf(SiteTreeURLSegmentField::class, $segmentField);
268
269
        $this->assertSame($expected, $segmentField->getURLPrefix());
270
    }
271
272
    /**
273
     * @return array[]
274
     */
275
    public function localePrefixUrlProvider()
276
    {
277
        return [
278
            'locale_with_domain' => ['en_US', 'about', 'http://www.example.com/usa/'],
279
            'locale_without_domain' => ['zh_CN', 'about', 'http://mocked/zh_CN/'],
280
            'locale_withalias_and_parent_page' => ['de_DE', 'staff', 'http://www.example.de/german/about-us/'],
281
        ];
282
    }
283
}
284