Completed
Pull Request — master (#20)
by Robbie
02:19
created

CwpStatsReportTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 9.4285
1
<?php
2
3
namespace CWP\Core\Tests;
4
5
use CWP\Core\Report\CwpStatsReport;
6
use Page;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Subsites\Model\Subsite;
9
10
class CwpStatsReportTest extends SapphireTest
11
{
12
    protected static $fixture_file = 'CwpStatsReportTest.yml';
13
14
    protected function setUp()
15
    {
16
        if (!class_exists(CwpStatsReport::class)) {
17
            // If CMS is not installed, do not run this test class
18
            static::$fixture_file = null;
19
            parent::setUp();
20
            $this->markTestSkipped('This test class requires the CMS module to be installed.');
21
        }
22
23
        parent::setUp();
24
    }
25
26
    public function testCount()
27
    {
28
        // Publish all pages apart from page3.
29
        $this->objFromFixture(Page::class, 'page1')->publishRecursive();
30
        $this->objFromFixture(Page::class, 'page2')->publishRecursive();
31
        $this->objFromFixture(Page::class, 'page3')->publishRecursive();
32
33
        // Add page5s to a subsite, if the module is installed.
34
        $page5s = $this->objFromFixture(Page::class, 'page5s');
35
        if (class_exists(Subsite::class)) {
36
            $subsite = Subsite::create();
37
            $subsite->Title = 'subsite';
38
            $subsite->write();
39
40
            $page5s->SubsiteID = $subsite->ID;
41
            $page5s->write();
42
        }
43
        $page5s->publishRecursive();
44
45
        $report = CwpStatsReport::create();
46
        $records = $report->sourceRecords([])->toArray();
47
        $i = 0;
48
        $this->assertEquals($records[$i++]['Count'], 4, 'Four pages in total, across locales, subsites, live only.');
49
        if (class_exists(Subsite::class)) {
50
            $this->assertEquals($records[$i++]['Count'], 3, 'Three pages in the main site, if subsites installed.');
51
            $this->assertEquals($records[$i++]['Count'], 1, 'One page in the subsite, if subsites installed');
52
        }
53
        $this->assertEquals($records[$i++]['Count'], 1, 'One file in total.');
54
    }
55
}
56