InitialPageLocalisationTaskTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 77
c 1
b 0
f 0
dl 0
loc 182
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A publishStateProvider() 0 65 1
A getLocalisedPages() 0 18 3
A setUp() 0 7 1
A testInitialPageLocalisation() 0 71 5
1
<?php
2
3
namespace TractorCow\Fluent\Tests\Task;
4
5
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...
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\ORM\ValidationException;
9
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
10
use TractorCow\Fluent\Model\Locale;
11
use TractorCow\Fluent\State\FluentState;
12
use TractorCow\Fluent\Task\InitialPageLocalisationTask;
13
14
class InitialPageLocalisationTaskTest extends SapphireTest
15
{
16
    protected static $fixture_file = 'InitialPageLocalisationTaskTest.yml';
17
18
    protected function setUp(): void
19
    {
20
        FluentState::singleton()->withState(function (FluentState $state): void {
21
            // We don't want to localise pages yet - create only base records
22
            $state->setLocale(null);
23
24
            parent::setUp();
25
        });
26
    }
27
28
    /**
29
     * @param bool $publish
30
     * @param int $limit
31
     * @param array $localised
32
     * @param array $published
33
     * @dataProvider publishStateProvider
34
     */
35
    public function testInitialPageLocalisation(bool $publish, int $limit, array $localised, array $published): void
36
    {
37
        // Check base records
38
        $pages = FluentState::singleton()->withState(function (FluentState $state): array {
39
            $state->setLocale(null);
40
41
            // Publish some base records (too much effort to set up via fixture)
42
            $pagesToPublish = [
43
                'page1',
44
                'page2',
45
            ];
46
47
            foreach ($pagesToPublish as $identifier) {
48
                /** @var SiteTree $page */
49
                $page = $this->objFromFixture(SiteTree::class, $identifier);
50
                $page->publishRecursive();
51
            }
52
53
            return SiteTree::get()
54
                ->sort('Title', 'ASC')
55
                ->column('Title');
56
        });
57
58
        $allPages = [
59
            'Page1',
60
            'Page2',
61
            'Page3',
62
            'Page4',
63
        ];
64
65
        $this->assertEquals($allPages, $pages);
66
67
        // Check localised records (should be empty)
68
        $pages = $this->getLocalisedPages();
69
        $this->assertCount(0, $pages);
70
71
        $getParams = [
72
            'publish' => $publish,
73
            'limit' => $limit,
74
        ];
75
76
        // Localise pages
77
        InitialPageLocalisationTask::singleton()->run(new HTTPRequest('GET', '/', $getParams));
78
79
        // Check localised records (should have all pages now)
80
        $pages = $this->getLocalisedPages();
81
        $this->assertEquals($localised, $pages);
82
83
        // Check published state
84
        $pages = FluentState::singleton()->withState(function (FluentState $state) use ($publish): array {
0 ignored issues
show
Unused Code introduced by
The import $publish is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
85
            $state->setLocale('en_NZ');
86
            $pages = SiteTree::get()->sort('Title', 'ASC');
87
            $publishedPages = [];
88
89
            /** @var SiteTree|FluentSiteTreeExtension $page */
90
            foreach ($pages as $page) {
91
                if (!$page->isDraftedInLocale()) {
92
                    continue;
93
                }
94
95
                if (!$page->isPublishedInLocale()) {
96
                    continue;
97
                }
98
99
                $publishedPages[] = $page->Title;
100
            }
101
102
            return $publishedPages;
103
        });
104
105
        $this->assertEquals($published, $pages);
106
    }
107
108
    public function publishStateProvider(): array
109
    {
110
        return [
111
            [
112
                false,
113
                0,
114
                [
115
                    'Page1',
116
                    'Page2',
117
                    'Page3',
118
                    'Page4',
119
                ],
120
                [],
121
            ],
122
            [
123
                true,
124
                0,
125
                [
126
                    'Page1',
127
                    'Page2',
128
                    'Page3',
129
                    'Page4',
130
                ],
131
                [
132
                    'Page1',
133
                    'Page2',
134
                ],
135
            ],
136
            [
137
                false,
138
                1,
139
                [
140
                    'Page1',
141
                ],
142
                [],
143
            ],
144
            [
145
                true,
146
                1,
147
                [
148
                    'Page1',
149
                ],
150
                [
151
                    'Page1',
152
                ],
153
            ],
154
            [
155
                false,
156
                2,
157
                [
158
                    'Page1',
159
                    'Page2',
160
                ],
161
                [],
162
            ],
163
            [
164
                true,
165
                2,
166
                [
167
                    'Page1',
168
                    'Page2',
169
                ],
170
                [
171
                    'Page1',
172
                    'Page2',
173
                ],
174
            ],
175
        ];
176
    }
177
178
    private function getLocalisedPages(): array
179
    {
180
        return FluentState::singleton()->withState(static function (FluentState $state): array {
181
            $state->setLocale('en_NZ');
182
183
            $pages = SiteTree::get()->sort('Title', 'ASC');
184
            $titles = [];
185
186
            /** @var SiteTree|FluentSiteTreeExtension $page */
187
            foreach ($pages as $page) {
188
                if (!$page->isDraftedInLocale()) {
189
                    continue;
190
                }
191
192
                $titles[] = $page->Title;
193
            }
194
195
            return $titles;
196
        });
197
    }
198
}
199