1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental\Tests\Reports; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Reports\ElementsInUseReport; |
6
|
|
|
use DNADesign\Elemental\Tests\Src\TestElement; |
7
|
|
|
use DNADesign\Elemental\Tests\Src\TestPage; |
8
|
|
|
use SilverStripe\Dev\FunctionalTest; |
9
|
|
|
use SilverStripe\ORM\ArrayList; |
10
|
|
|
use SilverStripe\View\ArrayData; |
11
|
|
|
|
12
|
|
|
class ElementsInUseReportTest extends FunctionalTest |
13
|
|
|
{ |
14
|
|
|
protected static $fixture_file = 'ElementsInUseReportTest.yml'; |
15
|
|
|
|
16
|
|
|
protected static $extra_dataobjects = [ |
17
|
|
|
TestElement::class, |
18
|
|
|
TestPage::class, |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
public function testReportShowsElementsInUse() |
22
|
|
|
{ |
23
|
|
|
$this->logInWithPermission('ADMIN'); |
24
|
|
|
|
25
|
|
|
$result = (string) $this->get('admin/reports/show/DNADesign-Elemental-Reports-ElementsInUseReport')->getBody(); |
26
|
|
|
|
27
|
|
|
$this->assertContains('Content blocks in use', $result, 'Title is displayed'); |
28
|
|
|
$this->assertContains('Show which content blocks are in use', $result, 'Description is displayed'); |
29
|
|
|
|
30
|
|
|
$this->assertContains( |
31
|
|
|
'data-class="DNADesign-Elemental-Models-ElementContent"', |
32
|
|
|
$result, |
33
|
|
|
'Report contains content elements (bundled with elemental)' |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$this->assertContains('HTML text block', $result, 'Content element "nice" type is shown'); |
37
|
|
|
|
38
|
|
|
$this->assertContains('My special content block', $result, 'Fixtured content element is shown'); |
39
|
|
|
$this->assertContains('Stubby Stub', $result, 'Fixtured stub element is shown'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function testSourceRecords() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$records = (new ElementsInUseReport)->sourceRecords(); |
45
|
|
|
|
46
|
|
|
$this->assertInstanceOf(ArrayList::class, $records); |
47
|
|
|
$this->assertNotContains(BaseElement::class, $records->toArray(), 'BaseElement is excluded'); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->assertContainsOnlyInstancesOf(ArrayData::class, $records); |
50
|
|
|
|
51
|
|
|
foreach ($records as $record) { |
52
|
|
|
$this->assertNotNull($record->Icon, 'Fields have an icon'); |
53
|
|
|
$this->assertNotNull($record->Type, 'Fields have a type'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testElementsAssociatedToPagesHaveEditLink() |
58
|
|
|
{ |
59
|
|
|
$records = (new ElementsInUseReport)->sourceRecords()->filter(['Title' => 'Welcome to Castros']); |
60
|
|
|
|
61
|
|
|
$castros = $records->first(); |
62
|
|
|
$this->assertNotNull($castros, 'Fixtured Castros page exists'); |
63
|
|
|
$this->assertTrue($castros->hasField('EditLink')); |
64
|
|
|
$this->assertContains( |
65
|
|
|
$this->idFromFixture(TestPage::class, 'castros_home'), |
66
|
|
|
$castros->EditLink, |
67
|
|
|
'Correct owner page ID is in edit link' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testSourceRecordsFilteredByClassName() |
72
|
|
|
{ |
73
|
|
|
$records = (new ElementsInUseReport)->sourceRecords([ |
74
|
|
|
'ClassName' => 'DNADesign-Elemental-Models-ElementContent', |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$this->assertInstanceOf(ArrayList::class, $records); |
78
|
|
|
$this->assertNotEmpty($records->toArray(), 'Results are returned when filtered'); |
79
|
|
|
$this->assertEquals( |
80
|
|
|
[ |
81
|
|
|
'DNADesign-Elemental-Models-ElementContent' |
82
|
|
|
], |
83
|
|
|
array_unique($records->column('ClassName')), |
84
|
|
|
'Only contains filtered element type' |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.