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 00: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
|
|
|
|
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.