You are here

public function ScheduledTransitionTest::testScheduledRevisionLatestNonDefault 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::testScheduledRevisionLatestNonDefault()

Tests a scheduled revision.

Publish the latest revision.

File

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

Class

ScheduledTransitionTest
Tests basic functionality of scheduled_transitions fields.

Namespace

Drupal\Tests\scheduled_transitions\Kernel

Code

public function testScheduledRevisionLatestNonDefault() {
  $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' => 3,
    '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('Transitioning latest revision 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: transitioning latest revision from Draft to Published', $entity
    ->getRevisionLogMessage());
}