BlogArchiveWidgetTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 4
dl 0
loc 118
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 2
A tearDown() 0 6 1
A testArchiveMonthlyFromStage() 0 23 1
A testArchiveMonthlyFromLive() 0 20 2
A testArchiveYearly() 0 21 1
A testArchiveMonthlyWithNewPostsAdded() 0 26 2
1
<?php
2
3
namespace SilverStripe\Blog\Tests;
4
5
use SilverStripe\Blog\Model\Blog;
6
use SilverStripe\Blog\Model\BlogPost;
7
use SilverStripe\Blog\Widgets\BlogArchiveWidget;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\ORM\FieldType\DBDatetime;
10
use SilverStripe\ORM\SS_List;
11
use SilverStripe\Versioned\Versioned;
12
use SilverStripe\Widgets\Model\Widget;
13
14
class BlogArchiveWidgetTest extends SapphireTest
15
{
16
    protected static $fixture_file = 'BlogArchiveWidgetTest.yml';
17
18
    protected function setUp()
19
    {
20
        if (!class_exists(Widget::class)) {
21
            self::$fixture_file = null;
22
            parent::setUp();
23
            $this->markTestSkipped('Test requires silverstripe/widgets to be installed.');
24
        }
25
26
        DBDatetime::set_mock_now('2017-09-20 12:00:00');
27
28
        parent::setUp();
29
    }
30
31
    protected function tearDown()
32
    {
33
        parent::tearDown();
34
35
        DBDatetime::clear_mock_now();
36
    }
37
38
    public function testArchiveMonthlyFromStage()
39
    {
40
        $widgetA = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly-a');
41
        $archiveA = $widgetA->getArchive();
42
43
        $this->assertInstanceOf(SS_List::class, $archiveA);
44
        $this->assertCount(3, $archiveA);
45
        $this->assertListContains([
0 ignored issues
show
Bug introduced by
The method assertListContains() does not exist on SilverStripe\Blog\Tests\BlogArchiveWidgetTest. Did you maybe mean contains()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
            ['Title' => 'August 2017'],
47
            ['Title' => 'September 2017'],
48
            ['Title' => 'May 2015'],
49
        ], $archiveA);
50
51
        $widgetB = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly-b');
52
        $archiveB = $widgetB->getArchive();
53
54
        $this->assertInstanceOf(SS_List::class, $archiveB);
55
        $this->assertCount(2, $archiveB);
56
        $this->assertListContains([
0 ignored issues
show
Bug introduced by
The method assertListContains() does not exist on SilverStripe\Blog\Tests\BlogArchiveWidgetTest. Did you maybe mean contains()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
57
            ['Title' => 'March 2016'],
58
            ['Title' => 'June 2016'],
59
        ], $archiveB);
60
    }
61
62
    public function testArchiveMonthlyFromLive()
63
    {
64
        $original = Versioned::get_stage();
65
66
        $this->objFromFixture(BlogPost::class, 'post-b')->publishRecursive();
67
        $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly-a')->publishRecursive();
68
        Versioned::set_stage(Versioned::LIVE);
69
70
        $widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly-a');
71
        $archive = $widget->getArchive();
72
73
        $this->assertCount(1, $archive);
74
        $this->assertListContains([
0 ignored issues
show
Bug introduced by
The method assertListContains() does not exist on SilverStripe\Blog\Tests\BlogArchiveWidgetTest. Did you maybe mean contains()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
75
            ['Title' => 'August 2017'],
76
        ], $archive);
77
78
        if ($original) {
79
            Versioned::set_stage($original);
80
        }
81
    }
82
83
    public function testArchiveYearly()
84
    {
85
        $widgetA = $this->objFromFixture(BlogArchiveWidget::class, 'archive-yearly-a');
86
        $archiveA = $widgetA->getArchive();
87
88
        $this->assertInstanceOf(SS_List::class, $archiveA);
89
        $this->assertCount(2, $archiveA);
90
        $this->assertListContains([
0 ignored issues
show
Bug introduced by
The method assertListContains() does not exist on SilverStripe\Blog\Tests\BlogArchiveWidgetTest. Did you maybe mean contains()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
91
            ['Title' => '2017'],
92
            ['Title' => '2015'],
93
        ], $archiveA);
94
95
        $widgetB = $this->objFromFixture(BlogArchiveWidget::class, 'archive-yearly-b');
96
        $archiveB = $widgetB->getArchive();
97
98
        $this->assertInstanceOf(SS_List::class, $archiveB);
99
        $this->assertCount(1, $archiveB);
100
        $this->assertListContains([
0 ignored issues
show
Bug introduced by
The method assertListContains() does not exist on SilverStripe\Blog\Tests\BlogArchiveWidgetTest. Did you maybe mean contains()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
101
            ['Title' => '2016'],
102
        ], $archiveB);
103
    }
104
105
    public function testArchiveMonthlyWithNewPostsAdded()
106
    {
107
        $original = Versioned::get_stage();
108
        Versioned::set_stage('Stage');
109
110
        $widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly-a');
111
        $archive = $widget->getArchive();
112
113
        $this->assertCount(3, $archive, 'Three months are shown in the blog archive list from fixtures');
114
115
        DBDatetime::set_mock_now('2018-01-01 12:00:00');
116
117
        $newPost = new BlogPost;
118
        $newPost->ParentID = $this->objFromFixture(Blog::class, 'blog-a')->ID;
119
        $newPost->Title = 'My new blog post';
120
        $newPost->PublishDate = '2018-01-01 08:00:00'; // Same day as the mocked now, but slightly earlier
121
        $newPost->write();
122
123
        $archive = $widget->getArchive();
124
125
        $this->assertCount(4, $archive, 'Four months are shown in the blog archive list after new post added');
126
127
        if ($original) {
128
            Versioned::set_stage($original);
129
        }
130
    }
131
}
132