Passed
Push — master ( eb27fd...f091a3 )
by
unknown
02:36
created

ElementalAreaTest::testElementsListIsCached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\Elemental\Tests;
4
5
use DNADesign\Elemental\Extensions\ElementalPageExtension;
6
use DNADesign\Elemental\Models\ElementalArea;
7
use DNADesign\Elemental\Models\ElementContent;
8
use DNADesign\Elemental\Tests\Src\TestElement;
9
use DNADesign\Elemental\Tests\Src\TestPage;
10
use Page;
0 ignored issues
show
Bug introduced by Will Rossiter
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...
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\ORM\ArrayList;
13
use SilverStripe\Versioned\Versioned;
14
15
class ElementalAreaTest extends SapphireTest
16
{
17
    protected static $fixture_file = 'ElementalAreaTest.yml';
18
19
    protected static $required_extensions = [
20
        Page::class => [
21
            ElementalPageExtension::class,
22
        ],
23
    ];
24
25
    protected static $extra_dataobjects = [
26
        TestElement::class,
27
        TestPage::class,
28
    ];
29
30
    public function testElementControllers()
31
    {
32
        $area = $this->objFromFixture(ElementalArea::class, 'area1');
33
        $controllers = $area->ElementControllers();
34
35
        $this->assertEquals(2, $controllers->count(), 'Should be a controller per element');
36
    }
37
38
    public function testCanViewTestElementIsFalseWhenLoggedInAsCmsEditor()
39
    {
40
        /** @var ElementalArea $area */
41
        $area = $this->objFromFixture(ElementalArea::class, 'area2');
42
        // Content editors do not have permission to view the TestElement
43
        $this->logInWithPermission('VIEW_DRAFT_CONTENT');
44
45
        $controllers = $area->ElementControllers();
46
        $this->assertCount(2, $area->Elements(), 'There are two elements in total');
47
        $this->assertCount(
48
            1,
49
            $controllers,
50
            'Should be one controller only, since TestElement is not viewable by non-admins'
51
        );
52
    }
53
54
    public function testCanViewTestElementIsTrueForAdmins()
55
    {
56
        /** @var ElementalArea $area */
57
        $area = $this->objFromFixture(ElementalArea::class, 'area2');
58
        // Admin users have permission to view the TestElement
59
        $this->logInWithPermission('ADMIN');
60
61
        $controllers = $area->ElementControllers();
62
        $this->assertCount(2, $area->Elements(), 'There are two elements in total');
63
        $this->assertCount(
64
            2,
65
            $controllers,
66
            'Should be two controllers when logged in as admin'
67
        );
68
    }
69
70
    public function testGetOwnerPage()
71
    {
72
        $area1 = $this->objFromFixture(ElementalArea::class, 'area1');
73
        $area2 = $this->objFromFixture(ElementalArea::class, 'area2');
74
75
        // OwnerClassName not set
76
        $ownerpage1 = $area1->getOwnerPage();
77
        // OwnerClassName set
78
        $ownerpage2 = $area2->getOwnerPage();
79
80
        $this->assertEquals("DNADesign\Elemental\Tests\Src\TestPage", $ownerpage1);
81
        $this->assertEquals("DNADesign\Elemental\Tests\Src\TestPage", $ownerpage2);
82
    }
83
84
    public function testForTemplate()
85
    {
86
        $area = $this->objFromFixture(ElementalArea::class, 'area1');
87
88
        $this->assertContains('Hello Test', $area->forTemplate());
89
        $this->assertContains('Hello Test 2', $area->forTemplate());
90
    }
91
92
    public function testCanBePublished()
93
    {
94
        $member = $this->logInWithPermission('SITETREE_EDIT_ALL');
95
96
        /** @var Page $page */
97
        $page = $this->objFromFixture(TestPage::class, 'page1');
98
        $this->assertTrue($page->canPublish($member));
99
100
        /** @var ElementalArea|Versioned $area */
101
        $area = $this->objFromFixture(ElementalArea::class, 'area1');
102
        $this->assertTrue($area->canPublish($member));
0 ignored issues
show
Bug introduced by Andrew Aitken-Fincham
The method canPublish() does not exist on DNADesign\Elemental\Models\ElementalArea. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

102
        $this->assertTrue($area->/** @scrutinizer ignore-call */ canPublish($member));
Loading history...
103
104
        /** @var TestElement|Versioned $element */
105
        $element = $this->objFromFixture(TestElement::class, 'element1');
106
        $this->assertTrue($element->canPublish($member));
0 ignored issues
show
Bug introduced by Andrew Aitken-Fincham
The method canPublish() does not exist on DNADesign\Elemental\Tests\Src\TestElement. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

106
        $this->assertTrue($element->/** @scrutinizer ignore-call */ canPublish($member));
Loading history...
107
    }
108
109
    public function testDuplicate()
110
    {
111
        /** @var ElementalArea $area */
112
        $area = $this->objFromFixture(ElementalArea::class, 'area1');
113
        $areaIds = $area->Elements()->column('ID');
114
        $this->assertCount(2, $areaIds);
115
116
        $duplicatedArea = $area->duplicate(true);
117
        $duplicatedAreaIds = $duplicatedArea->Elements()->column('ID');
118
        $this->assertCount(2, $duplicatedAreaIds);
119
        $this->assertNotEquals($areaIds, $duplicatedAreaIds);
120
    }
121
122
    public function testUnsavedRelationListOfElementsReturnsEmptyArrayList()
123
    {
124
        $area = new ElementalArea();
125
126
        $element = new ElementContent();
127
        $element->HTML = 'Test';
0 ignored issues
show
Bug Best Practice introduced by Robbie Averill
The property HTML does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __set, consider adding a @property annotation.
Loading history...
128
129
        $area->Elements()->add($element);
130
131
        $result = $area->ElementControllers();
132
        $this->assertInstanceOf(ArrayList::class, $result);
133
        $this->assertEmpty($result);
134
    }
135
136
    public function testElementsListIsCached()
137
    {
138
        $area = new ElementalArea();
139
140
        $element = new ElementContent();
141
        $element->HTML = 'Test';
0 ignored issues
show
Bug Best Practice introduced by Robbie Averill
The property HTML does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __set, consider adding a @property annotation.
Loading history...
142
143
        $elements = new ArrayList([$element]);
144
145
        $area->setElementsCached($elements);
146
147
        $this->assertSame($elements, $area->Elements());
148
    }
149
}
150