function FieldSqlStorageTest::testFieldLoad in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php \Drupal\system\Tests\Entity\FieldSqlStorageTest::testFieldLoad()
Tests field loading works correctly by inserting directly in the tables.
File
- core/
modules/ system/ src/ Tests/ Entity/ FieldSqlStorageTest.php, line 110 - Contains \Drupal\system\Tests\Entity\FieldSqlStorageTest.
Class
- FieldSqlStorageTest
- Tests Field SQL Storage .
Namespace
Drupal\system\Tests\EntityCode
function testFieldLoad() {
$entity_type = $bundle = 'entity_test_rev';
$storage = $this->container
->get('entity.manager')
->getStorage($entity_type);
$columns = array(
'bundle',
'deleted',
'entity_id',
'revision_id',
'delta',
'langcode',
$this->tableMapping
->getFieldColumnName($this->fieldStorage, 'value'),
);
// Create an entity with four revisions.
$revision_ids = array();
$entity = entity_create($entity_type);
$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 = array();
$query = db_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(array(
$bundle,
0,
$entity
->id(),
$revision_id,
$delta,
$entity
->language()
->getId(),
$value,
));
}
$query
->execute();
}
$query = db_insert($this->table)
->fields($columns);
foreach ($values[$revision_id] as $delta => $value) {
$query
->values(array(
$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
->assertFalse(array_key_exists($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
->assertFalse(array_key_exists($delta, $entity->{$this->fieldName}));
}
}
// Add a translation in an unavailable language code and verify it is not
// loaded.
$unavailable_langcode = 'xx';
$values = array(
$bundle,
0,
$entity
->id(),
$entity
->getRevisionId(),
0,
$unavailable_langcode,
mt_rand(1, 127),
);
db_insert($this->table)
->fields($columns)
->values($values)
->execute();
db_insert($this->revisionTable)
->fields($columns)
->values($values)
->execute();
$entity = $storage
->load($entity
->id());
$this
->assertFalse(array_key_exists($unavailable_langcode, $entity->{$this->fieldName}));
}