2013-10-10 00:09:28 +02: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;
|
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-14 01:28:43 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-12-15 04:41:49 +01:00
|
|
|
|
2015-05-09 16:33:12 +02:00
|
|
|
/**
|
|
|
|
* @mixin PHPUnit_Framework_TestCase
|
|
|
|
*/
|
2015-11-21 07:17:29 +01:00
|
|
|
class BlogTagTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
/**
|
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';
|
2013-10-10 00:09:28 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2013-10-10 00:09:28 +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-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2015-02-08 08:03:55 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* Tests that any blog posts returned from $tag->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();
|
2013-10-10 00:09:28 +02: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
|
|
|
}
|
2013-10-10 00:09:28 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$this->objFromFixture(BlogPost::class, 'FirstBlogPost');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @var BlogTag $tag
|
|
|
|
*/
|
2017-07-04 19:01:39 +02:00
|
|
|
$tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertEquals(1, $tag->BlogPosts()->count(), 'Tag blog post count');
|
|
|
|
}
|
2015-05-09 16:33:12 +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
|
|
|
$tag = new BlogTag();
|
|
|
|
$tag->BlogID = $blog->ID;
|
|
|
|
$tag->Title = 'تست';
|
|
|
|
$tag->write();
|
|
|
|
// urlencoded
|
|
|
|
$this->assertEquals('%D8%AA%D8%B3%D8%AA', $tag->URLSegment);
|
|
|
|
$link = Controller::join_links($tag->Blog()->Link(), 'tag', '%D8%AA%D8%B3%D8%AA');
|
|
|
|
$this->assertEquals($link, $tag->getLink());
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* The first blog can be viewed by anybody.
|
|
|
|
*/
|
|
|
|
public function testCanView()
|
|
|
|
{
|
|
|
|
$this->useDraftSite();
|
2014-07-27 10:40:08 +02:00
|
|
|
|
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
|
|
|
$tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.');
|
|
|
|
$this->assertTrue($tag->canView($editor), 'Editor should be able to view tag.');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$tag = $this->objFromFixture(BlogTag::class, 'SecondTag');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.');
|
|
|
|
$this->assertFalse($tag->canView($editor), 'Editor should not be able to view tag.');
|
|
|
|
}
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
public function testCanEdit()
|
|
|
|
{
|
|
|
|
$this->useDraftSite();
|
2014-07-27 10:40:08 +02:00
|
|
|
|
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
|
|
|
$tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.');
|
|
|
|
$this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$tag = $this->objFromFixture(BlogTag::class, 'SecondTag');
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.');
|
|
|
|
$this->assertFalse($tag->canEdit($editor), 'Editor should not be able to edit tag.');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$tag = $this->objFromFixture(BlogTag::class, 'ThirdTag');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($tag->canEdit($admin), 'Admin should always be able to edit tags.');
|
|
|
|
$this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.');
|
|
|
|
}
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
public function testCanCreate()
|
|
|
|
{
|
|
|
|
$this->useDraftSite();
|
2014-07-27 10:40:08 +02:00
|
|
|
|
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
|
|
|
$tag = singleton(BlogTag::class);
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($tag->canCreate($admin), 'Admin should be able to create tag.');
|
|
|
|
$this->assertTrue($tag->canCreate($editor), 'Editor should be able to create tag.');
|
|
|
|
}
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
public function testCanDelete()
|
|
|
|
{
|
|
|
|
$this->useDraftSite();
|
2014-07-27 10:40:08 +02:00
|
|
|
|
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
|
|
|
$tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.');
|
|
|
|
$this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$tag = $this->objFromFixture(BlogTag::class, 'SecondTag');
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.');
|
|
|
|
$this->assertFalse($tag->canDelete($editor), 'Editor should not be able to delete tag.');
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2017-07-04 19:01:39 +02:00
|
|
|
$tag = $this->objFromFixture(BlogTag::class, 'ThirdTag');
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->assertTrue($tag->canDelete($admin), 'Admin should always be able to delete tags.');
|
|
|
|
$this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.');
|
|
|
|
}
|
2016-01-21 17:46:10 +01:00
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
public function testDuplicateTagsForURLSegment()
|
|
|
|
{
|
2016-01-21 17:46:10 +01:00
|
|
|
$blog = new Blog();
|
|
|
|
$blog->Title = 'Testing for duplicates blog';
|
|
|
|
$blog->write();
|
|
|
|
$tag1 = new BlogTag();
|
2016-02-05 01:33:40 +01:00
|
|
|
$tag1->Title = 'cat-test';
|
2016-01-21 17:46:10 +01:00
|
|
|
$tag1->BlogID = $blog->ID;
|
|
|
|
$tag1->write();
|
2016-02-05 01:33:40 +01:00
|
|
|
$this->assertEquals('cat-test', $tag1->URLSegment);
|
2016-01-21 17:46:10 +01:00
|
|
|
|
|
|
|
$tag2 = new BlogTag();
|
2016-02-05 01:33:40 +01:00
|
|
|
$tag2->Title = 'cat test';
|
2016-01-21 17:46:10 +01:00
|
|
|
$tag2->BlogID = $blog->ID;
|
|
|
|
$tag2->write();
|
2016-06-02 03:11:05 +02:00
|
|
|
$this->assertEquals('cat-test-1', $tag2->URLSegment);
|
2016-01-21 17:46:10 +01:00
|
|
|
}
|
2016-02-05 01:33:40 +01:00
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
public function testDuplicateTags()
|
|
|
|
{
|
2016-02-05 01:33:40 +01:00
|
|
|
$blog = new Blog();
|
|
|
|
$blog->Title = 'Testing for duplicate tags';
|
|
|
|
$blog->write();
|
|
|
|
|
|
|
|
$tag = new BlogTag();
|
|
|
|
$tag->Title = 'Test';
|
|
|
|
$tag->BlogID = $blog->ID;
|
2016-06-02 03:11:05 +02:00
|
|
|
$tag->URLSegment = 'test';
|
2016-02-05 01:33:40 +01:00
|
|
|
$tag->write();
|
|
|
|
|
|
|
|
$tag = new BlogTag();
|
|
|
|
$tag->Title = 'Test';
|
2016-06-02 03:11:05 +02:00
|
|
|
$tag->URLSegment = 'test';
|
2016-02-05 01:33:40 +01:00
|
|
|
$tag->BlogID = $blog->ID;
|
|
|
|
try {
|
|
|
|
$tag->write();
|
|
|
|
$this->fail('Duplicate BlogTag 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
|
|
|
}
|
|
|
|
}
|
2017-05-11 02:29:44 +02:00
|
|
|
|
|
|
|
public function testBlogTagUrlSegmentsAreAutomaticallyUpdated()
|
|
|
|
{
|
|
|
|
$tag = new BlogTag;
|
|
|
|
$tag->Title = "a test";
|
|
|
|
$tag->write();
|
|
|
|
$this->assertEquals($tag->URLSegment, "a-test");
|
|
|
|
|
|
|
|
$tag->Title = "another test";
|
|
|
|
$tag->write();
|
|
|
|
$this->assertEquals($tag->URLSegment, "another-test");
|
|
|
|
}
|
2013-10-10 00:09:28 +02:00
|
|
|
}
|