You are here

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

Tests a scheduled revision.

The latest revision is published, ensure it doesnt get republished when recreate_non_default_head is TRUE.

File

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

Class

ScheduledTransitionTest
Tests basic functionality of scheduled_transitions fields.

Namespace

Drupal\Tests\scheduled_transitions\Kernel

Code

public function testScheduledRevisionRecreateDefaultHead() {
  $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 = 'published';
  $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(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 default revision.
  $entityStorage = \Drupal::entityTypeManager()
    ->getStorage('entity_test_revlog');
  $entity = EntityTestWithRevisionLog::load($entityId);
  $revision4 = $entityStorage
    ->loadRevision($revisionIds[3]);
  $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());
}