ElementsInUseReportTest::tearDown()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
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\GraphQL\Tests\Schema\NaiveSchemaBuilder;
0 ignored issues
show
Bug introduced by
The type SilverStripe\GraphQL\Tes...hema\NaiveSchemaBuilder 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...
13
use SilverStripe\ORM\DataList;
14
15
class ElementsInUseReportTest extends FunctionalTest
16
{
17
    protected static $fixture_file = 'ElementsInUseReportTest.yml';
18
19
    protected static $required_extensions = [
20
        TestPage::class => [
21
            ElementalPageExtension::class,
22
        ],
23
    ];
24
25
    protected static $extra_dataobjects = [
26
        TestElement::class,
27
        TestPage::class,
28
    ];
29
30
    protected function setUp(): void
31
    {
32
        parent::setUp();
33
34
        // GraphQL 4 only
35
        if (class_exists(NaiveSchemaBuilder::class)) {
36
            NaiveSchemaBuilder::activate();
37
        }
38
    }
39
40
    protected function tearDown(): void
41
    {
42
        parent::tearDown();
43
        
44
        // GraphQL 4 only
45
        if (class_exists(NaiveSchemaBuilder::class)) {
46
            NaiveSchemaBuilder::deactivate();
47
        }
48
    }
49
50
    public function testReportShowsElementsInUse()
51
    {
52
        $this->logInWithPermission('ADMIN');
53
54
        $result = (string) $this->get('admin/reports/show/DNADesign-Elemental-Reports-ElementsInUseReport')->getBody();
55
56
        $this->assertStringContainsString('Content blocks in use', $result, 'Title is displayed');
57
58
        $this->assertStringContainsString(
59
            'data-class="DNADesign\\Elemental\\Models\\ElementContent"',
60
            $result,
61
            'Report contains content elements (bundled with elemental)'
62
        );
63
64
        $this->assertStringContainsString('HTML text block', $result, 'Content element "nice" type is shown');
65
66
        $this->assertStringContainsString('My special content block', $result, 'Fixtured content element is shown');
67
        $this->assertStringContainsString('Stubby Stub', $result, 'Fixtured stub element is shown');
68
    }
69
70
    public function testSourceRecords()
71
    {
72
        $records = (new ElementsInUseReport)->sourceRecords();
73
74
        $this->assertInstanceOf(DataList::class, $records);
75
        $this->assertNotEmpty($records);
76
        $this->assertContainsOnlyInstancesOf(BaseElement::class, $records, 'Report contains elements');
77
78
        foreach ($records as $record) {
79
            $this->assertNotEquals(BaseElement::class, get_class($record), 'BaseElement does not exist in the report');
80
        }
81
    }
82
83
    public function testElementsAssociatedToPagesHaveEditLink()
84
    {
85
        $records = (new ElementsInUseReport)->sourceRecords()->filter(['Title' => 'Welcome to Castros']);
86
87
        $castros = $records->first();
88
        $this->assertNotNull($castros, 'Fixtured Castros page exists');
89
        $this->assertTrue($castros->hasField('EditLink'));
90
        $this->assertStringContainsString(
91
            (string) $this->idFromFixture(TestPage::class, 'castros_home'),
92
            $castros->EditLink,
93
            'Correct owner page ID is in edit link'
94
        );
95
    }
96
97
    public function testSourceRecordsFilteredByClassName()
98
    {
99
        $records = (new ElementsInUseReport)->sourceRecords([
100
            'ClassName' => 'DNADesign-Elemental-Models-ElementContent',
101
        ]);
102
103
        $this->assertInstanceOf(DataList::class, $records);
104
        $this->assertNotEmpty($records->toArray(), 'Results are returned when filtered');
105
        $this->assertContainsOnlyInstancesOf(
106
            ElementContent::class,
107
            $records->toArray(),
108
            'Only contains filtered element type'
109
        );
110
    }
111
}
112