You are here

function DomainCacheTest::testDomainPageCache in Domain Access 7.3

File

tests/domain.test, line 755
Simpletest for Domain Access.

Class

DomainCacheTest

Code

function testDomainPageCache() {
  $cache = variable_get('cache');
  $this
    ->assertTrue(!empty($cache), t('Page caching enabled.'));

  // This section borrowed from bootstrap.test.
  // Fill the cache.
  $this
    ->drupalGet('');

  // Check the output for our body classes.
  $domain_classes = domain_page_classes();
  $this
    ->assertRaw('domain-', t('Domain text found in body classes.'));
  $this
    ->assertRaw($domain_classes[0], t('Domain-specific body class rendered correctly: !class.', array(
    '!class' => $domain_classes[0],
  )));
  $this
    ->drupalHead('');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached on primary domain.'));
  $etag = $this
    ->drupalGetHeader('ETag');
  $last_modified = $this
    ->drupalGetHeader('Last-Modified');
  $user = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . $last_modified,
    'If-None-Match: ' . $etag,
  ));
  $this
    ->assertResponse(200, t('Conditional request returned 200 OK for authenticated user.'));
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), t('Primary domain was not cached for logged in user.'));

  // Try cache on a secondary domain.
  $domains = $this
    ->domainCreateDomains(array(
    'one',
  ));
  $this
    ->assertTrue(isset($domains['one']), t('Created test domain %domain', array(
    '%domain' => $domains['one']['subdomain'],
  )));

  // Test to see if this install supports the subdomain. If it does, test against it.
  $url = $domains['one']['path'] . drupal_get_path('module', 'domain') . '/tests/200.png';
  $response = drupal_http_request($url, array(
    'method' => 'HEAD',
  ));

  // If the check above fails, we cannot do further testing.
  if ($response->code != 200) {
    $this
      ->assertTrue(TRUE, t('The server cannot connect to %domain. Test complete.', array(
      '%domain' => $domains['one']['subdomain'],
    )));
    return;
  }
  $this
    ->assertTrue(TRUE, t('The server connected to %domain.', array(
    '%domain' => $domains['one']['subdomain'],
  )));

  // Logout the user and try again.
  $this
    ->drupalLogout($user);

  // Assert that page caching applies.
  $this
    ->drupalHead('');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached on primary domain.'));
  $options = array(
    'absolute' => TRUE,
  );
  $new_user = $this
    ->drupalCreateUser();

  // Fill the cache.
  $this
    ->drupalGet($domains['one']['path'], $options);

  // Check the output for our body classes.
  // Note that we set the domain to get the right variables.
  domain_set_domain($domains['one']['domain_id'], TRUE);
  $domain_classes2 = domain_page_classes();
  domain_reset_domain(TRUE);
  $this
    ->assertRaw('domain-', t('Domain text found in body classes.'));
  $this
    ->assertTrue($domain_classes[0] != $domain_classes2[0], t('Body class is different for secondary domain.'));
  $this
    ->assertRaw($domain_classes2[0], t('Domain-specific body class rendered correctly: !class.', array(
    '!class' => $domain_classes2[0],
  )));
  $this
    ->drupalHead($domains['one']['path'], $options);
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached on %domain.', array(
    '%domain' => $domains['one']['subdomain'],
  )));
  $etag = $this
    ->drupalGetHeader('ETag');
  $last_modified = $this
    ->drupalGetHeader('Last-Modified');

  // Try to login the user from the subdomain.
  // To do so, we must force $base_url to use the new domain for paths.
  $this
    ->domainSetBaseUrl($domains['one']);
  $this
    ->drupalLogin($new_user);
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . $last_modified,
    'If-None-Match: ' . $etag,
  ));
  $this
    ->assertResponse(200, t('Conditional request returned 200 OK for authenticated user.'));
  $this
    ->drupalGet($domains['one']['path'], $options, array(
    'If-Modified-Since: ' . $last_modified,
    'If-None-Match: ' . $etag,
  ));
  $this
    ->assertResponse(200, t('Conditional request returned 200 OK for authenticated user.'));
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), t('%domain was not cached for logged in user.', array(
    '%domain' => $domains['one']['path'],
  )));

  // Check that both pages were cached properly.
  $default = domain_default();
  $result = db_query("SELECT 1 FROM {cache_page} WHERE cid = :cid", array(
    ':cid' => $default['path'],
  ))
    ->fetchField();
  $this
    ->assertTrue(!empty($result), t('Primary domain cached correctly to {cache_page}.'));
  $result = db_query("SELECT 1 FROM {cache_page} WHERE cid = :cid", array(
    ':cid' => $domains['one']['path'],
  ))
    ->fetchField();
  $this
    ->assertTrue(!empty($result), t('%domain cached correctly to {cache_page}.', array(
    '%domain' => $domains['one']['path'],
  )));

  // Reset $base_url.
  $this
    ->domainResetBaseUrl();
}