function ContentEntityNonRevisionableFieldTest::testMulNonRevisionableField

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php \Drupal\KernelTests\Core\Entity\ContentEntityNonRevisionableFieldTest::testMulNonRevisionableField()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php \Drupal\KernelTests\Core\Entity\ContentEntityNonRevisionableFieldTest::testMulNonRevisionableField()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php \Drupal\KernelTests\Core\Entity\ContentEntityNonRevisionableFieldTest::testMulNonRevisionableField()

Tests non-revisionable fields on revisionable and translatable entities.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testMulNonRevisionableField() : void {
    $user1 = $this->createUser();
    $user2 = $this->createUser();
    // Create a test entity.
    $entity = EntityTestMulRev::create([
        'name' => $this->randomString(),
        'user_id' => $user1->id(),
        'language' => 'en',
        'non_rev_field' => 'Huron',
    ]);
    $entity->save();
    // Create a test entity.
    $entity2 = EntityTestMulRev::create([
        'name' => $this->randomString(),
        'user_id' => $user1->id(),
        'language' => 'en',
        'non_rev_field' => 'Michigan',
    ]);
    $entity2->save();
    $this->assertEquals('Huron', $entity->get('non_rev_field')->value, 'Huron found on entity 1');
    $this->assertEquals('Michigan', $entity2->get('non_rev_field')->value, 'Michigan 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');
    $entity->addTranslation('de');
    $entity->save();
    $entity2->addTranslation('de');
    $entity2->save();
    $expected_revision_ids = [
        4 => 2,
        3 => 1,
        2 => 2,
        1 => 1,
    ];
    $revision_ids = $this->mulRev
        ->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->mulRev
        ->getQuery()
        ->accessCheck(FALSE)
        ->allRevisions()
        ->condition('non_rev_field', 'Huron')
        ->sort('revision_id', 'DESC')
        ->execute();
    $this->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found');
}

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