ElementsInUseReportTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testElementsAssociatedToPagesHaveEditLink() 0 11 1
A testReportShowsElementsInUse() 0 18 1
A testSourceRecords() 0 10 2
A testSourceRecordsFilteredByClassName() 0 12 1
1
<?php
2
3
namespace DNADesign\Elemental\Tests\Reports;
4
5
use DNADesign\Elemental\Extensions\ElementalPageExtension;
6
use DNADesign\Elemental\Models\BaseElement;
7
use DNADesign\Elemental\Models\ElementContent;
8
use DNADesign\Elemental\Reports\ElementsInUseReport;
9
use DNADesign\Elemental\Tests\Src\TestElement;
10
use DNADesign\Elemental\Tests\Src\TestPage;
11
use SilverStripe\Dev\FunctionalTest;
12
use SilverStripe\ORM\DataList;
13
14
class ElementsInUseReportTest extends FunctionalTest
15
{
16
    protected static $fixture_file = 'ElementsInUseReportTest.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
    public function testReportShowsElementsInUse()
30
    {
31
        $this->logInWithPermission('ADMIN');
32
33
        $result = (string) $this->get('admin/reports/show/DNADesign-Elemental-Reports-ElementsInUseReport')->getBody();
34
35
        $this->assertContains('Content blocks in use', $result, 'Title is displayed');
36
37
        $this->assertContains(
38
            'data-class="DNADesign\\Elemental\\Models\\ElementContent"',
39
            $result,
40
            'Report contains content elements (bundled with elemental)'
41
        );
42
43
        $this->assertContains('HTML text block', $result, 'Content element "nice" type is shown');
44
45
        $this->assertContains('My special content block', $result, 'Fixtured content element is shown');
46
        $this->assertContains('Stubby Stub', $result, 'Fixtured stub element is shown');
47
    }
48
49
    public function testSourceRecords()
50
    {
51
        $records = (new ElementsInUseReport)->sourceRecords();
52
53
        $this->assertInstanceOf(DataList::class, $records);
54
        $this->assertNotEmpty($records);
55
        $this->assertContainsOnlyInstancesOf(BaseElement::class, $records, 'Report contains elements');
56
57
        foreach ($records as $record) {
58
            $this->assertNotEquals(BaseElement::class, get_class($record), 'BaseElement does not exist in the report');
59
        }
60
    }
61
62
    public function testElementsAssociatedToPagesHaveEditLink()
63
    {
64
        $records = (new ElementsInUseReport)->sourceRecords()->filter(['Title' => 'Welcome to Castros']);
65
66
        $castros = $records->first();
67
        $this->assertNotNull($castros, 'Fixtured Castros page exists');
68
        $this->assertTrue($castros->hasField('EditLink'));
69
        $this->assertContains(
70
            (string) $this->idFromFixture(TestPage::class, 'castros_home'),
71
            $castros->EditLink,
72
            'Correct owner page ID is in edit link'
73
        );
74
    }
75
76
    public function testSourceRecordsFilteredByClassName()
77
    {
78
        $records = (new ElementsInUseReport)->sourceRecords([
79
            'ClassName' => 'DNADesign-Elemental-Models-ElementContent',
80
        ]);
81
82
        $this->assertInstanceOf(DataList::class, $records);
83
        $this->assertNotEmpty($records->toArray(), 'Results are returned when filtered');
84
        $this->assertContainsOnlyInstancesOf(
85
            ElementContent::class,
86
            $records->toArray(),
87
            'Only contains filtered element type'
88
        );
89
    }
90
}
91