You are here

public function HttpResponseHeadersUITestCase::testHeaderRuleAdminUI in HTTP Response Headers 7

Test creating header rule.

File

modules/http_response_headers_ui/http_response_headers_ui.test, line 45
Tests for http_response_headers_ui.module.

Class

HttpResponseHeadersUITestCase
@file Tests for http_response_headers_ui.module.

Code

public function testHeaderRuleAdminUI() {

  // Confirm that the add rule link appears on header rule overview pages.
  $this
    ->drupalGet('admin/config/system/http-response-headers');
  $this
    ->assertRaw(l(t('Add'), 'admin/config/system/http-response-headers/add'), 'Add header rule link is present on overview page.');

  // Add a new header rule by filling out the input
  // form on the admin/config/system/http-response-headers/add page.
  $name = $this
    ->randomName(8);
  $visibility_options = array(
    'pages' => 'node/*',
    'visibility' => HTTP_RESPONSE_HEADERS_VISIBILITY_LISTED,
    'types[article]' => 'article',
  );
  $this
    ->createHeaderRule(strtolower($name), $name, 'Cache-Control', 'max-age=3600', $visibility_options);

  // Confirm that the header rule has been created, and then
  // query the created machine_name.
  $this
    ->assertText(t('!name has been created.', array(
    '!name' => strtolower($name),
  )), 'Header rule successfully created.');
  $machine_name = db_query("SELECT machine_name FROM {http_response_headers} WHERE description= :name", array(
    ':name' => strtolower($name),
  ))
    ->fetchField();

  // Check to see if the header rule was created by checking
  // that it's in the database.
  $this
    ->assertNotNull($machine_name, 'Header rule found in database');

  // Check that http_response_headers_rule_load() returns
  // the correct name and content.
  $data = http_response_headers_rule_load($machine_name);
  $this
    ->assertEqual('max-age=3600', $data->header_value, 'http_response_headers_rule_load() provides correct header details.');

  // Verify presence of configure and delete links for header rule.
  $this
    ->drupalGet('admin/config/system/http-response-headers');
  $this
    ->assertLinkByHref('admin/config/system/http-response-headers/list/' . $machine_name . '/edit', 0, 'Header rule configure link found.');
  $this
    ->assertLinkByHref('admin/config/system/http-response-headers/list/' . $machine_name . '/delete', 0, 'Header rule delete link found.');

  // Set visibility only for authenticated users,
  // to verify delete functionality.
  $edit = array();
  $edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = TRUE;
  $this
    ->drupalPost('admin/config/system/http-response-headers/list/' . $machine_name . '/edit', $edit, t('Save'));

  // Delete the created header rule  & verify that it's been deleted
  // and no longer applying on the page.
  $this
    ->deleteHeaderRule(strtolower($name));
  $this
    ->assertRaw(t('The item has been deleted.'), 'Header rule successfully deleted.');
  $count = db_query("SELECT 1 FROM {http_response_headers} WHERE description= :name", array(
    ':name' => $name,
  ))
    ->fetchField();
  $this
    ->assertFalse($count, 'Table http_response_headers being cleaned.');
}