function EntityOperationsTest::testArchive

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/EntityOperationsTest.php \Drupal\Tests\content_moderation\Kernel\EntityOperationsTest::testArchive()
  2. 8.9.x core/modules/content_moderation/tests/src/Kernel/EntityOperationsTest.php \Drupal\Tests\content_moderation\Kernel\EntityOperationsTest::testArchive()
  3. 10 core/modules/content_moderation/tests/src/Kernel/EntityOperationsTest.php \Drupal\Tests\content_moderation\Kernel\EntityOperationsTest::testArchive()

Verifies that an unpublished state may be made the default revision.

File

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

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 testArchive() : void {
    $page = Node::create([
        'type' => 'page',
        'title' => $this->randomString(),
    ]);
    $page->moderation_state->value = 'published';
    $page->save();
    $id = $page->id();
    // The newly-created page should already be published.
    $page = Node::load($id);
    $this->assertTrue($page->isPublished());
    // When the page is moderated to the archived state, then the latest
    // revision should be the default revision, and it should be unpublished.
    $page->moderation_state->value = 'archived';
    $page->save();
    $new_revision_id = $page->getRevisionId();
    $storage = \Drupal::entityTypeManager()->getStorage('node');
    $new_revision = $storage->loadRevision($new_revision_id);
    $this->assertFalse($new_revision->isPublished());
    $this->assertTrue($new_revision->isDefaultRevision());
}

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