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

tests/BaseElementTest.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\Controllers\ElementController;
6
use DNADesign\Elemental\Extensions\ElementalPageExtension;
7
use DNADesign\Elemental\Models\BaseElement;
8
use DNADesign\Elemental\Models\ElementalArea;
9
use DNADesign\Elemental\Models\ElementContent;
10
use DNADesign\Elemental\Tests\Src\TestElement;
11
use DNADesign\Elemental\Tests\Src\TestPage;
12
use Page;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Dev\FunctionalTest;
15
use SilverStripe\Forms\FieldList;
16
use SilverStripe\VersionedAdmin\Forms\HistoryViewerField;
17
18
class BaseElementTest extends FunctionalTest
19
{
20
    protected static $fixture_file = 'ElementalPageExtensionTest.yml';
21
22
    protected static $required_extensions = [
23
        Page::class => [
24
            ElementalPageExtension::class,
25
        ],
26
    ];
27
28
    protected static $extra_dataobjects = [
29
        TestPage::class,
30
        TestElement::class,
31
    ];
32
33
    public function testSimpleClassName()
34
    {
35
        $element = $this->objFromFixture(ElementContent::class, 'content1');
36
37
        $this->assertEquals('dnadesign__elemental__models__elementcontent', $element->getSimpleClassName());
38
    }
39
40
    /**
41
     * Test to ensure backwards compatibility with old Anchor IDs.
42
     */
43
    public function testDisablePrettyAnchor()
44
    {
45
        Config::modify()->set(BaseElement::class, 'disable_pretty_anchor_name', true);
46
47
        $area = ElementalArea::create();
48
        $area->Elements()->add(BaseElement::create(array('Title' => 'Element 1', 'Sort' => 1)));
49
        $area->Elements()->add(BaseElement::create(array('Title' => 'Element 1', 'Sort' => 2)));
50
        $area->Elements()->add(BaseElement::create(array('Title' => 'Element 1', 'Sort' => 3)));
51
        $area->Elements()->add(BaseElement::create(array('Title' => 'Element 1', 'Sort' => 4)));
52
        $area->write();
53
54
        $recordSet = $area->Elements()->toArray();
55
        $this->assertEquals('e'.$recordSet[0]->ID, $recordSet[0]->getAnchor());
56
        $this->assertEquals('e'.$recordSet[1]->ID, $recordSet[1]->getAnchor());
57
        $this->assertEquals('e'.$recordSet[2]->ID, $recordSet[2]->getAnchor());
58
        $this->assertEquals('e'.$recordSet[3]->ID, $recordSet[3]->getAnchor());
59
    }
60
61
    /**
62
     * Test the stop-clashing logic if two BaseElement classes have the same $Title.
63
     */
64
    public function testSameTitle()
65
    {
66
        Config::modify()->set(BaseElement::class, 'enable_title_in_template', true);
67
68
        $area = ElementalArea::create();
69
        $area->Elements()->add(BaseElement::create(array('Title' => 'Element 1', 'Sort' => 1)));
70
        $area->Elements()->add(BaseElement::create(array('Title' => 'Element 1', 'Sort' => 2)));
71
        $area->Elements()->add(BaseElement::create(array('Title' => 'Element 1', 'Sort' => 3)));
72
        $area->Elements()->add(BaseElement::create(array('Title' => 'Element 1', 'Sort' => 4)));
73
        $area->write();
74
75
        $recordSet = $area->Elements()->toArray();
76
        foreach ($recordSet as $record) {
77
            // NOTE: This puts it into the $_anchor protected variable
78
            //       and caches it.
79
            $record->getAnchor();
80
        }
81
        $this->assertEquals('element-1', $recordSet[0]->getAnchor());
82
        $this->assertEquals('element-1-2', $recordSet[1]->getAnchor());
83
        $this->assertEquals('element-1-3', $recordSet[2]->getAnchor());
84
        $this->assertEquals('element-1-4', $recordSet[3]->getAnchor());
85
    }
86
87
    public function testGetCmsFields()
88
    {
89
        $element = $this->objFromFixture(ElementContent::class, 'content1');
90
91
        $this->assertInstanceOf(FieldList::class, $element->getCMSFields());
92
    }
93
94
    public function testGetController()
95
    {
96
        $element = $this->objFromFixture(ElementContent::class, 'content1');
97
        $controller = $element->getController();
98
99
        $this->assertInstanceOf(ElementController::class, $controller);
100
101
        $this->assertEquals($element, $controller->getElement(), 'Controller has element');
102
        $this->assertEquals('Test Content', $controller->Title, 'Controller fallbacks to element');
103
    }
104
105
    public function testLink()
106
    {
107
        $element = $this->objFromFixture(ElementContent::class, 'content1');
108
109
        $this->assertContains($element->getPage()->Link(), $element->Link());
110
    }
111
112
    public function testGetIcon()
113
    {
114
        $element = new ElementContent;
115
        $this->assertContains('class="font-icon-block-content"', $element->getIcon());
116
117
        Config::modify()->set(ElementContent::class, 'icon', '');
118
        $this->assertEmpty($element->getIcon());
119
    }
120
121
    public function testStyleVariants()
122
    {
123
        $styles = [
124
            'option1' => 'Option 1',
125
            'option2' => 'Option 2'
126
        ];
127
128
        Config::modify()->set(ElementContent::class, 'styles', $styles);
129
        $element = $this->objFromFixture(ElementContent::class, 'content1');
130
131
        $this->assertEquals($styles, $element->getCMSFields()->dataFieldByName('Style')->getSource());
132
133
        $element->Style = 'option1';
134
        $this->assertEquals('option1', $element->getStyleVariant());
135
136
        // set a outdated style, should not add.
137
        $element->Style = 'old';
138
        $this->assertEquals('', $element->getStyleVariant());
139
    }
140
141
    public function testFirst()
142
    {
143
        $element = $this->objFromFixture(ElementContent::class, 'content1');
144
        $element2 = $this->objFromFixture(ElementContent::class, 'content2');
145
146
        $this->assertTrue($element->First());
147
        $this->assertFalse($element2->First());
148
    }
149
150
    public function testLast()
151
    {
152
        $element = $this->objFromFixture(ElementContent::class, 'content1');
153
        $element2 = $this->objFromFixture(ElementContent::class, 'content2');
154
155
        $this->assertFalse($element->Last());
156
        $this->assertTrue($element2->Last());
157
    }
158
159
    public function testTotalItems()
160
    {
161
        $element = $this->objFromFixture(ElementContent::class, 'content1');
162
        $element3 = $this->objFromFixture(ElementContent::class, 'content3');
163
164
        $this->assertEquals(2, $element->TotalItems());
165
        $this->assertEquals(1, $element3->TotalItems());
166
    }
167
168
    public function testEvenOdd()
169
    {
170
        $element = $this->objFromFixture(ElementContent::class, 'content1');
171
        $element2 = $this->objFromFixture(ElementContent::class, 'content2');
172
        $element3 = $this->objFromFixture(ElementContent::class, 'content3');
173
174
        $this->assertEquals('odd', $element->EvenOdd());
175
        $this->assertEquals('even', $element2->EvenOdd());
176
        $this->assertEquals('odd', $element3->EvenOdd());
177
    }
178
179
    public function testOnBeforeWrite()
180
    {
181
        /** @var ElementalArea $area */
182
        $area = $this->objFromFixture(ElementalArea::class, 'area51');
183
184
        $element1 = new ElementContent();
185
        $element1->ParentID = $area->ID;
0 ignored issues
show
Bug Best Practice introduced by Robbie Averill
The property ParentID does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __set, consider adding a @property annotation.
Loading history...
186
        $element1->write();
187
        $baselineSort = $element1->Sort;
188
189
        $element2 = new ElementContent();
190
        $element2->ParentID = $area->ID;
191
        $element2->write();
192
        $this->assertEquals($baselineSort + 1, $element2->Sort, 'Sort order should be higher than the max');
193
194
        // Use a different element type, ensuring that sort orders are relative to the BaseElement
195
        $element3 = new TestElement();
196
        $element3->ParentID = $area->ID;
0 ignored issues
show
Bug Best Practice introduced by Robbie Averill
The property ParentID does not exist on DNADesign\Elemental\Tests\Src\TestElement. Since you implemented __set, consider adding a @property annotation.
Loading history...
197
        $element3->write();
198
        $this->assertEquals($baselineSort + 2, $element3->Sort, 'Sort order should be higher than the max');
199
    }
200
}
201