diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-10-28 02:20:14 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-10-28 02:20:14 +0000 |
commit | 0344a78d9643d97e1aff8ab94fec551bc091bfbc (patch) | |
tree | 1baef104d17fbe810f4460abf4b853e49bbd5a1a /modules/simpletest | |
parent | 25bc5f030d30a391344e215fe1adb434d52d39ee (diff) | |
download | brdo-0344a78d9643d97e1aff8ab94fec551bc091bfbc.tar.gz brdo-0344a78d9643d97e1aff8ab94fec551bc091bfbc.tar.bz2 |
- Patch #654796 by chx, c960657, sun, effulgentsia: fix bugs with checkbox element.
Diffstat (limited to 'modules/simpletest')
-rw-r--r-- | modules/simpletest/tests/form.test | 81 | ||||
-rw-r--r-- | modules/simpletest/tests/form_test.module | 48 |
2 files changed, 129 insertions, 0 deletions
diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index 4661ad25f..1f76318f5 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -1352,3 +1352,84 @@ class FormsFileInclusionTestCase extends DrupalWebTestCase { $this->assertText('Submit callback called.'); } } + +/** + * Tests checkbox element. + */ +class FormCheckboxTestCase extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Form API checkbox', + 'description' => 'Tests form API checkbox handling of various combinations of #default_value and #return_value.', + 'group' => 'Form API', + ); + } + + function setUp() { + parent::setUp('form_test'); + } + + function testFormCheckbox() { + // Ensure that the checked state is determined and rendered correctly for + // tricky combinations of default and return values. + foreach (array(FALSE, NULL, TRUE, 0, '0', '', 1, '1', 'foobar', '1foobar') as $default_value) { + // Only values that can be used for array indeces are supported for + // #return_value, with the exception of integer 0, which is not supported. + // @see form_process_checkbox(). + foreach (array('0', '', 1, '1', 'foobar', '1foobar') as $return_value) { + $form_array = drupal_get_form('form_test_checkbox_type_juggling', $default_value, $return_value); + $form = drupal_render($form_array); + if ($default_value === TRUE) { + $checked = TRUE; + } + elseif ($return_value === '0') { + $checked = ($default_value === '0'); + } + elseif ($return_value === '') { + $checked = ($default_value === ''); + } + elseif ($return_value === 1 || $return_value === '1') { + $checked = ($default_value === 1 || $default_value === '1'); + } + elseif ($return_value === 'foobar') { + $checked = ($default_value === 'foobar'); + } + elseif ($return_value === '1foobar') { + $checked = ($default_value === '1foobar'); + } + $checked_in_html = strpos($form, 'checked') !== FALSE; + $message = t('#default_value is %default_value #return_value is %return_value.', array('%default_value' => var_export($default_value, TRUE), '%return_value' => var_export($return_value, TRUE))); + $this->assertIdentical($checked, $checked_in_html, $message); + } + } + + // Ensure that $form_state['values'] is populated correctly for a checkboxes + // group that includes a 0-indexed array of options. + $results = json_decode($this->drupalPost('form-test/checkboxes-zero', array(), 'Save')); + $this->assertIdentical($results->checkbox_off, array(0, 0, 0), t('All three in checkbox_off are zeroes: off.')); + $this->assertIdentical($results->checkbox_zero_default, array('0', 0, 0), t('The first choice is on in checkbox_zero_default')); + $this->assertIdentical($results->checkbox_string_zero_default, array('0', 0, 0), t('The first choice is on in checkbox_string_zero_default')); + $edit = array('checkbox_off[0]' => '0'); + $results = json_decode($this->drupalPost('form-test/checkboxes-zero', $edit, 'Save')); + $this->assertIdentical($results->checkbox_off, array('0', 0, 0), t('The first choice is on in checkbox_off but the rest is not')); + + // Ensure that each checkbox is rendered correctly for a checkboxes group + // that includes a 0-indexed array of options. + $this->drupalPost('form-test/checkboxes-zero/0', array(), 'Save'); + $checkboxes = $this->xpath('//input[@type="checkbox"]'); + foreach ($checkboxes as $checkbox) { + $checked = isset($checkbox['checked']); + $name = (string) $checkbox['name']; + $this->assertIdentical($checked, $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', t('Checkbox %name correctly checked', array('%name' => $name))); + } + $edit = array('checkbox_off[0]' => '0'); + $this->drupalPost('form-test/checkboxes-zero/0', $edit, 'Save'); + $checkboxes = $this->xpath('//input[@type="checkbox"]'); + foreach ($checkboxes as $checkbox) { + $checked = isset($checkbox['checked']); + $name = (string) $checkbox['name']; + $this->assertIdentical($checked, $name == 'checkbox_off[0]' || $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', t('Checkbox %name correctly checked', array('%name' => $name))); + } + } +} diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module index fbeb6de45..4d717c04b 100644 --- a/modules/simpletest/tests/form_test.module +++ b/modules/simpletest/tests/form_test.module @@ -175,6 +175,13 @@ function form_test_menu() { 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); + $items['form-test/checkboxes-zero'] = array( + 'title' => 'FAPI test involving checkboxes and zero', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_checkboxes_zero'), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); return $items; } @@ -1395,3 +1402,44 @@ function form_test_load_include_custom($form, &$form_state) { $form_state['cache'] = TRUE; return $form; } + +function form_test_checkbox_type_juggling($form, $form_state, $default_value, $return_value) { + $form['checkbox'] = array( + '#type' => 'checkbox', + '#return_value' => $return_value, + '#default_value' => $default_value, + ); + return $form; +} + +function form_test_checkboxes_zero($form, &$form_state, $json = TRUE) { + $form['checkbox_off'] = array( + '#type' => 'checkboxes', + '#options' => array('foo', 'bar', 'baz'), + ); + $form['checkbox_zero_default'] = array( + '#type' => 'checkboxes', + '#options' => array('foo', 'bar', 'baz'), + '#default_value' => array(0), + ); + $form['checkbox_string_zero_default'] = array( + '#type' => 'checkboxes', + '#options' => array('foo', 'bar', 'baz'), + '#default_value' => array('0'), + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Save', + ); + if ($json) { + $form['#submit'][] = '_form_test_checkbox_submit'; + } + else { + $form['#submit'][] = '_form_test_checkboxes_zero_no_redirect'; + } + return $form; +} + +function _form_test_checkboxes_zero_no_redirect($form, &$form_state) { + $form_state['redirect'] = FALSE; +} |