Passed
Pull Request — master (#343)
by
unknown
02:04
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\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 testUpdateCMSFields()
158
    {
159
        /** @var Page|FluentSiteTreeExtension $page */
160
        $page = $this->objFromFixture('Page', 'home');
161
        $fields = new FieldList();
162
163
        $page->updateCMSFields($fields);
164
165
        $this->assertNotNull($fields->fieldByName('LocaleStatusMessage'));
166
    }
167
168
    public function testUpdateCMSActionsInherited()
169
    {
170
        /** @var Page|FluentSiteTreeExtension $page */
171
        $page = $this->objFromFixture('Page', 'home');
172
        $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

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