Skip to content

Commit

Permalink
Merge pull request #480 from creative-commoners/pulls/2.4/archived-wi…
Browse files Browse the repository at this point in the history
…dget-years

FIX Display individual years in blog archive widget when set to "Yearly"
  • Loading branch information
kinglozzer authored Sep 26, 2017
2 parents 4aa468a + 8e684de commit 165ef46
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 16 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ before_install:

env:
global:
- DB=MYSQL CORE_RELEASE=3.1
- DB=MYSQL CORE_RELEASE=3.6

# Turn coverage off by default, as it's expensive time wise
- COVERAGE=0
Expand All @@ -27,13 +27,13 @@ matrix:
- php: 5.6
env: DB=MYSQL COVERAGE=1
- php: 5.5
env: DB=MYSQL
env: DB=MYSQL CORE_RELEASE=3.2
- php: 5.6
env: DB=PGSQL
env: DB=PGSQL CORE_RELEASE=3.3
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3.2
env: DB=MYSQL CORE_RELEASE=3.4
- php: 5.6
env: DB=PGSQL CORE_RELEASE=3.2
env: DB=PGSQL CORE_RELEASE=3.5
- php: 5.4
env: DB=SQLITE
- php: 5.3
Expand Down
20 changes: 11 additions & 9 deletions code/widgets/BlogArchiveWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,28 @@ public function getArchive()
$publishDate = DB::get_conn()->formattedDatetimeClause('"PublishDate"', $format);
$fields = array(
'PublishDate' => $publishDate,
'Total' => "Count('PublishDate')"
'Total' => "COUNT('\"PublishDate\"')"
);

$stage = Versioned::current_stage();
$suffix = ($stage == 'Stage') ? '' : "_{$stage}";
$query = SQLSelect::create($fields, "BlogPost{$suffix}")
$suffix = ($stage === 'Live') ? '_Live' : '';
$query = SQLSelect::create($fields, '"BlogPost' . $suffix . '"')
->addGroupBy($publishDate)
->addOrderBy('PublishDate Desc')
->addWhere(array('PublishDate < ?' => SS_Datetime::now()->Format('Y-m-d')));
->addOrderBy('"PublishDate" DESC')
->addWhere(array('"PublishDate" < ?' => SS_Datetime::now()->Format('Y-m-d')));

$posts = $query->execute();
$result = new ArrayList();
while ($next = $posts->next()) {
$date = Date::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');
}
Expand All @@ -124,6 +125,7 @@ public function getArchive()
}

$this->extend('updateGetArchive', $result);

return $result;
}
}
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"type": "silverstripe-module",
"require": {
"silverstripe/cms": "^3.1.0",
"silverstripe/cms": "^3.2.0",
"silverstripe/lumberjack": "~1.1",
"silverstripe/tagfield": "^1.0"
},
Expand All @@ -32,4 +32,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
71 changes: 71 additions & 0 deletions tests/Widgets/BlogArchiveWidgetTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions tests/Widgets/BlogArchiveWidgetTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Blog:
my-blog:
Title: My Blog

BlogPost:
post-a:
Title: September Digest
PublishDate: 2017-09-01 00:00:00
ParentID: =>Blog.my-blog
post-b:
Title: August is Awesome
PublishDate: 2017-08-01 00:00:00
ParentID: =>Blog.my-blog
post-c:
Title: 2015 is so two years ago
PublishDate: 2015-05-02 00:01:02
ParentID: =>Blog.my-blog

BlogArchiveWidget:
archive-monthly:
NumberToDisplay: 5
ArchiveType: Monthly
BlogID: =>Blog.my-blog
archive-yearly:
NumberToDisplay: 5
ArchiveType: Yearly
BlogID: =>Blog.my-blog

0 comments on commit 165ef46

Please sign in to comment.