function FieldKernelTestBase::assertFieldValues
Same name in other branches
- 9 core/modules/field/tests/src/Kernel/FieldKernelTestBase.php \Drupal\Tests\field\Kernel\FieldKernelTestBase::assertFieldValues()
- 10 core/modules/field/tests/src/Kernel/FieldKernelTestBase.php \Drupal\Tests\field\Kernel\FieldKernelTestBase::assertFieldValues()
- 11.x core/modules/field/tests/src/Kernel/FieldKernelTestBase.php \Drupal\Tests\field\Kernel\FieldKernelTestBase::assertFieldValues()
Assert that a field has the expected values in an entity.
This function only checks a single column in the field values.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity to test.
$field_name: The name of the field to test
$expected_values: The array of expected values.
$langcode: (Optional) The language code for the values. Defaults to \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_SPECIFIED.
$column: (Optional) The name of the column to check. Defaults to 'value'.
File
-
core/
modules/ field/ tests/ src/ Kernel/ FieldKernelTestBase.php, line 196
Class
- FieldKernelTestBase
- Parent class for Field API unit tests.
Namespace
Drupal\Tests\field\KernelCode
protected function assertFieldValues(EntityInterface $entity, $field_name, $expected_values, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $column = 'value') {
// Re-load the entity to make sure we have the latest changes.
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity->getEntityTypeId());
$storage->resetCache([
$entity->id(),
]);
$e = $storage->load($this->entityId);
$field = $values = $e->getTranslation($langcode)->{$field_name};
// Filter out empty values so that they don't mess with the assertions.
$field->filterEmptyItems();
$values = $field->getValue();
$this->assertEqual(count($values), count($expected_values), 'Expected number of values were saved.');
foreach ($expected_values as $key => $value) {
$this->assertEqual($values[$key][$column], $value, new FormattableMarkup('Value @value was saved correctly.', [
'@value' => $value,
]));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.