diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-01-28 07:43:26 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-01-28 07:43:26 +0000 |
commit | ad99dd761a7e11256200d959f10c9898befa8176 (patch) | |
tree | be658215f4af3a0a3fb56126fb11b829ce8f3b9a /modules/simpletest | |
parent | f16eccbe9d0199385e0334b1692d6a1ec7225917 (diff) | |
download | brdo-ad99dd761a7e11256200d959f10c9898befa8176.tar.gz brdo-ad99dd761a7e11256200d959f10c9898befa8176.tar.bz2 |
#242962 by Heine: Add a 'tablesort' Form API element to unify various places we use a big table with JS-enabled checkboxes next to each item (with tests).
Diffstat (limited to 'modules/simpletest')
-rw-r--r-- | modules/simpletest/tests/form.test | 203 | ||||
-rw-r--r-- | modules/simpletest/tests/form_test.info | 8 | ||||
-rw-r--r-- | modules/simpletest/tests/form_test.module | 182 |
3 files changed, 393 insertions, 0 deletions
diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index 64311a9c3..8d376bfb7 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -110,3 +110,206 @@ class FormsTestTypeCase extends DrupalWebTestCase { } } +/** + * Test the tableselect form element for expected behavior. + */ +class FormsElementsTableSelectFunctionalTest extends DrupalWebTestCase { + + function getInfo() { + return array( + 'name' => t('Tableselect form element type test'), + 'description' => t('Test the tableselect element for expected behavior'), + 'group' => t('Form API'), + ); + } + + function setUp() { + parent::setUp('form_test'); + } + + + /** + * Test the display of checkboxes when #multiple is TRUE. + */ + function testMultipleTrue() { + + $this->drupalGet('form_test/tableselect/multiple-true'); + + $this->assertNoText(t('Empty text.'), t('Empty text should not be displayed.')); + + // Test for the presence of the Select all rows tableheader. + $this->assertFieldByXPath('//th[@class="select-all"]', NULL, t('Presence of the "Select all" checkbox.')); + + $rows = array('row1', 'row2', 'row3'); + foreach($rows as $row) { + $this->assertFieldByXPath('//input[@type="checkbox"]', $row, t('Checkbox for value @row.', array('@row' => $row))); + } + } + + /** + * Test the display of radios when #multiple is FALSE. + */ + function testMultipleFalse() { + $this->drupalGet('form_test/tableselect/multiple-false'); + + $this->assertNoText(t('Empty text.'), t('Empty text should not be displayed.')); + + // Test for the absence of the Select all rows tableheader. + $this->assertNoFieldByXPath('//th[@class="select-all"]', '', t('Absence of the "Select all" checkbox.')); + + $rows = array('row1', 'row2', 'row3'); + foreach($rows as $row) { + $this->assertFieldByXPath('//input[@type="radio"]', $row, t('Radio button for value @row.', array('@row' => $row))); + } + } + + /** + * Test the display of the #empty text when #options is an empty array. + */ + function testEmptyText() { + $this->drupalGet('form_test/tableselect/empty-text'); + $this->assertText(t('Empty text.'), t('Empty text should be displayed.')); + } + + /** + * Test the submission of single and multiple values when #multiple is TRUE. + */ + function testMultipleTrueSubmit() { + + // Test a submission with one checkbox checked. + $edit = array(); + $edit['tableselect[row1]'] = TRUE; + $this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit'); + + $this->assertText(t('Submitted: row1 = row1'), t('Checked checkbox row1')); + $this->assertText(t('Submitted: row2 = 0'), t('Unchecked checkbox row2.')); + $this->assertText(t('Submitted: row3 = 0'), t('Unchecked checkbox row3.')); + + // Test a submission with multiple checkboxes checked. + $edit['tableselect[row1]'] = TRUE; + $edit['tableselect[row3]'] = TRUE; + $this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit'); + + $this->assertText(t('Submitted: row1 = row1'), t('Checked checkbox row1.')); + $this->assertText(t('Submitted: row2 = 0'), t('Unchecked checkbox row2.')); + $this->assertText(t('Submitted: row3 = row3'), t('Checked checkbox row3.')); + + } + + /** + * Test submission of values when #multiple is FALSE. + */ + function testMultipleFalseSubmit() { + $edit['tableselect'] = 'row1'; + $this->drupalPost('form_test/tableselect/multiple-false', $edit, 'Submit'); + $this->assertText(t('Submitted: row1'), t('Selected radio button')); + } + + /** + * Test the #js_select property. + */ + function testAdvancedSelect() { + // When #multiple = TRUE a Select all checkbox should be displayed by default. + $this->drupalGet('form_test/tableselect/advanced-select/multiple-true-default'); + $this->assertFieldByXPath('//th[@class="select-all"]', NULL, t('Display a "Select all" checkbox by default when #multiple is TRUE.')); + + // When #js_select is set to FALSE, a "Select all" checkbox should not be displayed. + $this->drupalGet('form_test/tableselect/advanced-select/multiple-true-no-advanced-select'); + $this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #js_select is FALSE.')); + + // A "Select all" checkbox never makes sense when #multiple = FALSE, regardless of the value of #js_select. + $this->drupalGet('form_test/tableselect/advanced-select/multiple-false-default'); + $this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #multiple is FALSE.')); + + $this->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select'); + $this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #multiple is FALSE, even when #js_select is TRUE.')); + } + + + /** + * Test the whether the option checker gives an error on invalid tableselect values for checkboxes. + */ + function testMultipleTrueOptionchecker() { + + list($header, $options) = _form_test_tableselect_get_data(); + + $form['tableselect'] = array( + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options, + ); + + // Test with a valid value. + list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'row1')); + $this->assertFalse(isset($errors['tableselect']), t('Option checker allows valid values for checkboxes.')); + + // Test with an invalid value. + list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'non_existing_value')); + $this->assertTrue(isset($errors['tableselect']), t('Option checker disallows invalid values for checkboxes.')); + + } + + + /** + * Test the whether the option checker gives an error on invalid tableselect values for radios. + */ + function testMultipleFalseOptionchecker() { + + list($header, $options) = _form_test_tableselect_get_data(); + + $form['tableselect'] = array( + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options, + '#multiple' => FALSE, + ); + + // Test with a valid value. + list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'row1')); + $this->assertFalse(isset($errors['tableselect']), t('Option checker allows valid values for radio buttons.')); + + // Test with an invalid value. + list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'non_existing_value')); + $this->assertTrue(isset($errors['tableselect']), t('Option checker disallows invalid values for radio buttons.')); + } + + + /** + * Helper function for the option check test to submit a form while collecting errors. + * + * @param $form_element + * A form element to test. + * @param $edit + * An array containing post data. + * + * @return + * An array containing the processed form, the form_state and any errors. + */ + private function formSubmitHelper($form_element, $edit) { + $form_id = $this->randomName(); + + $form = $form_state = array(); + + $form = array_merge($form, $form_element); + $form['op'] = array('#type' => 'submit', '#value' => t('Submit')); + + $form['#post'] = $edit; + $form['#post']['form_id'] = $form_id; + + drupal_prepare_form($form_id, $form, $form_state); + + drupal_process_form($form_id, $form, $form_state); + + $errors = form_get_errors(); + + // Clear errors and messages. + drupal_get_messages(); + form_set_error(NULL, '', TRUE); + + // Return the processed form together with form_state and errors + // to allow the caller lowlevel access to the form. + return array($form, $form_state, $errors); + } + +} + diff --git a/modules/simpletest/tests/form_test.info b/modules/simpletest/tests/form_test.info new file mode 100644 index 000000000..d138f15cc --- /dev/null +++ b/modules/simpletest/tests/form_test.info @@ -0,0 +1,8 @@ +; $Id$ +name = "FormAPI Test" +description = "Support module for Form API tests." +package = Testing +version = VERSION +core = 7.x +files[] = form_test.module +hidden = TRUE diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module new file mode 100644 index 000000000..a1eee25b2 --- /dev/null +++ b/modules/simpletest/tests/form_test.module @@ -0,0 +1,182 @@ +<?php +// $Id$ + +/** + * @file + * Helper module for the form API tests. + */ + +/** + * Implementation of hook_menu(). + */ +function form_test_menu() { + $items = array(); + + $items['form_test/tableselect/multiple-true'] = array( + 'title' => 'Tableselect checkboxes test', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('_form_test_tableselect_multiple_true_form'), + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + + $items['form_test/tableselect/multiple-false'] = array( + 'title' => 'Tableselect radio button test', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('_form_test_tableselect_multiple_false_form'), + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + + $items['form_test/tableselect/empty-text'] = array( + 'title' => 'Tableselect empty text test', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('_form_test_tableselect_empty_form'), + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + + $items['form_test/tableselect/advanced-select'] = array( + 'title' => 'Tableselect js_select tests', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('_form_test_tableselect_js_select_form'), + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + + return $items; +} + +/** + * Create a header and options array. Helper function for callbacks. + */ +function _form_test_tableselect_get_data() { + $header = array( + 'one' => t('One'), + 'two' => t('Two'), + 'three' => t('Three'), + 'four' => t('Four'), + ); + + $options['row1'] = array( + 'one' => 'row1col1', + 'two' => t('row1col2'), + 'three' => t('row1col3'), + 'four' => t('row1col4'), + ); + + $options['row2'] = array( + 'one' => 'row2col1', + 'two' => t('row2col2'), + 'three' => t('row2col3'), + 'four' => t('row2col4'), + ); + + $options['row3'] = array( + 'one' => 'row3col1', + 'two' => t('row3col2'), + 'three' => t('row3col3'), + 'four' => t('row3col4'), + ); + + return array($header, $options); +} + +/** + * Build a form to test the tableselect element. + * + * @param $form_state + * The form_state + * @param $element_properties + * An array of element properties for the tableselect element. + * + * @return + * A form with a tableselect element and a submit button. + */ +function _form_test_tableselect_form_builder($form_state, $element_properties) { + $form = array(); + + list($header, $options) = _form_test_tableselect_get_data(); + + $form['tableselect'] = $element_properties; + + $form['tableselect'] += array( + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options, + '#multiple' => FALSE, + '#empty' => t('Empty text.'), + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + + return $form; +} + +/** + * Test the tableselect #multiple = TRUE functionality. + */ +function _form_test_tableselect_multiple_true_form($form_state) { + return _form_test_tableselect_form_builder($form_state, array('#multiple' => TRUE)); +} + +/** + * Process the tableselect #multiple = TRUE submitted values. + */ +function _form_test_tableselect_multiple_true_form_submit($form, &$form_state) { + $selected = $form_state['values']['tableselect']; + foreach ($selected as $key => $value) { + drupal_set_message(t('Submitted: @key = @value', array('@key' => $key, '@value' => $value))); + } +} + +/** + * Test the tableselect #multiple = FALSE functionality. + */ +function _form_test_tableselect_multiple_false_form($form_state) { + return _form_test_tableselect_form_builder($form_state, array('#multiple' => FALSE)); +} + +/** + * Process the tableselect #multiple = FALSE submitted values. + */ +function _form_test_tableselect_multiple_false_form_submit($form, &$form_state) { + drupal_set_message(t('Submitted: @value', array('@value' => $form_state['values']['tableselect']))); +} + +/** + * Test functionality of the tableselect #empty property. + */ +function _form_test_tableselect_empty_form($form_state) { + return _form_test_tableselect_form_builder($form_state, array('#options' => array())); +} + +/** + * Test functionality of the tableselect #js_select property. + */ +function _form_test_tableselect_js_select_form($form_state, $action) { + + switch ($action) { + + case 'multiple-true-default': + $options = array('#multiple' => TRUE); + break; + + case 'multiple-false-default': + $options = array('#multiple' => FALSE); + break; + + case 'multiple-true-no-advanced-select': + $options = array('#multiple' => TRUE, '#js_select' => FALSE); + break; + + case 'multiple-false-advanced-select': + $options = array('#multiple' => FALSE, '#js_select' => TRUE); + break; + } + + return _form_test_tableselect_form_builder($form_state, $options); +} |