Added unit tests

This commit is contained in:
micmania1 2013-10-09 23:09:28 +01:00
parent e457c1be26
commit f8eeb3b2e6
12 changed files with 336 additions and 159 deletions

View File

@ -32,7 +32,7 @@ class BlogFilter extends Hierarchy {
$dataQuery = $staged->dataQuery()
->innerJoin("BlogPost", "BlogPost" . $stage . ".ID = SiteTree" . $stage . ".ID")
->where("PublishDate < NOW()");
->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'");
$staged = $staged->setDataQuery($dataQuery);
}
@ -56,7 +56,7 @@ class BlogFilter extends Hierarchy {
} else if(in_array($this->owner->ClassName, ClassInfo::subClassesFor("Blog")) && !Permission::check("VIEW_DRAFT_CONTENT")) {
$dataQuery = $staged->dataQuery()
->innerJoin("BlogPost", "BlogPost_Live.ID = SiteTree_Live.ID")
->where("PublishDate < NOW()");
->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'");
$staged = $staged->setDataQuery($dataQuery);
}
return $staged;

View File

@ -12,8 +12,8 @@ class BlogPostFilter extends DataExtension {
if($stage == "Stage") $stage = "";
else $stage = "_" . Convert::raw2sql($stage);
$query->addWhere("PublishDate < NOW()");
}
$query->addWhere("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'");
}
}

View File

@ -74,7 +74,8 @@ class Blog extends Page {
public function getSettingsFields() {
$fields = parent::getSettingsFields();
$fields->addFieldToTab("Root.Settings", NumericField::create("PostsPerPage", _t("Blog.PostsPerPage", "Posts Per Page")));
$fields->addFieldToTab("Root.Settings",
NumericField::create("PostsPerPage", _t("Blog.PostsPerPage", "Posts Per Page")));
return $fields;
}
@ -111,6 +112,35 @@ class Blog extends Page {
return $blogPosts;
}
/**
* Returns blogs posts for a given date period.
*
* @param $year int
* @param $month int
* @param $dat int
*
* @return DataList
**/
public function getArchivedBlogPosts($year, $month = null, $day = null) {
$query = $this->getBlogPosts()->dataQuery();
$stage = $query->getQueryParam("Versioned.stage");
if($stage) $stage = '_' . Convert::raw2sql($stage);
$query->innerJoin("BlogPost", "`SiteTree" . $stage . "`.`ID` = `BlogPost" . $stage . "`.`ID`");
$query->where("YEAR(PublishDate) = '" . Convert::raw2sql($year) . "'");
if($month) {
$query->where("MONTH(PublishDate) = '" . Convert::raw2sql($month) . "'");
if($day) {
$query->where("DAY(PublishDate) = '" . Convert::raw2sql($day) . "'");
}
}
return $this->getBlogPosts()->setDataQuery($query);
}
}
@ -176,21 +206,7 @@ class Blog_Controller extends Page_Controller {
}
if($year) {
$query = $this->getBlogPosts()->dataQuery();
$stage = $query->getQueryParam("Versioned.stage");
if($stage) $stage = '_' . Convert::raw2sql($stage);
$query->innerJoin("BlogPost", "`SiteTree" . $stage . "`.`ID` = `BlogPost" . $stage . "`.`ID`");
$query->where("YEAR(PublishDate) = '" . Convert::raw2sql($year) . "'");
if($month) {
$query->where("MONTH(PublishDate) = '" . Convert::raw2sql($month) . "'");
if($day) {
$query->where("DAY(PublishDate) = '" . Convert::raw2sql($day) . "'");
}
}
$this->blogPosts = $this->getBlogPosts()->setDataQuery($query);
$this->blogPosts = $this->getArchivedBlogPosts($year, $month, $day);
return $this->render();
}
return $this->httpError(404, "Not Found");

View File

@ -63,8 +63,10 @@ class BlogPost extends Page {
public function getCMSFields() {
// Assign to variable & pass for PHP <= 5.4 closure compatibility
$data['TagsMap'] = $this->Parent()->Tags()->map()->toArray();
$data['CategoryMap'] = $this->Parent()->Categories()->map()->toArray();
$data = array(
"TagsMap" => $this->Parent()->Tags()->map()->toArray(),
"CategoryMap" => $this->Parent()->Categories()->map()->toArray()
);
$this->beforeUpdateCMSFields(function($fields) use ($data) {
// Add Publish date fields
@ -108,7 +110,7 @@ class BlogPost extends Page {
/**
* Checks the publish date to see if the blog post as actually been published.
* Checks the publish date to see if the blog post has actually been published.
*
* @param $member Member|null
*

24
tests/BlogPostFilterTest.php Executable file
View File

@ -0,0 +1,24 @@
<?php
class BlogPostFilterTest extends SapphireTest {
static $fixture_file = "blog.yml";
public function setUp() {
SS_Datetime::set_mock_now("2013-10-10 20:00:00");
parent::setUp();
}
public function testFilter() {
$member = Member::currentUser();
if($member) $member->logout();
$count = BlogPost::get()->count();
$this->assertEquals(3, $count, "Filtered blog posts");
SS_Datetime::set_mock_now("2020-01-01 00:00:00");
$count = BlogPost::get()->count();
$this->assertEquals(5, $count, "Unfiltered blog posts");
}
}

22
tests/BlogTagTest.php Executable file
View File

@ -0,0 +1,22 @@
<?php
class BlogTagTest extends SapphireTest {
static $fixture_file = "blog.yml";
public function setUp() {
SS_Datetime::set_mock_now("2013-10-10 20:00:00");
parent::setUp();
}
public function testBlogPosts() {
$member = Member::currentUser();
if($member) $member->logout();
$post = $this->objFromFixture("BlogPost", "blogpost1");
$tag = $this->objFromFixture("BlogTag", "firsttag");
$posts = $tag->BlogPosts();
$this->assertEquals(1, $posts->count(), "Tag blog post count");
}
}

50
tests/BlogTest.php Executable file
View File

@ -0,0 +1,50 @@
<?php
class BlogTest extends SapphireTest {
static $fixture_file = "blog.yml";
public function setUp() {
SS_Datetime::set_mock_now("2013-10-10 20:00:00");
parent::setUp();
}
public function testGetExcludedSiteTreeClassNames() {
$member = Member::currentUser();
if($member) $member->logout();
$blog = $this->objFromFixture("Blog", 'firstblog');
Config::inst()->update("BlogPost", "show_in_sitetree", true);
$classes = $blog->getExcludedSiteTreeClassNames();
$this->assertEquals(0, count($classes), "No classes should be hidden.");
Config::inst()->update("BlogPost", "show_in_sitetree", false);
$classes = $blog->getExcludedSiteTreeClassNames();
$this->assertEquals(1, count($classes), "BlogPost class should be hidden.");
}
public function testGetArchivedBlogPosts() {
$member = Member::currentUser();
if($member) $member->logout();
$blog = $this->objFromFixture("Blog", "firstblog");
// Test yearly
$archive = $blog->getArchivedBlogPosts(2013);
$this->assertEquals(2, $archive->count(), "Incorrect Yearly Archive count for 2013");
$this->assertEquals("First post", $archive->first()->Title, "Incorrect First Blog post");
$this->assertEquals("Second post", $archive->last()->Title, "Incorrect Last Blog post");
// Test monthly
$archive = $blog->getArchivedBlogPosts(2013, 10);
$this->assertEquals(1, $archive->count(), "Incorrect monthly acrhive count.");
// Test daily
$archive = $blog->getArchivedBlogPosts(2013, 10, 01);
$this->assertEquals(1, $archive->count(), "Incorrect daily archive count.");
}
}

47
tests/blog.yml Executable file
View File

@ -0,0 +1,47 @@
#####################################################
# Mock date is set to 2013-10-01 20:00:00
#####################################################
Blog:
firstblog:
Title: 'First Blog'
BlogTag:
firsttag:
Title: 'First Tag'
URLSegment: 'first-tag';
Blog: =>Blog.firstblog
BlogCategory:
firstcategory:
Title: 'First Category'
URLSegment: 'first-category'
Blog: =>Blog.firstblog
BlogPost:
blogpost1:
Title: 'First post'
URLSegment: first-post
PublishDate: '2013-10-01 15:00:00'
Parent: =>Blog.firstblog
Tags: =>BlogTag.firsttag
blogpost2:
Title: 'Second post'
URLSegment: second-post
PublishDate: '2013-09-01 15:00:00'
Parent: =>Blog.firstblog
blogpost3:
Title: 'Old post'
URLSegment: old-post
PublishDate: '2012-01-09 15:00:00'
Parent: =>Blog.firstblog
futurepost:
Title: 'Future Post'
URLSegment: future-post
PublishDate: '2015-01-01 00:00:00'
Tags: =>BlogTag.firsttag
futurepost2:
Title: 'Future Post 2'
URLSegment: future-post-2
PublishDate: '2013-11-01 00:00:00'
Tags: =>BlogTag.firsttag

View File

@ -1,81 +1,85 @@
<?php
class BlogArchiveWidget extends Widget {
private static $title = "Archive";
if(class_exists("Widget")) {
private static $cmsTitle = "Archive";
class BlogArchiveWidget extends Widget {
private static $title = "Archive";
private static $description = "Displays an archive list of posts.";
private static $cmsTitle = "Archive";
private static $db = array(
"NumberToDisplay" => "Int",
"Type" => "Enum('Monthly, Yearly', 'Monthly')"
);
private static $description = "Displays an archive list of posts.";
private static $defaults = array(
"NumberOfMonths" => 12
);
private static $db = array(
"NumberToDisplay" => "Int",
"Type" => "Enum('Monthly, Yearly', 'Monthly')"
);
private static $has_one = array(
"Blog" => "Blog",
);
private static $defaults = array(
"NumberOfMonths" => 12
);
public function getCMSFields() {
$fields = parent::getCMSFields();
private static $has_one = array(
"Blog" => "Blog",
);
$type = $this->dbObject("Type")->enumValues();
foreach($type as $k => $v) {
$type[$k] = _t("BlogArchiveWidget." . ucfirst(strtolower($v)), $v);
public function getCMSFields() {
$fields = parent::getCMSFields();
$type = $this->dbObject("Type")->enumValues();
foreach($type as $k => $v) {
$type[$k] = _t("BlogArchiveWidget." . ucfirst(strtolower($v)), $v);
}
$fields->push(DropdownField::create("BlogID", _t("BlogArchiveWidget.Blog", "Blog"), Blog::get()->map()));
$fields->push(DropdownField::create("Type", _t("BlogArchiveWidget.Type", "Type"), $type));
$fields->push(NumericField::create("NumberToDisplay", _t("BlogArchiveWidget.NumberToDisplay", "No. to Display")));
return $fields;
}
/**
* Returns a list of months where blog posts are present.
*
* @return DataList
**/
public function getArchive() {
$query = $this->Blog()->getBlogPosts()->dataQuery();
if($this->Type == "Yearly") {
$query->groupBy("DATE_FORMAT(PublishDate, '%Y')");
} else {
$query->groupBy("DATE_FORMAT(PublishDate, '%Y-%M')");
}
$articles = $this->Blog()->getBlogPosts()->setDataQuery($query);
if($this->NumberToDisplay > 0) $articles = $articles->limit($this->NumberToDisplay);
$archive = new ArrayList();
if($articles->count() > 0) {
foreach($articles as $article) {
if($this->Type == "Yearly") {
$year = date('Y', strtotime($article->PublishDate));
$month = null;
$title = $year;
} else {
$year = date('Y', strtotime($article->PublishDate));
$month = date('m', strtotime($article->PublishDate));
$title = date('F Y', strtotime($article->PublishDate));
}
$archive->push(new ArrayData(array(
"Title" => $title,
"Link" => Controller::join_links($this->Blog()->Link("archive"), $year, $month)
)));
}
}
return $archive;
}
$fields->push(DropdownField::create("BlogID", _t("BlogArchiveWidget.Blog", "Blog"), Blog::get()->map()));
$fields->push(DropdownField::create("Type", _t("BlogArchiveWidget.Type", "Type"), $type));
$fields->push(NumericField::create("NumberToDisplay", _t("BlogArchiveWidget.NumberToDisplay", "No. to Display")));
return $fields;
}
/**
* Returns a list of months where blog posts are present.
*
* @return DataList
**/
public function getArchive() {
$query = $this->Blog()->getBlogPosts()->dataQuery();
if($this->Type == "Yearly") {
$query->groupBy("DATE_FORMAT(PublishDate, '%Y')");
} else {
$query->groupBy("DATE_FORMAT(PublishDate, '%Y-%M')");
}
$articles = $this->Blog()->getBlogPosts()->setDataQuery($query);
if($this->NumberToDisplay > 0) $articles = $articles->limit($this->NumberToDisplay);
class BlogArchiveWidget_Controller extends Widget_Controller {
$archive = new ArrayList();
if($articles->count() > 0) {
foreach($articles as $article) {
if($this->Type == "Yearly") {
$year = date('Y', strtotime($article->PublishDate));
$month = null;
$title = $year;
} else {
$year = date('Y', strtotime($article->PublishDate));
$month = date('m', strtotime($article->PublishDate));
$title = date('F Y', strtotime($article->PublishDate));
}
$archive->push(new ArrayData(array(
"Title" => $title,
"Link" => Controller::join_links($this->Blog()->Link("archive"), $year, $month)
)));
}
}
return $archive;
}
}
class BlogArchiveWidget_Controller extends Widget_Controller {
}

View File

@ -1,35 +1,39 @@
<?php
class BlogCategoriesWidget extends Widget {
private static $title = "Categories";
if(class_exists("Widget")) {
private static $cmsTitle = "Blog Categories";
class BlogCategoriesWidget extends Widget {
private static $title = "Categories";
private static $description = "Displays a list of blog categories.";
private static $cmsTitle = "Blog Categories";
private static $db = array();
private static $description = "Displays a list of blog categories.";
private static $has_one = array(
"Blog" => "Blog",
);
private static $db = array();
private static $has_one = array(
"Blog" => "Blog",
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->push(DropdownField::create("BlogID", _t("BlogCategoriesWidget.Blog", "Blog"), Blog::get()->map()));
return $fields;
}
public function getCategories() {
$blog = $this->Blog();
if($blog) {
return $blog->Categories();
}
return array();
}
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->push(DropdownField::create("BlogID", _t("BlogCategoriesWidget.Blog", "Blog"), Blog::get()->map()));
return $fields;
}
public function getCategories() {
$blog = $this->Blog();
if($blog) {
return $blog->Categories();
}
return array();
class BlogCategoriesWidget_Controller extends Widget_Controller {
}
}
class BlogCategoriesWidget_Controller extends Widget_Controller {
}

View File

@ -1,40 +1,44 @@
<?php
class BlogRecentPostsWidget extends Widget {
private static $title = "Recent Posts";
if(class_exists("Widget")) {
private static $cmsTitle = "Recent Posts";
class BlogRecentPostsWidget extends Widget {
private static $title = "Recent Posts";
private static $description = "Displays a list of recent blog posts.";
private static $cmsTitle = "Recent Posts";
private static $db = array(
"NumberOfPosts" => "Int",
);
private static $description = "Displays a list of recent blog posts.";
private static $has_one = array(
"Blog" => "Blog",
);
private static $db = array(
"NumberOfPosts" => "Int",
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->push(DropdownField::create("BlogID", _t("BlogRecentPostsWidget.Blog", "Blog"), Blog::get()->map()));
$fields->push(NumericField::create("NumberOfPosts", _t("BlogRecentPostsWidget.NumberOfPosts", "Number of Posts")));
return $fields;
}
private static $has_one = array(
"Blog" => "Blog",
);
public function getPosts() {
$blog = $this->Blog();
if($blog) {
return $blog->getBlogPosts()
->sort("PublishDate DESC")
->limit($this->NumberOfPosts);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->push(DropdownField::create("BlogID", _t("BlogRecentPostsWidget.Blog", "Blog"), Blog::get()->map()));
$fields->push(NumericField::create("NumberOfPosts", _t("BlogRecentPostsWidget.NumberOfPosts", "Number of Posts")));
return $fields;
}
return array();
public function getPosts() {
$blog = $this->Blog();
if($blog) {
return $blog->getBlogPosts()
->sort("PublishDate DESC")
->limit($this->NumberOfPosts);
}
return array();
}
}
}
class BlogRecentPostsWidget_Controller extends Widget_Controller {
}
class BlogRecentPostsWidget_Controller extends Widget_Controller {
}

View File

@ -1,35 +1,39 @@
<?php
class BlogTagsWidget extends Widget {
private static $title = "Tags";
if(class_exists("Widget")) {
private static $cmsTitle = "Blog Tags";
class BlogTagsWidget extends Widget {
private static $title = "Tags";
private static $description = "Displays a list of blog tags.";
private static $cmsTitle = "Blog Tags";
private static $db = array();
private static $description = "Displays a list of blog tags.";
private static $has_one = array(
"Blog" => "Blog",
);
private static $db = array();
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->push(DropdownField::create("BlogID", _t("BlogTagsWidget.Blog", "Blog"), Blog::get()->map()));
return $fields;
}
private static $has_one = array(
"Blog" => "Blog",
);
public function getTags() {
$blog = $this->Blog();
if($blog) {
return $blog->Tags();
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->push(DropdownField::create("BlogID", _t("BlogTagsWidget.Blog", "Blog"), Blog::get()->map()));
return $fields;
}
return array();
public function getTags() {
$blog = $this->Blog();
if($blog) {
return $blog->Tags();
}
return array();
}
}
}
class BlogTagsWidget_Controller extends Widget_Controller {
}
class BlogTagsWidget_Controller extends Widget_Controller {
}