You are here

public function ContentModerationStateTest::testBasicModeration in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationStateTest::testBasicModeration()

Tests basic monolingual content moderation through the API.

@dataProvider basicModerationTestCases

File

core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php, line 97

Class

ContentModerationStateTest
Tests links between a content entity and a content_moderation_state entity.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testBasicModeration($entity_type_id) {
  $entity = $this
    ->createEntity($entity_type_id, 'draft');
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertEquals('draft', $entity->moderation_state->value);
  $entity->moderation_state->value = 'published';
  $entity
    ->save();
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertEquals('published', $entity->moderation_state->value);

  // Change the state without saving the node.
  $content_moderation_state = ContentModerationState::load(1);
  $content_moderation_state
    ->set('moderation_state', 'draft');
  $content_moderation_state
    ->setNewRevision(TRUE);
  $content_moderation_state
    ->save();
  $entity = $this
    ->reloadEntity($entity, 3);
  $this
    ->assertEquals('draft', $entity->moderation_state->value);
  if ($entity instanceof EntityPublishedInterface) {
    $this
      ->assertFalse($entity
      ->isPublished());
  }

  // Check the default revision.
  $this
    ->assertDefaultRevision($entity, 2);
  $entity->moderation_state->value = 'published';
  $entity
    ->save();
  $entity = $this
    ->reloadEntity($entity, 4);
  $this
    ->assertEquals('published', $entity->moderation_state->value);

  // Check the default revision.
  $this
    ->assertDefaultRevision($entity, 4);

  // Update the node to archived which will then be the default revision.
  $entity->moderation_state->value = 'archived';
  $entity
    ->save();

  // Revert to the previous (published) revision.
  $entity_storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  $previous_revision = $entity_storage
    ->loadRevision(4);
  $previous_revision
    ->isDefaultRevision(TRUE);
  $previous_revision
    ->setNewRevision(TRUE);
  $previous_revision
    ->save();

  // Check the default revision.
  $this
    ->assertDefaultRevision($entity, 6);

  // Set an invalid moderation state.
  $this
    ->expectException(EntityStorageException::class);
  $entity->moderation_state->value = 'foobar';
  $entity
    ->save();
}