View source
<?php
namespace Drupal\Tests\scheduler\Functional;
use Drupal\Core\Entity\EntityInterface;
class SchedulerRevisioningTest extends SchedulerBrowserTestBase {
protected function scheduleAndRunCron(EntityInterface $entity, string $action) {
$entity->{$action . '_on'} = strtotime('-5 hour', $this->requestTime);
$entity
->save();
scheduler_cron();
$storage = $this
->entityStorageObject($entity
->getEntityTypeId());
$storage
->resetCache([
$entity
->id(),
]);
return $storage
->load($entity
->id());
}
protected function assertRevisionCount(EntityInterface $entity, int $expected, string $message = '') {
if (!$entity
->getEntityType()
->isRevisionable()) {
return;
}
$storage = $this
->entityStorageObject($entity
->getEntityTypeId());
$count = $storage
->getLatestRevisionId($entity
->id());
$this
->assertEquals($expected, (int) $count, $message);
}
public function testNewRevision($entityTypeId, $bundle) {
$entityType = $this
->entityTypeObject($entityTypeId, $bundle);
$entity = $this
->createEntity($entityTypeId, $bundle, [
'revision' => 0,
]);
$this
->assertRevisionCount($entity, 1, 'The initial revision count is 1 when the entity is created.');
$entityType
->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
->save();
$entity = $this
->scheduleAndRunCron($entity, 'publish');
$this
->assertRevisionCount($entity, 1, 'No new revision is created by default when entity is published. Revision count remains at 1.');
$entity = $this
->scheduleAndRunCron($entity, 'unpublish');
$this
->assertRevisionCount($entity, 1, 'No new revision is created by default when entity is unpublished. Revision count remains at 1.');
$entityType
->setThirdPartySetting('scheduler', 'publish_revision', TRUE)
->setThirdPartySetting('scheduler', 'unpublish_revision', TRUE)
->save();
$entity = $this
->scheduleAndRunCron($entity, 'publish');
$this
->assertTrue($entity
->isPublished(), 'Entity is published after cron.');
if ($entity
->getEntityType()
->isRevisionable()) {
$this
->assertRevisionCount($entity, 2, 'A new revision was created when the entity was published with revisioning enabled.');
$expected_message = sprintf('Published by Scheduler. The scheduled publishing date was %s.', $this->dateFormatter
->format(strtotime('-5 hour', $this->requestTime), 'short'));
$this
->assertEquals($entity
->getRevisionLogMessage(), $expected_message, 'The correct message was found in the entity revision log after scheduled publishing.');
}
$entity = $this
->scheduleAndRunCron($entity, 'unpublish');
$this
->assertFalse($entity
->isPublished(), 'Entity is unpublished after cron.');
if ($entity
->getEntityType()
->isRevisionable()) {
$this
->assertRevisionCount($entity, 3, 'A new revision was created when the entity was unpublished with revisioning enabled.');
$expected_message = sprintf('Unpublished by Scheduler. The scheduled unpublishing date was %s.', $this->dateFormatter
->format(strtotime('-5 hour', $this->requestTime), 'short'));
$this
->assertEquals($entity
->getRevisionLogMessage(), $expected_message, 'The correct message was found in the entity revision log after scheduled unpublishing.');
}
}
public function testAlterCreationDate($entityTypeId, $bundle) {
$entityType = $this
->entityTypeObject($entityTypeId, $bundle);
$entityType
->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
->save();
$created = strtotime('-2 day', $this->requestTime);
$settings = [
'created' => $created,
'status' => FALSE,
];
$entity = $this
->createEntity($entityTypeId, $bundle, $settings);
$this
->assertFalse($entity
->isPublished(), 'The entity is not published.');
$entity = $this
->scheduleAndRunCron($entity, 'publish');
$created_after_cron = $entity->created->value;
$this
->assertTrue($entity
->isPublished(), 'The entity has been published.');
$this
->assertEquals($created, $created_after_cron, 'The entity creation date is not changed by default.');
$entityType
->setThirdPartySetting('scheduler', 'publish_touch', TRUE)
->save();
$entity = $this
->scheduleAndRunCron($entity, 'publish');
$created_after_cron = $entity->created->value;
$this
->assertEquals(strtotime('-5 hour', $this->requestTime), $created_after_cron, "With 'touch' option set, the entity creation date is changed to match the publishing date.");
}
}