StatisticsLoggingTestCase::testLogging

7 statistics.test StatisticsLoggingTestCase::testLogging()
8 statistics.test StatisticsLoggingTestCase::testLogging()

Verifies request logging for cached and uncached pages.

File

modules/statistics/statistics.test, line 86
Tests for statistics.module.

Code

function testLogging() {
  $path = 'node/' . $this->node->nid;
  $expected = array(
    'title' => $this->node->title, 
    'path' => $path,
  );

  // Verify logging of an uncached page.
  $this->drupalGet($path);
  $this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', t('Testing an uncached page.'));
  $log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
  $this->assertTrue(is_array($log) && count($log) == 1, t('Page request was logged.'));
  $this->assertEqual(array_intersect_key($log[0], $expected), $expected);
  $node_counter = statistics_get($this->node->nid);
  $this->assertIdentical($node_counter['totalcount'], '1');

  // Verify logging of a cached page.
  $this->drupalGet($path);
  $this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Testing a cached page.'));
  $log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
  $this->assertTrue(is_array($log) && count($log) == 2, t('Page request was logged.'));
  $this->assertEqual(array_intersect_key($log[1], $expected), $expected);
  $node_counter = statistics_get($this->node->nid);
  $this->assertIdentical($node_counter['totalcount'], '2');

  // Test logging from authenticated users
  $this->drupalLogin($this->auth_user);
  $this->drupalGet($path);
  $log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
  // Check the 6th item since login and account pages are also logged
  $this->assertTrue(is_array($log) && count($log) == 6, t('Page request was logged.'));
  $this->assertEqual(array_intersect_key($log[5], $expected), $expected);
  $node_counter = statistics_get($this->node->nid);
  $this->assertIdentical($node_counter['totalcount'], '3');

  // Visit edit page to generate a title greater than 255.
  $path = 'node/' . $this->node->nid . '/edit';
  $expected = array(
    'title' => truncate_utf8(t('Edit Basic page') . ' ' . $this->node->title, 255), 
    'path' => $path,
  );
  $this->drupalGet($path);
  $log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
  $this->assertTrue(is_array($log) && count($log) == 7, t('Page request was logged.'));
  $this->assertEqual(array_intersect_key($log[6], $expected), $expected);

  // Create a path longer than 255 characters.
  $long_path = $this->randomName(256);

  // Test that the long path is properly truncated when logged.
  $this->drupalGet($long_path);
  $log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
  $this->assertTrue(is_array($log) && count($log) == 8, 'Page request was logged for a path over 255 characters.');
  $this->assertEqual($log[7]['path'], truncate_utf8($long_path, 255));

}
Login or register to post comments