View source
<?php
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\comment\CommentInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\node\NodeInterface;
use Drupal\comment\Entity\Comment;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\BrowserTestBase;
class EntityFilteringThemeTest extends BrowserTestBase {
use CommentTestTrait;
protected $defaultTheme = 'stark';
protected $profile = 'standard';
protected $themes;
protected $user;
protected $node;
protected $term;
protected $comment;
protected $xssLabel = "string with <em>HTML</em> and <script>alert('JS');</script>";
protected function setUp() : void {
parent::setUp();
$listing = new ExtensionDiscovery(\Drupal::root());
$this->themes = $listing
->scan('theme', FALSE);
\Drupal::service('theme_installer')
->install(array_keys($this->themes));
$this->user = $this
->drupalCreateUser([
'access content',
'access user profiles',
]);
$this->user->name = $this->xssLabel;
$this->user
->save();
$this
->drupalLogin($this->user);
$this->term = Term::create([
'name' => $this->xssLabel,
'vid' => 1,
]);
$this->term
->save();
$this
->addDefaultCommentField('node', 'article', 'comment', CommentItemInterface::OPEN);
$this->node = $this
->drupalCreateNode([
'title' => $this->xssLabel,
'type' => 'article',
'promote' => NodeInterface::PROMOTED,
'field_tags' => [
[
'target_id' => $this->term
->id(),
],
],
]);
$this->comment = Comment::create([
'entity_id' => $this->node
->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'status' => CommentInterface::PUBLISHED,
'subject' => $this->xssLabel,
'comment_body' => [
$this
->randomMachineName(),
],
]);
$this->comment
->save();
}
public function testThemedEntity() {
$paths = [
'user',
'node',
'node/' . $this->node
->id(),
'taxonomy/term/' . $this->term
->id(),
];
foreach ($this->themes as $name => $theme) {
$this
->config('system.theme')
->set('default', $name)
->save();
foreach ($paths as $path) {
$this
->drupalGet($path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseNotContains($this->xssLabel);
}
}
}
}