Issues (216)

tests/ElementalAreaTest.php (2 issues)

Labels
Severity
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
78
        // OwnerClassName set
79
        $ownerpage2 = $area2->getOwnerPage();
80
81
        $this->assertEquals("DNADesign\Elemental\Tests\Src\TestPage", $ownerpage1);
82
        $this->assertEquals("DNADesign\Elemental\Tests\Src\TestPage", $ownerpage2);
83
84
        // if ownerpage1 has draft changes then getOwnerPage() should return the
85
        // live version of the owner page, since the draft record will be
86
        // unviewable by logged out users
87
        $ownerpage1->publishRecursive();
88
89
        $ownerpage1->Title = 'I have edited the page';
90
        $ownerpage1->writeToStage(Versioned::DRAFT);
91
92
        $liveOwner = Versioned::withVersionedMode(function () use ($area1) {
93
            Versioned::set_stage(Versioned::LIVE);
94
            $page = $area1->getOwnerPage();
95
96
            return $page;
97
        });
98
99
        $this->assertEquals($liveOwner->Title, 'Page 1', 'getOwnerPage returns live version of page, not the draft');
100
    }
101
102
    public function testForTemplate()
103
    {
104
        $area = $this->objFromFixture(ElementalArea::class, 'area1');
105
106
        $this->assertStringContainsString('Hello Test', $area->forTemplate());
107
        $this->assertStringContainsString('Hello Test 2', $area->forTemplate());
108
    }
109
110
    public function testCanBePublished()
111
    {
112
        $member = $this->logInWithPermission('SITETREE_EDIT_ALL');
113
114
        /** @var Page $page */
115
        $page = $this->objFromFixture(TestPage::class, 'page1');
116
        $this->assertTrue($page->canPublish($member));
117
118
        /** @var ElementalArea|Versioned $area */
119
        $area = $this->objFromFixture(ElementalArea::class, 'area1');
120
        $this->assertTrue($area->canPublish($member));
0 ignored issues
show
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

120
        $this->assertTrue($area->/** @scrutinizer ignore-call */ canPublish($member));
Loading history...
121
122
        /** @var TestElement|Versioned $element */
123
        $element = $this->objFromFixture(TestElement::class, 'element1');
124
        $this->assertTrue($element->canPublish($member));
0 ignored issues
show
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

124
        $this->assertTrue($element->/** @scrutinizer ignore-call */ canPublish($member));
Loading history...
125
    }
126
127
    public function testDuplicate()
128
    {
129
        /** @var ElementalArea $area */
130
        $area = $this->objFromFixture(ElementalArea::class, 'area1');
131
        $areaIds = $area->Elements()->column('ID');
132
        $this->assertCount(2, $areaIds);
133
134
        $duplicatedArea = $area->duplicate(true);
135
        $duplicatedAreaIds = $duplicatedArea->Elements()->column('ID');
136
        $this->assertCount(2, $duplicatedAreaIds);
137
        $this->assertNotEquals($areaIds, $duplicatedAreaIds);
138
    }
139
140
    public function testUnsavedRelationListOfElementsReturnsEmptyArrayList()
141
    {
142
        $area = new ElementalArea();
143
144
        $element = new ElementContent();
145
        $element->HTML = 'Test';
146
147
        $area->Elements()->add($element);
148
149
        $result = $area->ElementControllers();
150
        $this->assertInstanceOf(ArrayList::class, $result);
151
        $this->assertEmpty($result);
152
    }
153
154
    public function testElementsListIsCached()
155
    {
156
        $area = new ElementalArea();
157
158
        $element = new ElementContent();
159
        $element->HTML = 'Test';
160
161
        $elements = new ArrayList([$element]);
162
163
        $area->setElementsCached($elements);
164
165
        $this->assertSame($elements, $area->Elements());
166
    }
167
}
168