function CommandsTest::testAttachedSettings

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php \Drupal\KernelTests\Core\Ajax\CommandsTest::testAttachedSettings()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php \Drupal\KernelTests\Core\Ajax\CommandsTest::testAttachedSettings()
  3. 10 core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php \Drupal\KernelTests\Core\Ajax\CommandsTest::testAttachedSettings()

Regression test: Settings command exists regardless of JS aggregation.

File

core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php, line 35

Class

CommandsTest
Performs tests on AJAX framework commands.

Namespace

Drupal\KernelTests\Core\Ajax

Code

public function testAttachedSettings() : void {
    $assert = function ($message) {
        $response = new AjaxResponse();
        $response->setAttachments([
            'library' => [
                'core/drupalSettings',
            ],
            'drupalSettings' => [
                'foo' => 'bar',
            ],
        ]);
        $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor');
        $subscriber = new AjaxResponseSubscriber(fn() => $ajax_response_attachments_processor);
        $event = new ResponseEvent(\Drupal::service('http_kernel'), new Request(), HttpKernelInterface::MAIN_REQUEST, $response);
        $subscriber->onResponse($event);
        $expected = [
            'command' => 'settings',
        ];
        $this->assertCommand($response->getCommands(), $expected, $message);
    };
    $config = $this->config('system.performance');
    $config->set('js.preprocess', FALSE)
        ->save();
    $assert('Settings command exists when JS aggregation is disabled.');
    $config->set('js.preprocess', TRUE)
        ->save();
    $assert('Settings command exists when JS aggregation is enabled.');
}

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