annotate core/modules/system/src/Tests/Common/NoJavaScriptAnonymousTest.php @ 14:1fec387a4317

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