You are here

class FormTest in Drupal 8

Same name in this branch
  1. 8 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest
  2. 8 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest
Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest
  2. 10 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest

Tests various form element validation mechanisms.

@group Form

Hierarchy

Expanded class hierarchy of FormTest

File

core/modules/system/tests/src/Functional/Form/FormTest.php, line 22

Namespace

Drupal\Tests\system\Functional\Form
View source
class FormTest extends BrowserTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'filter',
    'form_test',
    'file',
    'datetime',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';
  protected function setUp() {
    parent::setUp();
    $filtered_html_format = FilterFormat::create([
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
    ]);
    $filtered_html_format
      ->save();
    $filtered_html_permission = $filtered_html_format
      ->getPermissionName();
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
      $filtered_html_permission,
    ]);
  }

  /**
   * Check several empty values for required forms elements.
   *
   * Carriage returns, tabs, spaces, and unchecked checkbox elements are not
   * valid content for a required field.
   *
   * If the form field is found in $form_state->getErrors() then the test pass.
   */
  public function testRequiredFields() {

    // Originates from https://www.drupal.org/node/117748.
    // Sets of empty strings and arrays.
    $empty_strings = [
      '""' => "",
      '"\\n"' => "\n",
      '" "' => " ",
      '"\\t"' => "\t",
      '" \\n\\t "' => " \n\t ",
      '"\\n\\n\\n\\n\\n"' => "\n\n\n\n\n",
    ];
    $empty_arrays = [
      'array()' => [],
    ];
    $empty_checkbox = [
      NULL,
    ];
    $elements['textfield']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'textfield',
    ];
    $elements['textfield']['empty_values'] = $empty_strings;
    $elements['telephone']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'tel',
    ];
    $elements['telephone']['empty_values'] = $empty_strings;
    $elements['url']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'url',
    ];
    $elements['url']['empty_values'] = $empty_strings;
    $elements['search']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'search',
    ];
    $elements['search']['empty_values'] = $empty_strings;
    $elements['password']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'password',
    ];
    $elements['password']['empty_values'] = $empty_strings;
    $elements['password_confirm']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'password_confirm',
    ];

    // Provide empty values for both password fields.
    foreach ($empty_strings as $key => $value) {
      $elements['password_confirm']['empty_values'][$key] = [
        'pass1' => $value,
        'pass2' => $value,
      ];
    }
    $elements['textarea']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'textarea',
    ];
    $elements['textarea']['empty_values'] = $empty_strings;
    $elements['radios']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'radios',
      '#options' => [
        '' => t('None'),
        $this
          ->randomMachineName(),
        $this
          ->randomMachineName(),
        $this
          ->randomMachineName(),
      ],
    ];
    $elements['radios']['empty_values'] = $empty_arrays;
    $elements['checkbox']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'checkbox',
      '#required' => TRUE,
    ];
    $elements['checkbox']['empty_values'] = $empty_checkbox;
    $elements['checkboxes']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'checkboxes',
      '#options' => [
        $this
          ->randomMachineName(),
        $this
          ->randomMachineName(),
        $this
          ->randomMachineName(),
      ],
    ];
    $elements['checkboxes']['empty_values'] = $empty_arrays;
    $elements['select']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'select',
      '#options' => [
        '' => t('None'),
        $this
          ->randomMachineName(),
        $this
          ->randomMachineName(),
        $this
          ->randomMachineName(),
      ],
    ];
    $elements['select']['empty_values'] = $empty_strings;
    $elements['file']['element'] = [
      '#title' => $this
        ->randomMachineName(),
      '#type' => 'file',
    ];
    $elements['file']['empty_values'] = $empty_strings;

    // Regular expression to find the expected marker on required elements.
    $required_marker_preg = '@<.*?class=".*?js-form-required.*form-required.*?">@';

    // Go through all the elements and all the empty values for them.
    foreach ($elements as $type => $data) {
      foreach ($data['empty_values'] as $key => $empty) {
        foreach ([
          TRUE,
          FALSE,
        ] as $required) {
          $form_id = $this
            ->randomMachineName();
          $form = [];
          $form_state = new FormState();
          $form['op'] = [
            '#type' => 'submit',
            '#value' => t('Submit'),
          ];
          $element = $data['element']['#title'];
          $form[$element] = $data['element'];
          $form[$element]['#required'] = $required;
          $user_input[$element] = $empty;
          $user_input['form_id'] = $form_id;
          $form_state
            ->setUserInput($user_input);
          $form_state
            ->setFormObject(new StubForm($form_id, $form));
          $form_state
            ->setMethod('POST');

          // The form token CSRF protection should not interfere with this test,
          // so we bypass it by setting the token to FALSE.
          $form['#token'] = FALSE;
          \Drupal::formBuilder()
            ->prepareForm($form_id, $form, $form_state);
          \Drupal::formBuilder()
            ->processForm($form_id, $form, $form_state);
          $errors = $form_state
            ->getErrors();

          // Form elements of type 'radios' throw all sorts of PHP notices
          // when you try to render them like this, so we ignore those for
          // testing the required marker.
          // @todo Fix this work-around (https://www.drupal.org/node/588438).
          $form_output = $type == 'radios' ? '' : \Drupal::service('renderer')
            ->renderRoot($form);
          if ($required) {

            // Make sure we have a form error for this element.
            $this
              ->assertTrue(isset($errors[$element]), "Check empty({$key}) '{$type}' field '{$element}'");
            if (!empty($form_output)) {

              // Make sure the form element is marked as required.
              $this
                ->assertRegExp($required_marker_preg, (string) $form_output, "Required '{$type}' field is marked as required");
            }
          }
          else {
            if (!empty($form_output)) {

              // Make sure the form element is *not* marked as required.
              $this
                ->assertNotRegExp($required_marker_preg, (string) $form_output, "Optional '{$type}' field is not marked as required");
            }
            if ($type == 'select') {

              // Select elements are going to have validation errors with empty
              // input, since those are illegal choices. Just make sure the
              // error is not "field is required".
              $this
                ->assertTrue(empty($errors[$element]) || strpos('field is required', (string) $errors[$element]) === FALSE, "Optional '{$type}' field '{$element}' is not treated as a required element");
            }
            else {

              // Make sure there is *no* form error for this element. We're
              // not using assertEmpty() because the array key might not exist.
              $this
                ->assertTrue(empty($errors[$element]), "Optional '{$type}' field '{$element}' has no errors with empty input");
            }
          }
        }
      }
    }

    // Clear the expected form error messages so they don't appear as exceptions.
    \Drupal::messenger()
      ->deleteAll();
  }

  /**
   * Tests validation for required checkbox, select, and radio elements.
   *
   * Submits a test form containing several types of form elements. The form
   * is submitted twice, first without values for required fields and then
   * with values. Each submission is checked for relevant error messages.
   *
   * @see \Drupal\form_test\Form\FormTestValidateRequiredForm
   */
  public function testRequiredCheckboxesRadio() {
    $form = \Drupal::formBuilder()
      ->getForm('\\Drupal\\form_test\\Form\\FormTestValidateRequiredForm');

    // Attempt to submit the form with no required fields set.
    $edit = [];
    $this
      ->drupalPostForm('form-test/validate-required', $edit, 'Submit');

    // The only error messages that should appear are the relevant 'required'
    // messages for each field.
    $expected = [];
    foreach ([
      'textfield',
      'checkboxes',
      'select',
      'radios',
    ] as $key) {
      if (isset($form[$key]['#required_error'])) {
        $expected[] = $form[$key]['#required_error'];
      }
      elseif (isset($form[$key]['#form_test_required_error'])) {
        $expected[] = $form[$key]['#form_test_required_error'];
      }
      else {
        $expected[] = t('@name field is required.', [
          '@name' => $form[$key]['#title'],
        ]);
      }
    }

    // Check the page for error messages.
    $errors = $this
      ->xpath('//div[contains(@class, "error")]//li');
    foreach ($errors as $error) {
      $expected_key = array_search($error
        ->getText(), $expected);

      // If the error message is not one of the expected messages, fail.
      if ($expected_key === FALSE) {
        $this
          ->fail(new FormattableMarkup("Unexpected error message: @error", [
          '@error' => $error[0],
        ]));
      }
      else {
        unset($expected[$expected_key]);
      }
    }

    // Fail if any expected messages were not found.
    foreach ($expected as $not_found) {
      $this
        ->fail(new FormattableMarkup("Found error message: @error", [
        '@error' => $not_found,
      ]));
    }

    // Verify that input elements are still empty.
    $this
      ->assertFieldByName('textfield', '');
    $this
      ->assertNoFieldChecked('edit-checkboxes-foo');
    $this
      ->assertNoFieldChecked('edit-checkboxes-bar');
    $this
      ->assertOptionSelected('edit-select', '');
    $this
      ->assertNoFieldChecked('edit-radios-foo');
    $this
      ->assertNoFieldChecked('edit-radios-bar');
    $this
      ->assertNoFieldChecked('edit-radios-optional-foo');
    $this
      ->assertNoFieldChecked('edit-radios-optional-bar');
    $this
      ->assertNoFieldChecked('edit-radios-optional-default-value-false-foo');
    $this
      ->assertNoFieldChecked('edit-radios-optional-default-value-false-bar');

    // Submit again with required fields set and verify that there are no
    // error messages.
    $edit = [
      'textfield' => $this
        ->randomString(),
      'checkboxes[foo]' => TRUE,
      'select' => 'foo',
      'radios' => 'bar',
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Submit');
    $this
      ->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed when all required fields are filled.');
    $this
      ->assertRaw("The form_test_validate_required_form form was submitted successfully.", 'Validation form submitted successfully.');
  }

  /**
   * Tests that input is retained for safe elements even with an invalid token.
   *
   * Submits a test form containing several types of form elements.
   */
  public function testInputWithInvalidToken() {

    // We need to be logged in to have CSRF tokens.
    $account = $this
      ->createUser();
    $this
      ->drupalLogin($account);

    // Submit again with required fields set but an invalid form token and
    // verify that all the values are retained.
    $this
      ->drupalGet(Url::fromRoute('form_test.validate_required'));
    $this
      ->assertSession()
      ->elementExists('css', 'input[name="form_token"]')
      ->setValue('invalid token');
    $random_string = $this
      ->randomString();
    $edit = [
      'textfield' => $random_string,
      'checkboxes[bar]' => TRUE,
      'select' => 'bar',
      'radios' => 'foo',
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Submit');
    $this
      ->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.');
    $assert = $this
      ->assertSession();
    $element = $assert
      ->fieldExists('textfield');
    $this
      ->assertEmpty($element
      ->getValue());
    $assert
      ->responseNotContains($random_string);
    $this
      ->assertText('The form has become outdated.');

    // Ensure that we don't use the posted values.
    $this
      ->assertFieldByName('textfield', '');
    $this
      ->assertNoFieldChecked('edit-checkboxes-foo');
    $this
      ->assertNoFieldChecked('edit-checkboxes-bar');
    $this
      ->assertOptionSelected('edit-select', '');
    $this
      ->assertNoFieldChecked('edit-radios-foo');

    // Check another form that has a textarea input.
    $this
      ->drupalGet(Url::fromRoute('form_test.required'));
    $this
      ->assertSession()
      ->elementExists('css', 'input[name="form_token"]')
      ->setValue('invalid token');
    $edit = [
      'textfield' => $this
        ->randomString(),
      'textarea' => $this
        ->randomString() . "\n",
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Submit');
    $this
      ->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.');
    $this
      ->assertText('The form has become outdated.');
    $this
      ->assertFieldByName('textfield', '');
    $this
      ->assertFieldByName('textarea', '');

    // Check another form that has a number input.
    $this
      ->drupalGet(Url::fromRoute('form_test.number'));
    $this
      ->assertSession()
      ->elementExists('css', 'input[name="form_token"]')
      ->setValue('invalid token');
    $edit = [
      // We choose a random value which is higher than the default value,
      // so we don't accidentally generate the default value.
      'integer_step' => mt_rand(6, 100),
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Submit');
    $this
      ->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.');
    $this
      ->assertText('The form has become outdated.');
    $this
      ->assertFieldByName('integer_step', 5);

    // Check a form with a Url field
    $this
      ->drupalGet(Url::fromRoute('form_test.url'));
    $this
      ->assertSession()
      ->elementExists('css', 'input[name="form_token"]')
      ->setValue('invalid token');
    $edit = [
      'url' => $this
        ->randomString(),
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Submit');
    $this
      ->assertFieldByXpath('//div[contains(@class, "error")]', NULL, 'Error message is displayed with invalid token even when required fields are filled.');
    $this
      ->assertText('The form has become outdated.');
    $this
      ->assertFieldByName('url', '');
  }

  /**
   * CSRF tokens for GET forms should not be added by default.
   */
  public function testGetFormsCsrfToken() {

    // We need to be logged in to have CSRF tokens.
    $account = $this
      ->createUser();
    $this
      ->drupalLogin($account);
    $this
      ->drupalGet(Url::fromRoute('form_test.get_form'));
    $this
      ->assertNoRaw('form_token');
  }

  /**
   * Tests validation for required textfield element without title.
   *
   * Submits a test form containing a textfield form element without title.
   * The form is submitted twice, first without value for the required field
   * and then with value. Each submission is checked for relevant error
   * messages.
   *
   * @see \Drupal\form_test\Form\FormTestValidateRequiredNoTitleForm
   */
  public function testRequiredTextfieldNoTitle() {

    // Attempt to submit the form with no required field set.
    $edit = [];
    $this
      ->drupalPostForm('form-test/validate-required-no-title', $edit, 'Submit');
    $this
      ->assertNoRaw("The form_test_validate_required_form_no_title form was submitted successfully.", 'Validation form submitted successfully.');

    // Check the page for the error class on the textfield.
    $this
      ->assertFieldByXPath('//input[contains(@class, "error")]', FALSE, 'Error input form element class found.');

    // Check the page for the aria-invalid attribute on the textfield.
    $this
      ->assertFieldByXPath('//input[contains(@aria-invalid, "true")]', FALSE, 'Aria invalid attribute found.');

    // Submit again with required fields set and verify that there are no
    // error messages.
    $edit = [
      'textfield' => $this
        ->randomString(),
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Submit');
    $this
      ->assertNoFieldByXpath('//input[contains(@class, "error")]', FALSE, 'No error input form element class found.');
    $this
      ->assertRaw("The form_test_validate_required_form_no_title form was submitted successfully.", 'Validation form submitted successfully.');
  }

  /**
   * Test default value handling for checkboxes.
   *
   * @see _form_test_checkbox()
   */
  public function testCheckboxProcessing() {

    // First, try to submit without the required checkbox.
    $edit = [];
    $this
      ->drupalPostForm('form-test/checkbox', $edit, t('Submit'));
    $this
      ->assertRaw(t('@name field is required.', [
      '@name' => 'required_checkbox',
    ]), 'A required checkbox is actually mandatory');

    // Now try to submit the form correctly.
    $this
      ->drupalPostForm(NULL, [
      'required_checkbox' => 1,
    ], t('Submit'));
    $values = Json::decode($this
      ->getSession()
      ->getPage()
      ->getContent());
    $expected_values = [
      'disabled_checkbox_on' => 'disabled_checkbox_on',
      'disabled_checkbox_off' => 0,
      'checkbox_on' => 'checkbox_on',
      'checkbox_off' => 0,
      'zero_checkbox_on' => '0',
      'zero_checkbox_off' => 0,
    ];
    foreach ($expected_values as $widget => $expected_value) {
      $this
        ->assertSame($values[$widget], $expected_value, new FormattableMarkup('Checkbox %widget returns expected value (expected: %expected, got: %value)', [
        '%widget' => var_export($widget, TRUE),
        '%expected' => var_export($expected_value, TRUE),
        '%value' => var_export($values[$widget], TRUE),
      ]));
    }
  }

  /**
   * Tests validation of #type 'select' elements.
   */
  public function testSelect() {
    $form = \Drupal::formBuilder()
      ->getForm('Drupal\\form_test\\Form\\FormTestSelectForm');
    $this
      ->drupalGet('form-test/select');

    // Verify that the options are escaped as expected.
    $this
      ->assertEscaped('<strong>four</strong>');
    $this
      ->assertNoRaw('<strong>four</strong>');

    // Posting without any values should throw validation errors.
    $this
      ->drupalPostForm(NULL, [], 'Submit');
    $no_errors = [
      'select',
      'select_required',
      'select_optional',
      'empty_value',
      'empty_value_one',
      'no_default_optional',
      'no_default_empty_option_optional',
      'no_default_empty_value_optional',
      'multiple',
      'multiple_no_default',
    ];
    foreach ($no_errors as $key) {
      $this
        ->assertNoText(t('@name field is required.', [
        '@name' => $form[$key]['#title'],
      ]));
    }
    $expected_errors = [
      'no_default',
      'no_default_empty_option',
      'no_default_empty_value',
      'no_default_empty_value_one',
      'multiple_no_default_required',
    ];
    foreach ($expected_errors as $key) {
      $this
        ->assertText(t('@name field is required.', [
        '@name' => $form[$key]['#title'],
      ]));
    }

    // Post values for required fields.
    $edit = [
      'no_default' => 'three',
      'no_default_empty_option' => 'three',
      'no_default_empty_value' => 'three',
      'no_default_empty_value_one' => 'three',
      'multiple_no_default_required[]' => 'three',
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Submit');
    $values = Json::decode($this
      ->getSession()
      ->getPage()
      ->getContent());

    // Verify expected values.
    $expected = [
      'select' => 'one',
      'empty_value' => 'one',
      'empty_value_one' => 'one',
      'no_default' => 'three',
      'no_default_optional' => 'one',
      'no_default_optional_empty_value' => '',
      'no_default_empty_option' => 'three',
      'no_default_empty_option_optional' => '',
      'no_default_empty_value' => 'three',
      'no_default_empty_value_one' => 'three',
      'no_default_empty_value_optional' => 0,
      'multiple' => [
        'two' => 'two',
      ],
      'multiple_no_default' => [],
      'multiple_no_default_required' => [
        'three' => 'three',
      ],
    ];
    foreach ($expected as $key => $value) {
      $this
        ->assertIdentical($values[$key], $value, new FormattableMarkup('@name: @actual is equal to @expected.', [
        '@name' => $key,
        '@actual' => var_export($values[$key], TRUE),
        '@expected' => var_export($value, TRUE),
      ]));
    }
  }

  /**
   * Tests a select element when #options is not set.
   */
  public function testEmptySelect() {
    $this
      ->drupalGet('form-test/empty-select');
    $this
      ->assertFieldByXPath("//select[1]", NULL, 'Select element found.');
    $this
      ->assertNoFieldByXPath("//select[1]/option", NULL, 'No option element found.');
  }

  /**
   * Tests sorting and not sorting of options in a select element.
   */
  public function testSelectSorting() {
    $this
      ->drupalGet('form-test/select');

    // Verify the order of the select options.
    $this
      ->validateSelectSorting('unsorted', [
      'uso_first_element',
      'uso_second',
      'uso_zzgroup',
      'uso_gc',
      'uso_ga',
      'uso_gb',
      'uso_yygroup',
      'uso_ge',
      'uso_gd',
      'uso_gf',
      'uso_xxgroup',
      'uso_gz',
      'uso_gi',
      'uso_gh',
      'uso_d',
      'uso_c',
      'uso_b',
      'uso_a',
    ]);
    $this
      ->validateSelectSorting('sorted', [
      'sso_a',
      'sso_d',
      'sso_first_element',
      'sso_b',
      'sso_c',
      'sso_second',
      'sso_xxgroup',
      'sso_gz',
      'sso_gh',
      'sso_gi',
      'sso_yygroup',
      'sso_ge',
      'sso_gd',
      'sso_gf',
      'sso_zzgroup',
      'sso_ga',
      'sso_gb',
      'sso_gc',
    ]);
    $this
      ->validateSelectSorting('sorted_none', [
      'sno_empty',
      'sno_first_element',
      'sno_second',
      'sno_zzgroup',
      'sno_ga',
      'sno_gb',
      'sno_gc',
      'sno_a',
      'sno_d',
      'sno_b',
      'sno_c',
      'sno_xxgroup',
      'sno_gz',
      'sno_gi',
      'sno_gh',
      'sno_yygroup',
      'sno_ge',
      'sno_gd',
      'sno_gf',
    ]);
    $this
      ->validateSelectSorting('sorted_none_nostart', [
      'snn_empty',
      'snn_a',
      'snn_d',
      'snn_first_element',
      'snn_b',
      'snn_c',
      'snn_second',
      'snn_xxgroup',
      'snn_gz',
      'snn_gi',
      'snn_gh',
      'snn_yygroup',
      'snn_ge',
      'snn_gd',
      'snn_gf',
      'snn_zzgroup',
      'snn_ga',
      'snn_gb',
      'snn_gc',
    ]);

    // Verify that #sort_order and #sort_start are not in the page.
    $this
      ->assertSession()
      ->responseNotContains('#sort_order');
    $this
      ->assertSession()
      ->responseNotContains('#sort_start');
  }

  /**
   * Validates that the options are in the right order in a select.
   *
   * @param string $select
   *   Name of the select to verify.
   * @param string[] $order
   *   Expected order of its options.
   */
  protected function validateSelectSorting($select, array $order) {
    $option_map_function = function (NodeElement $node) {
      return $node
        ->getTagName() === 'optgroup' ? $node
        ->getAttribute('label') : $node
        ->getValue();
    };
    $option_nodes = $this
      ->getSession()
      ->getPage()
      ->findField($select)
      ->findAll('css', 'option, optgroup');
    $options = array_map($option_map_function, $option_nodes);
    $this
      ->assertIdentical($order, $options);
  }

  /**
   * Tests validation of #type 'number' and 'range' elements.
   */
  public function testNumber() {
    $form = \Drupal::formBuilder()
      ->getForm('\\Drupal\\form_test\\Form\\FormTestNumberForm');

    // Array with all the error messages to be checked.
    $error_messages = [
      'no_number' => '%name must be a number.',
      'too_low' => '%name must be higher than or equal to %min.',
      'too_high' => '%name must be lower than or equal to %max.',
      'step_mismatch' => '%name is not a valid number.',
    ];

    // The expected errors.
    $expected = [
      'integer_no_number' => 'no_number',
      'integer_no_step' => 0,
      'integer_no_step_step_error' => 'step_mismatch',
      'integer_step' => 0,
      'integer_step_error' => 'step_mismatch',
      'integer_step_min' => 0,
      'integer_step_min_error' => 'too_low',
      'integer_step_max' => 0,
      'integer_step_max_error' => 'too_high',
      'integer_step_min_border' => 0,
      'integer_step_max_border' => 0,
      'integer_step_based_on_min' => 0,
      'integer_step_based_on_min_error' => 'step_mismatch',
      'float_small_step' => 0,
      'float_step_no_error' => 0,
      'float_step_error' => 'step_mismatch',
      'float_step_hard_no_error' => 0,
      'float_step_hard_error' => 'step_mismatch',
      'float_step_any_no_error' => 0,
    ];

    // First test the number element type, then range.
    foreach ([
      'form-test/number',
      'form-test/number/range',
    ] as $path) {

      // Post form and show errors.
      $this
        ->drupalPostForm($path, [], 'Submit');
      foreach ($expected as $element => $error) {

        // Create placeholder array.
        $placeholders = [
          '%name' => $form[$element]['#title'],
          '%min' => isset($form[$element]['#min']) ? $form[$element]['#min'] : '0',
          '%max' => isset($form[$element]['#max']) ? $form[$element]['#max'] : '0',
        ];
        foreach ($error_messages as $id => $message) {

          // Check if the error exists on the page, if the current message ID is
          // expected. Otherwise ensure that the error message is not present.
          if ($id === $error) {
            $this
              ->assertRaw(new FormattableMarkup($message, $placeholders));
          }
          else {
            $this
              ->assertNoRaw(new FormattableMarkup($message, $placeholders));
          }
        }
      }
    }
  }

  /**
   * Tests default value handling of #type 'range' elements.
   */
  public function testRange() {
    $this
      ->drupalPostForm('form-test/range', [], 'Submit');
    $values = json_decode($this
      ->getSession()
      ->getPage()
      ->getContent());
    $this
      ->assertEqual($values->with_default_value, 18);
    $this
      ->assertEqual($values->float, 10.5);
    $this
      ->assertEqual($values->integer, 6);
    $this
      ->assertEqual($values->offset, 6.9);
    $this
      ->drupalPostForm('form-test/range/invalid', [], 'Submit');
    $this
      ->assertFieldByXPath('//input[@type="range" and contains(@class, "error")]', NULL, 'Range element has the error class.');
  }

  /**
   * Tests validation of #type 'color' elements.
   */
  public function testColorValidation() {

    // Keys are inputs, values are expected results.
    $values = [
      '' => '#000000',
      '#000' => '#000000',
      'AAA' => '#aaaaaa',
      '#af0DEE' => '#af0dee',
      '#99ccBc' => '#99ccbc',
      '#aabbcc' => '#aabbcc',
      '123456' => '#123456',
    ];

    // Tests that valid values are properly normalized.
    foreach ($values as $input => $expected) {
      $edit = [
        'color' => $input,
      ];
      $this
        ->drupalPostForm('form-test/color', $edit, 'Submit');
      $result = json_decode($this
        ->getSession()
        ->getPage()
        ->getContent());
      $this
        ->assertEqual($result->color, $expected);
    }

    // Tests invalid values are rejected.
    $values = [
      '#0008',
      '#1234',
      '#fffffg',
      '#abcdef22',
      '17',
      '#uaa',
    ];
    foreach ($values as $input) {
      $edit = [
        'color' => $input,
      ];
      $this
        ->drupalPostForm('form-test/color', $edit, 'Submit');
      $this
        ->assertRaw(t('%name must be a valid color.', [
        '%name' => 'Color',
      ]));
    }
  }

  /**
   * Test handling of disabled elements.
   *
   * @see _form_test_disabled_elements()
   */
  public function testDisabledElements() {

    // Get the raw form in its original state.
    $form_state = new FormState();
    $form = (new FormTestDisabledElementsForm())
      ->buildForm([], $form_state);

    // Build a submission that tries to hijack the form by submitting input for
    // elements that are disabled.
    $edit = [];
    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->getValues() that is passed to the submit handler.
    $this
      ->drupalPostForm('form-test/disabled-elements', [], t('Submit'));
    $returned_values['normal'] = Json::decode($this
      ->getSession()
      ->getPage()
      ->getContent());

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

    // All the elements should be marked as disabled, including the ones below
    // the disabled container.
    $actual_count = count($disabled_elements);
    $expected_count = 42;
    $this
      ->assertEqual($actual_count, $expected_count, new FormattableMarkup('Found @actual elements with disabled property (expected @expected).', [
      '@actual' => count($disabled_elements),
      '@expected' => $expected_count,
    ]));

    // Mink does not "see" hidden elements, so we need to set the value of the
    // hidden element directly.
    $this
      ->assertSession()
      ->elementExists('css', 'input[name="hidden"]')
      ->setValue($edit['hidden']);
    unset($edit['hidden']);
    $this
      ->drupalPostForm(NULL, $edit, t('Submit'));
    $returned_values['hijacked'] = Json::decode($this
      ->getSession()
      ->getPage()
      ->getContent());

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

  /**
   * Assert that the values submitted to a form matches the default values of the elements.
   */
  public function assertFormValuesDefault($values, $form) {
    foreach (Element::children($form) as $key) {
      if (isset($form[$key]['#default_value'])) {
        if (isset($form[$key]['#expected_value'])) {
          $expected_value = $form[$key]['#expected_value'];
        }
        else {
          $expected_value = $form[$key]['#default_value'];
        }
        if ($key == 'checkboxes_multiple') {

          // Checkboxes values are not filtered out.
          $values[$key] = array_filter($values[$key]);
        }
        $this
          ->assertIdentical($expected_value, $values[$key], new FormattableMarkup('Default value for %type: expected %expected, returned %returned.', [
          '%type' => $key,
          '%expected' => var_export($expected_value, TRUE),
          '%returned' => var_export($values[$key], TRUE),
        ]));
      }

      // Recurse children.
      $this
        ->assertFormValuesDefault($values, $form[$key]);
    }
  }

  /**
   * Verify markup for disabled form elements.
   *
   * @see _form_test_disabled_elements()
   */
  public function testDisabledMarkup() {
    $this
      ->drupalGet('form-test/disabled-elements');
    $form = \Drupal::formBuilder()
      ->getForm('\\Drupal\\form_test\\Form\\FormTestDisabledElementsForm');
    $type_map = [
      'textarea' => 'textarea',
      'select' => 'select',
      'weight' => 'select',
      'datetime' => 'datetime',
    ];
    foreach ($form as $name => $item) {

      // Skip special #types.
      if (!isset($item['#type']) || in_array($item['#type'], [
        'hidden',
        'text_format',
      ])) {
        continue;
      }

      // Setup XPath and CSS class depending on #type.
      if (in_array($item['#type'], [
        'button',
        'submit',
      ])) {
        $path = "//!type[contains(@class, :div-class) and @value=:value]";
        $class = 'is-disabled';
      }
      elseif (in_array($item['#type'], [
        'image_button',
      ])) {
        $path = "//!type[contains(@class, :div-class) and @value=:value]";
        $class = 'is-disabled';
      }
      else {

        // starts-with() required for checkboxes.
        $path = "//div[contains(@class, :div-class)]/descendant::!type[starts-with(@name, :name)]";
        $class = 'form-disabled';
      }

      // Replace DOM element name in $path according to #type.
      $type = 'input';
      if (isset($type_map[$item['#type']])) {
        $type = $type_map[$item['#type']];
      }
      if (isset($item['#value']) && is_object($item['#value'])) {
        $item['#value'] = (string) $item['#value'];
      }
      $path = strtr($path, [
        '!type' => $type,
      ]);

      // Verify that the element exists.
      $element = $this
        ->xpath($path, [
        ':name' => Html::escape($name),
        ':div-class' => $class,
        ':value' => isset($item['#value']) ? $item['#value'] : '',
      ]);
      $this
        ->assertTrue(isset($element[0]), new FormattableMarkup('Disabled form element class found for #type %type.', [
        '%type' => $item['#type'],
      ]));
    }

    // Verify special element #type text-format.
    $element = $this
      ->xpath('//div[contains(@class, :div-class)]/descendant::textarea[@name=:name]', [
      ':name' => 'text_format[value]',
      ':div-class' => 'form-disabled',
    ]);
    $this
      ->assertTrue(isset($element[0]), new FormattableMarkup('Disabled form element class found for #type %type.', [
      '%type' => 'text_format[value]',
    ]));
    $element = $this
      ->xpath('//div[contains(@class, :div-class)]/descendant::select[@name=:name]', [
      ':name' => 'text_format[format]',
      ':div-class' => 'form-disabled',
    ]);
    $this
      ->assertTrue(isset($element[0]), new FormattableMarkup('Disabled form element class found for #type %type.', [
      '%type' => 'text_format[format]',
    ]));
  }

  /**
   * Test Form API protections against input forgery.
   *
   * @see \Drupal\form_test\Form\FormTestInputForgeryForm
   */
  public function testInputForgery() {
    $this
      ->drupalGet('form-test/input-forgery');

    // The value for checkboxes[two] was changed using post render to simulate
    // an input forgery.
    // @see \Drupal\form_test\Form\FormTestInputForgeryForm::postRender
    $this
      ->drupalPostForm(NULL, [
      'checkboxes[one]' => TRUE,
      'checkboxes[two]' => TRUE,
    ], t('Submit'));
    $this
      ->assertText('An illegal choice has been detected.', 'Input forgery was detected.');
  }

  /**
   * Tests required attribute.
   */
  public function testRequiredAttribute() {
    $this
      ->drupalGet('form-test/required-attribute');
    $expected = 'required';

    // Test to make sure the elements have the proper required attribute.
    foreach ([
      'textfield',
      'password',
    ] as $type) {
      $element = $this
        ->xpath('//input[@id=:id and @required=:expected]', [
        ':id' => 'edit-' . $type,
        ':expected' => $expected,
      ]);
      $this
        ->assertTrue(!empty($element), new FormattableMarkup('The @type has the proper required attribute.', [
        '@type' => $type,
      ]));
    }

    // Test to make sure textarea has the proper required attribute.
    $element = $this
      ->xpath('//textarea[@id=:id and @required=:expected]', [
      ':id' => 'edit-textarea',
      ':expected' => $expected,
    ]);
    $this
      ->assertTrue(!empty($element), 'The textarea has the proper required attribute.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertHelperTrait::castSafeStrings protected static function Casts MarkupInterface objects into strings.
AssertLegacyTrait::assert protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertTrue() instead.
AssertLegacyTrait::assertCacheTag protected function Asserts whether an expected cache tag was present in the last response.
AssertLegacyTrait::assertElementNotPresent protected function Asserts that the element with the given CSS selector is not present.
AssertLegacyTrait::assertElementPresent protected function Asserts that the element with the given CSS selector is present.
AssertLegacyTrait::assertEqual protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertEquals() instead.
AssertLegacyTrait::assertEscaped protected function Passes if the raw text IS found escaped on the loaded page, fail otherwise.
AssertLegacyTrait::assertField protected function Asserts that a field exists with the given name or ID.
AssertLegacyTrait::assertFieldById protected function Asserts that a field exists with the given ID and value.
AssertLegacyTrait::assertFieldByName protected function Asserts that a field exists with the given name and value.
AssertLegacyTrait::assertFieldByXPath protected function Asserts that a field exists in the current page by the given XPath.
AssertLegacyTrait::assertFieldChecked protected function Asserts that a checkbox field in the current page is checked.
AssertLegacyTrait::assertFieldsByValue protected function Asserts that a field exists in the current page with a given Xpath result.
AssertLegacyTrait::assertHeader protected function Checks that current response header equals value.
AssertLegacyTrait::assertIdentical protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertSame() instead.
AssertLegacyTrait::assertIdenticalObject protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertEquals() instead.
AssertLegacyTrait::assertLink protected function Passes if a link with the specified label is found.
AssertLegacyTrait::assertLinkByHref protected function Passes if a link containing a given href (part) is found.
AssertLegacyTrait::assertNoCacheTag protected function Asserts whether an expected cache tag was absent in the last response.
AssertLegacyTrait::assertNoEscaped protected function Passes if the raw text is not found escaped on the loaded page.
AssertLegacyTrait::assertNoField protected function Asserts that a field does NOT exist with the given name or ID.
AssertLegacyTrait::assertNoFieldById protected function Asserts that a field does not exist with the given ID and value.
AssertLegacyTrait::assertNoFieldByName protected function Asserts that a field does not exist with the given name and value.
AssertLegacyTrait::assertNoFieldByXPath protected function Asserts that a field does not exist or its value does not match, by XPath.
AssertLegacyTrait::assertNoFieldChecked protected function Asserts that a checkbox field in the current page is not checked.
AssertLegacyTrait::assertNoLink protected function Passes if a link with the specified label is not found.
AssertLegacyTrait::assertNoLinkByHref protected function Passes if a link containing a given href (part) is not found.
AssertLegacyTrait::assertNoOption protected function Asserts that a select option does NOT exist in the current page.
AssertLegacyTrait::assertNoPattern protected function Triggers a pass if the Perl regex pattern is not found in the raw content.
AssertLegacyTrait::assertNoRaw protected function Passes if the raw text IS not found on the loaded page, fail otherwise. 1
AssertLegacyTrait::assertNotEqual protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertNotEquals() instead.
AssertLegacyTrait::assertNoText protected function Passes if the page (with HTML stripped) does not contains the text. 1
AssertLegacyTrait::assertNotIdentical protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertNotSame() instead.
AssertLegacyTrait::assertNoUniqueText protected function Passes if the text is found MORE THAN ONCE on the text version of the page.
AssertLegacyTrait::assertOption protected function Asserts that a select option in the current page exists.
AssertLegacyTrait::assertOptionByText protected function Asserts that a select option with the visible text exists.
AssertLegacyTrait::assertOptionSelected protected function Asserts that a select option in the current page is checked.
AssertLegacyTrait::assertPattern protected function Triggers a pass if the Perl regex pattern is found in the raw content.
AssertLegacyTrait::assertRaw protected function Passes if the raw text IS found on the loaded page, fail otherwise. 1
AssertLegacyTrait::assertResponse protected function Asserts the page responds with the specified response code. 1
AssertLegacyTrait::assertText protected function Passes if the page (with HTML stripped) contains the text. 1
AssertLegacyTrait::assertTextHelper protected function Helper for assertText and assertNoText.
AssertLegacyTrait::assertTitle protected function Pass if the page title is the given string.
AssertLegacyTrait::assertUniqueText protected function Passes if the text is found ONLY ONCE on the text version of the page.
AssertLegacyTrait::assertUrl protected function Passes if the internal browser's URL matches the given path.
AssertLegacyTrait::buildXPathQuery protected function Builds an XPath query.
AssertLegacyTrait::constructFieldXpath protected function Helper: Constructs an XPath for the given set of attributes and value.
AssertLegacyTrait::getAllOptions protected function Get all option elements, including nested options, in a select.
AssertLegacyTrait::getRawContent protected function Gets the current raw content.
AssertLegacyTrait::pass protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertTrue() instead.
AssertLegacyTrait::verbose protected function
BlockCreationTrait::placeBlock protected function Creates a block instance based on default settings. Aliased as: drupalPlaceBlock
BrowserHtmlDebugTrait::$htmlOutputBaseUrl protected property The Base URI to use for links to the output files.
BrowserHtmlDebugTrait::$htmlOutputClassName protected property Class name for HTML output logging.
BrowserHtmlDebugTrait::$htmlOutputCounter protected property Counter for HTML output logging.
BrowserHtmlDebugTrait::$htmlOutputCounterStorage protected property Counter storage for HTML output logging.
BrowserHtmlDebugTrait::$htmlOutputDirectory protected property Directory name for HTML output logging.
BrowserHtmlDebugTrait::$htmlOutputEnabled protected property HTML output output enabled.
BrowserHtmlDebugTrait::$htmlOutputFile protected property The file name to write the list of URLs to.
BrowserHtmlDebugTrait::$htmlOutputTestId protected property HTML output test ID.
BrowserHtmlDebugTrait::formatHtmlOutputHeaders protected function Formats HTTP headers as string for HTML output logging.
BrowserHtmlDebugTrait::getHtmlOutputHeaders protected function Returns headers in HTML output format. 1
BrowserHtmlDebugTrait::htmlOutput protected function Logs a HTML output message in a text file.
BrowserHtmlDebugTrait::initBrowserOutputFile protected function Creates the directory to store browser output.
BrowserTestBase::$baseUrl protected property The base URL.
BrowserTestBase::$configImporter protected property The config importer that can be used in a test.
BrowserTestBase::$customTranslations protected property An array of custom translations suitable for drupal_rewrite_settings().
BrowserTestBase::$databasePrefix protected property The database prefix of this test run.
BrowserTestBase::$mink protected property Mink session manager.
BrowserTestBase::$minkDefaultDriverArgs protected property
BrowserTestBase::$minkDefaultDriverClass protected property 1
BrowserTestBase::$originalContainer protected property The original container.
BrowserTestBase::$originalShutdownCallbacks protected property The original array of shutdown function callbacks.
BrowserTestBase::$preserveGlobalState protected property
BrowserTestBase::$profile protected property The profile to install as a basis for testing. 39
BrowserTestBase::$root protected property The app root.
BrowserTestBase::$runTestInSeparateProcess protected property Browser tests are run in separate processes to prevent collisions between code that may be loaded by tests.
BrowserTestBase::$timeLimit protected property Time limit in seconds for the test.
BrowserTestBase::$translationFilesDirectory protected property The translation file directory for the test environment.
BrowserTestBase::cleanupEnvironment protected function Clean up the Simpletest environment.
BrowserTestBase::config protected function Configuration accessor for tests. Returns non-overridden configuration.
BrowserTestBase::cssSelectToXpath protected function Translates a CSS expression to its XPath equivalent.
BrowserTestBase::drupalGetHeader protected function Gets the value of an HTTP response header.
BrowserTestBase::drupalGetHeaders Deprecated protected function Returns all response headers.
BrowserTestBase::filePreDeleteCallback public static function Ensures test files are deletable.
BrowserTestBase::getDefaultDriverInstance protected function Gets an instance of the default Mink driver.
BrowserTestBase::getDrupalSettings protected function Gets the JavaScript drupalSettings variable for the currently-loaded page. 1
BrowserTestBase::getHttpClient protected function Obtain the HTTP client for the system under test.
BrowserTestBase::getMinkDriverArgs protected function Get the Mink driver args from an environment variable, if it is set. Can be overridden in a derived class so it is possible to use a different value for a subset of tests, e.g. the JavaScript tests. 1
BrowserTestBase::getOptions protected function Helper function to get the options of select field.
BrowserTestBase::getResponseLogHandler protected function Provides a Guzzle middleware handler to log every response received. Overrides BrowserHtmlDebugTrait::getResponseLogHandler
BrowserTestBase::getSession public function Returns Mink session.
BrowserTestBase::getSessionCookies protected function Get session cookies from current session.
BrowserTestBase::getTestMethodCaller protected function Retrieves the current calling line in the class under test. Overrides BrowserHtmlDebugTrait::getTestMethodCaller
BrowserTestBase::initFrontPage protected function Visits the front page when initializing Mink. 3
BrowserTestBase::initMink protected function Initializes Mink sessions. 1
BrowserTestBase::installDrupal public function Installs Drupal into the Simpletest site. 1
BrowserTestBase::registerSessions protected function Registers additional Mink sessions.
BrowserTestBase::tearDown protected function 3
BrowserTestBase::translatePostValues protected function Transforms a nested array into a flat array suitable for drupalPostForm().
BrowserTestBase::xpath protected function Performs an xpath search on the contents of the internal browser.
BrowserTestBase::__construct public function 1
BrowserTestBase::__sleep public function Prevents serializing any properties.
ConfigTestTrait::configImporter protected function Returns a ConfigImporter object to import test configuration.
ConfigTestTrait::copyConfig protected function Copies configuration objects from source storage to target storage.
ContentTypeCreationTrait::createContentType protected function Creates a custom content type based on default settings. Aliased as: drupalCreateContentType 1
FormTest::$defaultTheme protected property The theme to install as the default for testing. Overrides BrowserTestBase::$defaultTheme
FormTest::$modules public static property Modules to enable. Overrides BrowserTestBase::$modules
FormTest::assertFormValuesDefault public function Assert that the values submitted to a form matches the default values of the elements.
FormTest::setUp protected function Overrides BrowserTestBase::setUp
FormTest::testCheckboxProcessing public function Test default value handling for checkboxes.
FormTest::testColorValidation public function Tests validation of #type 'color' elements.
FormTest::testDisabledElements public function Test handling of disabled elements.
FormTest::testDisabledMarkup public function Verify markup for disabled form elements.
FormTest::testEmptySelect public function Tests a select element when #options is not set.
FormTest::testGetFormsCsrfToken public function CSRF tokens for GET forms should not be added by default.
FormTest::testInputForgery public function Test Form API protections against input forgery.
FormTest::testInputWithInvalidToken public function Tests that input is retained for safe elements even with an invalid token.
FormTest::testNumber public function Tests validation of #type 'number' and 'range' elements.
FormTest::testRange public function Tests default value handling of #type 'range' elements.
FormTest::testRequiredAttribute public function Tests required attribute.
FormTest::testRequiredCheckboxesRadio public function Tests validation for required checkbox, select, and radio elements.
FormTest::testRequiredFields public function Check several empty values for required forms elements.
FormTest::testRequiredTextfieldNoTitle public function Tests validation for required textfield element without title.
FormTest::testSelect public function Tests validation of #type 'select' elements.
FormTest::testSelectSorting public function Tests sorting and not sorting of options in a select element.
FormTest::validateSelectSorting protected function Validates that the options are in the right order in a select.
FunctionalTestSetupTrait::$apcuEnsureUniquePrefix protected property The flag to set 'apcu_ensure_unique_prefix' setting. 1
FunctionalTestSetupTrait::$classLoader protected property The class loader to use for installation and initialization of setup.
FunctionalTestSetupTrait::$configDirectories Deprecated protected property The config directories used in this test.
FunctionalTestSetupTrait::$rootUser protected property The "#1" admin user.
FunctionalTestSetupTrait::doInstall protected function Execute the non-interactive installer. 1
FunctionalTestSetupTrait::getDatabaseTypes protected function Returns all supported database driver installer objects.
FunctionalTestSetupTrait::initConfig protected function Initialize various configurations post-installation. 2
FunctionalTestSetupTrait::initKernel protected function Initializes the kernel after installation.
FunctionalTestSetupTrait::initSettings protected function Initialize settings created during install.
FunctionalTestSetupTrait::initUserSession protected function Initializes user 1 for the site to be installed.
FunctionalTestSetupTrait::installDefaultThemeFromClassProperty protected function Installs the default theme defined by `static::$defaultTheme` when needed.
FunctionalTestSetupTrait::installModulesFromClassProperty protected function Install modules defined by `static::$modules`. 1
FunctionalTestSetupTrait::installParameters protected function Returns the parameters that will be used when Simpletest installs Drupal. 9
FunctionalTestSetupTrait::prepareEnvironment protected function Prepares the current environment for running the test. 23
FunctionalTestSetupTrait::prepareRequestForGenerator protected function Creates a mock request and sets it on the generator.
FunctionalTestSetupTrait::prepareSettings protected function Prepares site settings and services before installation. 2
FunctionalTestSetupTrait::rebuildAll protected function Resets and rebuilds the environment after setup.
FunctionalTestSetupTrait::rebuildContainer protected function Rebuilds \Drupal::getContainer().
FunctionalTestSetupTrait::resetAll protected function Resets all data structures after having enabled new modules.
FunctionalTestSetupTrait::setContainerParameter protected function Changes parameters in the services.yml file.
FunctionalTestSetupTrait::setupBaseUrl protected function Sets up the base URL based upon the environment variable.
FunctionalTestSetupTrait::writeSettings protected function Rewrites the settings.php file of the test site.
NodeCreationTrait::createNode protected function Creates a node based on default settings. Aliased as: drupalCreateNode
NodeCreationTrait::getNodeByTitle public function Get a node from the database based on its title. Aliased as: drupalGetNodeByTitle
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RandomGeneratorTrait::$randomGenerator protected property The random generator.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers. 1
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate public function Callback for random string validation.
RefreshVariablesTrait::refreshVariables protected function Refreshes in-memory configuration and state information. 3
SessionTestTrait::$sessionName protected property The name of the session cookie.
SessionTestTrait::generateSessionName protected function Generates a session cookie name.
SessionTestTrait::getSessionName protected function Returns the session name in use on the child site.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.
TestRequirementsTrait::checkModuleRequirements private function Checks missing module requirements.
TestRequirementsTrait::checkRequirements protected function Check module requirements for the Drupal use case. 1
TestRequirementsTrait::getDrupalRoot protected static function Returns the Drupal root directory.
TestSetupTrait::$configSchemaCheckerExclusions protected static property An array of config object names that are excluded from schema checking.
TestSetupTrait::$container protected property The dependency injection container used in the test.
TestSetupTrait::$kernel protected property The DrupalKernel instance used in the test.
TestSetupTrait::$originalSite protected property The site directory of the original parent site.
TestSetupTrait::$privateFilesDirectory protected property The private file directory for the test environment.
TestSetupTrait::$publicFilesDirectory protected property The public file directory for the test environment.
TestSetupTrait::$siteDirectory protected property The site directory of this test run.
TestSetupTrait::$strictConfigSchema protected property Set to TRUE to strict check all configuration saved. 2
TestSetupTrait::$tempFilesDirectory protected property The temporary file directory for the test environment.
TestSetupTrait::$testId protected property The test run ID.
TestSetupTrait::changeDatabasePrefix protected function Changes the database connection to the prefixed one.
TestSetupTrait::getConfigSchemaExclusions protected function Gets the config schema exclusions for this test.
TestSetupTrait::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
TestSetupTrait::prepareDatabasePrefix protected function Generates a database prefix for running tests. 2
UiHelperTrait::$loggedInUser protected property The current user logged in using the Mink controlled browser.
UiHelperTrait::$maximumMetaRefreshCount protected property The number of meta refresh redirects to follow, or NULL if unlimited.
UiHelperTrait::$metaRefreshCount protected property The number of meta refresh redirects followed during ::drupalGet().
UiHelperTrait::assertSession public function Returns WebAssert object. 1
UiHelperTrait::buildUrl protected function Builds an a absolute URL from a system path or a URL object.
UiHelperTrait::checkForMetaRefresh protected function Checks for meta refresh tag and if found call drupalGet() recursively.
UiHelperTrait::click protected function Clicks the element with the given CSS selector.
UiHelperTrait::clickLink protected function Follows a link by complete name.
UiHelperTrait::cssSelect protected function Searches elements using a CSS selector in the raw content.
UiHelperTrait::drupalGet protected function Retrieves a Drupal path or an absolute path. 3
UiHelperTrait::drupalLogin protected function Logs in a user using the Mink controlled browser.
UiHelperTrait::drupalLogout protected function Logs a user out of the Mink controlled browser and confirms.
UiHelperTrait::drupalPostForm protected function Executes a form submission.
UiHelperTrait::drupalUserIsLoggedIn protected function Returns whether a given user account is logged in.
UiHelperTrait::getAbsoluteUrl protected function Takes a path and returns an absolute path.
UiHelperTrait::getTextContent protected function Retrieves the plain-text content from the current page.
UiHelperTrait::getUrl protected function Get the current URL from the browser.
UiHelperTrait::prepareRequest protected function Prepare for a request to testing site. 1
UiHelperTrait::submitForm protected function Fills and submits a form.
UserCreationTrait::checkPermissions protected function Checks whether a given list of permission names is valid.
UserCreationTrait::createAdminRole protected function Creates an administrative role.
UserCreationTrait::createRole protected function Creates a role with specified permissions. Aliased as: drupalCreateRole
UserCreationTrait::createUser protected function Create a user with a given set of permissions. Aliased as: drupalCreateUser
UserCreationTrait::grantPermissions protected function Grant permissions to a user role.
UserCreationTrait::setCurrentUser protected function Switch the current logged in user.
UserCreationTrait::setUpCurrentUser protected function Creates a random user account and sets it as current user.
XdebugRequestTrait::extractCookiesFromRequest protected function Adds xdebug cookies, from request setup.