You are here

public function OptionsDynamicValuesValidationTest::testDynamicAllowedValues in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/options/tests/src/Functional/OptionsDynamicValuesValidationTest.php \Drupal\Tests\options\Functional\OptionsDynamicValuesValidationTest::testDynamicAllowedValues()

Tests that allowed values function gets the entity.

File

core/modules/options/tests/src/Functional/OptionsDynamicValuesValidationTest.php, line 20

Class

OptionsDynamicValuesValidationTest
Tests the Options field allowed values function.

Namespace

Drupal\Tests\options\Functional

Code

public function testDynamicAllowedValues() {

  // Verify that validation passes against every value we had.
  foreach ($this->test as $key => $value) {
    $this->entity->test_options->value = $value;
    $violations = $this->entity->test_options
      ->validate();
    $this
      ->assertCount(0, $violations, "{$key} is a valid value");
  }

  // Now verify that validation does not pass against anything else.
  foreach ($this->test as $key => $value) {
    $this->entity->test_options->value = is_numeric($value) ? 100 - $value : 'X' . $value;
    $violations = $this->entity->test_options
      ->validate();
    $this
      ->assertCount(1, $violations, "{$key} is not a valid value");
  }
}