public function FieldSqlStorageTest::testFieldWrite in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldWrite()
- 10 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldWrite()
Tests field saving works correctly by reading directly from the tables.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Entity/ FieldSqlStorageTest.php, line 180
Class
- FieldSqlStorageTest
- Tests Field SQL Storage .
Namespace
Drupal\KernelTests\Core\EntityCode
public function testFieldWrite() {
$entity_type = $bundle = 'entity_test_rev';
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create();
$revision_values = [];
// Check insert. Add one value too many.
$values = [];
for ($delta = 0; $delta <= $this->fieldCardinality; $delta++) {
$values[$delta]['value'] = mt_rand(1, 127);
}
$entity->{$this->fieldName} = $values;
$entity
->save();
$connection = Database::getConnection();
// Read the tables and check the correct values have been stored.
$rows = $connection
->select($this->table, 't')
->fields('t')
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertEqual(count($rows), $this->fieldCardinality);
foreach ($rows as $delta => $row) {
$expected = [
'bundle' => $bundle,
'deleted' => 0,
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'langcode' => $entity
->language()
->getId(),
'delta' => $delta,
$this->fieldName . '_value' => $values[$delta]['value'],
];
$this
->assertEqual($row, $expected, "Row {$delta} was stored as expected.");
}
// Test update. Add less values and check that the previous values did not
// persist.
$values = [];
for ($delta = 0; $delta <= $this->fieldCardinality - 2; $delta++) {
$values[$delta]['value'] = mt_rand(1, 127);
}
$entity->{$this->fieldName} = $values;
$entity
->save();
$rows = $connection
->select($this->table, 't')
->fields('t')
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertEqual(count($rows), count($values));
foreach ($rows as $delta => $row) {
$expected = [
'bundle' => $bundle,
'deleted' => 0,
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'langcode' => $entity
->language()
->getId(),
'delta' => $delta,
$this->fieldName . '_value' => $values[$delta]['value'],
];
$this
->assertEqual($row, $expected, "Row {$delta} was stored as expected.");
}
// Create a new revision.
$revision_values[$entity
->getRevisionId()] = $values;
$values = [];
for ($delta = 0; $delta < $this->fieldCardinality; $delta++) {
$values[$delta]['value'] = mt_rand(1, 127);
}
$entity->{$this->fieldName} = $values;
$entity
->setNewRevision();
$entity
->save();
$revision_values[$entity
->getRevisionId()] = $values;
// Check that data for both revisions are in the revision table.
foreach ($revision_values as $revision_id => $values) {
$rows = $connection
->select($this->revisionTable, 't')
->fields('t')
->condition('revision_id', $revision_id)
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertEqual(count($rows), min(count($values), $this->fieldCardinality));
foreach ($rows as $delta => $row) {
$expected = [
'bundle' => $bundle,
'deleted' => 0,
'entity_id' => $entity
->id(),
'revision_id' => $revision_id,
'langcode' => $entity
->language()
->getId(),
'delta' => $delta,
$this->fieldName . '_value' => $values[$delta]['value'],
];
$this
->assertEqual($row, $expected, "Row {$delta} was stored as expected.");
}
}
// Test emptying the field.
$entity->{$this->fieldName} = NULL;
$entity
->save();
$rows = $connection
->select($this->table, 't')
->fields('t')
->execute()
->fetchAllAssoc('delta', \PDO::FETCH_ASSOC);
$this
->assertCount(0, $rows);
}