From f9aa8d9ab2ea50cfc7266d1671e335c9eecb9948 Mon Sep 17 00:00:00 2001 From: Andrew O'Neil Date: Thu, 11 Sep 2008 23:36:15 +0000 Subject: [PATCH] Use MONTH() and YEAR() SQL functions to check date on entries. Refactored BlogEntries() to be more testable. Added tests for Entries(). --- code/BlogHolder.php | 76 ++++++++++++++++++++++++++-------------- tests/BlogHolderTest.php | 57 ++++++++++++++++++++++++++++++ tests/BlogTest.yml | 28 +++++++++++++++ 3 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 tests/BlogHolderTest.php create mode 100644 tests/BlogTest.yml diff --git a/code/BlogHolder.php b/code/BlogHolder.php index 9817ae4..07ef73f 100644 --- a/code/BlogHolder.php +++ b/code/BlogHolder.php @@ -55,43 +55,39 @@ class BlogHolder extends Page { } /** - * The DataObject of blog entries + * Get entries in this blog. + * @param string limit A clause to insert into the limit clause. + * @param string tag Only get blog entries with this tag + * @param string date Only get blog entries on this date - either a year, or a year-month eg '2008' or '2008-02' + * @return DataObjectSet */ - public function BlogEntries($limit = 10) { - $start = isset($_GET['start']) ? (int)$_GET['start'] : 0; + public function Entries($limit = '', $tag = '', $date = '') { $tagCheck = ''; - $dateCheck = ""; + $dateCheck = ''; - if(isset($_GET['tag'])) { - $tag = addslashes($_GET['tag']); - $tag = str_replace(array("\\",'_','%',"'"), array("\\\\","\\_","\\%","\\'"), $tag); + if($tag) { + $SQL_tag = addslashes($tag); + $SQL_tag = str_replace(array("\\",'_','%',"'"), array("\\\\","\\_","\\%","\\'"), $tag); $tagCheck = "AND `BlogEntry`.Tags LIKE '%$tag%'"; } - - if(Director::urlParams()){ - if(Director::urlParam('Action') == 'tag') { - $tag = addslashes(Director::urlParam('ID')); - $tag = str_replace(array("\\",'_','%',"'"), array("\\\\","\\_","\\%","\\'"), $tag); - $tagCheck = "AND `BlogEntry`.Tags LIKE '%$tag%'"; - } else { - $year = Director::urlParam('Action'); - $month = Director::urlParam('ID'); + if($date) { + if(strpos($date, '-')) { + $year = (int) substr($date, 0, strpos($date, '-')); + $month = (int) substr($date, strpos($date, '-') + 1); - if(is_numeric($month) && is_numeric($month)){ - $nextyear = ($month==12) ? $year + 1 : $year; - $nextmonth = $month % 12 + 1; - $dateCheck = "AND `BlogEntry`.Date BETWEEN '$year-$month-1' AND '$nextyear-$nextmonth-1'"; - } else if(isset($year)){ - $nextyear = $year + 1; - $dateCheck = "AND `BlogEntry`.Date BETWEEN '$year-1-1' AND '".$nextyear."-1-1'"; - } else if($this->LandingPageFreshness) { - $dateCheck = "AND `BlogEntry`.Date > NOW() - INTERVAL " . $this->LandingPageFreshness; + if($year && $month) { + $dateCheck = "AND MONTH(`BlogEntry`.Date) = $month AND YEAR(`BlogEntry`.Date) = $year"; + } + } else { + $year = (int) $date; + if($year) { + $dateCheck = "AND YEAR(`BlogEntry`.Date) = $year"; } } } - return DataObject::get("Page","`ParentID` = $this->ID AND ShowInMenus = 1 $tagCheck $dateCheck","`BlogEntry`.Date DESC",'',"$start, $limit"); + return DataObject::get("Page","`ParentID` = $this->ID AND ShowInMenus = 1 $tagCheck $dateCheck","`BlogEntry`.Date DESC",'',"$limit"); } /** @@ -227,6 +223,34 @@ class BlogHolder_Controller extends Page_Controller { } + function BlogEntries($limit = 10) { + $start = isset($_GET['start']) ? (int)$_GET['start'] : 0; + $tag = ''; + $date = ''; + + if(isset($_GET['tag'])) { + $tag = $_GET['tag']; + } + + + if(Director::urlParams()){ + if(Director::urlParam('Action') == 'tag') { + $tag = Director::urlParam('ID'); + } else { + $year = Director::urlParam('Action'); + $month = Director::urlParam('ID'); + + if($month && is_numeric($month) && $month >= 1 && $month <= 12 && is_numeric($year)) { + $date = "$year-$month"; + } else if(is_numeric($year)) { + $date = $year; + } + } + } + + return $this->Entries(array('start' => $start, 'limit' => $limit), $tag, $date); + } + /** * Gets the archived blogs for a particular month or year, in the format /year/month/ eg: /2008/10/ */ diff --git a/tests/BlogHolderTest.php b/tests/BlogHolderTest.php new file mode 100644 index 0000000..fe2c179 --- /dev/null +++ b/tests/BlogHolderTest.php @@ -0,0 +1,57 @@ +fixture->objFromFixture('BlogHolder', 'mainblog'); + + $this->assertEquals($mainblog->Entries()->Count(), 3); + } + + function testEntriesByMonth() { + $mainblog = $this->fixture->objFromFixture('BlogHolder', 'mainblog'); + + $entries = $mainblog->Entries('', '', '2008-01'); + $this->assertEquals($entries->Count(), 2); + $expectedEntries = array( + 'test-post-2', + 'test-post-3' + ); + + foreach($entries as $entry) { + $this->assertContains($entry->URLSegment, $expectedEntries); + } + } + + function textEntriesByYear() { + $mainblog = $this->fixture->objFromFixture('BlogHolder', 'mainblog'); + + $entries = $mainblog->Entries('', '', '2007'); + $this->assertEquals($entries->Count(), 1); + $expectedEntries = array( + 'test-post' + ); + + foreach($entries as $entry) { + $this->assertContains($entry->URLSegment, $expectedEntries); + } + } + + function testEntriesByTag() { + $mainblog = $this->fixture->objFromFixture('BlogHolder', 'mainblog'); + + $entries = $mainblog->Entries('', 'tag1'); + $this->assertEquals($entries->Count(), 2); + $expectedEntries = array( + 'test-post', + 'test-post-3' + ); + + foreach($entries as $entry) { + $this->assertContains($entry->URLSegment, $expectedEntries); + } + } +} + +?> diff --git a/tests/BlogTest.yml b/tests/BlogTest.yml new file mode 100644 index 0000000..1d49fe6 --- /dev/null +++ b/tests/BlogTest.yml @@ -0,0 +1,28 @@ +BlogHolder: + mainblog: + Title: Main Blog + otherblog: + Title: Other Blog + +BlogEntry: + testpost: + Title: Test Post + URLSegment: test-post + Date: 2007-02-17 18:45:00 + Parent: =>BlogHolder.mainblog + Tags: tag1,tag2 + testpost2: + Title: Test Post 2 + URLSegment: test-post-2 + Date: 2008-01-31 20:48:00 + Parent: =>BlogHolder.mainblog + Tags: tag2,tag3 + testpost3: + Title: Test Post 3 + URLSegment: test-post-3 + Date: 2008-01-17 18:45:00 + Parent: =>BlogHolder.mainblog + Tags: tag1,tag2,tag3 + + +