Passed
Pull Request — master (#774)
by
unknown
05:46
created

TopPageTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Importance

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

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\Models\BaseElement;
6
use DNADesign\Elemental\Models\ElementalArea;
7
use DNADesign\Elemental\TopPage;
8
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...
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\ORM\DataObject;
11
12
class TopPageTest extends SapphireTest
13
{
14
    /**
15
     * @var string
16
     */
17
    protected static $fixture_file = 'TopPageTest.yml';
18
19
    /**
20
     * @var array
21
     */
22
    protected static $required_extensions = [
23
        Page::class => [
24
            TopPage\SiteTreeExtension::class,
25
        ],
26
        ElementalArea::class => [
27
            TopPage\DataExtension::class,
28
        ],
29
        BaseElement::class => [
30
            TopPage\DataExtension::class,
31
        ],
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    protected static $extra_dataobjects = [
38
        TestContent::class,
39
        TestList::class,
40
        TestBlockPage::class,
41
        TestChildPage::class,
42
    ];
43
44
    protected function setUp(): void
45
    {
46
        /** @var TopPage\DataExtension $extension */
47
        $extension = singleton(TopPage\DataExtension::class);
48
49
        // this makes sure that the data objects start with no top page data
50
        $extension->withoutTopPageUpdate(function () {
51
            parent::setUp();
52
        });
53
    }
54
55
    /**
56
     * @param string $pageIdentifier
57
     * @param string $pageClass
58
     * @param string $objectIdentifier
59
     * @param string $objectClass
60
     * @dataProvider objectsProvider
61
     */
62
    public function testTestGetTopPage(
63
        string $pageIdentifier,
64
        string $pageClass,
65
        string $objectIdentifier,
66
        string $objectClass
67
    ): void {
68
        /** @var Page|TopPage\SiteTreeExtension $content */
69
        $page = $this->objFromFixture($pageClass, $pageIdentifier);
70
71
        /** @var DataObject|TopPage\DataExtension $object */
72
        $object = $this->objFromFixture($objectClass, $objectIdentifier);
73
74
        $topPage = $object->getTopPage();
75
76
        $this->assertNotNull($topPage);
77
        $this->assertEquals((int) $page->ID, (int) $topPage->ID);
78
    }
79
80
    /**
81
     * @param string $pageIdentifier
82
     * @param string $pageClass
83
     * @param string $objectIdentifier
84
     * @param string $objectClass
85
     * @dataProvider objectsProvider
86
     */
87
    public function testTestUpdateTopPageEmptyCache(
88
        string $pageIdentifier,
89
        string $pageClass,
90
        string $objectIdentifier,
91
        string $objectClass
92
    ): void {
93
        /** @var Page|TopPage\SiteTreeExtension $content */
94
        $page = $this->objFromFixture($pageClass, $pageIdentifier);
95
96
        /** @var DataObject|TopPage\DataExtension $object */
97
        $object = $this->objFromFixture($objectClass, $objectIdentifier);
98
99
        $this->assertEquals(0, (int) $object->TopPageID);
100
101
        $object->forceChange();
102
        $id = $object->write();
103
        $object = DataObject::get($object->ClassName)->byID($id);
104
105
        $this->assertEquals((int) $page->ID, (int) $object->TopPageID);
106
107
        // do a second write to make sure that we won't override existing top page
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
115
    public function testNewPage(): void
116
    {
117
        $page = TestBlockPage::create();
118
        $page->Title = 'New page test';
119
        $page->write();
120
121
        /** @var ElementalArea|TopPage\DataExtension $area */
122
        $area = $page->ElementalArea();
123
        $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...
124
    }
125
126
    /**
127
     * @param bool $populateTopPage
128
     * @dataProvider populateTopPageProvider
129
     */
130
    public function testNewBlock(bool $populateTopPage): void
131
    {
132
        if ($populateTopPage) {
133
            $this->populateTopPageForAllObjects();
134
        }
135
136
        /** @var TestBlockPage $page */
137
        $page = $this->objFromFixture(TestBlockPage::class, 'block-page1');
138
139
        /** @var ElementalArea $area */
140
        $area = $this->objFromFixture(ElementalArea::class, 'area3');
141
142
        /** @var TestContent|TopPage\DataExtension $content */
143
        $content = TestContent::create();
144
        $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...
145
146
        $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

146
        $area->Elements()->add(/** @scrutinizer ignore-type */ $content);
Loading history...
147
        $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...
148
149
        $this->assertEquals((int) $page->ID, (int) $content->TopPageID);
150
    }
151
152
    public function testPageDuplication(): void
153
    {
154
        $this->populateTopPageForAllObjects();
155
156
        /** @var TestBlockPage $page */
157
        $page = $this->objFromFixture(TestBlockPage::class, 'block-page1');
158
159
        /** @var TestChildPage $childPage */
160
        $childPage = $this->objFromFixture(TestChildPage::class, 'child-page1');
161
162
        $page->duplicate();
163
        $pages = TestBlockPage::get()->filter(['Title' => 'BlockPage1'])->sort('ID', 'DESC');
164
165
        $this->assertCount(2, $pages);
166
167
        $pageClone = $pages->first();
0 ignored issues
show
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

167
        /** @scrutinizer ignore-call */ 
168
        $pageClone = $pages->first();
Loading history...
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

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