Passed
Push — master ( a1de92...1a465b )
by Robbie
03:52
created

tests/ElementalAreaTest.php (2 issues)

Checks if accessed properties are documented accessible via '__set()'.

Best Practice Bug Major
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;
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));
103
104
        /** @var TestElement|Versioned $element */
105
        $element = $this->objFromFixture(TestElement::class, 'element1');
106
        $this->assertTrue($element->canPublish($member));
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