function StatisticsLoggingTestCase::testLogging

Verifies request logging for cached and uncached pages.

Archivo

drupal-7.x/modules/statistics/statistics.test, line 86
Tests for the Statistics module.

Class

StatisticsLoggingTestCase
Tests that logging via statistics_exit() works for all pages.

Código

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', 'Testing an uncached page.');
  $log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
  $this->assertTrue(is_array($log) && count($log) == 1, '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', 'Testing a cached page.');
  $log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
  $this->assertTrue(is_array($log) && count($log) == 2, '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, '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, 'Page request was logged.');
  $this->assertEqual(array_intersect_key($log[6], $expected), $expected);

  // Create a path longer than 255 characters. Drupal's .htaccess file
  // instructs Apache to test paths against the file system before routing to
  // index.php. Many file systems restrict file names to 255 characters
  // (http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits), and
  // Apache returns a 403 when testing longer file names, but the total path
  // length is not restricted.
  $long_path = $this->randomName(127) . '/' . $this->randomName(128);

  // 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));

}