function OptionsDynamicValuesValidationTest::testDynamicAllowedValues

Test that allowed values function gets the entity.

File

drupal/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesValidationTest.php, line 28
Definition of Drupal\options\Tests\OptionsDynamicValuesValidationTest.

Class

OptionsDynamicValuesValidationTest
Tests the Options field allowed values function.

Namespace

Drupal\options\Tests

Code

function testDynamicAllowedValues() {

  // Verify that the test passes against every value we had.
  foreach ($this->test as $key => $value) {
    $this->entity->test_options->value = $value;
    try {
      field_attach_validate($this->entity);
      $this
        ->pass("{$key} should pass");
    } catch (FieldValidationException $e) {

      // This will display as an exception, no need for a separate error.
      throw $e;
    }
  }

  // Now verify that the test does not pass against anything else.
  foreach ($this->test as $key => $value) {
    $this->entity->test_options->value = is_numeric($value) ? 100 - $value : 'X' . $value;
    $pass = FALSE;
    try {
      field_attach_validate($this->entity);
    } catch (FieldValidationException $e) {
      $pass = TRUE;
    }
    $this
      ->assertTrue($pass, $key . ' should not pass');
  }
}