You are here

public function CommandsTest::testAttachedSettings in Drupal 8

Same name and namespace in other branches
  1. 9 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 28

Class

CommandsTest
Performs tests on AJAX framework commands.

Namespace

Drupal\KernelTests\Core\Ajax

Code

public function testAttachedSettings() {
  $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($ajax_response_attachments_processor);
    $event = new FilterResponseEvent(\Drupal::service('http_kernel'), new Request(), HttpKernelInterface::MASTER_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.');
}