ElementalAreasExtensionTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
eloc 38
c 5
b 0
f 0
dl 0
loc 96
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetElementalTypesSortsAlphabetically() 0 9 1
A testGetElementalTypesAreNotSortedAlphabetically() 0 9 1
A assertContainsInOrder() 0 5 1
A provideContentFieldPreservationSettings() 0 11 1
A setUp() 0 13 1
A testContentFieldsAreRemovedByDefault() 0 7 1
1
<?php
2
3
namespace DNADesign\Elemental\Tests\Extensions;
4
5
use DNADesign\Elemental\Extensions\ElementalAreasExtension;
6
use DNADesign\Elemental\Models\ElementContent;
7
use DNADesign\Elemental\Tests\Src\TestElement;
8
use DNADesign\Elemental\Tests\Src\TestUnusedElement;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\CMS\Model\SiteTree;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
13
use SilverStripe\Forms\LiteralField;
14
15
class ElementalAreasExtensionTest extends SapphireTest
16
{
17
    protected static $required_extensions = [
18
        SiteTree::class => [
19
            ElementalAreasExtension::class,
20
        ],
21
    ];
22
23
    protected static $extra_dataobjects = [
24
        TestElement::class,
25
        TestUnusedElement::class,
26
    ];
27
28
    protected function setUp(): void
29
    {
30
        parent::setUp();
31
32
        $this->logInWithPermission('ADMIN');
33
34
        SiteTree::config()
35
            ->set('allowed_elements', [
36
                ElementContent::class,
37
                TestElement::class,
38
                TestUnusedElement::class,
39
            ])
40
            ->set('disallowed_elements', []);
41
    }
42
43
    public function testGetElementalTypesSortsAlphabetically()
44
    {
45
        SiteTree::config()->set('sort_types_alphabetically', true);
46
47
        /** @var SiteTree|ElementalAreasExtension $page */
48
        $page = new SiteTree();
49
        $types = $page->getElementalTypes();
50
51
        $this->assertContainsInOrder(['A test element', 'Content', 'Unused Element'], array_values($types ?? []));
52
    }
53
54
    public function testGetElementalTypesAreNotSortedAlphabetically()
55
    {
56
        SiteTree::config()->set('sort_types_alphabetically', false);
57
58
        /** @var SiteTree|ElementalAreasExtension $page */
59
        $page = new SiteTree();
60
        $types = $page->getElementalTypes();
61
62
        $this->assertContainsInOrder(['Content', 'A test element', 'Unused Element'], array_values($types ?? []));
63
    }
64
65
    /**
66
     * We need to check that the order of the elements is correct, but there might be more element types installed than
67
     * we're aware of, so we first extract the elements we want from the source list and check the order afterwards.
68
     *
69
     * @param array $expected
70
     * @param array $actual
71
     */
72
    private function assertContainsInOrder(array $expected, array $actual)
73
    {
74
        $matches = array_values(array_intersect($actual ?? [], $expected));
75
76
        $this->assertSame($expected, $matches);
77
    }
78
79
    /**
80
     * @dataProvider provideContentFieldPreservationSettings
81
     */
82
    public function testContentFieldsAreRemovedByDefault($keepGlobal, $keepClass, $expectedType)
83
    {
84
        Config::inst()->set(ElementalAreasExtension::class, 'keep_content_fields', $keepGlobal);
0 ignored issues
show
Bug introduced by
The method set() does not exist on SilverStripe\Config\Coll...nfigCollectionInterface. It seems like you code against a sub-type of SilverStripe\Config\Coll...nfigCollectionInterface such as SilverStripe\Config\Coll...nfigCollectionInterface. ( Ignorable by Annotation )

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

84
        Config::inst()->/** @scrutinizer ignore-call */ set(ElementalAreasExtension::class, 'keep_content_fields', $keepGlobal);
Loading history...
85
        Config::inst()->set(SiteTree::class, 'elemental_keep_content_field', $keepClass);
86
        $page = SiteTree::create();
87
        $fields = $page->getCMSFields();
88
        $this->assertInstanceOf($expectedType, $fields->fieldByName('Root.Main.Content'));
89
    }
90
91
    /**
92
     * Provide data for testing both settings and override precedence of Content field replacement
93
     * Settings provided as:
94
     * - ElementalAreasExtension.keep_content_fields (the global setting)
95
     * - SiteTree.elemental_keep_content_field (the class level setting - should take precedence)
96
     * - The expected class of the Field in the FieldList (LiteralField OR HTMLEditorField)
97
     *
98
     * @return array
99
     */
100
    public function provideContentFieldPreservationSettings()
101
    {
102
        // Test both unset (null) and explicitly declined (false) where applicable.
103
        return [
104
            [null, null, LiteralField::class],
105
            [false, false, LiteralField::class],
106
            [true, null, HTMLEditorField::class],
107
            [true, false, LiteralField::class],
108
            [null, true, HTMLEditorField::class],
109
            [false, true, HTMLEditorField::class],
110
            [true, true, HTMLEditorField::class],
111
        ];
112
    }
113
}
114