HtmxRendererTest.php
Namespace
Drupal\KernelTests\Core\HtmxFile
-
core/
tests/ Drupal/ KernelTests/ Core/ Htmx/ HtmxRendererTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\KernelTests\Core\Htmx;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Render\MainContent\HtmxRenderer;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Verifies HtmxRenderer.
*/
class HtmxRendererTest extends KernelTestBase {
use UserCreationTrait;
use BlockCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'block',
'user',
'test_htmx',
];
/**
* Injected kernel service.
*/
protected HttpKernelInterface $httpKernel;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->installConfig([
'system',
]);
$this->installEntitySchema('user');
$theme = $this->config('system.theme')
->get('default');
$this->container
->get('theme_installer')
->install([
$theme,
]);
$this->placeBlock('system_powered_by_block', [
'region' => 'header',
]);
$this->setCurrentUser($this->createUser([
'access content',
]));
$this->httpKernel = $this->container
->get('http_kernel');
}
/**
* Test triggering the renderer with _wrapper_format.
*/
public function testWrapperFormat() : void {
// Verify the "Powered by" block is rendered on a standard page.
$url = Url::fromRoute('test_htmx.attachments.replace');
$request = Request::create($url->toString());
$response = $this->httpKernel
->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringContainsString('Powered by', $response->getContent());
// Verify the body contains only the main content when using the new
// wrapper format.
$options = [
'query' => [
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_htmx',
],
];
$url = Url::fromRoute('test_htmx.attachments.replace', [], $options);
$request = Request::create($url->toString());
$response = $this->httpKernel
->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html; charset=UTF-8', $response->headers
->get('Content-Type'));
$oneLine = str_replace([
"\r",
"\n",
], "", $response->getContent());
$this->assertStringContainsString('<body><div class="ajax-content">Initial Content</div></body>', $oneLine);
}
}
Classes
Title | Deprecated | Summary |
---|---|---|
HtmxRendererTest | Verifies HtmxRenderer. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.