function EntityOperationsTest::testPendingRevisions

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()
  2. 8.9.x core/modules/content_moderation/tests/src/Kernel/EntityOperationsTest.php \Drupal\Tests\content_moderation\Kernel\EntityOperationsTest::testPendingRevisions()
  3. 10 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 63

Class

EntityOperationsTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21content_moderation%21src%21EntityOperations.php/class/EntityOperations/11.x" title="Defines a class for reacting to entity events." class="local">\Drupal\content_moderation\EntityOperations</a>

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testPendingRevisions() : void {
    // 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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.