function FieldDataCountTest::testCountWithIndex0

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/FieldDataCountTest.php \Drupal\Tests\field\Kernel\FieldDataCountTest::testCountWithIndex0()
  2. 10 core/modules/field/tests/src/Kernel/FieldDataCountTest.php \Drupal\Tests\field\Kernel\FieldDataCountTest::testCountWithIndex0()
  3. 11.x core/modules/field/tests/src/Kernel/FieldDataCountTest.php \Drupal\Tests\field\Kernel\FieldDataCountTest::testCountWithIndex0()

Verify that we can count a table that contains an entry with index 0.

File

core/modules/field/tests/src/Kernel/FieldDataCountTest.php, line 147

Class

FieldDataCountTest
Tests the count of field data records.

Namespace

Drupal\Tests\field\Kernel

Code

public function testCountWithIndex0() {
    // Create a field that will require dedicated storage.
    
    /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_int',
        'entity_type' => 'user',
        'type' => 'integer',
        'cardinality' => 2,
    ]);
    $field_storage->save();
    FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'user',
    ])->save();
    // Create an entry for the anonymous user, who has user ID 0.
    $user = $this->storageUser
        ->create([
        'uid' => 0,
        'name' => 'anonymous',
        'mail' => NULL,
        'status' => FALSE,
        'field_int' => 42,
    ]);
    $user->save();
    // Test shared table storage.
    $storage = $user->getFieldDefinition('name')
        ->getFieldStorageDefinition();
    $this->assertTrue($this->storageUser
        ->countFieldData($storage, TRUE));
    // Test dedicated table storage.
    $storage = $user->getFieldDefinition('field_int')
        ->getFieldStorageDefinition();
    $this->assertTrue($this->storageUser
        ->countFieldData($storage, TRUE));
}

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