View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\scheduled_transitions\Kernel;
use Drupal\entity_test_revlog\Entity\EntityTestWithRevisionLog;
use Drupal\KernelTests\KernelTestBase;
use Drupal\scheduled_transitions\Entity\ScheduledTransition;
use Drupal\scheduled_transitions\Entity\ScheduledTransitionInterface;
use Drupal\scheduled_transitions\ScheduledTransitionsTokenReplacements;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
class ScheduledTransitionsTokenTest extends KernelTestBase {
use ContentModerationTestTrait;
protected static $modules = [
'entity_test_revlog',
'scheduled_transitions',
'content_moderation',
'workflows',
'dynamic_entity_reference',
'user',
'language',
'system',
];
protected $testEntity;
protected $testWorkflow;
protected $scheduledTransition;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test_revlog');
$this
->installEntitySchema('content_moderation_state');
$this
->installEntitySchema('user');
$this
->installEntitySchema('scheduled_transition');
$this->testWorkflow = $this
->createEditorialWorkflow();
$this->testWorkflow
->getTypePlugin()
->addEntityTypeAndBundle('entity_test_revlog', 'entity_test_revlog');
$this->testWorkflow
->save();
$this->testEntity = EntityTestWithRevisionLog::create([
'type' => 'entity_test_revlog',
'moderation_state' => 'archived',
]);
$this->testEntity
->save();
$this->testEntity->moderation_state = 'draft';
$this->testEntity
->setNewRevision();
$this->testEntity
->save();
$this->scheduledTransition = ScheduledTransition::create([
'entity' => $this->testEntity,
'entity_revision_id' => 1,
'author' => 1,
'workflow' => $this->testWorkflow
->id(),
'moderation_state' => 'published',
'transition_on' => (new \DateTime('-1 day'))
->getTimestamp(),
]);
$this->scheduledTransition
->save();
}
public function testTokenReplacement() : void {
$latest = \Drupal::entityTypeManager()
->getStorage('entity_test_revlog')
->loadRevision(1);
$newRevision = \Drupal::entityTypeManager()
->getStorage('entity_test_revlog')
->loadRevision(2);
$replacement = new ScheduledTransitionsTokenReplacements($this->scheduledTransition, $newRevision, $latest);
$this
->assertEquals([
'from-revision-id' => '2',
'from-state' => 'Draft',
'to-state' => 'Published',
'latest-revision-id' => '1',
'latest-state' => 'Archived',
], $replacement
->getReplacements());
}
public function testReplacementWithRunner(string $token, string $expectedReplacement) : void {
\Drupal::configFactory()
->getEditable('scheduled_transitions.settings')
->set('message_transition_historical', $token)
->save(TRUE);
$this
->runTransition($this->scheduledTransition);
$entity = $this->testEntity::load($this->testEntity
->id());
$this
->assertEquals($expectedReplacement, $entity
->getRevisionLogMessage());
}
public function providerTokenReplacement() {
return [
'scheduled-transitions:to-state' => [
'[scheduled-transitions:to-state]',
'Published',
],
'scheduled-transitions:from-state' => [
'[scheduled-transitions:from-state]',
'Archived',
],
'scheduled-transitions:from-revision-id' => [
'[scheduled-transitions:from-revision-id]',
'1',
],
'scheduled-transitions:latest-state' => [
'[scheduled-transitions:latest-state]',
'Draft',
],
'scheduled-transitions:latest-revision-id' => [
'[scheduled-transitions:latest-revision-id]',
'2',
],
];
}
protected function runTransition(ScheduledTransitionInterface $scheduledTransition) : void {
$runner = \Drupal::service('scheduled_transitions.runner');
$runner
->runTransition($scheduledTransition);
}
}