You are here

function TriggeringElementTest::testNoButtonInfoInPost in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Form/TriggeringElementTest.php \Drupal\system\Tests\Form\TriggeringElementTest::testNoButtonInfoInPost()

Test the determination of the 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/system/src/Tests/Form/TriggeringElementTest.php, line 31
Contains \Drupal\system\Tests\Form\TriggeringElementTest.

Class

TriggeringElementTest
Tests that FAPI correctly determines the triggering element.

Namespace

Drupal\system\Tests\Form

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 triggering element
  // and the form submit handler not running.
  $this
    ->drupalPostForm($path, $edit, NULL, array(), array(), $form_html_id);
  $this
    ->assertText('There is no clicked button.', '$form_state->getTriggeringElement() 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 the
  // 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
    ->drupalPostForm($path . '/s', $edit, NULL, array(), array(), $form_html_id);
  $this
    ->assertText('The clicked button is button1.', '$form_state->getTriggeringElement() set to only button.');
  $this
    ->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
  $this
    ->drupalPostForm($path . '/s/s', $edit, NULL, array(), array(), $form_html_id);
  $this
    ->assertText('The clicked button is button1.', '$form_state->getTriggeringElement() set to first button.');
  $this
    ->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
  $this
    ->drupalPostForm($path . '/rs/s', $edit, NULL, array(), array(), $form_html_id);
  $this
    ->assertText('The clicked button is button2.', '$form_state->getTriggeringElement() 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 the
  // 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'(ubmit), 'b'(utton), and 'i'(mage_button).
  $this
    ->drupalPostForm($path . '/s/b/i', $edit, NULL, array(), array(), $form_html_id);
  $this
    ->assertText('The clicked button is button1.', '$form_state->getTriggeringElement() set to first button.');
  $this
    ->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
  $this
    ->drupalPostForm($path . '/b/s/i', $edit, NULL, array(), array(), $form_html_id);
  $this
    ->assertText('The clicked button is button1.', '$form_state->getTriggeringElement() set to first button.');
  $this
    ->assertNoText('Submit handler for form_test_clicked_button executed.', 'Form submit handler did not execute.');
  $this
    ->drupalPostForm($path . '/i/s/b', $edit, NULL, array(), array(), $form_html_id);
  $this
    ->assertText('The clicked button is button1.', '$form_state->getTriggeringElement() set to first button.');
  $this
    ->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
}