Use MONTH() and YEAR() SQL functions to check date on entries.

Refactored BlogEntries() to be more testable.
Added tests for Entries().
This commit is contained in:
Andrew O'Neil 2008-09-11 23:36:15 +00:00
parent 25735591bd
commit f9aa8d9ab2
3 changed files with 135 additions and 26 deletions

View File

@ -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) { public function Entries($limit = '', $tag = '', $date = '') {
$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$tagCheck = ''; $tagCheck = '';
$dateCheck = ""; $dateCheck = '';
if(isset($_GET['tag'])) { if($tag) {
$tag = addslashes($_GET['tag']); $SQL_tag = addslashes($tag);
$tag = str_replace(array("\\",'_','%',"'"), array("\\\\","\\_","\\%","\\'"), $tag); $SQL_tag = str_replace(array("\\",'_','%',"'"), array("\\\\","\\_","\\%","\\'"), $tag);
$tagCheck = "AND `BlogEntry`.Tags LIKE '%$tag%'"; $tagCheck = "AND `BlogEntry`.Tags LIKE '%$tag%'";
} }
if($date) {
if(Director::urlParams()){ if(strpos($date, '-')) {
if(Director::urlParam('Action') == 'tag') { $year = (int) substr($date, 0, strpos($date, '-'));
$tag = addslashes(Director::urlParam('ID')); $month = (int) substr($date, strpos($date, '-') + 1);
$tag = str_replace(array("\\",'_','%',"'"), array("\\\\","\\_","\\%","\\'"), $tag);
$tagCheck = "AND `BlogEntry`.Tags LIKE '%$tag%'";
} else {
$year = Director::urlParam('Action');
$month = Director::urlParam('ID');
if(is_numeric($month) && is_numeric($month)){ if($year && $month) {
$nextyear = ($month==12) ? $year + 1 : $year; $dateCheck = "AND MONTH(`BlogEntry`.Date) = $month AND YEAR(`BlogEntry`.Date) = $year";
$nextmonth = $month % 12 + 1; }
$dateCheck = "AND `BlogEntry`.Date BETWEEN '$year-$month-1' AND '$nextyear-$nextmonth-1'"; } else {
} else if(isset($year)){ $year = (int) $date;
$nextyear = $year + 1; if($year) {
$dateCheck = "AND `BlogEntry`.Date BETWEEN '$year-1-1' AND '".$nextyear."-1-1'"; $dateCheck = "AND YEAR(`BlogEntry`.Date) = $year";
} else if($this->LandingPageFreshness) {
$dateCheck = "AND `BlogEntry`.Date > NOW() - INTERVAL " . $this->LandingPageFreshness;
} }
} }
} }
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/ * Gets the archived blogs for a particular month or year, in the format /year/month/ eg: /2008/10/
*/ */

57
tests/BlogHolderTest.php Normal file
View File

@ -0,0 +1,57 @@
<?php
class BlogHolderTest extends SapphireTest {
static $fixture_file = 'blog/tests/BlogTest.yml';
function testGetAllBlogEntries() {
$mainblog = $this->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);
}
}
}
?>

28
tests/BlogTest.yml Normal file
View File

@ -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