Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / src / Tests / Common / NoJavaScriptAnonymousTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Common;
4
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\node\NodeInterface;
7
8 /**
9  * Tests that anonymous users are not served any JavaScript in the Standard
10  * installation profile.
11  *
12  * @group Common
13  */
14 class NoJavaScriptAnonymousTest extends WebTestBase {
15
16   protected $profile = 'standard';
17
18   protected function setUp() {
19     parent::setUp();
20
21     // Grant the anonymous user the permission to look at user profiles.
22     user_role_grant_permissions('anonymous', ['access user profiles']);
23   }
24
25   /**
26    * Tests that anonymous users are not served any JavaScript.
27    */
28   public function testNoJavaScript() {
29     // Create a node that is listed on the frontpage.
30     $this->drupalCreateNode([
31       'promote' => NodeInterface::PROMOTED,
32     ]);
33     $user = $this->drupalCreateUser();
34
35     // Test frontpage.
36     $this->drupalGet('');
37     $this->assertNoJavaScriptExceptHtml5Shiv();
38
39     // Test node page.
40     $this->drupalGet('node/1');
41     $this->assertNoJavaScriptExceptHtml5Shiv();
42
43     // Test user profile page.
44     $this->drupalGet('user/' . $user->id());
45     $this->assertNoJavaScriptExceptHtml5Shiv();
46   }
47
48   /**
49    * Passes if no JavaScript is found on the page except the HTML5 shiv.
50    *
51    * The HTML5 shiv is necessary for e.g. the <article> tag which Drupal 8 uses
52    * to work in older browsers like Internet Explorer 8.
53    */
54   protected function assertNoJavaScriptExceptHtml5Shiv() {
55     // Ensure drupalSettings is not set.
56     $settings = $this->getDrupalSettings();
57     $this->assertTrue(empty($settings), 'drupalSettings is not set.');
58
59     // Ensure the HTML5 shiv exists.
60     $this->assertRaw('html5shiv/html5shiv.min.js', 'HTML5 shiv JavaScript exists.');
61
62     // Ensure no other JavaScript file exists on the page, while ignoring the
63     // HTML5 shiv.
64     $this->assertNoPattern('/(?<!html5shiv\.min)\.js/', "No other JavaScript exists.");
65   }
66
67 }