Passed
Pull Request — master (#774)
by
unknown
04:16
created

TopPageTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 122
c 1
b 0
f 0
dl 0
loc 273
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testNewBlock() 0 20 2
A testTestGetTopPage() 0 16 1
A testTestUpdateTopPageEmptyCache() 0 26 1
A testPageDuplication() 0 49 2
A testNewPage() 0 9 1
A objectsProvider() 0 50 1
A populateTopPageProvider() 0 5 1
A setUp() 0 8 1
A populateTopPageForAllObjects() 0 13 2
1
<?php
2
3
namespace DNADesign\Elemental\Tests\TopPage;
4
5
use DNADesign\Elemental\Extensions\ElementalPageExtension;
6
use DNADesign\Elemental\Models\BaseElement;
7
use DNADesign\Elemental\Models\ElementalArea;
8
use DNADesign\Elemental\TopPage;
9
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...
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\ORM\DataObject;
12
13
class TopPageTest extends SapphireTest
14
{
15
    /**
16
     * @var string
17
     */
18
    protected static $fixture_file = 'TopPageTest.yml';
19
20
    /**
21
     * @var array
22
     */
23
    protected static $required_extensions = [
24
        TestBlockPage::class => [
25
            ElementalPageExtension::class,
26
        ],
27
        TestChildPage::class => [
28
            ElementalPageExtension::class,
29
        ],
30
        Page::class => [
31
            TopPage\SiteTreeExtension::class,
32
        ],
33
        ElementalArea::class => [
34
            TopPage\DataExtension::class,
35
        ],
36
        BaseElement::class => [
37
            TopPage\DataExtension::class,
38
        ],
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    protected static $extra_dataobjects = [
45
        TestContent::class,
46
        TestList::class,
47
        TestBlockPage::class,
48
        TestChildPage::class,
49
    ];
50
51
    protected function setUp(): void
52
    {
53
        /** @var TopPage\DataExtension $extension */
54
        $extension = singleton(TopPage\DataExtension::class);
55
56
        // this makes sure that the data objects start with no top page data
57
        $extension->withoutTopPageUpdate(function () {
58
            parent::setUp();
59
        });
60
    }
61
62
    /**
63
     * @param string $pageIdentifier
64
     * @param string $pageClass
65
     * @param string $objectIdentifier
66
     * @param string $objectClass
67
     * @dataProvider objectsProvider
68
     */
69
    public function testTestGetTopPage(
70
        string $pageIdentifier,
71
        string $pageClass,
72
        string $objectIdentifier,
73
        string $objectClass
74
    ): void {
75
        /** @var Page|TopPage\SiteTreeExtension $content */
76
        $page = $this->objFromFixture($pageClass, $pageIdentifier);
77
78
        /** @var DataObject|TopPage\DataExtension $object */
79
        $object = $this->objFromFixture($objectClass, $objectIdentifier);
80
81
        $topPage = $object->getTopPage();
82
83
        $this->assertNotNull($topPage);
84
        $this->assertEquals((int) $page->ID, (int) $topPage->ID);
85
    }
86
87
    /**
88
     * @param string $pageIdentifier
89
     * @param string $pageClass
90
     * @param string $objectIdentifier
91
     * @param string $objectClass
92
     * @dataProvider objectsProvider
93
     */
94
    public function testTestUpdateTopPageEmptyCache(
95
        string $pageIdentifier,
96
        string $pageClass,
97
        string $objectIdentifier,
98
        string $objectClass
99
    ): void {
100
        /** @var Page|TopPage\SiteTreeExtension $content */
101
        $page = $this->objFromFixture($pageClass, $pageIdentifier);
102
103
        /** @var DataObject|TopPage\DataExtension $object */
104
        $object = $this->objFromFixture($objectClass, $objectIdentifier);
105
106
        $this->assertEquals(0, (int) $object->TopPageID);
107
108
        $object->forceChange();
109
        $id = $object->write();
110
        $object = DataObject::get($object->ClassName)->byID($id);
111
112
        $this->assertEquals((int) $page->ID, (int) $object->TopPageID);
113
114
        // do a second write to make sure that we won't override existing top page
115
        $object->forceChange();
116
        $id = $object->write();
117
        $object = DataObject::get($object->ClassName)->byID($id);
118
119
        $this->assertEquals((int) $page->ID, (int) $object->TopPageID);
120
    }
121
122
    public function testNewPage(): void
123
    {
124
        $page = TestBlockPage::create();
125
        $page->Title = 'New page test';
126
        $page->write();
127
128
        /** @var ElementalArea|TopPage\DataExtension $area */
129
        $area = $page->ElementalArea();
130
        $this->assertEquals((int) $page->ID, (int) $area->TopPageID);
0 ignored issues
show
Bug Best Practice introduced by
The property TopPageID does not exist on DNADesign\Elemental\Models\ElementalArea. Since you implemented __get, consider adding a @property annotation.
Loading history...
131
    }
132
133
    /**
134
     * @param bool $populateTopPage
135
     * @dataProvider populateTopPageProvider
136
     */
137
    public function testNewBlock(bool $populateTopPage): void
138
    {
139
        if ($populateTopPage) {
140
            $this->populateTopPageForAllObjects();
141
        }
142
143
        /** @var TestBlockPage $page */
144
        $page = $this->objFromFixture(TestBlockPage::class, 'block-page1');
145
146
        /** @var ElementalArea $area */
147
        $area = $this->objFromFixture(ElementalArea::class, 'area3');
148
149
        /** @var TestContent|TopPage\DataExtension $content */
150
        $content = TestContent::create();
151
        $content->Title = 'Fresh block';
0 ignored issues
show
Bug introduced by
The property Title does not seem to exist on DNADesign\Elemental\TopPage\DataExtension.
Loading history...
152
153
        $area->Elements()->add($content);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type DNADesign\Elemental\TopPage\DataExtension; however, parameter $item of SilverStripe\ORM\HasManyList::add() does only seem to accept SilverStripe\ORM\DataObject|integer, 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

153
        $area->Elements()->add(/** @scrutinizer ignore-type */ $content);
Loading history...
154
        $content = DataObject::get($content->ClassName)->byID($content->ID);
0 ignored issues
show
Bug introduced by
The property ClassName does not seem to exist on DNADesign\Elemental\TopPage\DataExtension.
Loading history...
Bug introduced by
The property ID does not seem to exist on DNADesign\Elemental\TopPage\DataExtension.
Loading history...
155
156
        $this->assertEquals((int) $page->ID, (int) $content->TopPageID);
157
    }
158
159
    public function testPageDuplication(): void
160
    {
161
        $this->populateTopPageForAllObjects();
162
163
        /** @var TestBlockPage $page */
164
        $page = $this->objFromFixture(TestBlockPage::class, 'block-page1');
165
166
        /** @var TestChildPage $childPage */
167
        $childPage = $this->objFromFixture(TestChildPage::class, 'child-page1');
168
169
        $page->duplicate();
170
        $pages = TestBlockPage::get()->filter(['Title' => 'BlockPage1'])->sort('ID', 'DESC');
171
172
        $this->assertCount(2, $pages);
173
174
        $pageClone = $pages->first();
0 ignored issues
show
Bug introduced by
The method first() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as IntlCodePointBreakIterator or IntlRuleBasedBreakIterator or IntlBreakIterator or SilverStripe\ORM\SS_List or SilverStripe\View\ViewableData or Symfony\Component\DomCrawler\Crawler or SilverStripe\ORM\Connect\Query. ( Ignorable by Annotation )

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

174
        /** @scrutinizer ignore-call */ 
175
        $pageClone = $pages->first();
Loading history...
Bug introduced by
The method first() does not exist on Countable. It seems like you code against a sub-type of Countable such as SilverStripe\ORM\SS_List or Symfony\Component\DomCrawler\Crawler. ( Ignorable by Annotation )

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

174
        /** @scrutinizer ignore-call */ 
175
        $pageClone = $pages->first();
Loading history...
175
        $childPages = TestChildPage::get()->filter(['Title' => 'ChildPage1'])->sort('ID', 'DESC');
176
177
        $this->assertCount(2, $childPages);
178
179
        $childClone = $childPages->first();
180
181
        $this->assertNotEquals((int) $childPage->ID, (int) $childClone->ID);
182
183
        $objects = [
184
            [TestList::class, 'List1', $pageClone],
185
            [TestContent::class, 'Content1', $pageClone],
186
            [TestList::class, 'List2', $childClone],
187
            [TestContent::class, 'Content2', $childClone],
188
        ];
189
190
        foreach ($objects as $objectData) {
191
            $class = array_shift($objectData);
192
            $title = array_shift($objectData);
193
            $page = array_shift($objectData);
194
195
            $items = DataObject::get($class)->filter(['Title' => $title])->sort('ID', 'DESC');
196
197
            $this->assertCount(2, $items);
198
199
            /** @var DataObject|TopPage\DataExtension $objectClone */
200
            $objectClone = $items->first();
201
202
            $this->assertEquals((int) $page->ID, (int) $objectClone->TopPageID);
203
204
            /** @var ElementalArea|TopPage\DataExtension $area */
205
            $area = $objectClone->Parent();
206
207
            $this->assertEquals((int) $page->ID, (int) $area->TopPageID);
0 ignored issues
show
Bug Best Practice introduced by
The property TopPageID does not exist on DNADesign\Elemental\Models\ElementalArea. Since you implemented __get, consider adding a @property annotation.
Loading history...
208
        }
209
    }
210
211
    public function objectsProvider(): array
212
    {
213
        return [
214
            [
215
                'block-page1',
216
                TestBlockPage::class,
217
                'content1',
218
                TestContent::class,
219
            ],
220
            [
221
                'block-page1',
222
                TestBlockPage::class,
223
                'list1',
224
                TestList::class,
225
            ],
226
            [
227
                'block-page1',
228
                TestBlockPage::class,
229
                'area1',
230
                ElementalArea::class,
231
            ],
232
            [
233
                'block-page1',
234
                TestBlockPage::class,
235
                'area3',
236
                ElementalArea::class,
237
            ],
238
            [
239
                'child-page1',
240
                TestChildPage::class,
241
                'content2',
242
                TestContent::class,
243
            ],
244
            [
245
                'child-page1',
246
                TestChildPage::class,
247
                'list2',
248
                TestList::class,
249
            ],
250
            [
251
                'child-page1',
252
                TestChildPage::class,
253
                'area2',
254
                ElementalArea::class,
255
            ],
256
            [
257
                'child-page1',
258
                TestChildPage::class,
259
                'area4',
260
                ElementalArea::class,
261
            ],
262
        ];
263
    }
264
265
    public function populateTopPageProvider(): array
266
    {
267
        return [
268
            [true],
269
            [false],
270
        ];
271
    }
272
273
    private function populateTopPageForAllObjects(): void
274
    {
275
        $list = $this->objectsProvider();
276
277
        foreach ($list as $objects) {
278
            array_shift($objects);
279
            array_shift($objects);
280
            $identifier = array_shift($objects);
281
            $class = array_shift($objects);
282
283
            $object = $this->objFromFixture($class, $identifier);
284
            $object->forceChange();
285
            $object->write();
286
        }
287
    }
288
}
289