2015-07-08 07:42:00 +02:00
|
|
|
<?php
|
|
|
|
|
2017-01-26 09:28:42 +01:00
|
|
|
namespace SilverStripe\Blog\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Blog\Model\BlogPost;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2017-01-26 09:28:42 +01:00
|
|
|
use SilverStripe\Security\Member;
|
2016-12-15 04:41:49 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
class BlogPostTest extends SapphireTest
|
|
|
|
{
|
|
|
|
/**
|
2016-12-15 04:41:49 +01:00
|
|
|
* {@inheritDoc}
|
2015-11-21 07:17:29 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
2017-01-26 09:28:42 +01:00
|
|
|
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()
|
|
|
|
{
|
2016-12-15 04:41:49 +01:00
|
|
|
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)
|
|
|
|
{
|
2017-05-04 08:10:20 +02:00
|
|
|
$userRecord = $this->objFromFixture('Member', $user);
|
|
|
|
$pageRecord = $this->objFromFixture('SiteTree', $page);
|
2016-12-15 04:41:49 +01:00
|
|
|
DBDatetime::set_mock_now($date);
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertEquals($canView, $pageRecord->canView($userRecord));
|
|
|
|
}
|
2016-01-21 17:46:10 +01:00
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
/**
|
|
|
|
* @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';
|
|
|
|
return array(
|
|
|
|
// Check this post given the date has passed
|
|
|
|
array($someFutureDate, 'Editor', 'PostA', true),
|
|
|
|
array($someFutureDate, 'Contributor', 'PostA', true),
|
|
|
|
array($someFutureDate, 'BlogEditor', 'PostA', true),
|
|
|
|
array($someFutureDate, 'Writer', 'PostA', true),
|
2016-01-21 17:46:10 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
// Check unpublished pages
|
|
|
|
array($somePastDate, 'Editor', 'PostA', true),
|
|
|
|
array($somePastDate, 'Contributor', 'PostA', true),
|
|
|
|
array($somePastDate, 'BlogEditor', 'PostA', true),
|
|
|
|
array($somePastDate, 'Writer', 'PostA', true),
|
2016-01-21 17:46:10 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
// Test a page that was authored by another user
|
2015-10-29 23:13:51 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
// Check this post given the date has passed
|
|
|
|
array($someFutureDate, 'Editor', 'FirstBlogPost', true),
|
|
|
|
array($someFutureDate, 'Contributor', 'FirstBlogPost', true),
|
|
|
|
array($someFutureDate, 'BlogEditor', 'FirstBlogPost', true),
|
|
|
|
array($someFutureDate, 'Writer', 'FirstBlogPost', true),
|
2016-01-21 17:46:10 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
// Check future pages - non-editors shouldn't be able to see this
|
|
|
|
array($somePastDate, 'Editor', 'FirstBlogPost', true),
|
|
|
|
array($somePastDate, 'Contributor', 'FirstBlogPost', false),
|
|
|
|
array($somePastDate, 'BlogEditor', 'FirstBlogPost', false),
|
|
|
|
array($somePastDate, 'Writer', 'FirstBlogPost', false),
|
|
|
|
);
|
|
|
|
}
|
2015-10-29 23:13:51 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
public function testCandidateAuthors()
|
|
|
|
{
|
2017-05-04 08:10:20 +02:00
|
|
|
$blogpost = $this->objFromFixture('SiteTree', 'PostC');
|
2015-10-29 23:13:51 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertEquals(7, $blogpost->getCandidateAuthors()->count());
|
2015-10-29 23:13:51 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
//Set the group to draw Members from
|
2017-01-26 09:28:42 +01:00
|
|
|
Config::inst()->update(BlogPost::class, 'restrict_authors_to_group', 'blogusers');
|
2015-10-29 23:13:51 +01:00
|
|
|
|
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
|
|
|
}
|
2016-02-05 03:28:09 +01:00
|
|
|
|
|
|
|
public function testCanViewFuturePost()
|
|
|
|
{
|
2017-05-04 08:10:20 +02:00
|
|
|
$blogPost = $this->objFromFixture('SiteTree', 'NullPublishDate');
|
2016-02-05 03:28:09 +01:00
|
|
|
|
2017-05-04 08:10:20 +02:00
|
|
|
$editor = $this->objFromFixture('Member', 'BlogEditor');
|
2016-02-05 03:28:09 +01:00
|
|
|
$this->assertTrue($blogPost->canView($editor));
|
|
|
|
|
2017-05-04 08:10:20 +02:00
|
|
|
$visitor = $this->objFromFixture('Member', 'Visitor');
|
2016-02-05 03:28:09 +01:00
|
|
|
$this->assertFalse($blogPost->canView($visitor));
|
|
|
|
}
|
2016-05-31 04:23:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
2017-05-04 08:10:20 +02:00
|
|
|
$blogPost = $this->objFromFixture('SiteTree', 'NullPublishDate');
|
2016-05-31 04:23:45 +02:00
|
|
|
$this->assertNull($blogPost->getDate());
|
|
|
|
|
2017-05-04 08:10:20 +02:00
|
|
|
$blogPost = $this->objFromFixture('SiteTree', 'PostA');
|
2016-05-31 04:23:45 +02:00
|
|
|
$this->assertEquals('2012-01-09 15:00:00', $blogPost->getDate());
|
|
|
|
}
|
2015-07-08 07:42:00 +02:00
|
|
|
}
|