From 43bb2248a5781cca3e936157204b390ec7cf1db4 Mon Sep 17 00:00:00 2001 From: Cam Findlay Date: Mon, 9 Nov 2015 11:21:34 +1300 Subject: [PATCH] ENHANCEMENT Default archive year If no year is passed, rather than 404 it should grab the latest years posts. --- code/model/Blog.php | 12 ++++++++---- tests/BlogTest.php | 5 +++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/code/model/Blog.php b/code/model/Blog.php index b445f18..4c079f2 100644 --- a/code/model/Blog.php +++ b/code/model/Blog.php @@ -727,13 +727,17 @@ class Blog_Controller extends Page_Controller { /** * Fetches the archive year from the url. * - * @return null|int + * @return int */ public function getArchiveYear() { - $year = $this->request->param('Year'); - if(preg_match('/^[0-9]{4}$/', $year)) { - return (int) $year; + if($this->request->param('Year')){ + + if(preg_match('/^[0-9]{4}$/', $year = $this->request->param('Year'))) { + return (int) $year; + } + } else { + return SS_Datetime::now()->Year(); } return null; diff --git a/tests/BlogTest.php b/tests/BlogTest.php index 07e8404..b3bf9e0 100755 --- a/tests/BlogTest.php +++ b/tests/BlogTest.php @@ -109,8 +109,8 @@ class BlogTest extends SapphireTest { $this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200'); $link = Controller::join_links($blog->Link('archive')); - - $this->assertEquals(404, $this->getStatusOf($link), 'HTTP Status should be 404'); + $this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200'); + $this->assertEquals(SS_Datetime::now()->Year(), ModelAsController::controller_for($blog)->getArchiveYear(), 'Defaults to current year'); $link = Controller::join_links($blog->Link('archive'), 'invalid-year'); @@ -123,6 +123,7 @@ class BlogTest extends SapphireTest { $link = Controller::join_links($blog->Link('archive'), '2013', '10', '99'); $this->assertEquals(404, $this->getStatusOf($link), 'HTTP Status should be 404'); + } /**