2014-03-23 15:40:53 +01:00
|
|
|
<?php
|
|
|
|
|
2017-01-26 09:28:42 +01:00
|
|
|
namespace SilverStripe\Blog\Tests;
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Blog\Model\Blog;
|
|
|
|
use SilverStripe\Blog\Model\BlogCategory;
|
2017-01-26 09:28:42 +01:00
|
|
|
use SilverStripe\Blog\Model\BlogPost;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Blog\Model\BlogTag;
|
2017-01-26 09:28:42 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\ValidationException;
|
|
|
|
use SilverStripe\Security\Member;
|
2017-09-13 23:53:29 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-12-15 04:41:49 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
class BlogCategoryTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-01-26 09:28:42 +01:00
|
|
|
protected static $fixture_file = 'blog.yml';
|
2014-03-23 15:40:53 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2021-11-01 05:27:30 +01:00
|
|
|
protected function setUp(): void
|
2015-11-21 07:17:29 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
DBDatetime::set_mock_now('2013-10-10 20:00:00');
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
2014-03-23 15:40:53 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2021-11-01 05:27:30 +01:00
|
|
|
protected function tearDown(): void
|
2015-11-21 07:17:29 +01:00
|
|
|
{
|
2016-12-15 04:41:49 +01:00
|
|
|
DBDatetime::clear_mock_now();
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2014-03-23 15:40:53 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* Tests that any blog posts returned from $category->BlogPosts() many_many are published,
|
|
|
|
* both by normal 'save & publish' functionality and by publish date.
|
|
|
|
*/
|
|
|
|
public function testBlogPosts()
|
|
|
|
{
|
2017-09-13 23:53:29 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2014-03-23 15:40:53 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
if ($member) {
|
2017-09-14 01:28:43 +02:00
|
|
|
Security::setCurrentUser(null);
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$this->objFromFixture(BlogPost::class, 'FirstBlogPost');
|
2014-03-23 15:40:53 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @var BlogCategory $category
|
|
|
|
*/
|
2017-07-04 19:01:39 +02:00
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'FirstCategory');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-05-19 02:35:35 +02:00
|
|
|
$this->assertEquals(5, $category->BlogPosts()->count(), 'Category blog post count');
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2016-05-31 03:28:43 +02:00
|
|
|
/**
|
|
|
|
* @see https://github.com/silverstripe/silverstripe-blog/issues/376
|
|
|
|
*/
|
|
|
|
public function testAllowMultibyteUrlSegment()
|
|
|
|
{
|
2017-07-04 19:01:39 +02:00
|
|
|
$blog = $this->objFromFixture(Blog::class, 'FirstBlog');
|
2016-05-31 03:28:43 +02:00
|
|
|
$cat = new BlogCategory();
|
|
|
|
$cat->BlogID = $blog->ID;
|
|
|
|
$cat->Title = 'تست';
|
|
|
|
$cat->write();
|
|
|
|
// urlencoded
|
|
|
|
$this->assertEquals('%D8%AA%D8%B3%D8%AA', $cat->URLSegment);
|
|
|
|
$link = Controller::join_links($cat->Blog()->Link(), 'category', '%D8%AA%D8%B3%D8%AA');
|
|
|
|
$this->assertEquals($link, $cat->getLink());
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
public function testCanView()
|
|
|
|
{
|
2017-07-04 19:01:39 +02:00
|
|
|
$this->objFromFixture(Member::class, 'Admin');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor');
|
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'SecondCategory');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertFalse($category->canView($editor), 'Editor should not be able to view category.');
|
|
|
|
}
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* The first blog can be viewed by anybody.
|
|
|
|
*/
|
|
|
|
public function testCanEdit()
|
|
|
|
{
|
2017-07-04 19:01:39 +02:00
|
|
|
$admin = $this->objFromFixture(Member::class, 'Admin');
|
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'FirstCategory');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.');
|
|
|
|
$this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'SecondCategory');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.');
|
|
|
|
$this->assertFalse($category->canEdit($editor), 'Editor should not be able to edit category.');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'ThirdCategory');
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($category->canEdit($admin), 'Admin should always be able to edit category.');
|
|
|
|
$this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.');
|
|
|
|
}
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
public function testCanCreate()
|
|
|
|
{
|
2017-07-04 19:01:39 +02:00
|
|
|
$admin = $this->objFromFixture(Member::class, 'Admin');
|
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-01-26 09:28:42 +01:00
|
|
|
$category = singleton(BlogCategory::class);
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($category->canCreate($admin), 'Admin should be able to create category.');
|
|
|
|
$this->assertTrue($category->canCreate($editor), 'Editor should be able to create category.');
|
|
|
|
}
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
public function testCanDelete()
|
|
|
|
{
|
2017-07-04 19:01:39 +02:00
|
|
|
$admin = $this->objFromFixture(Member::class, 'Admin');
|
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'FirstCategory');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.');
|
|
|
|
$this->assertTrue($category->canDelete($editor), 'Editor should be able to category category.');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'SecondCategory');
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.');
|
|
|
|
$this->assertFalse($category->canDelete($editor), 'Editor should not be able to delete category.');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'ThirdCategory');
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($category->canDelete($admin), 'Admin should always be able to delete category.');
|
|
|
|
$this->assertTrue($category->canDelete($editor), 'Editor should be able to delete category.');
|
|
|
|
}
|
2016-02-05 01:33:40 +01:00
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
public function testDuplicateCategories()
|
|
|
|
{
|
2016-02-05 01:33:40 +01:00
|
|
|
$blog = new Blog();
|
|
|
|
$blog->Title = 'Testing for duplicate categories';
|
|
|
|
$blog->write();
|
|
|
|
|
|
|
|
$category = new BlogCategory();
|
|
|
|
$category->Title = 'Test';
|
|
|
|
$category->BlogID = $blog->ID;
|
2016-06-02 03:11:05 +02:00
|
|
|
$category->URLSegment = 'test';
|
2016-02-05 01:33:40 +01:00
|
|
|
$category->write();
|
|
|
|
|
|
|
|
$category = new BlogCategory();
|
|
|
|
$category->Title = 'Test';
|
2016-06-02 03:11:05 +02:00
|
|
|
$category->URLSegment = 'test';
|
2016-02-05 01:33:40 +01:00
|
|
|
$category->BlogID = $blog->ID;
|
|
|
|
try {
|
|
|
|
$category->write();
|
|
|
|
$this->fail('Duplicate BlogCategory written');
|
|
|
|
} catch (ValidationException $e) {
|
2016-12-15 04:41:49 +01:00
|
|
|
$messages = $e->getResult()->getMessages();
|
|
|
|
$this->assertCount(1, $messages);
|
|
|
|
$this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']);
|
2016-02-05 01:33:40 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-23 15:40:53 +01:00
|
|
|
}
|