You are here

public function ScheduledTransitionTest::testScheduledRevision in Scheduled Transitions 8

Same name and namespace in other branches
  1. 2.x tests/src/Kernel/ScheduledTransitionTest.php \Drupal\Tests\scheduled_transitions\Kernel\ScheduledTransitionTest::testScheduledRevision()

Tests a scheduled revision.

Publish a revision in the past (not latest).

File

tests/src/Kernel/ScheduledTransitionTest.php, line 70

Class

ScheduledTransitionTest
Tests basic functionality of scheduled_transitions fields.

Namespace

Drupal\Tests\scheduled_transitions\Kernel

Code

public function testScheduledRevision() {
  $workflow = $this
    ->createEditorialWorkflow();
  $workflow
    ->getTypePlugin()
    ->addEntityTypeAndBundle('entity_test_revlog', 'entity_test_revlog');
  $workflow
    ->save();
  $author = User::create([
    'uid' => 2,
    'name' => $this
      ->randomMachineName(),
  ]);
  $author
    ->save();
  $entity = EntityTestWithRevisionLog::create([
    'type' => 'entity_test_revlog',
  ]);
  $entity->moderation_state = 'draft';
  $entity
    ->save();
  $entityId = $entity
    ->id();
  $this
    ->assertEquals(1, $entity
    ->getRevisionId());
  $entity
    ->setNewRevision();
  $entity->moderation_state = 'draft';
  $entity
    ->save();
  $this
    ->assertEquals(2, $entity
    ->getRevisionId());
  $entity
    ->setNewRevision();
  $entity->moderation_state = 'draft';
  $entity
    ->save();
  $this
    ->assertEquals(3, $entity
    ->getRevisionId());
  $newState = 'published';
  $scheduledTransition = ScheduledTransition::create([
    'entity' => $entity,
    'entity_revision_id' => 2,
    'author' => $author,
    'workflow' => $workflow
      ->id(),
    'moderation_state' => $newState,
    'transition_on' => (new \DateTime('2 Feb 2018 11am'))
      ->getTimestamp(),
  ]);
  $scheduledTransition
    ->save();
  $this
    ->runTransition($scheduledTransition);
  $logs = $this
    ->getLogs();
  $this
    ->assertCount(2, $logs);
  $this
    ->assertEquals('Copied revision #2 and changed from Draft to Published', $logs[0]['message']);
  $this
    ->assertEquals('Deleted scheduled transition #1', $logs[1]['message']);
  $revisionIds = $this
    ->getRevisionIds($entity);
  $this
    ->assertCount(4, $revisionIds);

  // Reload the entity.
  $entity = EntityTestWithRevisionLog::load($entityId);
  $this
    ->assertEquals('published', $entity->moderation_state->value, sprintf('Entity is now %s.', $newState));
  $this
    ->assertEquals('Scheduled transition: copied revision #2 and changed from Draft to Published', $entity
    ->getRevisionLogMessage());
}