Source of file EmbargoExpiryFluentExtensionTest.php
Size: 8,450 Bytes - Last Modified: 2021-12-23T10:27:04+00:00
/var/www/docs.ssmods.com/process/src/tests/php/Extension/EmbargoExpiryFluentExtensionTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 | <?php namespace Terraformers\EmbargoExpiry\Tests\Extension; use Exception; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\ValidationException; use Symbiote\QueuedJobs\Services\QueuedJobService; use Terraformers\EmbargoExpiry\Extension\EmbargoExpiryExtension; use Terraformers\EmbargoExpiry\Extension\EmbargoExpiryFluentExtension; use Terraformers\EmbargoExpiry\Tests\Fake\TestQueuedJobService; use TractorCow\Fluent\Extension\FluentSiteTreeExtension; use TractorCow\Fluent\Model\Locale; use TractorCow\Fluent\State\FluentState; /** * Class EmbargoExpiryFluentExtensionTest * * @package Terraformers\EmbargoExpiry\Tests\Extension */ class EmbargoExpiryFluentExtensionTest extends SapphireTest { const LOCALE_INT = 'en_NZ'; const LOCALE_JP = 'ja_JP'; /** * @var string */ protected static $fixture_file = 'EmbargoExpiryFluentExtensionTest.yml'; /** * @var array */ protected static $required_extensions = [ SiteTree::class => [ EmbargoExpiryExtension::class, EmbargoExpiryFluentExtension::class, FluentSiteTreeExtension::class, ], ]; /** * @var array */ protected static $extra_dataobjects = [ Locale::class, ]; /** * @throws Exception */ protected function setUp(): void { DBDatetime::set_mock_now('2014-01-05 12:00:00'); // This doesn't play nicely with PHPUnit Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false); parent::setUp(); } protected function tearDown(): void { DBDatetime::clear_mock_now(); parent::tearDown(); } /** * @return TestQueuedJobService */ protected function getService(): TestQueuedJobService { return singleton(TestQueuedJobService::class); } public function testCorrectConfigSet(): void { /** @var SiteTree|EmbargoExpiryFluentExtension $page */ $page = $this->objFromFixture(SiteTree::class, 'home'); $expected = [ 'DesiredPublishDate', 'DesiredUnPublishDate', 'PublishOnDate', 'UnPublishOnDate', 'PublishJobID', 'UnPublishJobID', ]; $actual = $page->config()->get('field_include'); // Set canonicalize to true so that the order of the items in each array are standardised. $this->assertEquals($expected, $actual, '', 0.0, 10, true); } public function testPublishScheduled(): void { // Fetch the Page ID for this object from the fixture, so that we can use the ID later and fetch more naturally. /** @var SiteTree $page */ $page = $this->objFromFixture(SiteTree::class, 'home'); $pageID = $page->ID; // Check that an Embargo date is correctly set on the Int localisation. FluentState::singleton()->withState(function (FluentState $state) use ($pageID): void { $state->setLocale(static::LOCALE_INT); /** @var SiteTree|EmbargoExpiryExtension $page */ $page = SiteTree::get()->byID($pageID); $this->assertNotNull($page); $this->assertTrue($page->getIsPublishScheduled(), 'Embargo was not set on Int localisation'); }); // Check that an Embargo date is correctly NOT set on the Int localisation. FluentState::singleton()->withState(function (FluentState $state) use ($pageID): void { $state->setLocale(static::LOCALE_JP); /** @var SiteTree|EmbargoExpiryExtension $page */ $page = SiteTree::get()->byID($pageID); $this->assertFalse($page->getIsPublishScheduled(), 'Embargo was not set on Int localisation'); }); } public function testUnPublishScheduled(): void { // Check that an Expiry date is correctly set on the Int localisation. FluentState::singleton()->withState(function (FluentState $state): void { $state->setLocale(static::LOCALE_INT); /** @var SiteTree|EmbargoExpiryExtension $page */ $page = $this->objFromFixture(SiteTree::class, 'home'); $this->assertTrue($page->getIsUnPublishScheduled(), 'Expiry was not set on Int localisation'); }); // Check that an Expiry date is correctly NOT set on the Int localisation. FluentState::singleton()->withState(function (FluentState $state): void { $state->setLocale(static::LOCALE_JP); /** @var SiteTree|EmbargoExpiryExtension $page */ $page = $this->objFromFixture(SiteTree::class, 'home'); $this->assertFalse($page->getIsUnPublishScheduled(), 'Expiry was incorrectly set on JP localisation'); }); } /** * @throws Exception */ public function testPublishJobProcesses(): void { // Fetch the Page ID for this object from the fixture, so that we can use the ID later and fetch more naturally. /** @var SiteTree $page */ $page = $this->objFromFixture(SiteTree::class, 'embargo1'); $pageID = $page->ID; FluentState::singleton()->withState(function (FluentState $state) use ($pageID): void { $state->setLocale(static::LOCALE_INT); $service = $this->getService(); /** @var SiteTree|EmbargoExpiryExtension|FluentSiteTreeExtension $page */ $page = SiteTree::get()->byID($pageID); // PublishDate is in the past, so it should be run immediately when we initialise it. $page->DesiredPublishDate = '2014-01-01 12:00:00'; $page->write(); // Make sure we're not published before the Job runs. $this->assertFalse((bool) $page->isPublished()); $this->assertNotEquals(0, $page->PublishJobID); $job = $service->testInit($page->PublishJob()); $id = $service->queueJob($job); $service->runJob($id); // We should now be published. $this->assertTrue($page->isPublishedInLocale()); }); // Check that the JP localisation is still in draft. FluentState::singleton()->withState(function (FluentState $state) use ($pageID): void { $state->setLocale(static::LOCALE_JP); /** @var SiteTree|EmbargoExpiryExtension|FluentSiteTreeExtension $page */ $page = SiteTree::get()->byID($pageID); $this->assertFalse($page->isPublishedInLocale()); }); } /** * @throws Exception */ public function testUnPublishJobProcesses(): void { // Fetch the Page ID for this object from the fixture, so that we can use the ID later and fetch more naturally. /** @var SiteTree $page */ $page = $this->objFromFixture(SiteTree::class, 'expiry1'); $pageID = $page->ID; FluentState::singleton()->withState(function (FluentState $state) use ($pageID): void { $state->setLocale(static::LOCALE_INT); $service = $this->getService(); /** @var SiteTree|EmbargoExpiryExtension|FluentSiteTreeExtension $page */ $page = SiteTree::get()->byID($pageID); // UnPublishDate is in the past, so it should be run immediately when we initialise it. $page->DesiredUnPublishDate = '2014-01-01 12:00:00'; $page->write(); $page->publishSingle(); // Make sure we're published before the Job runs. $this->assertTrue((bool) $page->isPublished()); $this->assertNotEquals(0, $page->UnPublishJobID); $job = $service->testInit($page->UnPublishJob()); $id = $service->queueJob($job); $service->runJob($id); // We should now be un-published. $this->assertFalse($page->isPublishedInLocale()); }); // Check that the JP localisation is still published. FluentState::singleton()->withState(function (FluentState $state) use ($pageID): void { $state->setLocale(static::LOCALE_JP); /** @var SiteTree|EmbargoExpiryExtension|FluentSiteTreeExtension $page */ $page = SiteTree::get()->byID($pageID); $this->assertTrue($page->isPublishedInLocale()); }); } } |