function HtmxRequestTest::testHxRequest

Test all 5 request verb methods.

Attributes

#[DataProvider('htmxRequestProvider')]

File

core/tests/Drupal/KernelTests/Core/Htmx/HtmxRequestTest.php, line 61

Class

HtmxRequestTest
Test the request attributes for HTMX.

Namespace

Drupal\KernelTests\Core\Htmx

Code

public function testHxRequest(string $method) : void {
  $attribute = "data-hx-{$method}";
  $render = [];
  (new Htmx())->{$method}($this->url)
    ->applyTo($render);
  $this->assertArrayHasKey($attribute, $render['#attributes']);
  $expected = '/htmx-test-attachments/page';
  // The paths in GitLabCI include a subfolder.
  $this->assertStringEndsWith($expected, $render['#attributes'][$attribute]);
  $this->assertArrayNotHasKey('data-hx-drupal-only-main-content', $render['#attributes']);
  // Verify wrapper format.
  $render = [];
  (new Htmx())->{$method}($this->url)
    ->onlyMainContent()
    ->applyTo($render);
  $this->assertArrayHasKey($attribute, $render['#attributes']);
  $this->assertStringEndsWith('/htmx-test-attachments/page', $render['#attributes'][$attribute]);
  $this->assertTrue($render['#attributes']['data-hx-drupal-only-main-content']);
  // Verify no parameters with no wrapper format.
  $url = Url::fromRoute('test_htmx.attachments.replace');
  $request = Request::create($url->toString());
  $this->httpKernel
    ->handle($request);
  $render = [];
  (new Htmx())->{$method}()
    ->applyTo($render);
  $this->assertArrayHasKey($attribute, $render['#attributes']);
  $expected = '';
  $this->assertSame($expected, $render['#attributes'][$attribute]);
  $this->assertArrayNotHasKey('data-hx-drupal-only-main-content', $render['#attributes']);
  // Verify no parameters.
  $render = [];
  (new Htmx())->{$method}()
    ->onlyMainContent()
    ->applyTo($render);
  $this->assertArrayHasKey($attribute, $render['#attributes']);
  $this->assertSame('', $render['#attributes'][$attribute]);
  $this->assertTrue($render['#attributes']['data-hx-drupal-only-main-content']);
}

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