Passed
Push — master ( c5d5b8...0bcd4c )
by Robbie
04:01
created

testCanViewTestElementIsTrueForAdmins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

100
        $this->assertTrue($area->/** @scrutinizer ignore-call */ canPublish($member));
Loading history...
Bug introduced by
$member of type integer is incompatible with the type SilverStripe\Security\Member expected by parameter $member of SilverStripe\Versioned\Versioned::canPublish(). ( Ignorable by Annotation )

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

100
        $this->assertTrue($area->canPublish(/** @scrutinizer ignore-type */ $member));
Loading history...
101
102
        /** @var TestElement|Versioned $element */
103
        $element = $this->objFromFixture(TestElement::class, 'element1');
104
        $this->assertTrue($element->canPublish($member));
0 ignored issues
show
Bug introduced by
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

104
        $this->assertTrue($element->/** @scrutinizer ignore-call */ canPublish($member));
Loading history...
105
    }
106
107
    public function testDuplicate()
108
    {
109
        /** @var ElementalArea $area */
110
        $area = $this->objFromFixture(ElementalArea::class, 'area1');
111
        $areaIds = $area->Elements()->column('ID');
112
        $this->assertCount(2, $areaIds);
113
114
        $duplicatedArea = $area->duplicate(true);
115
        $duplicatedAreaIds = $duplicatedArea->Elements()->column('ID');
116
        $this->assertCount(2, $duplicatedAreaIds);
117
        $this->assertNotEquals($areaIds, $duplicatedAreaIds);
118
    }
119
}
120