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;
|
2017-11-03 03:39:57 +01:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-12-15 04:41:49 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
class BlogPostTest extends SapphireTest
|
|
|
|
{
|
2017-01-26 09:28:42 +01:00
|
|
|
protected static $fixture_file = 'blog.yml';
|
2015-07-08 07:42:00 +02:00
|
|
|
|
2018-04-06 00:22:58 +02:00
|
|
|
protected function tearDown()
|
2015-11-21 07:17:29 +01:00
|
|
|
{
|
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
|
|
|
|
*/
|
2017-11-02 23:42:23 +01:00
|
|
|
public function testCanView($date, $user, $page, $canView, $stage)
|
2015-11-21 07:17:29 +01:00
|
|
|
{
|
2017-07-04 19:01:39 +02:00
|
|
|
$userRecord = $this->objFromFixture(Member::class, $user);
|
|
|
|
$pageRecord = $this->objFromFixture(BlogPost::class, $page);
|
2016-12-15 04:41:49 +01:00
|
|
|
DBDatetime::set_mock_now($date);
|
2017-11-02 23:42:23 +01:00
|
|
|
if ($stage === 'Live') {
|
2017-11-03 03:39:57 +01:00
|
|
|
$pageRecord->publishSingle();
|
2017-11-02 23:42:23 +01:00
|
|
|
}
|
|
|
|
|
2017-11-03 03:39:57 +01:00
|
|
|
Versioned::set_stage($stage);
|
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
|
|
|
/**
|
2017-11-02 23:42:23 +01:00
|
|
|
* @return array Format:
|
|
|
|
* - mock now date
|
|
|
|
* - user role (see fixture)
|
|
|
|
* - blog post fixture ID
|
|
|
|
* - expected result
|
|
|
|
* - versioned stage
|
2016-12-15 04:41:49 +01:00
|
|
|
*/
|
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-11-03 03:39:57 +01:00
|
|
|
[$someFutureDate, 'Editor', 'PostA', true, 'Stage'],
|
|
|
|
[$someFutureDate, 'Contributor', 'PostA', true, 'Stage'],
|
|
|
|
[$someFutureDate, 'BlogEditor', 'PostA', true, 'Stage'],
|
|
|
|
[$someFutureDate, 'Writer', 'PostA', true, 'Stage'],
|
2016-01-21 17:46:10 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
// Check unpublished pages
|
2017-11-03 03:39:57 +01:00
|
|
|
[$somePastDate, 'Editor', 'PostA', true, 'Stage'],
|
|
|
|
[$somePastDate, 'Contributor', 'PostA', true, 'Stage'],
|
|
|
|
[$somePastDate, 'BlogEditor', 'PostA', true, 'Stage'],
|
|
|
|
[$somePastDate, 'Writer', 'PostA', true, 'Stage'],
|
|
|
|
|
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
|
2017-11-03 03:39:57 +01:00
|
|
|
[$someFutureDate, 'Editor', 'FirstBlogPost', true, 'Stage'],
|
|
|
|
[$someFutureDate, 'Contributor', 'FirstBlogPost', true, 'Stage'],
|
|
|
|
[$someFutureDate, 'BlogEditor', 'FirstBlogPost', true, 'Stage'],
|
|
|
|
[$someFutureDate, 'Writer', 'FirstBlogPost', true, 'Stage'],
|
2017-11-02 23:42:23 +01:00
|
|
|
|
|
|
|
// Check future pages in draft stage - users with "view draft pages" permission should
|
|
|
|
// be able to see this, but visitors should not
|
2017-11-03 03:39:57 +01:00
|
|
|
[$somePastDate, 'Editor', 'FirstBlogPost', true, 'Stage'],
|
|
|
|
[$somePastDate, 'Contributor', 'FirstBlogPost', true, 'Stage'],
|
|
|
|
[$somePastDate, 'BlogEditor', 'FirstBlogPost', true, 'Stage'],
|
|
|
|
[$somePastDate, 'Writer', 'FirstBlogPost', true, 'Stage'],
|
|
|
|
[$somePastDate, 'Visitor', 'FirstBlogPost', false, 'Stage'],
|
2017-11-02 23:42:23 +01:00
|
|
|
|
|
|
|
// No future pages in live stage should be visible, even to users that can edit them (in draft)
|
2017-11-03 03:39:57 +01:00
|
|
|
[$somePastDate, 'Editor', 'FirstBlogPost', false, 'Live'],
|
|
|
|
[$somePastDate, 'Contributor', 'FirstBlogPost', false, 'Live'],
|
|
|
|
[$somePastDate, 'BlogEditor', 'FirstBlogPost', false, 'Live'],
|
|
|
|
[$somePastDate, 'Writer', 'FirstBlogPost', false, 'Live'],
|
|
|
|
[$somePastDate, 'Visitor', 'FirstBlogPost', false, 'Live'],
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
2015-10-29 23:13:51 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
public function testCandidateAuthors()
|
|
|
|
{
|
2017-07-04 19:01:39 +02:00
|
|
|
$blogpost = $this->objFromFixture(BlogPost::class, '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-07-04 19:01:39 +02:00
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'NullPublishDate');
|
2016-02-05 03:28:09 +01:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$editor = $this->objFromFixture(Member::class, 'BlogEditor');
|
2016-02-05 03:28:09 +01:00
|
|
|
$this->assertTrue($blogPost->canView($editor));
|
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$visitor = $this->objFromFixture(Member::class, '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-07-04 19:01:39 +02:00
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'NullPublishDate');
|
2016-05-31 04:23:45 +02:00
|
|
|
$this->assertNull($blogPost->getDate());
|
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'PostA');
|
2016-05-31 04:23:45 +02:00
|
|
|
$this->assertEquals('2012-01-09 15:00:00', $blogPost->getDate());
|
|
|
|
}
|
2018-03-14 09:01:09 +01:00
|
|
|
|
|
|
|
public function testMinutesToRead()
|
|
|
|
{
|
|
|
|
/** @var BlogPost $blogPost */
|
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'FirstBlogPost');
|
|
|
|
|
|
|
|
// over 400 words, should take slightly longer than 2 minutes
|
|
|
|
$this->assertEquals(2, $blogPost->MinutesToRead());
|
|
|
|
|
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'SecondBlogPost');
|
|
|
|
|
|
|
|
// over 200 words, should take slighter longer than 1 minute
|
|
|
|
$this->assertEquals(1, $blogPost->MinutesToRead());
|
|
|
|
|
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'ThirdBlogPost');
|
|
|
|
// less than 200 words, should take less than a minute thus return an integer of 0 (zero)
|
|
|
|
$this->assertEquals(0, $blogPost->MinutesToRead());
|
|
|
|
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$blogPost->MinutesToRead('not-a-number');
|
|
|
|
}
|
2015-07-08 07:42:00 +02:00
|
|
|
}
|