summaryrefslogtreecommitdiff
path: root/modules/simpletest
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2010-11-07 21:46:09 +0000
committerDries Buytaert <dries@buytaert.net>2010-11-07 21:46:09 +0000
commit0d74d52c2a2dec423cd35a903df593554da90a93 (patch)
tree579f788f4fcadd4fc6edfe0a4faeecef0446ec53 /modules/simpletest
parentd3852cd3f5b45e0c50c2b2ee29015d5a9d04f9a2 (diff)
downloadbrdo-0d74d52c2a2dec423cd35a903df593554da90a93.tar.gz
brdo-0d74d52c2a2dec423cd35a903df593554da90a93.tar.bz2
- Patch #915936 by sun: make it easier to define checkboxes/radios with customized sub-elements.
Diffstat (limited to 'modules/simpletest')
-rw-r--r--modules/simpletest/tests/form.test60
-rw-r--r--modules/simpletest/tests/form_test.module71
2 files changed, 131 insertions, 0 deletions
diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test
index 1f76318f5..a0df09052 100644
--- a/modules/simpletest/tests/form.test
+++ b/modules/simpletest/tests/form.test
@@ -370,6 +370,66 @@ class FormsTestCase extends DrupalWebTestCase {
}
/**
+ * Tests building and processing of core form elements.
+ */
+class FormElementTestCase extends DrupalWebTestCase {
+ protected $profile = 'testing';
+
+ public static function getInfo() {
+ return array(
+ 'name' => 'Element processing',
+ 'description' => 'Tests building and processing of core form elements.',
+ 'group' => 'Form API',
+ );
+ }
+
+ function setUp() {
+ parent::setUp(array('form_test'));
+ }
+
+ /**
+ * Tests expansion of #options for #type checkboxes and radios.
+ */
+ function testOptions() {
+ $this->drupalGet('form-test/checkboxes-radios');
+
+ // Verify that all options appear in their defined order.
+ foreach (array('checkbox', 'radio') as $type) {
+ $elements = $this->xpath('//input[@type=:type]', array(':type' => $type));
+ $expected_values = array('0', 'foo', '1', 'bar');
+ foreach ($elements as $element) {
+ $expected = array_shift($expected_values);
+ $this->assertIdentical((string) $element['value'], $expected);
+ }
+ }
+
+ // Enable customized option sub-elements.
+ $this->drupalGet('form-test/checkboxes-radios/customize');
+
+ // Verify that all options appear in their defined order, taking a custom
+ // #weight into account.
+ foreach (array('checkbox', 'radio') as $type) {
+ $elements = $this->xpath('//input[@type=:type]', array(':type' => $type));
+ $expected_values = array('0', 'foo', 'bar', '1');
+ foreach ($elements as $element) {
+ $expected = array_shift($expected_values);
+ $this->assertIdentical((string) $element['value'], $expected);
+ }
+ }
+ // Verify that custom #description properties are output.
+ foreach (array('checkboxes', 'radios') as $type) {
+ $elements = $this->xpath('//input[@name=:name]/following-sibling::div[@class=:class]', array(
+ ':name' => $type . '[foo]',
+ ':class' => 'description',
+ ));
+ $this->assertTrue(count($elements), t('Custom %type option description found.', array(
+ '%type' => $type,
+ )));
+ }
+ }
+}
+
+/**
* Test form alter hooks.
*/
class FormAlterTestCase extends DrupalWebTestCase {
diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module
index 4d717c04b..abaae06c1 100644
--- a/modules/simpletest/tests/form_test.module
+++ b/modules/simpletest/tests/form_test.module
@@ -98,6 +98,12 @@ function form_test_menu() {
'page arguments' => array('form_test_select'),
'access callback' => TRUE,
);
+ $items['form-test/checkboxes-radios'] = array(
+ 'title' => t('Checkboxes, Radios'),
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('form_test_checkboxes_radios'),
+ 'access callback' => TRUE,
+ );
$items['form-test/disabled-elements'] = array(
'title' => t('Form test'),
@@ -187,6 +193,14 @@ function form_test_menu() {
}
/**
+ * Form submit handler to return form values as JSON.
+ */
+function _form_test_submit_values_json($form, &$form_state) {
+ drupal_json_output($form_state['values']);
+ drupal_exit();
+}
+
+/**
* Form builder for testing hook_form_alter() and hook_form_FORM_ID_alter().
*/
function form_test_alter_form($form, &$form_state) {
@@ -893,6 +907,63 @@ function form_test_select_submit($form, &$form_state) {
}
/**
+ * Form constructor to test expansion of #type checkboxes and radios.
+ */
+function form_test_checkboxes_radios($form, &$form_state, $customize = FALSE) {
+ $form['#submit'] = array('_form_test_submit_values_json');
+
+ // Expand #type checkboxes, setting custom element properties for some but not
+ // all options.
+ $form['checkboxes'] = array(
+ '#type' => 'checkboxes',
+ '#title' => 'Checkboxes',
+ '#options' => array(
+ 0 => 'Zero',
+ 'foo' => 'Foo',
+ 1 => 'One',
+ 'bar' => 'Bar',
+ ),
+ );
+ if ($customize) {
+ $form['checkboxes'] += array(
+ 'foo' => array(
+ '#description' => 'Enable to foo.',
+ ),
+ 1 => array(
+ '#weight' => 10,
+ ),
+ );
+ }
+
+ // Expand #type radios, setting custom element properties for some but not
+ // all options.
+ $form['radios'] = array(
+ '#type' => 'radios',
+ '#title' => 'Radios',
+ '#options' => array(
+ 0 => 'Zero',
+ 'foo' => 'Foo',
+ 1 => 'One',
+ 'bar' => 'Bar',
+ ),
+ );
+ if ($customize) {
+ $form['radios'] += array(
+ 'foo' => array(
+ '#description' => 'Enable to foo.',
+ ),
+ 1 => array(
+ '#weight' => 10,
+ ),
+ );
+ }
+
+ $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
+
+ return $form;
+}
+
+/**
* Build a form to test disabled elements.
*/
function _form_test_disabled_elements($form, &$form_state) {