2017-09-26 05:15:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class BlogArchiveWidgetTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'BlogArchiveWidgetTest.yml';
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
if (!class_exists('Widget')) {
|
|
|
|
self::$fixture_file = null;
|
|
|
|
parent::setUp();
|
|
|
|
$this->markTestSkipped('Test requires silverstripe/widgets to be installed.');
|
|
|
|
}
|
|
|
|
|
2017-09-28 04:08:30 +02:00
|
|
|
SS_Datetime::set_mock_now('2017-09-20 12:00:00');
|
2017-09-26 05:15:57 +02:00
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
SS_Datetime::clear_mock_now();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArchiveMonthlyFromStage()
|
|
|
|
{
|
2018-06-29 14:00:38 +02:00
|
|
|
$widgetA = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly-a');
|
|
|
|
$archiveA = $widgetA->getArchive();
|
2017-09-26 05:15:57 +02:00
|
|
|
|
2018-06-29 14:00:38 +02:00
|
|
|
$this->assertInstanceOf('SS_List', $archiveA);
|
|
|
|
$this->assertCount(3, $archiveA);
|
2017-09-26 05:15:57 +02:00
|
|
|
$this->assertDOSContains(array(
|
|
|
|
array('Title' => 'August 2017'),
|
|
|
|
array('Title' => 'September 2017'),
|
|
|
|
array('Title' => 'May 2015'),
|
2018-06-29 14:00:38 +02:00
|
|
|
), $archiveA);
|
|
|
|
|
|
|
|
$widgetB = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly-b');
|
|
|
|
$archiveB = $widgetB->getArchive();
|
|
|
|
|
|
|
|
$this->assertInstanceOf('SS_List', $archiveB);
|
|
|
|
$this->assertCount(2, $archiveB);
|
|
|
|
$this->assertDOSContains(array(
|
|
|
|
array('Title' => 'March 2016'),
|
|
|
|
array('Title' => 'June 2016'),
|
|
|
|
), $archiveB);
|
2017-09-26 05:15:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArchiveMonthlyFromLive()
|
|
|
|
{
|
|
|
|
$original = Versioned::current_stage();
|
|
|
|
|
|
|
|
$this->objFromFixture('BlogPost', 'post-b')->doPublish();
|
|
|
|
Versioned::reading_stage('Live');
|
|
|
|
|
2018-06-29 14:00:38 +02:00
|
|
|
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly-a');
|
2017-09-26 05:15:57 +02:00
|
|
|
$archive = $widget->getArchive();
|
|
|
|
|
|
|
|
$this->assertCount(1, $archive);
|
|
|
|
$this->assertDOSContains(array(
|
|
|
|
array('Title' => 'August 2017'),
|
|
|
|
), $archive);
|
|
|
|
|
2018-06-19 15:39:17 +02:00
|
|
|
if ($original) {
|
|
|
|
Versioned::reading_stage($original);
|
|
|
|
}
|
2017-09-26 05:15:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArchiveYearly()
|
|
|
|
{
|
2018-06-29 14:00:38 +02:00
|
|
|
$widgetA = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly-a');
|
|
|
|
$archiveA = $widgetA->getArchive();
|
2017-09-26 05:15:57 +02:00
|
|
|
|
2018-06-29 14:00:38 +02:00
|
|
|
$this->assertInstanceOf('SS_List', $archiveA);
|
|
|
|
$this->assertCount(2, $archiveA);
|
2017-09-26 05:15:57 +02:00
|
|
|
$this->assertDOSContains(array(
|
|
|
|
array('Title' => '2017'),
|
|
|
|
array('Title' => '2015'),
|
2018-06-29 14:00:38 +02:00
|
|
|
), $archiveA);
|
|
|
|
|
|
|
|
$widgetB = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly-b');
|
|
|
|
$archiveB = $widgetB->getArchive();
|
|
|
|
|
|
|
|
$this->assertInstanceOf('SS_List', $archiveB);
|
|
|
|
$this->assertCount(1, $archiveB);
|
|
|
|
$this->assertDOSContains(array(
|
|
|
|
array('Title' => '2016'),
|
|
|
|
), $archiveB);
|
2017-09-26 05:15:57 +02:00
|
|
|
}
|
2017-09-28 04:08:30 +02:00
|
|
|
|
|
|
|
public function testArchiveMonthlyWithNewPostsAdded()
|
|
|
|
{
|
|
|
|
$original = Versioned::current_stage();
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
|
2018-06-29 14:00:38 +02:00
|
|
|
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly-a');
|
2017-09-28 04:08:30 +02:00
|
|
|
$archive = $widget->getArchive();
|
|
|
|
|
|
|
|
$this->assertCount(3, $archive, 'Three months are shown in the blog archive list from fixtures');
|
|
|
|
|
|
|
|
SS_Datetime::set_mock_now('2018-01-01 12:00:00');
|
|
|
|
|
|
|
|
$newPost = new BlogPost;
|
2018-06-29 14:00:38 +02:00
|
|
|
$newPost->ParentID = $this->objFromFixture('Blog', 'blog-a')->ID;
|
2017-09-28 04:08:30 +02:00
|
|
|
$newPost->Title = 'My new blog post';
|
|
|
|
$newPost->PublishDate = '2018-01-01 08:00:00'; // Same day as the mocked now, but slightly earlier
|
|
|
|
$newPost->write();
|
|
|
|
|
|
|
|
$archive = $widget->getArchive();
|
|
|
|
|
|
|
|
$this->assertCount(4, $archive, 'Four months are shown in the blog archive list after new post added');
|
|
|
|
|
2018-06-19 15:39:17 +02:00
|
|
|
if ($original) {
|
|
|
|
Versioned::reading_stage($original);
|
|
|
|
}
|
2017-09-28 04:08:30 +02:00
|
|
|
}
|
2017-09-26 05:15:57 +02:00
|
|
|
}
|