FIX Update BlogArchiveWidget and tests for SS4

This commit is contained in:
Robbie Averill 2017-09-27 11:49:57 +13:00
parent 75f5042d60
commit 6525f1c109
2 changed files with 42 additions and 30 deletions

View File

@ -127,12 +127,12 @@ class BlogArchiveWidget extends Widget
$month = null; $month = null;
$title = $year; $title = $year;
} else { } else {
$date = Date::create(); $date = DBDate::create();
$date->setValue(strtotime($next['PublishDate'])); $date->setValue(strtotime($next['PublishDate']));
$year = $date->Format('Y'); $year = $date->Format('y');
$month = $date->Format('m'); $month = $date->Format('MM');
$title = $date->FormatI18N('%B %Y'); $title = $date->Format('MMMM y');
} }
$result->push(ArrayData::create([ $result->push(ArrayData::create([

View File

@ -1,71 +1,83 @@
<?php <?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;
class BlogArchiveWidgetTest extends SapphireTest class BlogArchiveWidgetTest extends SapphireTest
{ {
protected static $fixture_file = 'BlogArchiveWidgetTest.yml'; protected static $fixture_file = 'BlogArchiveWidgetTest.yml';
public function setUp() protected function setUp()
{ {
if (!class_exists('Widget')) { if (!class_exists(Widget::class)) {
self::$fixture_file = null; self::$fixture_file = null;
parent::setUp(); parent::setUp();
$this->markTestSkipped('Test requires silverstripe/widgets to be installed.'); $this->markTestSkipped('Test requires silverstripe/widgets to be installed.');
} }
SS_Datetime::set_mock_now('2017-09-20 00:00:00'); DBDatetime::set_mock_now('2017-09-20 00:00:00');
parent::setUp(); parent::setUp();
} }
public function tearDown() protected function tearDown()
{ {
parent::tearDown(); parent::tearDown();
SS_Datetime::clear_mock_now(); DBDatetime::clear_mock_now();
} }
public function testArchiveMonthlyFromStage() public function testArchiveMonthlyFromStage()
{ {
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); $widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly');
$archive = $widget->getArchive(); $archive = $widget->getArchive();
$this->assertInstanceOf('SS_List', $archive); $this->assertInstanceOf(SS_List::class, $archive);
$this->assertCount(3, $archive); $this->assertCount(3, $archive);
$this->assertDOSContains(array( $this->assertDOSContains([
array('Title' => 'August 2017'), ['Title' => 'August 2017'],
array('Title' => 'September 2017'), ['Title' => 'September 2017'],
array('Title' => 'May 2015'), ['Title' => 'May 2015'],
), $archive); ], $archive);
} }
public function testArchiveMonthlyFromLive() public function testArchiveMonthlyFromLive()
{ {
$original = Versioned::current_stage(); $original = Versioned::get_stage();
$this->objFromFixture('BlogPost', 'post-b')->doPublish(); $this->objFromFixture(BlogPost::class, 'post-b')->publishRecursive();
Versioned::reading_stage('Live'); Versioned::set_stage(Versioned::LIVE);
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); $widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly');
$archive = $widget->getArchive(); $archive = $widget->getArchive();
$this->assertCount(1, $archive); $this->assertCount(1, $archive);
$this->assertDOSContains(array( $this->assertDOSContains([
array('Title' => 'August 2017'), ['Title' => 'August 2017'],
), $archive); ], $archive);
Versioned::reading_stage($original); if ($original) {
Versioned::set_stage($original);
}
} }
public function testArchiveYearly() public function testArchiveYearly()
{ {
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly'); $widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-yearly');
$archive = $widget->getArchive(); $archive = $widget->getArchive();
$this->assertInstanceOf('SS_List', $archive); $this->assertInstanceOf(SS_List::class, $archive);
$this->assertCount(2, $archive); $this->assertCount(2, $archive);
$this->assertDOSContains(array( $this->assertDOSContains([
array('Title' => '2017'), ['Title' => '2017'],
array('Title' => '2015'), ['Title' => '2015'],
), $archive); ], $archive);
} }
} }