Completed
Push — master ( 84107b...f0cb22 )
by Robbie
11s
created

LocaleTest::testGetSiblings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace TractorCow\Fluent\Tests\Model;
4
5
use SilverStripe\Dev\SapphireTest;
6
use TractorCow\Fluent\Model\Domain;
7
use TractorCow\Fluent\Model\Locale;
8
use TractorCow\Fluent\State\FluentState;
9
10
class LocaleTest extends SapphireTest
11
{
12
    protected static $fixture_file = 'LocaleTest.yml';
13
14
    public function setUp()
15
    {
16
        parent::setUp();
17
18
        // Clear cache
19
        Locale::clearCached();
20
        Domain::clearCached();
21
        FluentState::singleton()
22
            ->setLocale('es_US')
23
            ->setDomain('fluent.co.nz')
24
            ->setIsDomainMode(true);
25
    }
26
27
    public function testGetDefaultWithoutArguments()
28
    {
29
        $result = Locale::getDefault();
30
31
        $this->assertInstanceOf(Locale::class, $result);
32
        // Note: default_sort order is included here
33
        $this->assertSame('en_AU', $result->Locale, 'First Locale with IsDefault true is returned');
34
    }
35
36
    public function testGetDefaultWithDomainArgument()
37
    {
38
        // spanish has_one default locale
39
        /** @var Domain $domain */
40
        $domain = $this->objFromFixture(Domain::class, 'spanish');
41
        $result = Locale::getDefault($domain->Domain);
42
43
        $this->assertInstanceOf(Locale::class, $result);
44
        $this->assertSame('es_US', $result->Locale, 'Domain respects has_one to DefaultLocale');
45
46
        // kiwi doesn't has_one to any default, but the IsDefault is a child
47
        /** @var Domain $domain2 */
48
        $domain2 = $this->objFromFixture(Domain::class, 'kiwi');
49
        $result2 = Locale::getDefault($domain2->Domain);
50
51
        $this->assertInstanceOf(Locale::class, $result2);
52
        $this->assertSame('en_AU', $result2->Locale, 'First Locale in Domain with IsDefault true is returned');
53
    }
54
55
    /**
56
     * @dataProvider isLocaleProvider
57
     * @param string $locale
58
     * @param string $input
59
     * @param bool $expected
60
     */
61
    public function testIsLocale($locale, $input, $expected)
62
    {
63
        $localeObj = Locale::create()->setField('Locale', $locale);
64
        $this->assertSame($expected, $localeObj->isLocale($input));
65
    }
66
67
    /**
68
     * @return array[]
69
     */
70
    public function isLocaleProvider()
71
    {
72
        return [
73
            ['en_NZ', 'en_NZ', true],
74
            ['en_nz', 'en-NZ', true],
75
            ['en-NZ', 'en_nz', true],
76
            ['en-nz', 'en-nz', true],
77
            ['en_NZ', 'en-NZ-1990', true],
78
            ['en_NZ', 'en_AU', false],
79
            ['en_NZ', 'fr-fr-1990', false],
80
        ];
81
    }
82
83
    public function testGetNativeName()
84
    {
85
        $this->assertSame('Spanish', Locale::getByLocale('es_US')->getNativeName());
86
    }
87
88 View Code Duplication
    public function testGetBaseURLContainsDomainAndURLSegmentForNonDefaultLocale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        // es_ES has a domain but is not the default locale for that domain
91
        $result = Locale::getByLocale('es_ES')->getBaseURL();
92
        $this->assertContains('fluent.es', $result, "Locale's domain is in the URL");
93
        $this->assertContains('/es/', $result, 'URL segment for non-default locale is in the URL');
94
95
        // Turning off domain mode removes domain but not prefix
96
        FluentState::singleton()->setIsDomainMode(false);
97
        $result = Locale::getByLocale('es_ES')->getBaseURL();
98
        $this->assertNotContains('fluent.es', $result, "Locale's domain is in the URL");
99
        $this->assertContains('/es/', $result, 'URL segment for non-default locale is in the URL');
100
    }
101
102 View Code Duplication
    public function testGetBaseURLOnlyContainsDomainForDefaultLocale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        // es_US has a domain and is the default
105
        $result = Locale::getByLocale('es_US')->getBaseURL();
106
        $this->assertContains('fluent.es', $result, "Locale's domain is in the URL");
107
        $this->assertNotContains('/es-usa/', $result, 'URL segment is not in the URL for default locales');
108
109
        // When domain mode is turned off, prefix is now necessary
110
        FluentState::singleton()->setIsDomainMode(false);
111
        $result = Locale::getByLocale('es_US')->getBaseURL();
112
        $this->assertNotContains('fluent.es', $result, "Domain not used");
113
        $this->assertContains('/es-usa/', $result, 'URL Segment necessary for non-global default');
114
    }
115
116
    public function testGetSiblings()
117
    {
118
        $esUS = Locale::getByLocale('es_US');
119
        $this->assertDOSEquals([
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Dev\SapphireTest::assertDOSEquals() has been deprecated: 4.0.0:5.0.0 Use assertListEquals() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

119
        /** @scrutinizer ignore-deprecated */ $this->assertDOSEquals([

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
120
            [ 'Locale' => 'es_US' ],
121
            [ 'Locale' => 'es_ES' ],
122
        ], $esUS->getSiblingLocales());
123
124
        // Test without domain mode
125
        FluentState::singleton()->setIsDomainMode(false);
126
127
        $esUS = Locale::getByLocale('es_US');
128
        $this->assertDOSEquals([
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Dev\SapphireTest::assertDOSEquals() has been deprecated: 4.0.0:5.0.0 Use assertListEquals() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

128
        /** @scrutinizer ignore-deprecated */ $this->assertDOSEquals([

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
129
            [ 'Locale' => 'es_US' ],
130
            [ 'Locale' => 'es_ES' ],
131
            [ 'Locale' => 'en_NZ' ],
132
            [ 'Locale' => 'en_AU' ],
133
        ], $esUS->getSiblingLocales());
134
    }
135
136
    public function testGetIsDefault()
137
    {
138
        $esUS = Locale::getByLocale('es_US');
139
        $esES = Locale::getByLocale('es_ES');
140
        $enNZ = Locale::getByLocale('en_NZ');
141
        $enAU = Locale::getByLocale('en_AU');
142
143
        // In domain mode, two are default
144
        $this->assertTrue($esUS->getIsDefault()); // Locale.DefaultLocale = this
145
        $this->assertTrue($enAU->getIsDefault()); // IsGlobalDefault = 1
146
        $this->assertFalse($enNZ->getIsDefault());
147
        $this->assertFalse($esES->getIsDefault());
148
149
        // In non-domain mode, only one default
150
        FluentState::singleton()->setIsDomainMode(false);
151
        $this->assertFalse($esUS->getIsDefault());
152
        $this->assertTrue($enAU->getIsDefault()); // IsGlobalDefault = 1
153
        $this->assertFalse($enNZ->getIsDefault());
154
        $this->assertFalse($esES->getIsDefault());
155
    }
156
157
    public function testGetIsOnlyLocale()
158
    {
159
        $esUS = Locale::getByLocale('es_US');
160
        $esES = Locale::getByLocale('es_ES');
161
162
        $this->assertFalse($esUS->getIsOnlyLocale());
163
        $this->assertFalse($esUS->getIsOnlyLocale());
164
165
        // Delete esES will affect this
166
        $esES->delete();
167
        Locale::clearCached();
168
        Domain::clearCached();
169
170
        $this->assertTrue($esUS->getIsOnlyLocale());
171
172
        // Turning off domain mode means this locale is joined with all the other domain locales
173
        FluentState::singleton()->setIsDomainMode(false);
174
        $this->assertFalse($esUS->getIsOnlyLocale());
175
    }
176
177
    public function testGlobalDefaultCheckedOnFirstLocale()
178
    {
179
        Locale::get()->removeAll();
180
        Locale::clearCached();
181
182
        $firstLocale = new Locale;
183
184
        /** @var SilverStripe\Forms\FieldList $fields  */
185
        $fields = $firstLocale->getCMSFields();
186
187
        /** @var SilverStripe\Forms\CheckboxField $checkbox */
188
        $checkbox = $fields->fieldByName('Root.Main.IsGlobalDefault');
189
        $this->assertTrue((bool) $checkbox->Value());
190
    }
191
}
192