Commit cc42c55f authored by webchick's avatar webchick

#410926 by Damien Tournoud: Tests to ensure disabled Form API fields are working properly.

parent 4d5ec09a
......@@ -149,6 +149,33 @@ class FormsTestCase extends DrupalWebTestCase {
)));
}
}
/**
* Test handling of disabled elements.
*
* @see _form_test_disabled_elements().
*/
function testDisabledElements() {
// Submit the form, and fetch the default values.
$this->drupalPost('form-test/disabled-elements', array(), t('Submit'));
$returned_values = drupal_json_decode($this->content);
// Get the default value from the form.
$form_state = array();
$form = _form_test_disabled_elements(array(), $form_state);
foreach (element_children($form) as $key) {
if (isset($form[$key]['#default_value'])) {
$expected_value = $form[$key]['#default_value'];
if ($key == 'checkboxes_multiple') {
// Checkboxes values are not filtered out.
$returned_values[$key] = array_filter($returned_values[$key]);
}
$this->assertEqual($expected_value, $returned_values[$key], t('Default value for %type: expected %expected, returned %returned.', array('%type' => $key, '%expected' => var_export($expected_value, TRUE), '%returned' => var_export($returned_values[$key], TRUE))));
}
}
}
}
/**
......
......@@ -86,6 +86,14 @@ function form_test_menu() {
'type' => MENU_CALLBACK,
);
$items['form-test/disabled-elements'] = array(
'title' => t('Form test'),
'page callback' => 'drupal_get_form',
'page arguments' => array('_form_test_disabled_elements'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['form-test/form-rebuild-preserve-values'] = array(
'title' => 'Form values preservation during rebuild test',
'page callback' => 'drupal_get_form',
......@@ -673,6 +681,104 @@ function _form_test_checkbox_submit($form, &$form_state) {
exit();
}
/**
* Build a form to test disabled elements.
*/
function _form_test_disabled_elements($form, &$form_state) {
// Elements that take a simple default value.
foreach (array('textfield', 'textarea', 'hidden') as $type) {
$form[$type] = array(
'#type' => $type,
'#title' => $type,
'#default_value' => $type,
'#disabled' => TRUE,
);
}
// Multiple values option elements.
foreach (array('checkboxes', 'select') as $type) {
$form[$type . '_multiple'] = array(
'#type' => $type,
'#title' => $type . ' (multiple)',
'#options' => array(
'test_1' => 'Test 1',
'test_2' => 'Test 2',
),
'#multiple' => TRUE,
'#default_value' => array('test_2' => 'test_2'),
'#disabled' => TRUE,
);
}
// Single values option elements.
foreach (array('radios', 'select') as $type) {
$form[$type . '_single'] = array(
'#type' => $type,
'#title' => $type . ' (single)',
'#options' => array(
'test_1' => 'Test 1',
'test_2' => 'Test 2',
),
'#multiple' => FALSE,
'#default_value' => 'test_2',
'#disabled' => TRUE,
);
}
// Checkbox and radio.
foreach (array('checkbox', 'radio') as $type) {
$form[$type . '_unchecked'] = array(
'#type' => $type,
'#title' => $type . ' (unchecked)',
'#return_value' => 1,
'#default_value' => 0,
'#disabled' => TRUE,
);
$form[$type . '_checked'] = array(
'#type' => $type,
'#title' => $type . ' (checked)',
'#return_value' => 1,
'#default_value' => 1,
'#disabled' => TRUE,
);
}
// Weight.
$form['weight'] = array(
'#type' => 'weight',
'#title' => 'weight',
'#default_value' => 10,
'#disabled' => TRUE,
);
// Date.
$form['date'] = array(
'#type' => 'date',
'#title' => 'date',
'#disabled' => TRUE,
'#default_value' => array(
'day' => 19,
'month' => 11,
'year' => 1978,
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Return the form values via JSON.
*/
function _form_test_disabled_elements_submit($form, &$form_state) {
drupal_json_output($form_state['values']);
exit();
}
/**
* Form builder for testing preservation of values during a rebuild.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment