3 namespace Drupal\Tests\node\Functional;
5 use Drupal\Component\Utility\Html;
8 * Create a node with dangerous tags in its title and test that they are
13 class NodeTitleXSSTest extends NodeTestBase {
16 * Tests XSS functionality with a node entity.
18 public function testNodeTitleXSS() {
19 // Prepare a user to do the stuff.
20 $web_user = $this->drupalCreateUser(['create page content', 'edit any page content']);
21 $this->drupalLogin($web_user);
23 $xss = '<script>alert("xss")</script>';
24 $title = $xss . $this->randomMachineName();
26 $edit['title[0][value]'] = $title;
28 $this->drupalPostForm('node/add/page', $edit, t('Preview'));
29 $this->assertNoRaw($xss, 'Harmful tags are escaped when previewing a node.');
31 $settings = ['title' => $title];
32 $node = $this->drupalCreateNode($settings);
34 $this->drupalGet('node/' . $node->id());
35 // Titles should be escaped.
36 $this->assertRaw('<title>' . Html::escape($title) . ' | Drupal</title>', 'Title is displayed when viewing a node.');
37 $this->assertNoRaw($xss, 'Harmful tags are escaped when viewing a node.');
39 $this->drupalGet('node/' . $node->id() . '/edit');
40 $this->assertNoRaw($xss, 'Harmful tags are escaped when editing a node.');