1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Blog\Model\BlogPost; |
6
|
|
|
use SilverStripe\Blog\Widgets\BlogArchiveWidget; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
9
|
|
|
use SilverStripe\ORM\SS_List; |
10
|
|
|
use SilverStripe\Versioned\Versioned; |
11
|
|
|
use SilverStripe\Widgets\Model\Widget; |
12
|
|
|
|
13
|
|
|
class BlogArchiveWidgetTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
protected static $fixture_file = 'BlogArchiveWidgetTest.yml'; |
16
|
|
|
|
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
if (!class_exists(Widget::class)) { |
20
|
|
|
self::$fixture_file = null; |
21
|
|
|
parent::setUp(); |
22
|
|
|
$this->markTestSkipped('Test requires silverstripe/widgets to be installed.'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
DBDatetime::set_mock_now('2017-09-20 12:00:00'); |
26
|
|
|
|
27
|
|
|
parent::setUp(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function tearDown() |
31
|
|
|
{ |
32
|
|
|
parent::tearDown(); |
33
|
|
|
|
34
|
|
|
DBDatetime::clear_mock_now(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function testArchiveMonthlyFromStage() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly'); |
40
|
|
|
$archive = $widget->getArchive(); |
41
|
|
|
|
42
|
|
|
$this->assertInstanceOf(SS_List::class, $archive); |
43
|
|
|
$this->assertCount(3, $archive); |
44
|
|
|
$this->assertDOSContains([ |
45
|
|
|
['Title' => 'August 2017'], |
46
|
|
|
['Title' => 'September 2017'], |
47
|
|
|
['Title' => 'May 2015'], |
48
|
|
|
], $archive); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testArchiveMonthlyFromLive() |
52
|
|
|
{ |
53
|
|
|
$original = Versioned::get_stage(); |
54
|
|
|
|
55
|
|
|
$this->objFromFixture(BlogPost::class, 'post-b')->publishRecursive(); |
56
|
|
|
Versioned::set_stage(Versioned::LIVE); |
57
|
|
|
|
58
|
|
|
$widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly'); |
59
|
|
|
$archive = $widget->getArchive(); |
60
|
|
|
|
61
|
|
|
$this->assertCount(1, $archive); |
62
|
|
|
$this->assertDOSContains([ |
63
|
|
|
['Title' => 'August 2017'], |
64
|
|
|
], $archive); |
65
|
|
|
|
66
|
|
|
if ($original) { |
67
|
|
|
Versioned::set_stage($original); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function testArchiveYearly() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-yearly'); |
74
|
|
|
$archive = $widget->getArchive(); |
75
|
|
|
|
76
|
|
|
$this->assertInstanceOf(SS_List::class, $archive); |
77
|
|
|
$this->assertCount(2, $archive); |
78
|
|
|
$this->assertDOSContains([ |
79
|
|
|
['Title' => '2017'], |
80
|
|
|
['Title' => '2015'], |
81
|
|
|
], $archive); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testArchiveMonthlyWithNewPostsAdded() |
85
|
|
|
{ |
86
|
|
|
$original = Versioned::current_stage(); |
|
|
|
|
87
|
|
|
Versioned::reading_stage('Stage'); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); |
90
|
|
|
$archive = $widget->getArchive(); |
91
|
|
|
|
92
|
|
|
$this->assertCount(3, $archive, 'Three months are shown in the blog archive list from fixtures'); |
93
|
|
|
|
94
|
|
|
SS_Datetime::set_mock_now('2018-01-01 12:00:00'); |
95
|
|
|
|
96
|
|
|
$newPost = new BlogPost; |
97
|
|
|
$newPost->ParentID = $this->objFromFixture('Blog', 'my-blog')->ID; |
98
|
|
|
$newPost->Title = 'My new blog post'; |
99
|
|
|
$newPost->PublishDate = '2018-01-01 08:00:00'; // Same day as the mocked now, but slightly earlier |
100
|
|
|
$newPost->write(); |
101
|
|
|
|
102
|
|
|
$archive = $widget->getArchive(); |
103
|
|
|
|
104
|
|
|
$this->assertCount(4, $archive, 'Four months are shown in the blog archive list after new post added'); |
105
|
|
|
|
106
|
|
|
Versioned::reading_stage($original); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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.