View source
<?php
namespace Drupal\Tests\component_blocks\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Symfony\Component\DomCrawler\Crawler;
class ComponentBlocksOutputTest extends KernelTestBase {
use NodeCreationTrait;
use UserCreationTrait;
use ContentTypeCreationTrait;
use BlockCreationTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'component_blocks',
'components',
'block',
'component_blocks_test',
'field',
'entity_test',
'ui_patterns',
'ui_patterns_library',
'system',
'node',
'filter',
'options',
'text',
'user',
'system',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installConfig([
'node',
'filter',
]);
$this
->installEntitySchema('user');
$this
->setUpCurrentUser([], [
'access content',
]);
}
public function testComponentBlock() {
$block = $this
->placeBlock('component_blocks:node:test_component', [
'variables' => [
'subtitle' => [
'source' => '__fixed',
'value' => '[node:title]',
],
],
'context_mapping' => [
'entity' => '@component_blocks_test.context:node',
],
]);
$this
->createContentType([
'type' => 'page',
]);
$node = $this
->createNode([
'type' => 'page',
'status' => 1,
'title' => 'This & That',
]);
\Drupal::service('component_blocks_test.context')
->setNode($node);
$builder = \Drupal::entityTypeManager()
->getViewBuilder('block');
$build = $builder
->view($block, 'block');
$output = \Drupal::service('renderer')
->renderPlain($build);
$crawler = new Crawler((string) $output);
$title = $crawler
->filter('.subtitle');
$this
->assertEquals(1, $title
->count(), 'Subtitle is output');
$this
->assertContains('This & That', $title
->html());
$node = $this
->createNode([
'type' => 'page',
'status' => 1,
'title' => '<script type="text/javascript">alert("hi");</script>',
]);
\Drupal::service('component_blocks_test.context')
->setNode($node);
$builder = \Drupal::entityTypeManager()
->getViewBuilder('block');
$build = $builder
->view($block, 'block');
$output = \Drupal::service('renderer')
->renderPlain($build);
$crawler = new Crawler((string) $output);
$title = $crawler
->filter('.subtitle');
$this
->assertEquals(1, $title
->count(), 'Subtitle is output');
$this
->assertNotContains('<script', $title
->html());
$this
->assertNotContains('</script>', $title
->html());
}
}