silverstripe-contentreview/tests/php/SiteTreeContentReviewTest.php

355 lines
11 KiB
PHP
Raw Normal View History

2014-02-18 03:39:13 +01:00
<?php
namespace SilverStripe\ContentReview\Tests;
use Page;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ContentReview\Extensions\ContentReviewCMSExtension;
use SilverStripe\ContentReview\Extensions\ContentReviewDefaultSettings;
use SilverStripe\ContentReview\Extensions\ContentReviewOwner;
use SilverStripe\ContentReview\Extensions\SiteTreeContentReview;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Security\Group;
use SilverStripe\Security\Member;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Versioned\Versioned;
2015-11-02 00:27:42 +01:00
/**
* @mixin PHPUnit_Framework_TestCase
*/
class SiteTreeContentReviewTest extends ContentReviewBaseTest
{
/**
* @var string
*/
protected static $fixture_file = 'ContentReviewTest.yml';
2015-11-02 00:27:42 +01:00
/**
* @var array
*/
protected static $required_extensions = [
SiteTree::class => [SiteTreeContentReview::class],
Group::class => [ContentReviewOwner::class],
Member::class => [ContentReviewOwner::class],
CMSPageEditController::class => [ContentReviewCMSExtension::class],
SiteConfig::class => [ContentReviewDefaultSettings::class],
];
2015-11-02 00:27:42 +01:00
public function testOwnerNames()
{
/** @var Member $editor */
$editor = $this->objFromFixture(Member::class, "editor");
2015-11-02 00:27:42 +01:00
$this->logInAs($editor);
/** @var Page|SiteTreeContentReview $page */
$page = new Page();
$page->ReviewPeriodDays = 10;
$page->ContentReviewType = "Custom";
$page->ContentReviewUsers()->push($editor);
$page->write();
$this->assertTrue($page->canPublish());
$this->assertTrue($page->doPublish());
$this->assertEquals($page->OwnerNames, "Test Editor", "Test Editor should be the owner");
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "about");
2015-11-02 00:27:42 +01:00
$page->OwnerUsers()->removeAll();
$page->write();
$this->assertTrue($page->canPublish());
$this->assertTrue($page->publishRecursive());
2015-11-02 00:27:42 +01:00
$this->assertEquals("", $page->OwnerNames);
}
public function testPermissionsExists()
{
$perms = singleton(SiteTreeContentReview::class)->providePermissions();
2015-11-02 00:27:42 +01:00
$this->assertTrue(isset($perms["EDIT_CONTENT_REVIEW_FIELDS"]));
}
public function testUserWithPermissionCanEdit()
{
/** @var Member $editor */
$editor = $this->objFromFixture(Member::class, "editor");
2015-11-02 00:27:42 +01:00
$this->logInAs($editor);
/** @var Page|SiteTreeContentReview $page */
$page = new Page();
$fields = $page->getSettingsFields();
$this->assertNotNull($fields->dataFieldByName("NextReviewDate"));
}
public function testUserWithoutPermissionCannotEdit()
{
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "author");
2015-11-02 00:27:42 +01:00
$this->logInAs($author);
/** @var Page|SiteTreeContentReview $page */
$page = new Page();
$fields = $page->getSettingsFields();
$this->assertNull($fields->dataFieldByName("NextReviewDate"));
}
public function testAutomaticallyToNotSetReviewDate()
{
/** @var Member $editor */
$editor = $this->objFromFixture(Member::class, "editor");
2015-11-02 00:27:42 +01:00
$this->logInAs($editor);
/** @var Page|SiteTreeContentReview $page */
$page = new Page();
$page->ReviewPeriodDays = 10;
$page->write();
$this->assertTrue($page->doPublish());
$this->assertEquals(null, $page->NextReviewDate);
}
public function testAddReviewNote()
{
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "author");
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "home");
2015-11-02 00:27:42 +01:00
$page->addReviewNote($author, "This is a message");
/** @var Page|SiteTreeContentReview $page */
$homepage = $this->objFromFixture(Page::class, "home");
2015-11-02 00:27:42 +01:00
$this->assertEquals(1, $homepage->ReviewLogs()->count());
$this->assertEquals("This is a message", $homepage->ReviewLogs()->first()->Note);
}
public function testGetContentReviewOwners()
{
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "group-owned");
2015-11-02 00:27:42 +01:00
$owners = $page->ContentReviewOwners();
$this->assertEquals(1, $owners->count());
$this->assertEquals("author@example.com", $owners->first()->Email);
}
public function testCanNotBeReviewBecauseNoReviewDate()
{
DBDatetime::set_mock_now("2010-01-01 12:00:00");
2015-11-02 00:27:42 +01:00
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "author");
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "no-review");
2015-11-02 00:27:42 +01:00
$this->assertFalse($page->canBeReviewedBy($author));
DBDatetime::clear_mock_now();
2015-11-02 00:27:42 +01:00
}
public function testCanNotBeReviewedBecauseInFuture()
{
DBDatetime::set_mock_now("2010-01-01 12:00:00");
2015-11-02 00:27:42 +01:00
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "author");
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "staff");
2015-11-02 00:27:42 +01:00
$this->assertFalse($page->canBeReviewedBy($author));
DBDatetime::clear_mock_now();
2015-11-02 00:27:42 +01:00
}
public function testCanNotBeReviewedByUser()
{
DBDatetime::set_mock_now("2010-03-01 12:00:00");
2015-11-02 00:27:42 +01:00
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "author");
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "home");
2015-11-02 00:27:42 +01:00
$this->assertFalse($page->canBeReviewedBy($author));
DBDatetime::clear_mock_now();
2015-11-02 00:27:42 +01:00
}
public function testCanBeReviewedByUser()
{
DBDatetime::set_mock_now("2010-03-01 12:00:00");
2015-11-02 00:27:42 +01:00
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "author");
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "staff");
2015-11-02 00:27:42 +01:00
$this->assertTrue($page->canBeReviewedBy($author));
DBDatetime::clear_mock_now();
2015-11-02 00:27:42 +01:00
}
public function testCanNotBeReviewedByGroup()
{
DBDatetime::set_mock_now("2010-03-01 12:00:00");
2015-11-02 00:27:42 +01:00
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "editor");
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "contact");
2015-11-02 00:27:42 +01:00
$this->assertFalse($page->canBeReviewedBy($author));
DBDatetime::clear_mock_now();
2015-11-02 00:27:42 +01:00
}
public function testCanBeReviewedByGroup()
{
DBDatetime::set_mock_now("2010-03-01 12:00:00");
2015-11-02 00:27:42 +01:00
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "author");
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "contact");
2015-11-02 00:27:42 +01:00
$this->assertTrue($page->canBeReviewedBy($author));
DBDatetime::clear_mock_now();
2015-11-02 00:27:42 +01:00
}
public function testCanBeReviewedFromInheritedSetting()
{
DBDatetime::set_mock_now("2013-03-01 12:00:00");
2015-11-02 00:27:42 +01:00
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "author");
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $parentPage */
$parentPage = $this->objFromFixture(Page::class, "contact");
2015-11-02 00:27:42 +01:00
$parentPage->NextReviewDate = "2013-01-01";
$parentPage->write();
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "contact-child");
2015-11-02 00:27:42 +01:00
$this->assertTrue($page->canBeReviewedBy($author));
DBDatetime::clear_mock_now();
2015-11-02 00:27:42 +01:00
}
public function testUnModifiedPagesDontChangeEditor()
{
DBDatetime::set_mock_now("2013-03-01 12:00:00");
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "author");
$this->logInAs($author);
// Page which is un-modified doesn't advance version of have an editor assigned
$contactPage = $this->objFromFixture(Page::class, "contact");
$contactPageVersion = $contactPage->Version;
$contactPage->write();
$this->assertEmpty($contactPage->LastEditedByName);
$this->assertEquals(
$contactPageVersion,
Versioned::get_versionnumber_by_stage(SiteTree::class, 'Stage', $contactPage->ID, false)
);
// Page with modifications gets marked
$homePage = $this->objFromFixture(Page::class, "home");
$homePageVersion = $homePage->Version;
$homePage->Content = '<p>Welcome!</p>';
$homePage->write();
$this->assertNotEmpty($homePage->LastEditedByName);
$this->assertEquals($author->getTitle(), $homePage->LastEditedByName);
$this->assertGreaterThan(
$homePageVersion,
Versioned::get_versionnumber_by_stage(SiteTree::class, 'Stage', $homePage->ID, false)
);
DBDatetime::clear_mock_now();
}
2015-11-02 00:27:42 +01:00
public function testReviewActionVisibleForAuthor()
{
DBDatetime::set_mock_now('2020-03-01 12:00:00');
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, 'contact');
2015-11-02 00:27:42 +01:00
/** @var Member $author */
$author = $this->objFromFixture(Member::class, 'author');
2015-11-02 00:27:42 +01:00
$this->logInAs($author);
$fields = $page->getCMSActions();
$this->assertInstanceOf(LiteralField::class, $fields->fieldByName('ContentReviewButton'));
2015-11-02 00:27:42 +01:00
DBDatetime::clear_mock_now();
2015-11-02 00:27:42 +01:00
}
public function testReviewActionNotVisibleForEditor()
{
DBDatetime::set_mock_now("2020-03-01 12:00:00");
2015-11-02 00:27:42 +01:00
/** @var Page|SiteTreeContentReview $page */
$page = $this->objFromFixture(Page::class, "contact");
2015-11-02 00:27:42 +01:00
/** @var Member $author */
$author = $this->objFromFixture(Member::class, "editor");
2015-11-02 00:27:42 +01:00
$this->logInAs($author);
$fields = $page->getCMSActions();
2015-11-12 03:56:19 +01:00
$this->assertNull($fields->fieldByName("ActionMenus.ReviewContent"));
2015-11-02 00:27:42 +01:00
DBDatetime::clear_mock_now();
2015-11-02 00:27:42 +01:00
}
public function testSiteConfigSettingsAreUsedAsDefaults()
{
2017-10-12 02:22:24 +02:00
DBDatetime::set_mock_now("2020-03-01 12:00:00");
/** @var Member $author */
2017-10-12 02:22:24 +02:00
$author = $this->objFromFixture(Member::class, 'editor');
/** @var SiteConfig $siteConfig */
$siteConfig = SiteConfig::current_site_config();
// Set the author to a default user for reviewing
$siteConfig->OwnerUsers()->add($author);
$emptyPage = new Page;
$emptyPage->NextReviewDate = '2020-02-20 12:00:00';
$this->assertTrue($emptyPage->canBeReviewedBy($author));
2017-10-12 02:22:24 +02:00
DBDatetime::clear_mock_now();
}
2014-02-18 03:39:13 +01:00
}