Merge pull request #481 from creative-commoners/pulls/2.4/fix-archive-widget-date-today

FIX Archive widget shows months from posts published that day
This commit is contained in:
Franco Springveldt 2017-09-28 16:40:49 +13:00 committed by GitHub
commit 246b203452
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);
}
}