You are here

protected function FieldUnitTestBase::assertFieldValues in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FieldUnitTestBase.php \Drupal\field\Tests\FieldUnitTestBase::assertFieldValues()

Assert that a field has the expected values in an entity.

This function only checks a single column in the field values.

Parameters

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/src/Tests/FieldUnitTestBase.php, line 192
Contains \Drupal\field\Tests\FieldUnitTestBase.

Class

FieldUnitTestBase
Parent class for Field API unit tests.

Namespace

Drupal\field\Tests

Code

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.
  \Drupal::entityManager()
    ->getStorage($entity
    ->getEntityTypeId())
    ->resetCache(array(
    $entity
      ->id(),
  ));
  $e = entity_load($entity
    ->getEntityTypeId(), $entity
    ->id());
  $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, format_string('Value @value was saved correctly.', array(
      '@value' => $value,
    )));
  }
}