Passed
Push — master ( f0cb22...699e9b )
by Robbie
01:46
created

FluentSiteTreeExtensionTest::testFluentURLs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

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

151
        $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...
152
    }
153
}
154