silverstripe-blog/tests/Widgets/BlogArchiveWidgetTest.php

84 lines
2.3 KiB
PHP
Raw Normal View History

2017-09-26 05:15:57 +02:00
<?php
namespace SilverStripe\Blog\Tests;
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';
protected function setUp()
2017-09-26 05:15: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.');
}
DBDatetime::set_mock_now('2017-09-20 00:00:00');
2017-09-26 05:15:57 +02:00
parent::setUp();
}
protected function tearDown()
2017-09-26 05:15:57 +02:00
{
parent::tearDown();
DBDatetime::clear_mock_now();
2017-09-26 05:15:57 +02:00
}
public function testArchiveMonthlyFromStage()
{
$widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly');
2017-09-26 05:15:57 +02:00
$archive = $widget->getArchive();
$this->assertInstanceOf(SS_List::class, $archive);
2017-09-26 05:15:57 +02:00
$this->assertCount(3, $archive);
$this->assertDOSContains([
['Title' => 'August 2017'],
['Title' => 'September 2017'],
['Title' => 'May 2015'],
], $archive);
2017-09-26 05:15:57 +02:00
}
public function testArchiveMonthlyFromLive()
{
$original = Versioned::get_stage();
2017-09-26 05:15:57 +02:00
$this->objFromFixture(BlogPost::class, 'post-b')->publishRecursive();
Versioned::set_stage(Versioned::LIVE);
2017-09-26 05:15: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);
$this->assertDOSContains([
['Title' => 'August 2017'],
], $archive);
2017-09-26 05:15:57 +02:00
if ($original) {
Versioned::set_stage($original);
}
2017-09-26 05:15:57 +02:00
}
public function testArchiveYearly()
{
$widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-yearly');
2017-09-26 05:15:57 +02:00
$archive = $widget->getArchive();
$this->assertInstanceOf(SS_List::class, $archive);
2017-09-26 05:15:57 +02:00
$this->assertCount(2, $archive);
$this->assertDOSContains([
['Title' => '2017'],
['Title' => '2015'],
], $archive);
2017-09-26 05:15:57 +02:00
}
}