class FormTest

Same name in this branch
  1. 9 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest
Same name and namespace in other branches
  1. 8.9.x core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest
  2. 8.9.x core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest
  3. 10 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest
  4. 10 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest
  5. 11.x core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest
  6. 11.x 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
     */
    protected static $modules = [
        'filter',
        'form_test',
        'file',
        'datetime',
    ];
    
    /**
     * {@inheritdoc}
     */
    protected $defaultTheme = 'starterkit_theme';
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        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' => [
                '' => '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' => [
                '' => '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' => '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_output = \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->assertMatchesRegularExpression($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->assertDoesNotMatchRegularExpression($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->assertArrayNotHasKey($element, $errors, "Optional '{$type}' field '{$element}' should have 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->drupalGet('form-test/validate-required');
        $this->submitForm($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[] = $form[$key]['#title'] . ' field is required.';
            }
        }
        // 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->assertSession()
            ->fieldValueEquals('textfield', '');
        $this->assertSession()
            ->checkboxNotChecked('edit-checkboxes-foo');
        $this->assertSession()
            ->checkboxNotChecked('edit-checkboxes-bar');
        $this->assertTrue($this->assertSession()
            ->optionExists('edit-select', '')
            ->isSelected());
        $this->assertSession()
            ->checkboxNotChecked('edit-radios-foo');
        $this->assertSession()
            ->checkboxNotChecked('edit-radios-bar');
        $this->assertSession()
            ->checkboxNotChecked('edit-radios-optional-foo');
        $this->assertSession()
            ->checkboxNotChecked('edit-radios-optional-bar');
        $this->assertSession()
            ->checkboxNotChecked('edit-radios-optional-default-value-false-foo');
        $this->assertSession()
            ->checkboxNotChecked('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->submitForm($edit, 'Submit');
        // Verify that no error message is displayed when all required fields are
        // filled.
        $this->assertSession()
            ->elementNotExists('xpath', '//div[contains(@class, "error")]');
        $this->assertSession()
            ->pageTextContains("The form_test_validate_required_form form was 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->submitForm($edit, 'Submit');
        // Verify that error message is displayed with invalid token even when
        // required fields are filled.
        $this->assertSession()
            ->elementExists('xpath', '//div[contains(@class, "error")]');
        $assert = $this->assertSession();
        $element = $assert->fieldExists('textfield');
        $this->assertEmpty($element->getValue());
        $assert->responseNotContains($random_string);
        $this->assertSession()
            ->pageTextContains('The form has become outdated.');
        // Ensure that we don't use the posted values.
        $this->assertSession()
            ->fieldValueEquals('textfield', '');
        $this->assertSession()
            ->checkboxNotChecked('edit-checkboxes-foo');
        $this->assertSession()
            ->checkboxNotChecked('edit-checkboxes-bar');
        $this->assertTrue($this->assertSession()
            ->optionExists('edit-select', '')
            ->isSelected());
        $this->assertSession()
            ->checkboxNotChecked('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->submitForm($edit, 'Submit');
        // Verify that the error message is displayed with invalid token even when
        // required fields are filled.
        $this->assertSession()
            ->elementExists('xpath', '//div[contains(@class, "error")]');
        $this->assertSession()
            ->pageTextContains('The form has become outdated.');
        $this->assertSession()
            ->fieldValueEquals('textfield', '');
        $this->assertSession()
            ->fieldValueEquals('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->submitForm($edit, 'Submit');
        // Verify that the error message is displayed with invalid token even when
        // required fields are filled.'
        $this->assertSession()
            ->elementExists('xpath', '//div[contains(@class, "error")]');
        $this->assertSession()
            ->pageTextContains('The form has become outdated.');
        $this->assertSession()
            ->fieldValueEquals('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->submitForm($edit, 'Submit');
        // Verify that the error message is displayed with invalid token even when
        // required fields are filled.
        $this->assertSession()
            ->elementExists('xpath', '//div[contains(@class, "error")]');
        $this->assertSession()
            ->pageTextContains('The form has become outdated.');
        $this->assertSession()
            ->fieldValueEquals('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->assertSession()
            ->responseNotContains('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->drupalGet('form-test/validate-required-no-title');
        $this->submitForm($edit, 'Submit');
        $this->assertSession()
            ->pageTextNotContains("The form_test_validate_required_form_no_title form was submitted successfully.");
        // Check the page for the error class on the textfield.
        $this->assertSession()
            ->elementExists('xpath', '//input[contains(@class, "error")]');
        // Check the page for the aria-invalid attribute on the textfield.
        $this->assertSession()
            ->elementExists('xpath', '//input[contains(@aria-invalid, "true")]');
        // Submit again with required fields set and verify that there are no
        // error messages.
        $edit = [
            'textfield' => $this->randomString(),
        ];
        $this->submitForm($edit, 'Submit');
        // Verify that no error input form element class is present.
        $this->assertSession()
            ->elementNotExists('xpath', '//input[contains(@class, "error")]');
        $this->assertSession()
            ->pageTextContains("The form_test_validate_required_form_no_title form was submitted successfully.");
    }
    
    /**
     * Tests default value handling for checkboxes.
     *
     * @see _form_test_checkbox()
     */
    public function testCheckboxProcessing() {
        // First, try to submit without the required checkbox.
        $edit = [];
        $this->drupalGet('form-test/checkbox');
        $this->submitForm($edit, 'Submit');
        $this->assertSession()
            ->pageTextContains("required_checkbox field is required.");
        // Now try to submit the form correctly.
        $this->submitForm([
            'required_checkbox' => 1,
        ], '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->assertSession()
            ->assertEscaped('<strong>four</strong>');
        $this->assertSession()
            ->responseNotContains('<strong>four</strong>');
        // Posting without any values should throw validation errors.
        $this->submitForm([], '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->assertSession()
                ->pageTextNotContains($form[$key]['#title'] . ' field is required.');
        }
        $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->assertSession()
                ->pageTextContains($form[$key]['#title'] . ' field is required.');
        }
        // 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->submitForm($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->assertSame($value, $values[$key], 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->assertSession()
            ->elementExists('xpath', "//select[1]");
        $this->assertSession()
            ->elementNotExists('xpath', "//select[1]/option");
    }
    
    /**
     * 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->assertSame($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->drupalGet($path);
            $this->submitForm([], 'Submit');
            foreach ($expected as $element => $error) {
                // Create placeholder array.
                $placeholders = [
                    '%name' => $form[$element]['#title'],
                    '%min' => $form[$element]['#min'] ?? '0',
                    '%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->assertSession()
                            ->responseContains(new FormattableMarkup($message, $placeholders));
                    }
                    else {
                        $this->assertSession()
                            ->responseNotContains(new FormattableMarkup($message, $placeholders));
                    }
                }
            }
        }
    }
    
    /**
     * Tests default value handling of #type 'range' elements.
     */
    public function testRange() {
        $this->drupalGet('form-test/range');
        $this->submitForm([], 'Submit');
        $values = json_decode($this->getSession()
            ->getPage()
            ->getContent());
        $this->assertEquals(18, $values->with_default_value);
        $this->assertEquals(10.5, $values->float);
        $this->assertEquals(6, $values->integer);
        $this->assertEquals(6.9, $values->offset);
        $this->drupalGet('form-test/range/invalid');
        $this->submitForm([], 'Submit');
        // Verify that the 'range' element has the error class.
        $this->assertSession()
            ->elementExists('xpath', '//input[@type="range" and contains(@class, "error")]');
    }
    
    /**
     * 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->drupalGet('form-test/color');
            $this->submitForm($edit, 'Submit');
            $result = json_decode($this->getSession()
                ->getPage()
                ->getContent());
            $this->assertEquals($expected, $result->color);
        }
        // Tests invalid values are rejected.
        // cspell:ignore fffffg
        $values = [
            '#0008',
            '#1234',
            '#fffffg',
            '#abcdef22',
            '17',
            '#uaa',
        ];
        foreach ($values as $input) {
            $edit = [
                'color' => $input,
            ];
            $this->drupalGet('form-test/color');
            $this->submitForm($edit, 'Submit');
            $this->assertSession()
                ->pageTextContains("Color must be a valid color.");
        }
    }
    
    /**
     * Tests 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->drupalGet('form-test/disabled-elements');
        $this->submitForm([], 'Submit');
        $returned_values['normal'] = Json::decode($this->getSession()
            ->getPage()
            ->getContent());
        // Do the same with input, as could happen if JavaScript un-disables an
        // element. submitForm() 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 = 44;
        $this->assertEquals($expected_count, $actual_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->submitForm($edit, '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.
     *
     * @internal
     */
    public function assertFormValuesDefault(array $values, array $form) : void {
        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 (in_array($key, [
                    'checkboxes_multiple',
                    'checkboxes_single_select',
                    'checkboxes_single_unselect',
                ], TRUE)) {
                    // Checkboxes values are not filtered out.
                    $values[$key] = array_filter($values[$key]);
                }
                $this->assertSame($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.
            $this->assertSession()
                ->elementExists('xpath', $this->assertSession()
                ->buildXPathQuery($path, [
                ':name' => Html::escape($name),
                ':div-class' => $class,
                ':value' => $item['#value'] ?? '',
            ]));
        }
        // Verify special element #type text-format.
        $this->assertSession()
            ->elementExists('xpath', "//div[contains(@class, 'form-disabled')]/descendant::textarea[@name='text_format[value]']");
        $this->assertSession()
            ->elementExists('xpath', "//div[contains(@class, 'form-disabled')]/descendant::select[@name='text_format[format]']");
    }
    
    /**
     * Tests 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->submitForm([
            'checkboxes[one]' => TRUE,
            'checkboxes[two]' => TRUE,
        ], 'Submit');
        $this->assertSession()
            ->pageTextContains('An illegal choice has been detected.');
    }
    
    /**
     * Tests required attribute.
     */
    public function testRequiredAttribute() {
        $this->drupalGet('form-test/required-attribute');
        foreach ([
            'textfield',
            'password',
            'textarea',
        ] as $type) {
            $field = $this->assertSession()
                ->fieldExists("edit-{$type}");
            $this->assertSame('required', $field->getAttribute('required'), "The {$type} has the proper required attribute.");
        }
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Member alias Overriden Title Overrides
AssertLegacyTrait::assert Deprecated protected function
AssertLegacyTrait::assertCacheTag Deprecated protected function Asserts whether an expected cache tag was present in the last response.
AssertLegacyTrait::assertElementNotPresent Deprecated protected function Asserts that the element with the given CSS selector is not present.
AssertLegacyTrait::assertElementPresent Deprecated protected function Asserts that the element with the given CSS selector is present.
AssertLegacyTrait::assertEqual Deprecated protected function
AssertLegacyTrait::assertEscaped Deprecated protected function Passes if the raw text IS found escaped on the loaded page, fail otherwise.
AssertLegacyTrait::assertField Deprecated protected function Asserts that a field exists with the given name or ID.
AssertLegacyTrait::assertFieldById Deprecated protected function Asserts that a field exists with the given ID and value.
AssertLegacyTrait::assertFieldByName Deprecated protected function Asserts that a field exists with the given name and value.
AssertLegacyTrait::assertFieldByXPath Deprecated protected function Asserts that a field exists in the current page by the given XPath.
AssertLegacyTrait::assertFieldChecked Deprecated protected function Asserts that a checkbox field in the current page is checked.
AssertLegacyTrait::assertFieldsByValue Deprecated protected function Asserts that a field exists in the current page with a given Xpath result.
AssertLegacyTrait::assertHeader Deprecated protected function Checks that current response header equals value.
AssertLegacyTrait::assertIdentical Deprecated protected function
AssertLegacyTrait::assertIdenticalObject Deprecated protected function
AssertLegacyTrait::assertLink Deprecated protected function Passes if a link with the specified label is found.
AssertLegacyTrait::assertLinkByHref Deprecated protected function Passes if a link containing a given href (part) is found.
AssertLegacyTrait::assertNoCacheTag Deprecated protected function Asserts whether an expected cache tag was absent in the last response.
AssertLegacyTrait::assertNoEscaped Deprecated protected function Passes if the raw text is not found escaped on the loaded page.
AssertLegacyTrait::assertNoField Deprecated protected function Asserts that a field does NOT exist with the given name or ID.
AssertLegacyTrait::assertNoFieldById Deprecated protected function Asserts that a field does not exist with the given ID and value.
AssertLegacyTrait::assertNoFieldByName Deprecated protected function Asserts that a field does not exist with the given name and value.
AssertLegacyTrait::assertNoFieldByXPath Deprecated protected function Asserts that a field does not exist or its value does not match, by XPath.
AssertLegacyTrait::assertNoFieldChecked Deprecated protected function Asserts that a checkbox field in the current page is not checked.
AssertLegacyTrait::assertNoLink Deprecated protected function Passes if a link with the specified label is not found.
AssertLegacyTrait::assertNoLinkByHref Deprecated protected function Passes if a link containing a given href (part) is not found.
AssertLegacyTrait::assertNoOption Deprecated protected function Asserts that a select option does NOT exist in the current page.
AssertLegacyTrait::assertNoPattern Deprecated protected function Triggers a pass if the Perl regex pattern is not found in the raw content.
AssertLegacyTrait::assertNoRaw Deprecated protected function Passes if the raw text IS not found on the loaded page, fail otherwise.
AssertLegacyTrait::assertNotEqual Deprecated protected function
AssertLegacyTrait::assertNoText Deprecated protected function Passes if the page (with HTML stripped) does not contains the text.
AssertLegacyTrait::assertNotIdentical Deprecated protected function
AssertLegacyTrait::assertNoUniqueText Deprecated protected function Passes if the text is found MORE THAN ONCE on the text version of the page.
AssertLegacyTrait::assertOption Deprecated protected function Asserts that a select option in the current page exists.
AssertLegacyTrait::assertOptionByText Deprecated protected function Asserts that a select option with the visible text exists.
AssertLegacyTrait::assertOptionSelected Deprecated protected function Asserts that a select option in the current page is checked.
AssertLegacyTrait::assertPattern Deprecated protected function Triggers a pass if the Perl regex pattern is found in the raw content.
AssertLegacyTrait::assertRaw Deprecated protected function Passes if the raw text IS found on the loaded page, fail otherwise.
AssertLegacyTrait::assertResponse Deprecated protected function Asserts the page responds with the specified response code.
AssertLegacyTrait::assertText Deprecated protected function Passes if the page (with HTML stripped) contains the text.
AssertLegacyTrait::assertTextHelper Deprecated protected function Helper for assertText and assertNoText.
AssertLegacyTrait::assertTitle Deprecated protected function Pass if the page title is the given string.
AssertLegacyTrait::assertUniqueText Deprecated protected function Passes if the text is found ONLY ONCE on the text version of the page.
AssertLegacyTrait::assertUrl Deprecated protected function Passes if the internal browser&#039;s URL matches the given path.
AssertLegacyTrait::buildXPathQuery Deprecated protected function Builds an XPath query.
AssertLegacyTrait::constructFieldXpath Deprecated protected function Helper: Constructs an XPath for the given set of attributes and value.
AssertLegacyTrait::getAllOptions Deprecated protected function Get all option elements, including nested options, in a select.
AssertLegacyTrait::getRawContent Deprecated protected function Gets the current raw content.
AssertLegacyTrait::pass Deprecated protected function
AssertLegacyTrait::verbose Deprecated 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 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::getResponseLogHandler protected function Provides a Guzzle middleware handler to log every response received.
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::$mink protected property Mink session manager.
BrowserTestBase::$minkDefaultDriverArgs protected property Mink default driver params.
BrowserTestBase::$minkDefaultDriverClass protected property Mink class for the default driver to use. 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. 37
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::drupalGetHeader Deprecated protected function Gets the value of an HTTP response header.
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 Gets the Mink driver args from an environment variable. 1
BrowserTestBase::getOptions protected function Helper function to get the options of select field.
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::setUpAppRoot protected function Sets up the root application path.
BrowserTestBase::setUpBeforeClass public static function 1
BrowserTestBase::tearDown protected function 3
BrowserTestBase::translatePostValues protected function Transforms a nested array into a flat array suitable for submitForm().
BrowserTestBase::xpath protected function Performs an xpath search on the contents of the internal browser.
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
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
FormTest::$defaultTheme protected property The theme to install as the default for testing. Overrides BrowserTestBase::$defaultTheme
FormTest::$modules protected 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 Tests default value handling for checkboxes.
FormTest::testColorValidation public function Tests validation of #type &#039;color&#039; elements.
FormTest::testDisabledElements public function Tests 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 Tests 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 &#039;number&#039; and &#039;range&#039; elements.
FormTest::testRange public function Tests default value handling of #type &#039;range&#039; 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 &#039;select&#039; 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 &#039;apcu_ensure_unique_prefix&#039; setting. 1
FunctionalTestSetupTrait::$classLoader protected property The class loader to use for installation and initialization of setup.
FunctionalTestSetupTrait::$rootUser protected property The &quot;#1&quot; 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. 1
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. 8
FunctionalTestSetupTrait::prepareEnvironment protected function Prepares the current environment for running the test. 22
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. 3
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. 1
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
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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. 1
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.
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. 1
TestSetupTrait::$container protected property The dependency injection container used in the test.
TestSetupTrait::$databasePrefix protected property The database prefix of this test run.
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::$root protected property The app root.
TestSetupTrait::$siteDirectory protected property The site directory of this test run.
TestSetupTrait::$strictConfigSchema protected property Set to TRUE to strict check all configuration saved. 1
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. 1
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 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::cssSelectToXpath protected function Translates a CSS expression to its XPath equivalent.
UiHelperTrait::drupalGet protected function Retrieves a Drupal path or an absolute path. 2
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 Deprecated 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::isTestUsingGuzzleClient protected function Determines if test is using DrupalTestBrowser.
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.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.