function EntityQueryTest::testCount

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

Tests that count queries are separated across entity types.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php, line 553

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testCount() : void {
    // Create a field with the same name in a different entity type.
    $field_name = $this->figures;
    $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'type' => 'shape',
        'cardinality' => 2,
        'translatable' => TRUE,
    ]);
    $field_storage->save();
    $bundle = $this->randomMachineName();
    entity_test_create_bundle($bundle);
    FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => $bundle,
    ])->save();
    $entity = EntityTest::create([
        'id' => 1,
        'type' => $bundle,
    ]);
    $entity->enforceIsNew();
    $entity->save();
    // As the single entity of this type we just saved does not have a value
    // in the color field, the result should be 0.
    $count = $this->container
        ->get('entity_type.manager')
        ->getStorage('entity_test')
        ->getQuery()
        ->accessCheck(FALSE)
        ->exists("{$field_name}.color")
        ->count()
        ->execute();
    $this->assertSame(0, $count);
}

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