1 form.test FormsTriggeringElementTestCase::testNoButtonInfoInPost()

Test the determination of $form_state['triggering_element'] when no button information is included in the POST data, as is sometimes the case when the ENTER key is pressed in a textfield in Internet Explorer.

File

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

Class

FormsTriggeringElementTestCase
Test that FAPI correctly determines $form_state['triggering_element'].

Code

function testNoButtonInfoInPost() {
  $path = 'form-test/clicked-button';
  $edit = array();
  $form_html_id = 'form-test-clicked-button';

  // Ensure submitting a form with no buttons results in no
  // $form_state['triggering_element'] and the form submit handler not
  // running.
  $this->backdropPost($path, $edit, NULL, array(), array(), $form_html_id);
  $this->assertText('There is no clicked button.', '$form_state[\'triggering_element\'] set to NULL.');
  $this->assertNoText('Submit handler for form_test_clicked_button executed.', 'Form submit handler did not execute.');

  // Ensure submitting a form with one or more submit buttons results in
  // $form_state['triggering_element'] being set to the first one the user has
  // access to. An argument with 'r' in it indicates a restricted
  // (#access=FALSE) button.
  $this->backdropPost($path . '/s', $edit, NULL, array(), array(), $form_html_id);
  $this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to only button.');
  $this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');

  $this->backdropPost($path . '/s/s', $edit, NULL, array(), array(), $form_html_id);
  $this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to first button.');
  $this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');

  $this->backdropPost($path . '/rs/s', $edit, NULL, array(), array(), $form_html_id);
  $this->assertText('The clicked button is button2.', '$form_state[\'triggering_element\'] set to first available button.');
  $this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');

  // Ensure submitting a form with buttons of different types results in
  // $form_state['triggering_element'] being set to the first button,
  // regardless of type. For the FAPI 'button' type, this should result in the
  // submit handler not executing. The types are 's' (submit), 'b' (button),
  // and 'i' (image_button).
  $this->backdropPost($path . '/s/b/i', $edit, NULL, array(), array(), $form_html_id);
  $this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to first button.');
  $this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');

  $this->backdropPost($path . '/b/s/i', $edit, NULL, array(), array(), $form_html_id);
  $this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to first button.');
  $this->assertNoText('Submit handler for form_test_clicked_button executed.', 'Form submit handler did not execute.');

  $this->backdropPost($path . '/i/s/b', $edit, NULL, array(), array(), $form_html_id);
  $this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to first button.');
  $this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
}