function FieldSqlStorageTest::testFieldLoad

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldLoad()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldLoad()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldLoad()

Tests field loading works correctly by inserting directly in the tables.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php, line 105

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldLoad() {
    $entity_type = $bundle = 'entity_test_rev';
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type);
    $columns = [
        'bundle',
        'deleted',
        'entity_id',
        'revision_id',
        'delta',
        'langcode',
        $this->tableMapping
            ->getFieldColumnName($this->fieldStorage, 'value'),
    ];
    // Create an entity with four revisions.
    $revision_ids = [];
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create();
    $entity->save();
    $revision_ids[] = $entity->getRevisionId();
    for ($i = 0; $i < 4; $i++) {
        $entity->setNewRevision();
        $entity->save();
        $revision_ids[] = $entity->getRevisionId();
    }
    // Generate values and insert them directly in the storage tables.
    $values = [];
    $connection = Database::getConnection();
    $query = $connection->insert($this->revisionTable)
        ->fields($columns);
    foreach ($revision_ids as $revision_id) {
        // Put one value too many.
        for ($delta = 0; $delta <= $this->fieldCardinality; $delta++) {
            $value = mt_rand(1, 127);
            $values[$revision_id][] = $value;
            $query->values([
                $bundle,
                0,
                $entity->id(),
                $revision_id,
                $delta,
                $entity->language()
                    ->getId(),
                $value,
            ]);
        }
        $query->execute();
    }
    $query = $connection->insert($this->table)
        ->fields($columns);
    foreach ($values[$revision_id] as $delta => $value) {
        $query->values([
            $bundle,
            0,
            $entity->id(),
            $revision_id,
            $delta,
            $entity->language()
                ->getId(),
            $value,
        ]);
    }
    $query->execute();
    // Load every revision and check the values.
    foreach ($revision_ids as $revision_id) {
        $entity = $storage->loadRevision($revision_id);
        foreach ($values[$revision_id] as $delta => $value) {
            if ($delta < $this->fieldCardinality) {
                $this->assertEqual($entity->{$this->fieldName}[$delta]->value, $value);
            }
            else {
                $this->assertArrayNotHasKey($delta, $entity->{$this->fieldName});
            }
        }
    }
    // Load the "current revision" and check the values.
    $entity = $storage->load($entity->id());
    foreach ($values[$revision_id] as $delta => $value) {
        if ($delta < $this->fieldCardinality) {
            $this->assertEqual($entity->{$this->fieldName}[$delta]->value, $value);
        }
        else {
            $this->assertArrayNotHasKey($delta, $entity->{$this->fieldName});
        }
    }
    // Add a translation in an unavailable language code and verify it is not
    // loaded.
    $unavailable_langcode = 'xx';
    $values = [
        $bundle,
        0,
        $entity->id(),
        $entity->getRevisionId(),
        0,
        $unavailable_langcode,
        mt_rand(1, 127),
    ];
    $connection->insert($this->table)
        ->fields($columns)
        ->values($values)
        ->execute();
    $connection->insert($this->revisionTable)
        ->fields($columns)
        ->values($values)
        ->execute();
    $entity = $storage->load($entity->id());
    $this->assertArrayNotHasKey($unavailable_langcode, $entity->{$this->fieldName});
}

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