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

ElementalPageExtensionTest::testUpdateCmsFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
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\BaseElement;
7
use DNADesign\Elemental\Models\ElementContent;
8
use DNADesign\Elemental\Tests\Src\TestElement;
9
use DNADesign\Elemental\Tests\Src\TestPage;
10
use SilverStripe\CMS\Model\RedirectorPage;
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Dev\FunctionalTest;
13
14
class ElementalPageExtensionTest extends FunctionalTest
15
{
16
    protected static $fixture_file = 'ElementalPageExtensionTest.yml';
17
18
    protected static $required_extensions = [
19
        TestPage::class => [
20
            ElementalPageExtension::class,
21
        ],
22
    ];
23
24
    protected static $extra_dataobjects = [
25
        TestElement::class,
26
        TestPage::class,
27
    ];
28
29
    protected function setUp()
30
    {
31
        parent::setUp();
32
33
        $this->logInWithPermission('ADMIN');
34
    }
35
36
    public function testUpdateCmsFields()
37
    {
38
        $page = $this->objFromFixture(TestPage::class, 'elementaldemo');
39
40
        $elementalArea = $page->getCMSFields()->dataFieldByName('ElementalArea');
41
        $this->assertNotNull($elementalArea);
42
43
        $content = $page->getCMSFields()->dataFieldByName('Content');
44
        $this->assertNull($content);
45
46
        $redirect = $this->objFromFixture(RedirectorPage::class, 'elementredirectpage');
47
        $elementalArea = $redirect->getCMSFields()->dataFieldByName('ElementalArea');
48
49
        $this->assertNull($elementalArea);
50
    }
51
52
    public function testGetElementalTypes()
53
    {
54
        $page = $this->objFromFixture(TestPage::class, 'elementaldemo');
55
        $types = $page->getElementalTypes();
56
57
        $this->assertArrayHasKey(ElementContent::class, $types);
58
        $this->assertArrayNotHasKey(BaseElement::class, $types, 'Base class should not appear');
59
60
        // if we disallow a type then it should remove it
61
        Config::modify()->set(TestPage::class, 'disallowed_elements', [
62
            ElementContent::class
63
        ]);
64
65
        $types = $page->getElementalTypes();
66
        $this->assertArrayNotHasKey(ElementContent::class, $types, 'Disallowed items should not appear');
67
68
        // conversely, if we set allowed items to a number of classes then they
69
        // should be the only ones to appear.
70
        Config::modify()->set(TestPage::class, 'allowed_elements', [
71
            TestElement::class
72
        ]);
73
74
        Config::modify()->remove(TestPage::class, 'disallowed_elements');
75
        $types = $page->getElementalTypes();
76
77
        $this->assertArrayNotHasKey(ElementContent::class, $types, 'Disallowed items should not appear');
78
        $this->assertArrayHasKey(TestElement::class, $types);
79
80
        $this->assertEquals('A test element', $types[TestElement::class], 'Types should use their "type"');
81
    }
82
83
    public function testDuplicatingPageDuplicatesElements()
84
    {
85
        /** @var TestPage $page */
86
        $page = $this->objFromFixture(TestPage::class, 'page_with_elements');
87
        $this->assertCount(2, $page->ElementalArea()->Elements());
88
89
        /** @var TestPage $newPage */
90
        $newPage = $page->duplicate();
91
        $this->assertNotEquals($page->ElementalArea->ID, $newPage->ElementalArea->ID, 'Area is duplicated');
92
        $this->assertCount(2, $newPage->ElementalArea()->Elements());
93
94
        $this->assertNotEquals(
95
            $page->ElementalArea()->Elements()->column('ID'),
0 ignored issues
show
Bug introduced by Robbie Averill
The method column() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as SilverStripe\ORM\SS_List or SilverStripe\View\ViewableData or SilverStripe\ORM\Connect\Query or SQLiteResult. ( Ignorable by Annotation )

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

95
            $page->ElementalArea()->Elements()->/** @scrutinizer ignore-call */ column('ID'),
Loading history...
Bug introduced by Robbie Averill
The method column() does not exist on Countable. It seems like you code against a sub-type of Countable such as SilverStripe\ORM\SS_List or SQLiteResult. ( Ignorable by Annotation )

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

95
            $page->ElementalArea()->Elements()->/** @scrutinizer ignore-call */ column('ID'),
Loading history...
96
            $newPage->ElementalArea()->Elements()->column('ID'),
97
            'Duplicated page has duplicated area and duplicated elements, i.e. not shared'
98
        );
99
    }
100
}
101