improvements-2.x-dev/tests/src/Functional/ImprovementsThemingTest.php
tests/src/Functional/ImprovementsThemingTest.php
<?php
namespace Drupal\Tests\improvements\Functional;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\improvements\Traits\ImprovementsTestTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class ImprovementsThemingTest extends BrowserTestBase {
use BlockCreationTrait;
use ImprovementsTestTrait;
use NodeCreationTrait;
/**
* {@inheritDoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritDoc}
*/
protected static $modules = ['improvements'];
/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();
}
/**
* Test '#theme'=>'simple_field'.
*/
public function testThemeSimpleField(): void {
$renderer = $this->container->get('renderer');
$build = [
'#theme' => 'simple_field',
'#field_name' => 'custom_field',
'#title' => 'Custom field',
'#entity_type' => 'node',
'#bundle' => 'article',
'#items' => 'Field value',
];
$html = (string)$renderer->renderRoot($build);
$this->assertStringContainsString($build['#title'], $html);
$this->assertStringContainsString($build['#items'], $html);
}
/**
* Test '#theme'=>'picture'.
*/
public function testThemePicture(): void {
$renderer = $this->container->get('renderer');
$build = [
'#theme' => 'picture',
'#img' => [
'src' => '/sites/default/files/logo.jpg',
'alt' => '',
],
'#source' => [
'mobile' => [
'srcset' => '/sites/default/files/logo-mobile.jpg',
'media' => '(min-width: 600px)',
],
],
];
$html = (string)$renderer->renderRoot($build);
$this->assertStringContainsString('<picture>', $html);
$this->assertStringContainsString('<source srcset="/sites/default/files/logo-mobile.jpg" media="(min-width: 600px)" />', $html);
$this->assertStringContainsString('<img src="/sites/default/files/logo.jpg" alt="" />', $html);
}
/**
* Test '#theme_wrappers'=>['noindex'].
*/
public function testThemeNoindex(): void {
$build = [
'#markup' => 'Markup',
'#theme_wrappers' => ['noindex'],
];
$renderer = $this->container->get('renderer');
$html = (string)$renderer->renderRoot($build);
$this->assertStringContainsString("<!--noindex-->\nMarkup\n<!--/noindex-->", $html);
}
/**
* Test remove "off canvas page wrapper" for anonymouse users.
*
* @covers \Drupal\improvements\ImprovementsTrustedCallbacks::pagePreRender()
*/
public function testRemoveOffCanvasPageWrapper(): void {
$this->drupalGetFrontPage();
$this->dontSeeErrorMessage();
$this->assertSession()->elementNotExists('css', '.dialog-off-canvas-main-canvas');
$this->drupalLoginAsRoot();
$this->drupalGetFrontPage();
$this->dontSeeErrorMessage();
$this->assertSession()->elementExists('css', '.dialog-off-canvas-main-canvas');
}
/**
* Test twig function preg_matches().
*
* @covers \Drupal\improvements\ImprovementsTwigExtension::pregMatches()
*/
public function testTwigFunctionPregMatch(): void {
$renderer = $this->container->get('renderer');
$build = [
'#type' => 'inline_template',
'#template' => "{{ preg_matches('/[0-9]+/', 'Drupal 9', 0) }}",
];
$output = (string)$renderer->renderRoot($build);
$this->assertSame('9', $output);
$build = [
'#type' => 'inline_template',
'#template' => "{% if preg_matches('/Drupal [0-9]+/', 'Drupal 9') %}ok{% endif %}",
];
$output = (string)$renderer->renderRoot($build);
$this->assertSame('ok', $output);
}
}
