silverstripe-blog/tests/BlogPostTest.php

115 lines
3.7 KiB
PHP
Raw Normal View History

2015-07-08 07:42:00 +02:00
<?php
namespace SilverStripe\Blog\Tests;
use SilverStripe\Blog\Model\BlogPost;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Security\Member;
2015-11-21 07:17:29 +01:00
class BlogPostTest extends SapphireTest
{
/**
* {@inheritDoc}
2015-11-21 07:17:29 +01:00
* @var string
*/
protected static $fixture_file = 'blog.yml';
2015-07-08 07:42:00 +02:00
2015-11-21 07:17:29 +01:00
/**
* {@inheritdoc}
*/
public function tearDown()
{
DBDatetime::clear_mock_now();
2015-11-21 07:17:29 +01:00
parent::tearDown();
}
2015-07-08 07:42:00 +02:00
2015-11-21 07:17:29 +01:00
/**
* @dataProvider canViewProvider
*/
public function testCanView($date, $user, $page, $canView)
{
$userRecord = $this->objFromFixture(Member::class, $user);
$pageRecord = $this->objFromFixture(BlogPost::class, $page);
DBDatetime::set_mock_now($date);
2015-11-21 07:17:29 +01:00
$this->assertEquals($canView, $pageRecord->canView($userRecord));
}
/**
* @return array
*/
2015-11-21 07:17:29 +01:00
public function canViewProvider()
{
$someFutureDate = '2013-10-10 20:00:00';
$somePastDate = '2009-10-10 20:00:00';
2017-09-14 00:59:01 +02:00
return [
2015-11-21 07:17:29 +01:00
// Check this post given the date has passed
2017-09-14 00:59:01 +02:00
[$someFutureDate, 'Editor', 'PostA', true],
[$someFutureDate, 'Contributor', 'PostA', true],
[$someFutureDate, 'BlogEditor', 'PostA', true],
[$someFutureDate, 'Writer', 'PostA', true],
2015-11-21 07:17:29 +01:00
// Check unpublished pages
2017-09-14 00:59:01 +02:00
[$somePastDate, 'Editor', 'PostA', true],
[$somePastDate, 'Contributor', 'PostA', true],
[$somePastDate, 'BlogEditor', 'PostA', true],
[$somePastDate, 'Writer', 'PostA', true],
2015-11-21 07:17:29 +01:00
// Test a page that was authored by another user
2015-11-21 07:17:29 +01:00
// Check this post given the date has passed
2017-09-14 00:59:01 +02:00
[$someFutureDate, 'Editor', 'FirstBlogPost', true],
[$someFutureDate, 'Contributor', 'FirstBlogPost', true],
[$someFutureDate, 'BlogEditor', 'FirstBlogPost', true],
[$someFutureDate, 'Writer', 'FirstBlogPost', true],
2015-11-21 07:17:29 +01:00
// Check future pages - non-editors shouldn't be able to see this
2017-09-14 00:59:01 +02:00
[$somePastDate, 'Editor', 'FirstBlogPost', true],
[$somePastDate, 'Contributor', 'FirstBlogPost', false],
[$somePastDate, 'BlogEditor', 'FirstBlogPost', false],
[$somePastDate, 'Writer', 'FirstBlogPost', false],
];
2015-11-21 07:17:29 +01:00
}
2015-11-21 07:17:29 +01:00
public function testCandidateAuthors()
{
$blogpost = $this->objFromFixture(BlogPost::class, 'PostC');
2015-11-21 07:17:29 +01:00
$this->assertEquals(7, $blogpost->getCandidateAuthors()->count());
2015-11-21 07:17:29 +01:00
//Set the group to draw Members from
Config::inst()->update(BlogPost::class, 'restrict_authors_to_group', 'blogusers');
2015-11-21 07:17:29 +01:00
$this->assertEquals(3, $blogpost->getCandidateAuthors()->count());
2015-11-23 03:43:27 +01:00
// Test cms field is generated
$fields = $blogpost->getCMSFields();
$this->assertNotEmpty($fields->dataFieldByName('Authors'));
2015-11-21 07:17:29 +01:00
}
public function testCanViewFuturePost()
{
$blogPost = $this->objFromFixture(BlogPost::class, 'NullPublishDate');
$editor = $this->objFromFixture(Member::class, 'BlogEditor');
$this->assertTrue($blogPost->canView($editor));
$visitor = $this->objFromFixture(Member::class, 'Visitor');
$this->assertFalse($blogPost->canView($visitor));
}
/**
* The purpose of getDate() is to act as a proxy for PublishDate in the default RSS
* template, rather than copying the entire template.
*/
public function testGetDate()
{
$blogPost = $this->objFromFixture(BlogPost::class, 'NullPublishDate');
$this->assertNull($blogPost->getDate());
$blogPost = $this->objFromFixture(BlogPost::class, 'PostA');
$this->assertEquals('2012-01-09 15:00:00', $blogPost->getDate());
}
2015-07-08 07:42:00 +02:00
}