mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
Merge branch '2'
This commit is contained in:
commit
75f5042d60
@ -2,10 +2,6 @@
|
||||
|
||||
namespace SilverStripe\Blog\Widgets;
|
||||
|
||||
if (!class_exists('\\SilverStripe\\Widgets\\Model\\Widget')) {
|
||||
return;
|
||||
}
|
||||
|
||||
use SilverStripe\Blog\Model\Blog;
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Forms\DropdownField;
|
||||
@ -19,6 +15,10 @@ use SilverStripe\Versioned\Versioned;
|
||||
use SilverStripe\View\ArrayData;
|
||||
use SilverStripe\Widgets\Model\Widget;
|
||||
|
||||
if (!class_exists(Widget::class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @method Blog Blog()
|
||||
*
|
||||
@ -109,27 +109,28 @@ class BlogArchiveWidget extends Widget
|
||||
$publishDate = DB::get_conn()->formattedDatetimeClause('"PublishDate"', $format);
|
||||
$fields = [
|
||||
'PublishDate' => $publishDate,
|
||||
'Total' => "Count('PublishDate')"
|
||||
'Total' => "COUNT('\"PublishDate\"')"
|
||||
];
|
||||
|
||||
$stage = Versioned::get_stage();
|
||||
$suffix = ($stage == 'Stage') ? '' : "_{$stage}";
|
||||
$query = SQLSelect::create($fields, "BlogPost{$suffix}")
|
||||
$suffix = ($stage === Versioned::LIVE) ? '_' . Versioned::LIVE : '';
|
||||
$query = SQLSelect::create($fields, '"BlogPost' . $suffix . '"')
|
||||
->addGroupBy($publishDate)
|
||||
->addOrderBy('PublishDate Desc')
|
||||
->addWhere(['PublishDate < ?' => DBDatetime::now()->Format('Y-m-d')]);
|
||||
->addOrderBy('"PublishDate" DESC')
|
||||
->addWhere(['"PublishDate" < ?' => DBDatetime::now()->getISOFormat()]);
|
||||
|
||||
$posts = $query->execute();
|
||||
$result = ArrayList::create();
|
||||
while ($next = $posts->next()) {
|
||||
$date = DBDate::create();
|
||||
$date->setValue(strtotime($next['PublishDate']));
|
||||
$year = $date->Format('Y');
|
||||
|
||||
if ($this->ArchiveType == 'Yearly') {
|
||||
$year = $next['PublishDate'];
|
||||
$month = null;
|
||||
$title = $year;
|
||||
} else {
|
||||
$date = Date::create();
|
||||
$date->setValue(strtotime($next['PublishDate']));
|
||||
|
||||
$year = $date->Format('Y');
|
||||
$month = $date->Format('m');
|
||||
$title = $date->FormatI18N('%B %Y');
|
||||
}
|
||||
@ -141,6 +142,7 @@ class BlogArchiveWidget extends Widget
|
||||
}
|
||||
|
||||
$this->extend('updateGetArchive', $result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
71
tests/Widgets/BlogArchiveWidgetTest.php
Normal file
71
tests/Widgets/BlogArchiveWidgetTest.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
class BlogArchiveWidgetTest extends SapphireTest
|
||||
{
|
||||
protected static $fixture_file = 'BlogArchiveWidgetTest.yml';
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (!class_exists('Widget')) {
|
||||
self::$fixture_file = null;
|
||||
parent::setUp();
|
||||
$this->markTestSkipped('Test requires silverstripe/widgets to be installed.');
|
||||
}
|
||||
|
||||
SS_Datetime::set_mock_now('2017-09-20 00:00:00');
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
SS_Datetime::clear_mock_now();
|
||||
}
|
||||
|
||||
public function testArchiveMonthlyFromStage()
|
||||
{
|
||||
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly');
|
||||
$archive = $widget->getArchive();
|
||||
|
||||
$this->assertInstanceOf('SS_List', $archive);
|
||||
$this->assertCount(3, $archive);
|
||||
$this->assertDOSContains(array(
|
||||
array('Title' => 'August 2017'),
|
||||
array('Title' => 'September 2017'),
|
||||
array('Title' => 'May 2015'),
|
||||
), $archive);
|
||||
}
|
||||
|
||||
public function testArchiveMonthlyFromLive()
|
||||
{
|
||||
$original = Versioned::current_stage();
|
||||
|
||||
$this->objFromFixture('BlogPost', 'post-b')->doPublish();
|
||||
Versioned::reading_stage('Live');
|
||||
|
||||
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly');
|
||||
$archive = $widget->getArchive();
|
||||
|
||||
$this->assertCount(1, $archive);
|
||||
$this->assertDOSContains(array(
|
||||
array('Title' => 'August 2017'),
|
||||
), $archive);
|
||||
|
||||
Versioned::reading_stage($original);
|
||||
}
|
||||
|
||||
public function testArchiveYearly()
|
||||
{
|
||||
$widget = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly');
|
||||
$archive = $widget->getArchive();
|
||||
|
||||
$this->assertInstanceOf('SS_List', $archive);
|
||||
$this->assertCount(2, $archive);
|
||||
$this->assertDOSContains(array(
|
||||
array('Title' => '2017'),
|
||||
array('Title' => '2015'),
|
||||
), $archive);
|
||||
}
|
||||
}
|
27
tests/Widgets/BlogArchiveWidgetTest.yml
Normal file
27
tests/Widgets/BlogArchiveWidgetTest.yml
Normal file
@ -0,0 +1,27 @@
|
||||
SilverStripe\Blog\Model\Blog:
|
||||
my-blog:
|
||||
Title: My Blog
|
||||
|
||||
SilverStripe\Blog\Model\BlogPost:
|
||||
post-a:
|
||||
Title: September Digest
|
||||
PublishDate: 2017-09-01 00:00:00
|
||||
ParentID: =>SilverStripe\Blog\Model\Blog.my-blog
|
||||
post-b:
|
||||
Title: August is Awesome
|
||||
PublishDate: 2017-08-01 00:00:00
|
||||
ParentID: =>SilverStripe\Blog\Model\Blog.my-blog
|
||||
post-c:
|
||||
Title: 2015 is so two years ago
|
||||
PublishDate: 2015-05-02 00:01:02
|
||||
ParentID: =>SilverStripe\Blog\Model\Blog.my-blog
|
||||
|
||||
SilverStripe\Blog\Widgets\BlogArchiveWidget:
|
||||
archive-monthly:
|
||||
NumberToDisplay: 5
|
||||
ArchiveType: Monthly
|
||||
BlogID: =>SilverStripe\Blog\Model\Blog.my-blog
|
||||
archive-yearly:
|
||||
NumberToDisplay: 5
|
||||
ArchiveType: Yearly
|
||||
BlogID: =>SilverStripe\Blog\Model\Blog.my-blog
|
Loading…
Reference in New Issue
Block a user