You are here

public function ContentEntityNonRevisionableFieldTest::testNonRevisionableField in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php \Drupal\KernelTests\Core\Entity\ContentEntityNonRevisionableFieldTest::testNonRevisionableField()

Tests non-revisionable fields on revisionable entities.

File

core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php, line 126

Class

ContentEntityNonRevisionableFieldTest
Tests non-revisionable fields on revisionable (and translatable) entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testNonRevisionableField() {
  $user1 = $this
    ->createUser();
  $user2 = $this
    ->createUser();

  // Create a test entity.
  $entity = EntityTestRev::create([
    'name' => $this
      ->randomString(),
    'user_id' => $user1
      ->id(),
    'non_rev_field' => 'Superior',
  ]);
  $entity
    ->save();

  // Create a test entity.
  $entity2 = EntityTestRev::create([
    'name' => $this
      ->randomString(),
    'user_id' => $user1
      ->id(),
    'non_rev_field' => 'Ontario',
  ]);
  $entity2
    ->save();
  $this
    ->assertEquals('Superior', $entity
    ->get('non_rev_field')->value, 'Superior found on entity 1');
  $this
    ->assertEquals('Ontario', $entity2
    ->get('non_rev_field')->value, 'Ontario found on entity 2');
  $entity
    ->setNewRevision();
  $entity
    ->setOwner($user2);
  $entity
    ->save();
  $entity2
    ->setNewRevision();
  $entity2
    ->setOwner($user2);
  $entity2
    ->save();
  $this
    ->assertEquals($user2
    ->id(), $entity
    ->getOwner()
    ->id(), 'User 2 found on entity 1');
  $this
    ->assertEquals($user2
    ->id(), $entity2
    ->getOwner()
    ->id(), 'User 2 found on entity 2');
  $expected_revision_ids = [
    4 => 2,
    3 => 1,
    2 => 2,
    1 => 1,
  ];
  $revision_ids = $this->rev
    ->getQuery()
    ->accessCheck(FALSE)
    ->allRevisions()
    ->sort('revision_id', 'DESC')
    ->execute();
  $this
    ->assertEquals($expected_revision_ids, $revision_ids, 'Revision ids found');
  $expected_non_rev_field_revision_ids = [
    3 => 1,
    1 => 1,
  ];
  $non_rev_field_revision_ids = $this->rev
    ->getQuery()
    ->accessCheck(FALSE)
    ->allRevisions()
    ->condition('non_rev_field', 'Superior')
    ->sort('revision_id', 'DESC')
    ->execute();
  $this
    ->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found');
}