function FormTest::assertFormValuesDefault

Same name and namespace in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::assertFormValuesDefault()
  2. 10 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::assertFormValuesDefault()
  3. 11.x core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::assertFormValuesDefault()

Assert that the values submitted to a form matches the default values of the elements.

@internal

1 call to FormTest::assertFormValuesDefault()
FormTest::testDisabledElements in core/modules/system/tests/src/Functional/Form/FormTest.php
Tests handling of disabled elements.

File

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

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

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]);
  }
}

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