You are here

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

Tests a scheduled revision.

File

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

Class

ScheduledTransitionTest
Tests basic functionality of scheduled_transitions fields.

Namespace

Drupal\Tests\scheduled_transitions\Kernel

Code

public function testScheduledRevisionRecreateNonDefaultHead() {
  $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->name = 'foobar1';
  $entity->moderation_state = 'draft';
  $entity
    ->save();
  $entityId = $entity
    ->id();
  $this
    ->assertEquals(1, $entity
    ->getRevisionId());
  $entity
    ->setNewRevision();
  $entity->name = 'foobar2';
  $entity->moderation_state = 'draft';
  $entity
    ->save();
  $this
    ->assertEquals(2, $entity
    ->getRevisionId());
  $revision3State = 'draft';
  $entity
    ->setNewRevision();
  $entity->name = 'foobar3';
  $entity->moderation_state = $revision3State;
  $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(),
    'options' => [
      [
        ScheduledTransition::OPTION_RECREATE_NON_DEFAULT_HEAD => TRUE,
      ],
    ],
  ]);
  $scheduledTransition
    ->save();
  $this
    ->runTransition($scheduledTransition);
  $logs = $this
    ->getLogs();
  $this
    ->assertCount(3, $logs);
  $this
    ->assertEquals('Copied revision #2 and changed from Draft to Published', $logs[0]['message']);
  $this
    ->assertEquals('Reverted Draft revision #3 back to top', $logs[1]['message']);
  $this
    ->assertEquals('Deleted scheduled transition #1', $logs[2]['message']);
  $revisionIds = $this
    ->getRevisionIds($entity);
  $this
    ->assertCount(5, $revisionIds);

  // Reload the entity default revision.
  $entityStorage = \Drupal::entityTypeManager()
    ->getStorage('entity_test_revlog');
  $entity = EntityTestWithRevisionLog::load($entityId);
  $revision4 = $entityStorage
    ->loadRevision($revisionIds[3]);
  $revision5 = $entityStorage
    ->loadRevision($revisionIds[4]);
  $this
    ->assertEquals($revision4
    ->getRevisionId(), $entity
    ->getRevisionId(), 'Default revision is revision 4');
  $this
    ->assertEquals($newState, $entity->moderation_state->value, sprintf('Entity is now %s.', $newState));
  $this
    ->assertEquals($revision4->name->value, 'foobar2');
  $this
    ->assertEquals('Scheduled transition: copied revision #2 and changed from Draft to Published', $revision4
    ->getRevisionLogMessage());
  $this
    ->assertEquals($revision5->name->value, 'foobar3');
  $this
    ->assertEquals('Scheduled transition: reverted Draft revision #3 back to top', $revision5
    ->getRevisionLogMessage());
}