mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
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:
parent
25735591bd
commit
f9aa8d9ab2
@ -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($date) {
|
||||
if(strpos($date, '-')) {
|
||||
$year = (int) substr($date, 0, strpos($date, '-'));
|
||||
$month = (int) substr($date, strpos($date, '-') + 1);
|
||||
|
||||
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%'";
|
||||
if($year && $month) {
|
||||
$dateCheck = "AND MONTH(`BlogEntry`.Date) = $month AND YEAR(`BlogEntry`.Date) = $year";
|
||||
}
|
||||
} else {
|
||||
$year = Director::urlParam('Action');
|
||||
$month = Director::urlParam('ID');
|
||||
|
||||
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;
|
||||
$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/
|
||||
*/
|
||||
|
57
tests/BlogHolderTest.php
Normal file
57
tests/BlogHolderTest.php
Normal 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
28
tests/BlogTest.yml
Normal 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
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user