1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class BlogArchiveWidgetTest extends SapphireTest |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
protected static $fixture_file = 'BlogArchiveWidgetTest.yml'; |
6
|
|
|
|
7
|
|
|
public function setUp() |
8
|
|
|
{ |
9
|
|
|
if (!class_exists('Widget')) { |
10
|
|
|
self::$fixture_file = null; |
11
|
|
|
parent::setUp(); |
12
|
|
|
$this->markTestSkipped('Test requires silverstripe/widgets to be installed.'); |
|
|
|
|
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
SS_Datetime::set_mock_now('2017-09-20 12:00:00'); |
16
|
|
|
|
17
|
|
|
parent::setUp(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function tearDown() |
21
|
|
|
{ |
22
|
|
|
parent::tearDown(); |
23
|
|
|
|
24
|
|
|
SS_Datetime::clear_mock_now(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function testArchiveMonthlyFromStage() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); |
30
|
|
|
$archive = $widget->getArchive(); |
31
|
|
|
|
32
|
|
|
$this->assertInstanceOf('SS_List', $archive); |
|
|
|
|
33
|
|
|
$this->assertCount(3, $archive); |
|
|
|
|
34
|
|
|
$this->assertDOSContains(array( |
35
|
|
|
array('Title' => 'August 2017'), |
36
|
|
|
array('Title' => 'September 2017'), |
37
|
|
|
array('Title' => 'May 2015'), |
38
|
|
|
), $archive); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testArchiveMonthlyFromLive() |
42
|
|
|
{ |
43
|
|
|
$original = Versioned::current_stage(); |
44
|
|
|
|
45
|
|
|
$this->objFromFixture('BlogPost', 'post-b')->doPublish(); |
46
|
|
|
Versioned::reading_stage('Live'); |
47
|
|
|
|
48
|
|
|
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); |
49
|
|
|
$archive = $widget->getArchive(); |
50
|
|
|
|
51
|
|
|
$this->assertCount(1, $archive); |
|
|
|
|
52
|
|
|
$this->assertDOSContains(array( |
53
|
|
|
array('Title' => 'August 2017'), |
54
|
|
|
), $archive); |
55
|
|
|
|
56
|
|
|
Versioned::reading_stage($original); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function testArchiveYearly() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly'); |
62
|
|
|
$archive = $widget->getArchive(); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf('SS_List', $archive); |
|
|
|
|
65
|
|
|
$this->assertCount(2, $archive); |
|
|
|
|
66
|
|
|
$this->assertDOSContains(array( |
67
|
|
|
array('Title' => '2017'), |
68
|
|
|
array('Title' => '2015'), |
69
|
|
|
), $archive); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testArchiveMonthlyWithNewPostsAdded() |
73
|
|
|
{ |
74
|
|
|
$original = Versioned::current_stage(); |
75
|
|
|
Versioned::reading_stage('Stage'); |
76
|
|
|
|
77
|
|
|
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); |
78
|
|
|
$archive = $widget->getArchive(); |
79
|
|
|
|
80
|
|
|
$this->assertCount(3, $archive, 'Three months are shown in the blog archive list from fixtures'); |
|
|
|
|
81
|
|
|
|
82
|
|
|
SS_Datetime::set_mock_now('2018-01-01 12:00:00'); |
83
|
|
|
|
84
|
|
|
$newPost = new BlogPost; |
85
|
|
|
$newPost->ParentID = $this->objFromFixture('Blog', 'my-blog')->ID; |
86
|
|
|
$newPost->Title = 'My new blog post'; |
87
|
|
|
$newPost->PublishDate = '2018-01-01 08:00:00'; // Same day as the mocked now, but slightly earlier |
88
|
|
|
$newPost->write(); |
89
|
|
|
|
90
|
|
|
$archive = $widget->getArchive(); |
91
|
|
|
|
92
|
|
|
$this->assertCount(4, $archive, 'Four months are shown in the blog archive list after new post added'); |
|
|
|
|
93
|
|
|
|
94
|
|
|
Versioned::reading_stage($original); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.