You are here

public function FieldDataCountTest::testCountWithIndex0 in Drupal 8

Same name and namespace in other branches
  1. 9 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()

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

File

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

Class

FieldDataCountTest
Tests counting field data records and the hasData() method on FieldStorageConfig entity.

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
    ->assertIdentical(TRUE, $this->storageUser
    ->countFieldData($storage, TRUE));

  // Test dedicated table storage.
  $storage = $user
    ->getFieldDefinition('field_int')
    ->getFieldStorageDefinition();
  $this
    ->assertIdentical(TRUE, $this->storageUser
    ->countFieldData($storage, TRUE));
}