You are here

public function ContentModerationStateStorageSchemaTest::testUniqueKeys in Drupal 9

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

Tests the ContentModerationState unique keys.

@covers ::getEntitySchema

File

core/modules/content_moderation/tests/src/Kernel/ContentModerationStateStorageSchemaTest.php, line 60

Class

ContentModerationStateStorageSchemaTest
Test the ContentModerationState storage schema.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testUniqueKeys() {

  // Create a node which will create a new ContentModerationState entity.
  $node = Node::create([
    'title' => 'Test title',
    'type' => 'example',
    'moderation_state' => 'draft',
  ]);
  $node
    ->save();

  // Ensure an exception when all values match.
  $this
    ->assertStorageException([
    'content_entity_type_id' => $node
      ->getEntityTypeId(),
    'content_entity_id' => $node
      ->id(),
    'content_entity_revision_id' => $node
      ->getRevisionId(),
  ], TRUE);

  // No exception for the same values, with a different langcode.
  $this
    ->assertStorageException([
    'content_entity_type_id' => $node
      ->getEntityTypeId(),
    'content_entity_id' => $node
      ->id(),
    'content_entity_revision_id' => $node
      ->getRevisionId(),
    'langcode' => 'de',
  ], FALSE);

  // A different workflow should not trigger an exception.
  $this
    ->assertStorageException([
    'content_entity_type_id' => $node
      ->getEntityTypeId(),
    'content_entity_id' => $node
      ->id(),
    'content_entity_revision_id' => $node
      ->getRevisionId(),
    'workflow' => 'foo',
  ], FALSE);

  // Different entity types should not trigger an exception.
  $this
    ->assertStorageException([
    'content_entity_type_id' => 'entity_test',
    'content_entity_id' => $node
      ->id(),
    'content_entity_revision_id' => $node
      ->getRevisionId(),
  ], FALSE);

  // Different entity and revision IDs should not trigger an exception.
  $this
    ->assertStorageException([
    'content_entity_type_id' => $node
      ->getEntityTypeId(),
    'content_entity_id' => 9999,
    'content_entity_revision_id' => 9999,
  ], FALSE);

  // Creating a version of the entity with a previously used, but not current
  // revision ID should trigger an exception.
  $old_revision_id = $node
    ->getRevisionId();
  $node
    ->setNewRevision(TRUE);
  $node->title = 'Updated title';
  $node->moderation_state = 'published';
  $node
    ->save();
  $this
    ->assertStorageException([
    'content_entity_type_id' => $node
      ->getEntityTypeId(),
    'content_entity_id' => $node
      ->id(),
    'content_entity_revision_id' => $old_revision_id,
  ], TRUE);
}