2014-02-24 01:38:16 +01:00
|
|
|
<?php
|
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
namespace SilverStripe\ContentReview\Tests;
|
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
use function date;
|
2017-09-06 05:49:23 +02:00
|
|
|
use Page;
|
2018-01-08 22:11:31 +01:00
|
|
|
use PHPUnit_Framework_TestCase;
|
2017-09-06 05:49:23 +02:00
|
|
|
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\Dev\SapphireTest;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDate;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\Security\Group;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\SiteConfig\SiteConfig;
|
|
|
|
|
2014-02-24 01:38:16 +01:00
|
|
|
/**
|
2015-11-02 00:27:42 +01:00
|
|
|
* This class tests that settings are inherited correctly based on the inherited,
|
|
|
|
* custom or disabled settings.
|
|
|
|
*
|
|
|
|
* @mixin PHPUnit_Framework_TestCase
|
2014-02-24 01:38:16 +01:00
|
|
|
*/
|
2015-11-02 00:27:42 +01:00
|
|
|
class ContentReviewSettingsTest extends SapphireTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-09-06 05:49:23 +02:00
|
|
|
protected static $fixture_file = 'ContentReviewSettingsTest.yml';
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-06 05:49:23 +02:00
|
|
|
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 testAdvanceReviewDate10Days()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
|
|
|
$page = new Page();
|
|
|
|
|
|
|
|
$page->ContentReviewType = "Custom";
|
|
|
|
$page->ReviewPeriodDays = 10;
|
|
|
|
|
|
|
|
$this->assertTrue($page->advanceReviewDate());
|
|
|
|
|
|
|
|
$page->write();
|
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->assertEquals(date('Y-m-d', strtotime("now + 10 days")), $page->NextReviewDate);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdvanceReviewDateNull()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
|
|
|
$page = new Page();
|
|
|
|
|
|
|
|
$page->ContentReviewType = "Custom";
|
|
|
|
$page->ReviewPeriodDays = 0;
|
|
|
|
|
|
|
|
$this->assertFalse($page->advanceReviewDate());
|
|
|
|
|
|
|
|
$page->write();
|
|
|
|
|
|
|
|
$this->assertEquals(null, $page->NextReviewDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdvanceReviewFromCustomSettings()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "custom");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertTrue($page->advanceReviewDate());
|
|
|
|
|
|
|
|
$page->write();
|
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
date('Y-m-d', strtotime("now + " . $page->ReviewPeriodDays . " days")),
|
|
|
|
$page->NextReviewDate
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdvanceReviewFromInheritedSettings()
|
|
|
|
{
|
2015-11-18 04:38:28 +01:00
|
|
|
// When a parent page is advanced, the next review date of the child is not automatically advanced
|
2017-09-06 05:49:23 +02:00
|
|
|
$parentPage = $this->objFromFixture(Page::class, "page-1");
|
2015-11-18 04:38:28 +01:00
|
|
|
$this->assertTrue($parentPage->advanceReviewDate());
|
|
|
|
$parentPage->write();
|
2017-09-06 05:49:23 +02:00
|
|
|
|
|
|
|
$page = $this->objFromFixture(Page::class, "page-1-1");
|
|
|
|
$this->assertEquals(date('Y-m-d', strtotime("now + 5 days")), $parentPage->NextReviewDate);
|
2015-11-18 04:38:28 +01:00
|
|
|
$this->assertEquals('2011-04-12', $page->NextReviewDate);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2015-11-18 04:38:28 +01:00
|
|
|
// When a sub page is advanced, the next review date is advanced by the number of days in the parent
|
2015-11-02 00:27:42 +01:00
|
|
|
$this->assertTrue($page->advanceReviewDate());
|
|
|
|
$page->write();
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->assertEquals(date('Y-m-d', strtotime("now + 5 days")), $page->NextReviewDate);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdvanceReviewFromInheritedSiteConfigSettings()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "inherit");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/** @var SiteConfig|ContentReviewDefaultSettings $siteConfig */
|
2017-09-06 05:49:23 +02:00
|
|
|
$siteConfig = $this->objFromFixture(SiteConfig::class, "default");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertTrue($page->advanceReviewDate());
|
|
|
|
|
|
|
|
$page->write();
|
|
|
|
|
2018-01-08 04:47:31 +01:00
|
|
|
$this->assertEquals(
|
2018-01-08 22:11:31 +01:00
|
|
|
date(
|
|
|
|
'Y-m-d',
|
|
|
|
strtotime("now + " . $siteConfig->ReviewPeriodDays . " days")
|
|
|
|
),
|
2018-01-08 04:47:31 +01:00
|
|
|
$page->NextReviewDate
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSettingsObjectFromCustom()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "custom");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertEquals("Custom", $page->ContentReviewType);
|
|
|
|
$this->assertEquals($page, $page->getOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSettingsObjectFromDisabled()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "disabled");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertEquals("Disabled", $page->ContentReviewType);
|
|
|
|
$this->assertFalse($page->getOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetOptionObjectFromInheritedDisabled()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "page-2-1-1");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertEquals("Inherit", $page->ContentReviewType);
|
|
|
|
$this->assertFalse($page->getOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetOptionObjectFromDeeplyInheritedPage()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "page-3-1-1-1");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertEquals("Inherit", $page->ContentReviewType);
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->assertInstanceOf(SiteConfig::class, $page->getOptions());
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSettingsObjectFromInheritPage()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "page-1-1");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/** @var Page|SiteTreeContentReview $parentPage */
|
2017-09-06 05:49:23 +02:00
|
|
|
$parentPage = $this->objFromFixture(Page::class, "page-1");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertEquals("Inherit", $page->ContentReviewType);
|
|
|
|
$this->assertEquals(get_class($parentPage), get_class($page->getOptions()));
|
|
|
|
$this->assertEquals($parentPage->ID, $page->getOptions()->ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSettingsObjectFromInheritedRootPage()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "inherit");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertEquals("Inherit", $page->ContentReviewType);
|
2018-01-08 22:11:31 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$this->objFromFixture(
|
|
|
|
SiteConfig::class,
|
|
|
|
"default"
|
|
|
|
)->ID,
|
|
|
|
$page->getOptions()->ID
|
|
|
|
);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNextReviewDateFromCustomSettings()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, 'custom');
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$date = $page->getReviewDate();
|
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->assertEquals('2010-02-01', $date->format('Y-MM-dd'));
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNextReviewDateFromSiteConfigInheritedSetting()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "inherit");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$nextReviewDate = $page->getReviewDate();
|
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->assertInstanceOf(DBDate::class, $nextReviewDate);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/** @var SiteConfig|ContentReviewDefaultSettings $siteConfig */
|
2017-09-06 05:49:23 +02:00
|
|
|
$siteConfig = $this->objFromFixture(SiteConfig::class, "default");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
$expected = $this->addDaysToDate(DBDatetime::now(), $siteConfig->ReviewPeriodDays);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->assertEquals($expected, $nextReviewDate->format('Y-MM-dd'));
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNextReviewDateFromPageInheritedSetting()
|
|
|
|
{
|
2015-11-18 04:38:28 +01:00
|
|
|
// Although page-1-1 inherits from page-1, it has an independent review date
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "page-1-1");
|
2015-11-02 00:27:42 +01:00
|
|
|
$nextReviewDate = $page->getReviewDate();
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->assertInstanceOf(DBDate::class, $nextReviewDate);
|
|
|
|
$this->assertEquals('2011-04-12', $nextReviewDate->format('Y-MM-dd'));
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateNextReviewDateFromCustomToDisabled()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "custom");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
// before write()
|
|
|
|
$this->assertEquals("2010-02-01", $page->NextReviewDate);
|
|
|
|
|
|
|
|
$page->ContentReviewType = "Disabled";
|
|
|
|
$page->write();
|
|
|
|
|
|
|
|
DataObject::flush_and_destroy_cache();
|
|
|
|
unset($page);
|
|
|
|
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "custom");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertNull($page->NextReviewDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateNextReviewDateFromDisabledToCustom()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "disabled");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertNull($page->NextReviewDate);
|
|
|
|
|
|
|
|
$page->ContentReviewType = "Custom";
|
|
|
|
$page->ReviewPeriodDays = "7";
|
|
|
|
$page->write();
|
|
|
|
|
|
|
|
DataObject::flush_and_destroy_cache();
|
|
|
|
unset($page);
|
|
|
|
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$page = $this->objFromFixture(Page::class, "disabled");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
$expected = date('Y-m-d', strtotime("+ " . $page->ReviewPeriodDays . " days"));
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $page->NextReviewDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testParentChangedOptionsAndChildShouldToo()
|
|
|
|
{
|
|
|
|
/** @var Page|SiteTreeContentReview $parentPage */
|
2017-09-06 05:49:23 +02:00
|
|
|
$parentPage = $this->objFromFixture(Page::class, "page-1");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/** @var Page|SiteTreeContentReview $childPage */
|
2017-09-06 05:49:23 +02:00
|
|
|
$childPage = $this->objFromFixture(Page::class, "page-1-1");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2015-11-18 04:38:28 +01:00
|
|
|
// Parent and child pages have different review dates
|
2015-11-17 03:12:20 +01:00
|
|
|
$this->assertNotEquals($parentPage->NextReviewDate, $childPage->NextReviewDate);
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2015-11-17 03:12:20 +01:00
|
|
|
// But if we change the parent page ReviewPeriodDays to 10, the childs stays the same
|
2015-11-02 00:27:42 +01:00
|
|
|
$parentPage->ReviewPeriodDays = 10;
|
|
|
|
$parentPage->write();
|
|
|
|
|
|
|
|
// Flush all the caches!
|
|
|
|
DataObject::flush_and_destroy_cache();
|
|
|
|
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$parentPage = $this->objFromFixture(Page::class, "page-1");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/** @var Page|SiteTreeContentReview $page */
|
2017-09-06 05:49:23 +02:00
|
|
|
$childPage = $this->objFromFixture(Page::class, "page-1-1");
|
2015-11-02 00:27:42 +01:00
|
|
|
|
2015-11-17 03:12:20 +01:00
|
|
|
// The parent page's date advances, but not the child's
|
|
|
|
$this->assertEquals('2011-04-12', $childPage->NextReviewDate);
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->assertEquals($this->addDaysToDate(date('Y-m-d'), 10), $parentPage->NextReviewDate);
|
2015-11-17 03:12:20 +01:00
|
|
|
|
2015-11-18 04:38:28 +01:00
|
|
|
// Reviewing the child page should, however, advance its review by 10 days
|
|
|
|
$childPage->advanceReviewDate();
|
|
|
|
$childPage->write();
|
2017-09-06 05:49:23 +02:00
|
|
|
$this->assertEquals($this->addDaysToDate(date('Y-m-d'), 10), $childPage->NextReviewDate);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-06 05:49:23 +02:00
|
|
|
* Note: all input dates should be DBDatetime or strings in CLDR date format. See {@link DBDate} for information
|
|
|
|
*
|
|
|
|
* @param string|DBDatetime $date
|
|
|
|
* @param int $days
|
|
|
|
* @param string $format
|
2015-11-02 00:27:42 +01:00
|
|
|
*
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
2017-09-06 05:49:23 +02:00
|
|
|
private function addDaysToDate($date, $days, $format = 'Y-MM-dd')
|
2015-11-02 00:27:42 +01:00
|
|
|
{
|
|
|
|
if (is_object($date)) {
|
2017-09-06 05:49:23 +02:00
|
|
|
$sec = strtotime("+ " . $days . " days", $date->getTimestamp());
|
2015-11-02 00:27:42 +01:00
|
|
|
} else {
|
2017-09-06 05:49:23 +02:00
|
|
|
$sec = strtotime("+ " . $days . " days", DBDate::create()->setValue($date)->getTimestamp());
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
return DBDate::create()->setValue($sec)->format($format);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
}
|