Merge branch '2.4' into 2

This commit is contained in:
Franco Springveldt 2017-09-28 16:46:58 +13:00
commit f29b73f532
2 changed files with 30 additions and 5 deletions

View File

@ -100,18 +100,18 @@ class BlogArchiveWidget extends Widget
$query = SQLSelect::create($fields, '"BlogPost' . $suffix . '"') $query = SQLSelect::create($fields, '"BlogPost' . $suffix . '"')
->addGroupBy($publishDate) ->addGroupBy($publishDate)
->addOrderBy('"PublishDate" DESC') ->addOrderBy('"PublishDate" DESC')
->addWhere(array('"PublishDate" < ?' => SS_Datetime::now()->Format('Y-m-d'))); ->addWhere(array('"PublishDate" <= ?' => SS_Datetime::now()->Format('Y-m-d H:i:s')));
$posts = $query->execute(); $posts = $query->execute();
$result = new ArrayList(); $result = new ArrayList();
while ($next = $posts->next()) { foreach ($posts as $post) {
if ($this->ArchiveType == 'Yearly') { if ($this->ArchiveType == 'Yearly') {
$year = $next['PublishDate']; $year = $post['PublishDate'];
$month = null; $month = null;
$title = $year; $title = $year;
} else { } else {
$date = Date::create(); $date = Date::create();
$date->setValue(strtotime($next['PublishDate'])); $date->setValue(strtotime($post['PublishDate']));
$year = $date->Format('Y'); $year = $date->Format('Y');
$month = $date->Format('m'); $month = $date->Format('m');

View File

@ -12,7 +12,7 @@ class BlogArchiveWidgetTest extends SapphireTest
$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'); SS_Datetime::set_mock_now('2017-09-20 12:00:00');
parent::setUp(); parent::setUp();
} }
@ -68,4 +68,29 @@ class BlogArchiveWidgetTest extends SapphireTest
array('Title' => '2015'), array('Title' => '2015'),
), $archive); ), $archive);
} }
public function testArchiveMonthlyWithNewPostsAdded()
{
$original = Versioned::current_stage();
Versioned::reading_stage('Stage');
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly');
$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;
$newPost->ParentID = $this->objFromFixture('Blog', 'my-blog')->ID;
$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');
Versioned::reading_stage($original);
}
} }