You are here

public function ScheduledTransitionTest::testScheduledRevisionRecreateDefaultHead in Scheduled Transitions 2.x

Same name and namespace in other branches
  1. 8 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 264

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::createFrom($workflow, $newState, $entity, new \DateTime('2 Feb 2018 11am'), $author)
    ->setEntityRevisionId(2)
    ->setOptions([
    [
      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());
}