testIsPublishedInLocale()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
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;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree 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...
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\ORM\ValidationException;
9
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
10
use TractorCow\Fluent\Extension\FluentVersionedExtension;
11
use TractorCow\Fluent\Model\Domain;
12
use TractorCow\Fluent\Model\Locale;
13
use TractorCow\Fluent\State\FluentState;
14
15
class FluentVersionedExtensionTest extends SapphireTest
16
{
17
    protected static $fixture_file = 'FluentVersionedExtensionTest.yml';
18
19
    protected static $required_extensions = [
20
        SiteTree::class => [
21
            FluentSiteTreeExtension::class,
22
        ],
23
    ];
24
25
    protected function setUp(): void
26
    {
27
        parent::setUp();
28
29
        // Clear cache
30
        Locale::clearCached();
31
        Domain::clearCached();
32
        (new FluentVersionedExtension)->flushCache();
33
34
        FluentState::singleton()
35
            ->setLocale('en_NZ')
36
            ->setIsDomainMode(false);
37
    }
38
39
    public function testIsDraftedInLocale()
40
    {
41
        FluentState::singleton()->withState(function (FluentState $newState) {
42
            $newState
43
                ->setLocale('en_NZ')
44
                ->setIsDomainMode(false);
45
46
            /** @var Page $page */
47
            $page = $this->objFromFixture(Page::class, 'home');
48
49
            $this->assertTrue($page->isDraftedInLocale());
50
        });
51
    }
52
53
    public function testIsPublishedInLocale()
54
    {
55
        FluentState::singleton()->withState(function (FluentState $newState) {
56
            $newState
57
                ->setLocale('en_NZ')
58
                ->setIsDomainMode(false);
59
60
            /** @var Page $page */
61
            $page = $this->objFromFixture(Page::class, 'home');
62
63
            $this->assertTrue($page->isPublishedInLocale());
64
        });
65
    }
66
67
    public function testExistsInLocale()
68
    {
69
        FluentState::singleton()->withState(function (FluentState $newState) {
70
            $newState
71
                ->setLocale('en_NZ')
72
                ->setIsDomainMode(false);
73
74
            /** @var Page $page */
75
            $page = $this->objFromFixture(Page::class, 'home');
76
77
            $this->assertTrue($page->existsInLocale());
78
        });
79
    }
80
81
    public function testExistsInLocaleReturnsTheRightValueFromCache()
82
    {
83
        /** @var Page $page */
84
        $page = $this->objFromFixture(Page::class, 'home');
85
86
        //warm up cache
87
        $this->assertTrue($page->existsInLocale());
88
        $this->assertTrue($page->existsInLocale('en_NZ'));
89
        $this->assertFalse($page->existsInLocale('de_AT'), 'Homepage does not exist in de_AT');
90
91
        //get results from cache
92
        $this->assertTrue($page->existsInLocale());
93
        $this->assertTrue($page->existsInLocale('en_NZ'));
94
        $this->assertFalse($page->existsInLocale('de_AT'), 'Homepage does not exist in de_AT, cache does not return false');
95
    }
96
97
    public function testSourceLocaleIsCurrentWhenPageExistsInIt()
98
    {
99
        FluentState::singleton()->withState(function (FluentState $newState) {
100
            $newState
101
                ->setLocale('en_NZ')
102
                ->setIsDomainMode(false);
103
104
            // Read from the locale that the page exists in already
105
            /** @var Page $page */
106
            $page = $this->objFromFixture(Page::class, 'home');
107
108
            $this->assertEquals('en_NZ', $page->getSourceLocale()->Locale);
109
        });
110
    }
111
112
    public function testLocalisedStageCacheIsUsedForIsLocalisedInLocale()
113
    {
114
        /** @var Page $page */
115
        $page = $this->objFromFixture(Page::class, 'home');
116
117
        /** @var FluentVersionedExtension $extension */
118
        $extension = $this->getMockBuilder(FluentVersionedExtension::class)
119
            ->setMethods(['findRecordInLocale'])
120
            ->getMock();
121
        $extension->setOwner($page);
122
123
        // We only expect one call to this method, because subsequent calls should be cached
124
        $extension->expects($this->once())->method('findRecordInLocale')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on TractorCow\Fluent\Extens...luentVersionedExtension. ( Ignorable by Annotation )

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

124
        $extension->/** @scrutinizer ignore-call */ 
125
                    expects($this->once())->method('findRecordInLocale')->willReturn(true);

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...
125
126
        // Initial request
127
        $result = $extension->isPublishedInLocale('en_NZ');
128
        $this->assertSame(true, $result, 'Original method result is returned');
129
130
        // Checking the cache
131
        $result2 = $extension->isPublishedInLocale('en_NZ');
132
        $this->assertSame(true, $result2, 'Cached result is returned');
133
    }
134
135
    public function testIdsInLocaleCacheIsUsedForIsLocalisedInLocale()
136
    {
137
        // Optimistically generate the cache
138
        FluentVersionedExtension::prepoulateIdsInLocale('en_NZ', Page::class, true, true);
139
140
        /** @var Page $page */
141
        $page = $this->objFromFixture(Page::class, 'home');
142
143
        /** @var FluentVersionedExtension $extension */
144
        $extension = $this->getMockBuilder(FluentVersionedExtension::class)
145
            ->setMethods(['findRecordInLocale'])
146
            ->getMock();
147
        $extension->setOwner($page);
148
149
        // We expect the lookup method to never get called, because the results are optimistically cached
150
        $extension->expects($this->never())->method('findRecordInLocale');
151
        $this->assertTrue($extension->isPublishedInLocale('en_NZ'), 'Fixtured page is published');
152
    }
153
154
    /**
155
     * @throws ValidationException
156
     */
157
    public function testStagesDifferInLocale(): void
158
    {
159
        $pageId = FluentState::singleton()->withState(function (FluentState $state): int {
160
            $state->setLocale(null);
161
162
            $page = Page::create();
163
            $page->Title = 'Test page stages differ';
164
            $page->URLSegment = 'test-page-stages-differ';
165
166
            // Not in DB
167
            $this->assertFalse($page->stagesDifferInLocale());
168
169
            return (int) $page->write();
170
        });
171
172
        /** @var Page $page */
173
        $page = Page::get()->byID($pageId);
174
175
        // Not Localised in Draft
176
        $this->assertFalse($page->stagesDifferInLocale());
177
178
        // Localise to Draft
179
        $page->write();
180
181
        // Not Localised in Live (draft only)
182
        $this->assertTrue($page->stagesDifferInLocale());
183
184
        // Publish
185
        $page->publishRecursive();
186
187
        // Localised in both Draft and Live (same content)
188
        $this->assertFalse($page->stagesDifferInLocale());
189
190
        // Update draft content
191
        $page->MetaDescription = 'New description';
192
        $page->write();
193
194
        // Draft has newer content
195
        $this->assertTrue($page->stagesDifferInLocale());
196
197
        // Publish
198
        $page->publishRecursive();
199
200
        // Same content on Draft and Live
201
        $this->assertFalse($page->stagesDifferInLocale());
202
203
        // Unpublish
204
        $page->doUnpublish();
205
206
        // No Live version
207
        $this->assertTrue($page->stagesDifferInLocale());
208
    }
209
}
210