You are here

resource_hints.test in Resource Hints 7

Same filename and directory in other branches
  1. 7.2 resource_hints.test

Tests for resource hints module.

File

resource_hints.test
View source
<?php

/**
 * @file
 * Tests for resource hints module.
 */

/**
 * Test case.
 */
class ResourceHintsWebTestCase extends DrupalWebTestCase {
  protected $profile = 'testing';

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Resources Hints Web Test Case',
      'description' => 'Test resource hints are output based on configuration.',
      'group' => 'Resource Hints',
    );
  }

  /**
   * Implements setUp().
   */
  public function setUp() {
    parent::setUp('resource_hints');
    $admin_user = $this
      ->drupalCreateUser(array(
      'access administration pages',
      'administer nodes',
      'administer site configuration',
      'administer resource hints',
    ));
    $this
      ->drupalLogin($admin_user);
  }

  /**
   * Test the prefetch resource hints.
   */
  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.');
  }

}

Classes

Namesort descending Description
ResourceHintsWebTestCase Test case.