function FieldTestBase::assertFieldValues

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/FieldTestBase.php \Drupal\Tests\field\Functional\FieldTestBase::assertFieldValues()
  2. 8.9.x core/modules/field/src/Tests/FieldTestBase.php \Drupal\field\Tests\FieldTestBase::assertFieldValues()
  3. 8.9.x core/modules/field/tests/src/Functional/FieldTestBase.php \Drupal\Tests\field\Functional\FieldTestBase::assertFieldValues()
  4. 11.x core/modules/field/tests/src/Functional/FieldTestBase.php \Drupal\Tests\field\Functional\FieldTestBase::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_DEFAULT.

$column: (Optional) The name of the column to check. Defaults to 'value'.

7 calls to FieldTestBase::assertFieldValues()
MultipleWidgetFormTest::testFieldFormMultipleWidget in core/modules/field/tests/src/Functional/MultipleWidgetFormTest.php
Tests widgets handling multiple values.
NestedFormTest::testNestedFieldForm in core/modules/field/tests/src/Functional/NestedFormTest.php
Tests Field API form integration within a subform.
OptionsWidgetsTest::testCheckBoxes in core/modules/options/tests/src/Functional/OptionsWidgetsTest.php
Tests the 'options_buttons' widget (multiple select).
OptionsWidgetsTest::testRadioButtons in core/modules/options/tests/src/Functional/OptionsWidgetsTest.php
Tests the 'options_buttons' widget (single select).
OptionsWidgetsTest::testSelectListFloat in core/modules/options/tests/src/Functional/OptionsWidgetsTest.php
Tests the 'options_select' widget (float values).

... See full list

File

core/modules/field/tests/src/Functional/FieldTestBase.php, line 51

Class

FieldTestBase
Parent class for Field API tests.

Namespace

Drupal\Tests\field\Functional

Code

public function assertFieldValues(EntityInterface $entity, $field_name, $expected_values, $langcode = LanguageInterface::LANGCODE_DEFAULT, $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($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->assertSameSize($expected_values, $values, 'Expected number of values were saved.');
    foreach ($expected_values as $key => $value) {
        $this->assertEquals($value, $values[$key][$column], "Value {$value} was saved correctly.");
    }
}

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