You are here

public function EntityOperationsTest::testForwardRevisions in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/EntityOperationsTest.php \Drupal\Tests\workbench_moderation\Kernel\EntityOperationsTest::testForwardRevisions()

Verifies that the process of saving forward-revisions works as expected.

File

tests/src/Kernel/EntityOperationsTest.php, line 58

Class

EntityOperationsTest
Class EntityOperationsTest.

Namespace

Drupal\Tests\workbench_moderation\Kernel

Code

public function testForwardRevisions() {

  // Create a new node in draft.
  $page = Node::create([
    'type' => 'page',
    'title' => 'A',
  ]);
  $page->moderation_state->target_id = 'draft';
  $page
    ->save();
  $id = $page
    ->id();

  // Verify the entity saved correctly, and that the presence of forward
  // revisions doesn't affect the default node load.

  /** @var \Drupal\node\Entity\Node $page */
  $page = Node::load($id);
  $this
    ->assertEquals('A', $page
    ->getTitle());
  $this
    ->assertTrue($page
    ->isDefaultRevision());
  $this
    ->assertFalse($page
    ->isPublished());

  // Moderate the entity to published.
  $page
    ->setTitle('B');
  $page->moderation_state->target_id = 'published';
  $page
    ->save();

  // Verify the entity is now published and public.
  $page = Node::load($id);
  $this
    ->assertEquals('B', $page
    ->getTitle());
  $this
    ->assertTrue($page
    ->isDefaultRevision());
  $this
    ->assertTrue($page
    ->isPublished());

  // Make a new forward-revision in Draft.
  $page
    ->setTitle('C');
  $page->moderation_state->target_id = 'draft';
  $page
    ->save();

  // Verify normal loads return the still-default previous version.
  $page = Node::load($id);
  $this
    ->assertEquals('B', $page
    ->getTitle());

  // Verify we can load the forward revision, even if the mechanism is kind
  // of gross. Note: revisionIds() is only available on NodeStorageInterface,
  // so this won't work for non-nodes. We'd need to use entity queries. This
  // is a core bug that should get fixed.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('node');
  $revision_ids = $storage
    ->revisionIds($page);
  sort($revision_ids);
  $latest = end($revision_ids);
  $page = $storage
    ->loadRevision($latest);
  $this
    ->assertEquals('C', $page
    ->getTitle());
  $page
    ->setTitle('D');
  $page->moderation_state->target_id = 'published';
  $page
    ->save();

  // Verify normal loads return the still-default previous version.
  $page = Node::load($id);
  $this
    ->assertEquals('D', $page
    ->getTitle());
  $this
    ->assertTrue($page
    ->isDefaultRevision());
  $this
    ->assertTrue($page
    ->isPublished());

  // Now check that we can immediately add a new published revision over it.
  $page
    ->setTitle('E');
  $page->moderation_state->target_id = 'published';
  $page
    ->save();
  $page = Node::load($id);
  $this
    ->assertEquals('E', $page
    ->getTitle());
  $this
    ->assertTrue($page
    ->isDefaultRevision());
  $this
    ->assertTrue($page
    ->isPublished());
}