diff --git a/core/modules/field/modules/list/tests/list.test b/core/modules/field/modules/list/tests/list.test index 988f6a3..935d061 100644 --- a/core/modules/field/modules/list/tests/list.test +++ b/core/modules/field/modules/list/tests/list.test @@ -113,6 +113,81 @@ class ListFieldTestCase extends FieldTestCase { } } +class ListDynamicValuesTestCase extends FieldTestCase { + function setUp() { + parent::setUp(array('list', 'field_test', 'list_test')); + + $this->field_name = 'test_list'; + $this->field = array( + 'field_name' => $this->field_name, + 'type' => 'list_text', + 'cardinality' => 1, + 'settings' => array( + 'allowed_values_function' => 'list_test_dynamic_values_callback', + ), + ); + $this->field = field_create_field($this->field); + + $this->instance = array( + 'field_name' => $this->field_name, + 'entity_type' => 'test_entity', + 'bundle' => 'test_bundle', + 'required' => TRUE, + 'widget' => array( + 'type' => 'options_select', + ), + ); + $this->instance = field_create_instance($this->instance); + $this->test = array( + 'id' => mt_rand(1, 10), + 'vid' => mt_rand(1, 10), + 'bundle' => 'test_bundle', + 'label' => $this->randomString(), + ); + $this->entity = call_user_func_array('field_test_create_stub_entity', $this->test); + } +} + +class ListDynamicValuesValidationTestCase extends ListDynamicValuesTestCase { + public static function getInfo() { + return array( + 'name' => 'List field dynamic values', + 'description' => 'Test the List field allowed values function.', + 'group' => 'Field types', + ); + } + + /** + * Test that allowed values function gets the entity. + */ + function testDynamicAllowedValues() { + // Verify that the test passes against every value we had. + foreach ($this->test as $key => $value) { + $entity->test_list[LANGUAGE_NOT_SPECIFIED][0]['value'] = $value; + try { + field_attach_validate('test_entity', $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) { + $entity->test_list[LANGUAGE_NOT_SPECIFIED][0]['value'] = is_numeric($value) ? (100 - $value) : ('X' . $value); + $pass = FALSE; + try { + field_attach_validate('test_entity', $entity); + } + catch (FieldValidationException $e) { + $pass = TRUE; + } + $this->assertTrue($pass, $key . ' should not pass'); + } + } +} + /** * List module UI tests. */ diff --git a/core/modules/field/modules/list/tests/list_test.module b/core/modules/field/modules/list/tests/list_test.module index 8d53404..a60e369 100644 --- a/core/modules/field/modules/list/tests/list_test.module +++ b/core/modules/field/modules/list/tests/list_test.module @@ -21,3 +21,9 @@ function list_test_allowed_values_callback($field) { return $values; } + +function list_test_dynamic_values_callback($field, $instance, $entity_type, $entity, &$cacheable) { + $cacheable = FALSE; + // We need the values of the entity as keys. + return drupal_map_assoc(array_merge(array($entity->ftlabel), entity_extract_ids($entity_type, $entity))); +} diff --git a/core/modules/field/modules/options/options.test b/core/modules/field/modules/options/options.test index b945949..b3fb8f1 100644 --- a/core/modules/field/modules/options/options.test +++ b/core/modules/field/modules/options/options.test @@ -519,3 +519,35 @@ class OptionsWidgetsTestCase extends FieldTestCase { } } +class OptionsSelectDynamicValuesTestCase extends ListDynamicValuesTestCase { + public static function getInfo() { + return array( + 'name' => 'Options select dynamic values', + 'description' => 'Test an options select on a list field with an allowed values function.', + 'group' => 'Field types', + ); + } + + /** + * Tests the 'options_select' widget (single select). + */ + function testSelectListDynamic() { + // Create an entity. + $this->entity->is_new = TRUE; + field_test_entity_save($this->entity); + // Create a web user. + $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content')); + $this->drupalLogin($web_user); + + // Display form. + $this->drupalGet('test-entity/manage/' . $this->entity->ftid . '/edit'); + $options = $this->xpath('//select[@id="edit-test-list-und"]/option'); + $this->assertEqual(count($options), count($this->test) + 1); + foreach ($options as $option) { + $value = (string) $option['value']; + if ($value != '_none') { + $this->assertTrue(array_search($value, $this->test)); + } + } + } +}