function StatisticsLoggingTest::testLogging

Verifies request logging for cached and uncached pages.

File

drupal/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsLoggingTest.php, line 77
Definition of Drupal\statistics\Tests\StatisticsLoggingTest.

Class

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

Namespace

Drupal\statistics\Tests

Code

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

  // Verify logging of an uncached page.
  $this
    ->drupalGet($path);

  // Manually calling statistics.php, simulating ajax behavior.
  $nid = $this->node->nid;
  $post = http_build_query(array(
    'nid' => $nid,
  ));
  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  );
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  $this->client
    ->post($stats_path, $headers, $post)
    ->send();
  $this
    ->assertIdentical($this
    ->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Testing an uncached page.');
  $node_counter = statistics_get($this->node->nid);
  $this
    ->assertIdentical($node_counter['totalcount'], '1');

  // Verify logging of a cached page.
  $this
    ->drupalGet($path);

  // Manually calling statistics.php, simulating ajax behavior.
  $this->client
    ->post($stats_path, $headers, $post)
    ->send();
  $this
    ->assertIdentical($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Testing a cached page.');
  $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);

  // Manually calling statistics.php, simulating ajax behavior.
  $this->client
    ->post($stats_path, $headers, $post)
    ->send();
  $node_counter = statistics_get($this->node->nid);
  $this
    ->assertIdentical($node_counter['totalcount'], '3');
}