You are here

public function ResourceHintsWebTestCase::testResourceHints in Resource Hints 7

Same name and namespace in other branches
  1. 7.2 resource_hints.test \ResourceHintsWebTestCase::testResourceHints()

Test the prefetch resource hints.

File

./resource_hints.test, line 44
Tests for resource hints module.

Class

ResourceHintsWebTestCase
Test case.

Code

public function testResourceHints() {
  $edit = array();

  // DNS Prefetch Link header.
  $edit['resource_hints_dns_prefetch_resources'] = '//dns-prefetch.com';
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertTrue(strstr($this
    ->drupalGetHeader('Link'), '<//dns-prefetch.com>; rel="dns-prefetch"'));
  $this
    ->assertTrue(strstr($this
    ->drupalGetHeader('X-DNS-Prefetch-Control'), RESOURCE_HINTS_DNS_PREFETCH_ENABLED));

  // DNS Prefetch Link element.
  $edit['resource_hints_dns_prefetch_output'] = RESOURCE_HINTS_OUTPUT_LINK_ELEMENT;
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertRaw('<link rel="dns-prefetch" href="//dns-prefetch.com" />');
  $this
    ->assertRaw('<meta http-equiv="x-dns-prefetch-control" content="on" />');

  // DNS Prefetch Control Off Header.
  $edit['resource_hints_dns_prefetch_output'] = RESOURCE_HINTS_OUTPUT_LINK_HEADER;
  $edit['resource_hints_dns_prefetch_control'] = RESOURCE_HINTS_DNS_PREFETCH_DISABLED;
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertFalse(strstr($this
    ->drupalGetHeader('Link'), '<//dns-prefetch.com>; rel=dns-prefetch'));
  $this
    ->assertTrue(strstr($this
    ->drupalGetHeader('X-DNS-Prefetch-Control'), 'off'));

  // DNS Prefetch Control Off Element.
  $edit['resource_hints_dns_prefetch_output'] = RESOURCE_HINTS_OUTPUT_LINK_ELEMENT;
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertNoRaw('<link rel="dns-prefetch" href="//dns-prefetch.com" />');
  $this
    ->assertRaw('<meta http-equiv="x-dns-prefetch-control" content="off" />');

  // Preconnect Link header.
  $edit['resource_hints_preconnect_resources'] = '//preconnect.com';
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertTrue(strstr($this
    ->drupalGetHeader('Link'), '<//preconnect.com>; rel="preconnect"'));

  // Preconnect Link element.
  $edit['resource_hints_preconnect_output'] = RESOURCE_HINTS_OUTPUT_LINK_ELEMENT;
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertRaw('<link rel="preconnect" href="//preconnect.com" />');

  // Prefetch Link header.
  $edit['resource_hints_prefetch_resources'] = '//prefetch.com';
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertTrue(strstr($this
    ->drupalGetHeader('Link'), '<//prefetch.com>; rel="prefetch"'));

  // Prefetch Link element.
  $edit['resource_hints_prefetch_output'] = RESOURCE_HINTS_OUTPUT_LINK_ELEMENT;
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertRaw('<link rel="prefetch" href="//prefetch.com" />');

  // Prerender Link header.
  $edit['resource_hints_prerender_resources'] = '//prerender.com';
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertTrue(strstr($this
    ->drupalGetHeader('Link'), '<//prerender.com>; rel="prerender"'));

  // Prerender Link element.
  $edit['resource_hints_prerender_output'] = RESOURCE_HINTS_OUTPUT_LINK_ELEMENT;
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertRaw('<link rel="prerender" href="//prerender.com" />');

  // Output with check_url().
  variable_set('resource_hints_prerender_resources', 'javascript://prerender.com');
  $this
    ->drupalGet('admin/config/development/resources-hints');
  $this
    ->assertNoRaw('<link rel="prerender" href="javascript://prerender.com" />');
  $this
    ->assertRaw('<link rel="prerender" href="//prerender.com" />');

  // Reject Invalid URL.
  $edit['resource_hints_prerender_resources'] = '<bad stuff>';
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertRaw('<link rel="prerender" href="//prerender.com" />');
  $this
    ->assertRaw('Please enter valid URLs.');

  // Reject Bad Protocol.
  $edit['resource_hints_prerender_resources'] = 'javascript://prerender.com';
  $this
    ->drupalPost('admin/config/development/resources-hints', $edit, t('Save configuration'));
  $this
    ->assertRaw('<link rel="prerender" href="//prerender.com" />');
  $this
    ->assertRaw('Please enter valid URLs.');
}