Skip to content
Snippets Groups Projects
Commit b8adc1c4 authored by Angie Byron's avatar Angie Byron
Browse files

Issue #2120457 by Wim Leers: Add test to guarantee that the Standard profile...

Issue #2120457 by Wim Leers: Add test to guarantee that the Standard profile does not load any JavaScript for anonymous users on critical pages.
parent 62df5f1c
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
<?php
/**
* @file
* Contains \Drupal\system\Tests\Common\NoJavaScriptAnonymousTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
/**
* Tests that anonymous users are not served any JavaScript.
*/
class NoJavaScriptAnonymousTest extends WebTestBase {
protected $profile = 'standard';
public static function getInfo() {
return array(
'name' => 'No JavaScript for anonymous users in Standard profile',
'description' => 'Tests that anonymous users are not served any JavaScript in the Standard installation profile.',
'group' => 'Common',
);
}
protected function setUp() {
parent::setUp();
// Grant the anonymous user the permission to look at user profiles.
user_role_grant_permissions('anonymous', array('access user profiles'));
}
/**
* Tests that anonymous users are not served any JavaScript.
*/
public function testNoJavaScript() {
// Create a node that is listed on the frontpage.
$this->drupalCreateNode(array(
'promote' => NODE_PROMOTED,
));
$user = $this->drupalCreateUser();
// Test frontpage.
$this->drupalGet('');
$this->assertNoJavaScriptExceptHtml5Shiv();
// Test node page.
$this->drupalGet('node/1');
$this->assertNoJavaScriptExceptHtml5Shiv();
// Test user profile page.
$this->drupalGet('user/' . $user->id());
$this->assertNoJavaScriptExceptHtml5Shiv();
}
/**
* Passes if no JavaScript is found on the page except the HTML5 shiv.
*
* The HTML5 shiv is necessary for e.g. the <article> tag which Drupal 8 uses
* to work in older browsers like Internet Explorer 8.
*/
protected function assertNoJavaScriptExceptHtml5Shiv() {
// Ensure drupalSettings is not set.
$this->assertNoRaw('var drupalSettings = {', 'drupalSettings is not set.');
// Ensure the HTML5 shiv exists.
$system_libraries = system_library_info();
$html5_shiv_version = $system_libraries['html5shiv']['version'];
$html5_shiv_markup = 'core/assets/vendor/html5shiv/html5.js?v=' . $html5_shiv_version . '"></script>';
$this->assertRaw($html5_shiv_markup, 'HTML5 shiv JavaScript exists.');
// Ensure no other JavaScript file exists on the page, while ignoring the
// HTML5 shiv.
$content = $this->drupalGetContent();
$this->drupalSetContent(str_replace($html5_shiv_markup, '', $content));
$this->assertNoRaw('.js', "No other JavaScript exists.");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment