summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2009-10-18 18:46:11 +0000
committerDries Buytaert <dries@buytaert.net>2009-10-18 18:46:11 +0000
commit29a574e08252d3fe7e1e805abbb6373a2cddba54 (patch)
treeb4463c245564639d768a34b3476b4ec2e6c74796
parent6e9edc1379f2ee97a7fa42cabd4dd7c56241d21e (diff)
downloadbrdo-29a574e08252d3fe7e1e805abbb6373a2cddba54.tar.gz
brdo-29a574e08252d3fe7e1e805abbb6373a2cddba54.tar.bz2
- Patch #605930 by bjaspan: fixed problem with list field static caching in field_update_field(). Added some first list tests too!
-rw-r--r--modules/field/modules/list/list.info1
-rw-r--r--modules/field/modules/list/list.module26
-rw-r--r--modules/field/modules/list/list.test89
3 files changed, 116 insertions, 0 deletions
diff --git a/modules/field/modules/list/list.info b/modules/field/modules/list/list.info
index 96a98d500..af5202474 100644
--- a/modules/field/modules/list/list.info
+++ b/modules/field/modules/list/list.info
@@ -5,4 +5,5 @@ package = Core - fields
version = VERSION
core = 7.x
files[]=list.module
+files[]=list.test
required = TRUE
diff --git a/modules/field/modules/list/list.module b/modules/field/modules/list/list.module
index a87c146a5..b569f8c8e 100644
--- a/modules/field/modules/list/list.module
+++ b/modules/field/modules/list/list.module
@@ -131,9 +131,35 @@ function list_field_settings_form($field, $instance, $has_data) {
}
/**
+ * Implement hook_field_create_field().
+ */
+function list_field_create_field($field) {
+ if (array_key_exists($field['type'], list_field_info())) {
+ // Clear the static cache of allowed values for $field.
+ $allowed_values = &drupal_static('list_allowed_values', array());
+ unset($allowed_values[$field['field_name']]);
+ }
+}
+
+/**
+ * Implement hook_field_update_field().
+ */
+function list_field_update_field($field, $prior_field, $has_data) {
+ if (array_key_exists($field['type'], list_field_info())) {
+ // Clear the static cache of allowed values for $field.
+ $allowed_values = &drupal_static('list_allowed_values', array());
+ unset($allowed_values[$field['field_name']]);
+ }
+}
+
+/**
* Create an array of allowed values for this field.
*/
function list_allowed_values($field) {
+ // This static cache must be cleared whenever $field['field_name']
+ // changes. This includes when it is created because a different
+ // field with the same name may have previously existed, as well
+ // as when it is updated.
$allowed_values = &drupal_static(__FUNCTION__, array());
if (isset($allowed_values[$field['field_name']])) {
diff --git a/modules/field/modules/list/list.test b/modules/field/modules/list/list.test
new file mode 100644
index 000000000..a6c1ae1dc
--- /dev/null
+++ b/modules/field/modules/list/list.test
@@ -0,0 +1,89 @@
+<?php
+// $Id$
+
+class ListFieldTestCase extends DrupalWebTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'List field',
+ 'description' => "Test the List field type.",
+ 'group' => 'Field'
+ );
+ }
+
+ function setUp() {
+ parent::setUp('field_test');
+
+ $this->card_1 = array(
+ 'field_name' => 'card_1',
+ 'type' => 'list',
+ 'cardinality' => 1,
+ 'settings' => array(
+ 'allowed_values' => "1|One\n2|Two\n3|Three\n",
+ ),
+ );
+ $this->card_1 = field_create_field($this->card_1);
+
+ $this->instance_1 = array(
+ 'field_name' => $this->card_1['field_name'],
+ 'object_type' => 'test_entity',
+ 'bundle' => FIELD_TEST_BUNDLE,
+ 'widget' => array(
+ 'type' => 'options_buttons',
+ ),
+ );
+ $this->instance_1 = field_create_instance($this->instance_1);
+ }
+
+ /**
+ * Test that allowed values can be updated and that the updates are
+ * reflected in generated forms.
+ */
+ function testUpdateAllowedValues() {
+ // All three options appear.
+ $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);
+ $form = drupal_get_form('field_test_entity_form', $entity);
+ $this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][1]), t('Option 1 exists'));
+ $this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][2]), t('Option 2 exists'));
+ $this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][3]), t('Option 3 exists'));
+
+ // Removed options do not appear.
+ $this->card_1['settings']['allowed_values'] = "2|Two";
+ field_update_field($this->card_1);
+ $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);
+ $form = drupal_get_form('field_test_entity_form', $entity);
+ $this->assertTrue(empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][1]), t('Option 1 does not exist'));
+ $this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][2]), t('Option 2 exists'));
+ $this->assertTrue(empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][3]), t('Option 3 does not exist'));
+
+ // Completely new options appear.
+ $this->card_1['settings']['allowed_values'] = "10|Update\n20|Twenty";
+ field_update_field($this->card_1);
+ $form = drupal_get_form('field_test_entity_form', $entity);
+ $this->assertTrue(empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][1]), t('Option 1 does not exist'));
+ $this->assertTrue(empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][2]), t('Option 2 does not exist'));
+ $this->assertTrue(empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][3]), t('Option 3 does not exist'));
+ $this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][10]), t('Option 10 exists'));
+ $this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][20]), t('Option 20 exists'));
+
+ // Options are reset when a new field with the same name is created.
+ field_delete_field($this->card_1['field_name']);
+ unset($this->card_1['id']);
+ $this->card_1['settings']['allowed_values'] = "1|One\n2|Two\n3|Three\n";
+ $this->card_1 = field_create_field($this->card_1);
+ $this->instance_1 = array(
+ 'field_name' => $this->card_1['field_name'],
+ 'object_type' => 'test_entity',
+ 'bundle' => FIELD_TEST_BUNDLE,
+ 'widget' => array(
+ 'type' => 'options_buttons',
+ ),
+ );
+ $this->instance_1 = field_create_instance($this->instance_1);
+ $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);
+ $form = drupal_get_form('field_test_entity_form', $entity);
+ $this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][1]), t('Option 1 exists'));
+ $this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][2]), t('Option 2 exists'));
+ $this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE]['value'][3]), t('Option 3 exists'));
+ }
+}
+