Passed
Push — master ( 1b2674...3c6afc )
by Robbie
01:36
created

SitewideContentReportTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSourceRecords() 0 17 1
A setUp() 0 19 4
A testGetCMSFields() 0 14 2
B testReportFields() 0 70 5
1
<?php
2
3
namespace SilverStripe\SiteWideContentReport\Tests;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\ORM\DataList;
7
use SilverStripe\SiteWideContentReport\SitewideContentReport;
8
use SilverStripe\Forms\GridField\GridFieldDataColumns;
9
use SilverStripe\Forms\GridField\GridFieldExportButton;
10
use SilverStripe\SiteWideContentReport\Model\SitewideContentTaxonomy;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\Subsites\Model\Subsite;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Subsites\Model\Subsite 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\ContentReview\Extensions\SiteTreeContentReview;
0 ignored issues
show
Bug introduced by
The type SilverStripe\ContentRevi...s\SiteTreeContentReview 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...
14
15
/**
16
 * Class SitewideContentReportTest
17
 * @package SilverStripe\SiteWideContentReport\Tests
18
 */
19
class SitewideContentReportTest extends SapphireTest
20
{
21
    /**
22
     * @var string
23
     */
24
    protected static $fixture_file = 'SitewideContentReportTest.yml';
25
26
    public function setUp()
27
    {
28
        // This module is made to work with subsites, but will still operate
29
        // without it (although presumably being of far less value).
30
        // The fixture includes subsite definitions, which is a problem if
31
        // the module isn't installed. So we'll use the same fixture without
32
        // the subsites definitions if this is the case.
33
        if (!class_exists(Subsite::class)) {
34
            static::$fixture_file = 'SitewideContentReportNoSubsitesTest.yml';
35
        }
36
37
        parent::setUp();
38
39
        foreach (range(1, 5) as $i) {
40
            /** @var SiteTree $page */
41
            $page = $this->objFromFixture('Page', "page{$i}");
42
43
            if ($i <= 3) {
44
                $page->publishRecursive();
45
            }
46
        }
47
    }
48
49
    public function testSourceRecords()
50
    {
51
        $report = SitewideContentReport::create();
52
        $records = $report->sourceRecords();
53
54
        $this->assertEquals(count($records), 2, 'Returns an array with 2 items, one for pages and one for files');
55
        $this->assertArrayHasKey('Pages', $records);
56
        $this->assertArrayHasKey('Files', $records);
57
58
        /** @var DataList $pages */
59
        $pages = $records['Pages'];
60
61
        /** @var DataList $files */
62
        $files = $records['Files'];
63
64
        $this->assertEquals($pages->count(), 5, 'Total number of pages');
65
        $this->assertEquals($files->count(), 1, 'Total number of files');
66
    }
67
68
    public function testGetCMSFields()
69
    {
70
        $report = SitewideContentReport::create();
71
        $fields = $report->getCMSFields();
72
73
        if (class_exists(Subsite::class)) {
74
            $field = $fields->fieldByName('AllSubsites');
75
            $count = count(array_filter(array_keys($field->getSource()), function ($value) {
76
                return is_int($value);
77
            }));
78
79
            $this->assertEquals(4, $count, '2 subsites plus 2 added options to filter by subsite');
80
        } else {
81
            $this->assertNull($fields->fieldByName('AllSubsites'));
82
        }
83
    }
84
85
    public function testReportFields()
86
    {
87
        $report = SitewideContentReport::create();
88
89
        // Test pages view
90
        $gridField = $report->getReportField('Pages');
91
92
        /* @var $columns GridFieldDataColumns */
93
        $columns = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);
94
        $displayed = $columns->getDisplayFields($gridField);
95
96
        $this->assertArrayHasKey('Title', $displayed);
97
        $this->assertArrayHasKey('Created', $displayed);
98
        $this->assertArrayHasKey('LastEdited', $displayed);
99
        $this->assertArrayHasKey('i18n_singular_name', $displayed);
100
        $this->assertArrayHasKey('StageState', $displayed);
101
102
        // Use correct link
103
        $this->assertArrayHasKey('RelativeLink', $displayed);
104
        $this->assertArrayNotHasKey('AbsoluteLink', $displayed);
105
106
        if (class_exists(Subsite::class)) {
107
            $this->assertArrayHasKey('SubsiteName', $displayed);
108
        } else {
109
            $this->assertArrayNotHasKey('SubsiteName', $displayed);
110
        }
111
112
        // Export-only fields are not in display list
113
        $this->assertArrayNotHasKey('Terms', $displayed);
114
        $this->assertArrayNotHasKey('OwnerNames', $displayed);
115
        $this->assertArrayNotHasKey('ReviewDate', $displayed);
116
        $this->assertArrayNotHasKey('MetaDescription', $displayed);
117
118
        // Tests print / export field
119
        /* @var $export GridFieldExportButton */
120
        $export = $gridField->getConfig()->getComponentByType(GridFieldExportButton::class);
121
        $exported = $export->getExportColumns();
122
123
        // Make sure all shared columns are in this report
124
        $this->assertArrayHasKey('Title', $exported);
125
        $this->assertArrayHasKey('Created', $exported);
126
        $this->assertArrayHasKey('LastEdited', $exported);
127
        $this->assertArrayHasKey('i18n_singular_name', $exported);
128
        $this->assertArrayHasKey('StageState', $exported);
129
130
        // Export-only fields
131
        $this->assertArrayHasKey('MetaDescription', $exported);
132
133
        // Use correct link
134
        $this->assertArrayHasKey('AbsoluteLink', $exported);
135
        $this->assertArrayNotHasKey('RelativeLink', $exported);
136
137
        if (class_exists(Subsite::class)) {
138
            $this->assertArrayHasKey('SubsiteName', $exported);
139
        } else {
140
            $this->assertArrayNotHasKey('SubsiteName', $exported);
141
        }
142
143
        if (SitewideContentTaxonomy::enabled()) {
144
            $this->assertArrayHasKey('Terms', $exported);
145
        } else {
146
            $this->assertArrayNotHasKey('Terms', $exported);
147
        }
148
149
        if (class_exists(SiteTreeContentReview::class)) {
150
            $this->assertArrayHasKey('OwnerNames', $exported);
151
            $this->assertArrayHasKey('ReviewDate', $exported);
152
        } else {
153
            $this->assertArrayNotHasKey('OwnerNames', $exported);
154
            $this->assertArrayNotHasKey('ReviewDate', $exported);
155
        }
156
    }
157
}
158