FIX Archive widget shows months from posts published that day

This commit is contained in:
Robbie Averill 2017-09-28 15:08:30 +13:00
parent 165ef46b64
commit d47648a86d
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 . '"')
->addGroupBy($publishDate)
->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();
$result = new ArrayList();
while ($next = $posts->next()) {
foreach ($posts as $post) {
if ($this->ArchiveType == 'Yearly') {
$year = $next['PublishDate'];
$year = $post['PublishDate'];
$month = null;
$title = $year;
} else {
$date = Date::create();
$date->setValue(strtotime($next['PublishDate']));
$date->setValue(strtotime($post['PublishDate']));
$year = $date->Format('Y');
$month = $date->Format('m');

View File

@ -12,7 +12,7 @@ class BlogArchiveWidgetTest extends SapphireTest
$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();
}
@ -68,4 +68,29 @@ class BlogArchiveWidgetTest extends SapphireTest
array('Title' => '2015'),
), $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);
}
}