1 form.test FormsTestCase::testDisabledElements()

Test handling of disabled elements.

See also



core/modules/simpletest/tests/form.test, line 452
Unit tests for the Backdrop Form API.




function testDisabledElements() {
  // Get the raw form in its original state.
  $form_state = array();
  $form = _form_test_disabled_elements(array(), $form_state);

  // Build a submission that tries to hijack the form by submitting input for
  // elements that are disabled.
  $edit = array();
  foreach (element_children($form) as $key) {
    if (isset($form[$key]['#test_hijack_value'])) {
      if (is_array($form[$key]['#test_hijack_value'])) {
        foreach ($form[$key]['#test_hijack_value'] as $subkey => $value) {
          $edit[$key . '[' . $subkey . ']'] = $value;
      else {
        $edit[$key] = $form[$key]['#test_hijack_value'];

  // Submit the form with no input, as the browser does for disabled elements,
  // and fetch the $form_state['values'] that is passed to the submit handler.
  $this->backdropPost('form-test/disabled-elements', array(), t('Submit'));
  $returned_values['normal'] = backdrop_json_decode($this->content);

  // Do the same with input, as could happen if JavaScript un-disables an
  // element. backdropPost() emulates a browser by not submitting input for
  // disabled elements, so we need to un-disable those elements first.
  $disabled_elements = array();
  foreach ($this->xpath('//*[@disabled]') as $element) {
    $disabled_elements[] = (string) $element['name'];

  // All the elements should be marked as disabled, including the ones below
  // the disabled container.
  $this->assertEqual(count($disabled_elements), 39, 'The correct elements have the disabled property in the HTML code.');

  $this->backdropPost(NULL, $edit, t('Submit'));
  $returned_values['hijacked'] = backdrop_json_decode($this->content);

  // Ensure that the returned values match the form's default values in both
  // cases.
  foreach ($returned_values as $type => $values) {
    $this->assertFormValuesDefault($values, $form);