You are here

public function RevisionableContentEntityBaseTest::testWasDefaultRevision in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php \Drupal\KernelTests\Core\Entity\RevisionableContentEntityBaseTest::testWasDefaultRevision()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php \Drupal\KernelTests\Core\Entity\RevisionableContentEntityBaseTest::testWasDefaultRevision()

Tests the behavior of the "revision_default" flag.

@covers \Drupal\Core\Entity\ContentEntityBase::wasDefaultRevision

@expectedDeprecation The revision_user revision metadata key is not set for entity type: entity_test_mul_revlog See: https://www.drupal.org/node/2831499 @expectedDeprecation The revision_created revision metadata key is not set for entity type: entity_test_mul_revlog See: https://www.drupal.org/node/2831499 @expectedDeprecation The revision_log_message revision metadata key is not set for entity type: entity_test_mul_revlog See: https://www.drupal.org/node/2831499

File

core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php, line 108

Class

RevisionableContentEntityBaseTest
Test the revision system.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testWasDefaultRevision() {
  $entity_type_id = 'entity_test_mul_revlog';
  $entity = EntityTestMulWithRevisionLog::create([
    'type' => $entity_type_id,
  ]);

  // Checks that in a new entity ::wasDefaultRevision() always matches
  // ::isDefaultRevision().
  $this
    ->assertEquals($entity
    ->isDefaultRevision(), $entity
    ->wasDefaultRevision());
  $entity
    ->isDefaultRevision(FALSE);
  $this
    ->assertEquals($entity
    ->isDefaultRevision(), $entity
    ->wasDefaultRevision());

  // Check that a new entity is always flagged as a default revision on save,
  // regardless of its default revision status.
  $entity
    ->save();
  $this
    ->assertTrue($entity
    ->wasDefaultRevision());

  // Check that a pending revision is not flagged as default.
  $entity
    ->setNewRevision();
  $entity
    ->isDefaultRevision(FALSE);
  $entity
    ->save();
  $this
    ->assertFalse($entity
    ->wasDefaultRevision());

  // Check that a default revision is flagged as such.
  $entity
    ->setNewRevision();
  $entity
    ->isDefaultRevision(TRUE);
  $entity
    ->save();
  $this
    ->assertTrue($entity
    ->wasDefaultRevision());

  // Check that a manually set value for the "revision_default" flag is
  // ignored on save.
  $entity
    ->setNewRevision();
  $entity
    ->isDefaultRevision(FALSE);
  $entity
    ->set('revision_default', TRUE);
  $this
    ->assertTrue($entity
    ->wasDefaultRevision());
  $entity
    ->save();
  $this
    ->assertFalse($entity
    ->wasDefaultRevision());

  // Check that the default revision status was stored correctly.
  $storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  foreach ([
    TRUE,
    FALSE,
    TRUE,
    FALSE,
  ] as $index => $expected) {

    /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $revision */
    $revision = $storage
      ->loadRevision($index + 1);
    $this
      ->assertEquals($expected, $revision
      ->wasDefaultRevision());
  }

  // Check that the default revision is flagged correctly.

  /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $entity */
  $entity = $storage
    ->loadUnchanged($entity
    ->id());
  $this
    ->assertTrue($entity
    ->wasDefaultRevision());

  // Check that the "revision_default" flag cannot be changed once set.

  /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $entity2 */
  $entity2 = EntityTestMulWithRevisionLog::create([
    'type' => $entity_type_id,
  ]);
  $entity2
    ->save();
  $this
    ->assertTrue($entity2
    ->wasDefaultRevision());
  $entity2
    ->isDefaultRevision(FALSE);
  $entity2
    ->save();
  $this
    ->assertTrue($entity2
    ->wasDefaultRevision());
}