diff --git a/date_popup/date_popup.module b/date_popup/date_popup.module index b7f2b4b..aa460f5 100644 --- a/date_popup/date_popup.module +++ b/date_popup/date_popup.module @@ -554,7 +554,9 @@ function date_popup_process_time_part(&$element) { * contains a string, after submission it contains an array. */ function date_popup_validate($element, &$form_state) { - if (date_hidden_element($element)) { + // Disabled and hidden elements won't have any input and don't need + // validation. + if (date_hidden_element($element) || !empty($element['#disabled'])) { return; } diff --git a/tests/date_field.test b/tests/date_field.test index 335bb8a..9ea0f11 100644 --- a/tests/date_field.test +++ b/tests/date_field.test @@ -395,3 +395,93 @@ class DateFieldTestCase extends DateFieldBasic { } } + +/** + * Tests date fields when the form is altered by another module. + */ +class DateFormAlterTestCase extends DateFieldBasic { + + public static function getInfo() { + return array( + 'name' => 'Date form alters', + 'description' => 'Tests date fields when the form is altered by another module.', + 'group' => 'Date', + ); + } + + public function setUp() { + parent::setUp(array('date_test')); + } + + /** + * Tests that disabled date elements are submitted correctly. + */ + public function testDisabled() { + // Create a date fields with simple values for each field type and widget. + $parameters = array( + 'field_type' => array('date', 'datestamp', 'datetime'), + 'widget_type' => array('date_select', 'date_popup', 'date_text'), + ); + foreach ($this->generatePermutations($parameters) as $p) { + $field_name = 'field_test_disabled'; + $id = 'edit-field-test-disabled'; + $label = 'Test'; + $options = array( + 'label' => $label, + 'widget_type' => $p['widget_type'], + 'field_name' => $field_name, + 'field_type' => $p['field_type'], + 'input_format' => 'm/d/Y - H:i', + 'default_format' => 'short' + ); + $this->createDateField($options); + + // Save a node with the API. + $node = new stdClass(); + $node->type = 'story'; + $node->title = $this->randomName(); + $node->{$field_name}['und'][0]['value'] =date('Y-m-d h:i:s'); + node_save($node); + + // Try editing both the existing node and a new node. + foreach (array("node/{$node->nid}/edit", 'node/add/story') as $path) { + $this->drupalGet($path); + + // Ensure that elements within the field div are rendered as disabled. + $this->assertFieldByXpath( + "//div[@id='$id']", + NULL, + "$id div is displayed." + ); + $this->assertFieldByXpath( + "//div[@id='$id']//input[@disabled='disabled']|//div[@id='$id']//select[@disabled='disabled']", + NULL, + "There is a disabled select or input for $id." + ); + // Ensure that no elements within the field div are not disabled. + $this->assertNoFieldByXpath( + "//div[@id='$id']//input[not(@disabled)]", + NULL, + "No child input of $id is not disabled." + ); + $this->assertNoFieldByXpath( + "//div[@id='$id']//select[not(@disabled)]", + NULL, + "No child select of $id is not disabled." + ); + + // Add title value and submit the form. + $title = 'Turn around, bright eyes'; + $edit = array('title' => $title); + $this->drupalPost(NULL, $edit, t('Save')); + + // Verify that the form was submitted correctly and that the default + // date is displayed. + $this->assertText("Story $title has been"); + $this->assertText(date('m/d/Y')); + } + $this->deleteDateField($label); + } + } + +} diff --git a/tests/date_test/date_test.module b/tests/date_test/date_test.module index 72842b5..eba347e 100644 --- a/tests/date_test/date_test.module +++ b/tests/date_test/date_test.module @@ -38,3 +38,12 @@ function date_test_sample_form($form, &$form_state) { return $form; } + +/** + * Implements hook_form_FORM_ID_alter() for story node forms. + * + * @see DateFieldTestCase::testDisabled() + */ +function date_test_form_story_node_form_alter(&$form, &$form_state) { + $form['field_test_disabled']['#disabled'] = TRUE; +}