2017-09-26 05:15:57 +02:00
|
|
|
<?php
|
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
namespace SilverStripe\Blog\Tests;
|
|
|
|
|
2017-09-28 06:03:45 +02:00
|
|
|
use SilverStripe\Blog\Model\Blog;
|
2017-09-27 00:49:57 +02:00
|
|
|
use SilverStripe\Blog\Model\BlogPost;
|
|
|
|
use SilverStripe\Blog\Widgets\BlogArchiveWidget;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
use SilverStripe\Widgets\Model\Widget;
|
|
|
|
|
2017-09-26 05:15:57 +02:00
|
|
|
class BlogArchiveWidgetTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'BlogArchiveWidgetTest.yml';
|
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
protected function setUp()
|
2017-09-26 05:15:57 +02:00
|
|
|
{
|
2017-09-27 00:49:57 +02:00
|
|
|
if (!class_exists(Widget::class)) {
|
2017-09-26 05:15:57 +02:00
|
|
|
self::$fixture_file = null;
|
|
|
|
parent::setUp();
|
|
|
|
$this->markTestSkipped('Test requires silverstripe/widgets to be installed.');
|
|
|
|
}
|
|
|
|
|
2017-09-28 05:53:32 +02:00
|
|
|
DBDatetime::set_mock_now('2017-09-20 12:00:00');
|
2017-09-26 05:15:57 +02:00
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
protected function tearDown()
|
2017-09-26 05:15:57 +02:00
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
DBDatetime::clear_mock_now();
|
2017-09-26 05:15:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArchiveMonthlyFromStage()
|
|
|
|
{
|
2017-09-27 00:49:57 +02:00
|
|
|
$widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly');
|
2017-09-26 05:15:57 +02:00
|
|
|
$archive = $widget->getArchive();
|
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
$this->assertInstanceOf(SS_List::class, $archive);
|
2017-09-26 05:15:57 +02:00
|
|
|
$this->assertCount(3, $archive);
|
2017-09-27 00:49:57 +02:00
|
|
|
$this->assertDOSContains([
|
|
|
|
['Title' => 'August 2017'],
|
|
|
|
['Title' => 'September 2017'],
|
|
|
|
['Title' => 'May 2015'],
|
|
|
|
], $archive);
|
2017-09-26 05:15:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArchiveMonthlyFromLive()
|
|
|
|
{
|
2017-09-27 00:49:57 +02:00
|
|
|
$original = Versioned::get_stage();
|
2017-09-26 05:15:57 +02:00
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
$this->objFromFixture(BlogPost::class, 'post-b')->publishRecursive();
|
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2017-09-26 05:15:57 +02:00
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
$widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly');
|
2017-09-26 05:15:57 +02:00
|
|
|
$archive = $widget->getArchive();
|
|
|
|
|
|
|
|
$this->assertCount(1, $archive);
|
2017-09-27 00:49:57 +02:00
|
|
|
$this->assertDOSContains([
|
|
|
|
['Title' => 'August 2017'],
|
|
|
|
], $archive);
|
2017-09-26 05:15:57 +02:00
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
if ($original) {
|
|
|
|
Versioned::set_stage($original);
|
|
|
|
}
|
2017-09-26 05:15:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArchiveYearly()
|
|
|
|
{
|
2017-09-27 00:49:57 +02:00
|
|
|
$widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-yearly');
|
2017-09-26 05:15:57 +02:00
|
|
|
$archive = $widget->getArchive();
|
|
|
|
|
2017-09-27 00:49:57 +02:00
|
|
|
$this->assertInstanceOf(SS_List::class, $archive);
|
2017-09-26 05:15:57 +02:00
|
|
|
$this->assertCount(2, $archive);
|
2017-09-27 00:49:57 +02:00
|
|
|
$this->assertDOSContains([
|
|
|
|
['Title' => '2017'],
|
|
|
|
['Title' => '2015'],
|
|
|
|
], $archive);
|
2017-09-26 05:15:57 +02:00
|
|
|
}
|
2017-09-28 04:08:30 +02:00
|
|
|
|
|
|
|
public function testArchiveMonthlyWithNewPostsAdded()
|
|
|
|
{
|
2017-09-28 06:03:45 +02:00
|
|
|
$original = Versioned::get_stage();
|
|
|
|
Versioned::set_stage('Stage');
|
2017-09-28 04:08:30 +02:00
|
|
|
|
2017-09-28 06:03:45 +02:00
|
|
|
$widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly');
|
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');
|
|
|
|
|
2017-09-28 06:03:45 +02:00
|
|
|
DBDatetime::set_mock_now('2018-01-01 12:00:00');
|
2017-09-28 04:08:30 +02:00
|
|
|
|
|
|
|
$newPost = new BlogPost;
|
2017-09-28 06:03:45 +02:00
|
|
|
$newPost->ParentID = $this->objFromFixture(Blog::class, 'my-blog')->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');
|
|
|
|
|
2017-09-28 06:03:45 +02:00
|
|
|
if ($original) {
|
|
|
|
Versioned::set_stage($original);
|
|
|
|
}
|
2017-09-28 04:08:30 +02:00
|
|
|
}
|
2017-09-26 05:15:57 +02:00
|
|
|
}
|