You are here

function ListDynamicValuesValidationTestCase::testDynamicAllowedValues in Drupal 7

Test that allowed values function gets the entity.

File

modules/field/modules/list/tests/list.test, line 171
Tests for list.module.

Class

ListDynamicValuesValidationTestCase
Tests the List field allowed values function.

Code

function testDynamicAllowedValues() {

  // Verify that the test passes against every value we had.
  foreach ($this->test as $key => $value) {
    $this->entity->test_list[LANGUAGE_NONE][0]['value'] = $value;
    try {
      field_attach_validate('test_entity', $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_list[LANGUAGE_NONE][0]['value'] = is_numeric($value) ? 100 - $value : 'X' . $value;
    $pass = FALSE;
    try {
      field_attach_validate('test_entity', $this->entity);
    } catch (FieldValidationException $e) {
      $pass = TRUE;
    }
    $this
      ->assertTrue($pass, $key . ' should not pass');
  }
}