3 namespace Drupal\Tests\system\Functional\Common;
5 use Drupal\node\NodeInterface;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests that anonymous users are not served any JavaScript in the Standard
10 * installation profile.
14 class NoJavaScriptAnonymousTest extends BrowserTestBase {
16 protected $profile = 'standard';
18 protected function setUp() {
21 // Grant the anonymous user the permission to look at user profiles.
22 user_role_grant_permissions('anonymous', ['access user profiles']);
26 * Tests that anonymous users are not served any JavaScript.
28 public function testNoJavaScript() {
29 // Create a node that is listed on the frontpage.
30 $this->drupalCreateNode([
31 'promote' => NodeInterface::PROMOTED,
33 $user = $this->drupalCreateUser();
37 $this->assertNoJavaScriptExceptHtml5Shiv();
40 $this->drupalGet('node/1');
41 $this->assertNoJavaScriptExceptHtml5Shiv();
43 // Test user profile page.
44 $this->drupalGet('user/' . $user->id());
45 $this->assertNoJavaScriptExceptHtml5Shiv();
49 * Passes if no JavaScript is found on the page except the HTML5 shiv.
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.
54 protected function assertNoJavaScriptExceptHtml5Shiv() {
55 // Ensure drupalSettings is not set.
56 $settings = $this->getDrupalSettings();
57 $this->assertTrue(empty($settings), 'drupalSettings is not set.');
59 // Ensure the HTML5 shiv exists.
60 $this->assertRaw('html5shiv/html5shiv.min.js', 'HTML5 shiv JavaScript exists.');
62 // Ensure no other JavaScript file exists on the page, while ignoring the
64 $this->assertNoPattern('/(?<!html5shiv\.min)\.js/', "No other JavaScript exists.");