More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / system / src / Tests / Theme / EntityFilteringThemeTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Theme;
4
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\Core\Extension\ExtensionDiscovery;
7 use Drupal\comment\CommentInterface;
8 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
9 use Drupal\node\NodeInterface;
10 use Drupal\simpletest\WebTestBase;
11 use Drupal\comment\Entity\Comment;
12 use Drupal\taxonomy\Entity\Term;
13
14 /**
15  * Tests themed output for each entity type in all available themes to ensure
16  * entity labels are filtered for XSS.
17  *
18  * @group Theme
19  */
20 class EntityFilteringThemeTest extends WebTestBase {
21
22   use CommentTestTrait;
23
24   /**
25    * Use the standard profile.
26    *
27    * We test entity theming with the default node, user, comment, and taxonomy
28    * configurations at several paths in the standard profile.
29    *
30    * @var string
31    */
32   protected $profile = 'standard';
33
34   /**
35    * A list of all available themes.
36    *
37    * @var \Drupal\Core\Extension\Extension[]
38    */
39   protected $themes;
40
41   /**
42    * A test user.
43    *
44    * @var \Drupal\user\User
45    */
46   protected $user;
47
48
49   /**
50    * A test node.
51    *
52    * @var \Drupal\node\Node
53    */
54   protected $node;
55
56
57   /**
58    * A test taxonomy term.
59    *
60    * @var \Drupal\taxonomy\Term
61    */
62   protected $term;
63
64
65   /**
66    * A test comment.
67    *
68    * @var \Drupal\comment\Comment
69    */
70   protected $comment;
71
72   /**
73    * A string containing markup and JS.
74    *
75    * @var string
76    */
77   protected $xssLabel = "string with <em>HTML</em> and <script>alert('JS');</script>";
78
79   protected function setUp() {
80     parent::setUp();
81
82     // Install all available non-testing themes.
83     $listing = new ExtensionDiscovery(\Drupal::root());
84     $this->themes = $listing->scan('theme', FALSE);
85     \Drupal::service('theme_handler')->install(array_keys($this->themes));
86
87     // Create a test user.
88     $this->user = $this->drupalCreateUser(['access content', 'access user profiles']);
89     $this->user->name = $this->xssLabel;
90     $this->user->save();
91     $this->drupalLogin($this->user);
92
93     // Create a test term.
94     $this->term = Term::create([
95       'name' => $this->xssLabel,
96       'vid' => 1,
97     ]);
98     $this->term->save();
99
100     // Add a comment field.
101     $this->addDefaultCommentField('node', 'article', 'comment', CommentItemInterface::OPEN);
102     // Create a test node tagged with the test term.
103     $this->node = $this->drupalCreateNode([
104       'title' => $this->xssLabel,
105       'type' => 'article',
106       'promote' => NodeInterface::PROMOTED,
107       'field_tags' => [['target_id' => $this->term->id()]],
108     ]);
109
110     // Create a test comment on the test node.
111     $this->comment = Comment::create([
112       'entity_id' => $this->node->id(),
113       'entity_type' => 'node',
114       'field_name' => 'comment',
115       'status' => CommentInterface::PUBLISHED,
116       'subject' => $this->xssLabel,
117       'comment_body' => [$this->randomMachineName()],
118     ]);
119     $this->comment->save();
120   }
121
122   /**
123    * Checks each themed entity for XSS filtering in available themes.
124    */
125   public function testThemedEntity() {
126     // Check paths where various view modes of the entities are rendered.
127     $paths = [
128       'user',
129       'node',
130       'node/' . $this->node->id(),
131       'taxonomy/term/' . $this->term->id(),
132     ];
133
134     // Check each path in all available themes.
135     foreach ($this->themes as $name => $theme) {
136       $this->config('system.theme')
137         ->set('default', $name)
138         ->save();
139       foreach ($paths as $path) {
140         $this->drupalGet($path);
141         $this->assertResponse(200);
142         $this->assertNoRaw($this->xssLabel);
143       }
144     }
145   }
146
147 }