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,7 +12,7 @@ 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,6 +1,8 @@
<?php
class BlogArchiveWidget extends Widget {
if(class_exists("Widget")) {
class BlogArchiveWidget extends Widget {
private static $title = "Archive";
@ -74,8 +76,10 @@ class BlogArchiveWidget extends Widget {
return $archive;
}
}
}
class BlogArchiveWidget_Controller extends Widget_Controller {
class BlogArchiveWidget_Controller extends Widget_Controller {
}
}

View File

@ -1,6 +1,8 @@
<?php
class BlogCategoriesWidget extends Widget {
if(class_exists("Widget")) {
class BlogCategoriesWidget extends Widget {
private static $title = "Categories";
@ -28,8 +30,10 @@ class BlogCategoriesWidget extends Widget {
return array();
}
}
}
class BlogCategoriesWidget_Controller extends Widget_Controller {
class BlogCategoriesWidget_Controller extends Widget_Controller {
}
}

View File

@ -1,6 +1,8 @@
<?php
class BlogRecentPostsWidget extends Widget {
if(class_exists("Widget")) {
class BlogRecentPostsWidget extends Widget {
private static $title = "Recent Posts";
@ -33,8 +35,10 @@ class BlogRecentPostsWidget extends Widget {
return array();
}
}
}
class BlogRecentPostsWidget_Controller extends Widget_Controller {
class BlogRecentPostsWidget_Controller extends Widget_Controller {
}
}

View File

@ -1,6 +1,8 @@
<?php
class BlogTagsWidget extends Widget {
if(class_exists("Widget")) {
class BlogTagsWidget extends Widget {
private static $title = "Tags";
@ -28,8 +30,10 @@ class BlogTagsWidget extends Widget {
return array();
}
}
}
class BlogTagsWidget_Controller extends Widget_Controller {
class BlogTagsWidget_Controller extends Widget_Controller {
}
}