function AlterTest::testExecutionOrder

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

Tests execution order of hook_form_alter() and hook_form_FORM_ID_alter().

File

core/modules/system/tests/src/Functional/Form/AlterTest.php, line 32

Class

AlterTest
Tests <a href="/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/11.x" title="Perform alterations before a form is rendered." class="local">hook_form_alter</a>() and <a href="/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_FORM_ID_alter/11.x" title="Provide a form-specific alteration instead of the global hook_form_alter()." class="local">hook_form_FORM_ID_alter</a>().

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testExecutionOrder() : void {
    $this->drupalGet('form-test/alter');
    // Ensure that the order is first by module, then for a given module, the
    // id-specific one after the generic one.
    $expected = [
        'block_form_form_test_alter_form_alter() executed.',
        'form_test_form_alter() executed.',
        'form_test_form_form_test_alter_form_alter() executed.',
        'system_form_form_test_alter_form_alter() executed.',
    ];
    $content = preg_replace('/\\s+/', ' ', Xss::filter($this->getSession()
        ->getPage()
        ->getContent(), []));
    $this->assertStringContainsString(implode(' ', $expected), $content, 'Form alter hooks executed in the expected order.');
}

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