You are here

public function EntityOperationsTest::testPendingRevisions in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/EntityOperationsTest.php \Drupal\Tests\content_moderation\Kernel\EntityOperationsTest::testPendingRevisions()

Verifies that the process of saving pending revisions works as expected.

File

core/modules/content_moderation/tests/src/Kernel/EntityOperationsTest.php, line 61

Class

EntityOperationsTest
@coversDefaultClass \Drupal\content_moderation\EntityOperations

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testPendingRevisions() {

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

  // Verify the entity saved correctly, and that the presence of pending
  // 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->value = '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 pending revision in Draft.
  $page
    ->setTitle('C');
  $page->moderation_state->value = '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 pending 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->value = '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->value = 'published';
  $page
    ->save();
  $page = Node::load($id);
  $this
    ->assertEquals('E', $page
    ->getTitle());
  $this
    ->assertTrue($page
    ->isDefaultRevision());
  $this
    ->assertTrue($page
    ->isPublished());
}