You are here

function StatisticsLoggingTest::testLogging in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/statistics/src/Tests/StatisticsLoggingTest.php \Drupal\statistics\Tests\StatisticsLoggingTest::testLogging()

Verifies node hit counter logging and script placement.

File

core/modules/statistics/src/Tests/StatisticsLoggingTest.php, line 98
Contains \Drupal\statistics\Tests\StatisticsLoggingTest.

Class

StatisticsLoggingTest
Tests request logging for cached and uncached pages.

Namespace

Drupal\statistics\Tests

Code

function testLogging() {
  $path = 'node/' . $this->node
    ->id();
  $module_path = drupal_get_path('module', 'statistics');
  $stats_path = base_path() . $module_path . '/statistics.php';
  $lib_path = base_path() . $module_path . '/statistics.js';
  $expected_library = '/<script src=".*?' . preg_quote($lib_path, '/.') . '.*?">/is';

  // Verify that logging scripts are not found on a non-node page.
  $this
    ->drupalGet('node');
  $settings = $this
    ->getDrupalSettings();
  $this
    ->assertNoPattern($expected_library, 'Statistics library JS not found on node page.');
  $this
    ->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');

  // Verify that logging scripts are not found on a non-existent node page.
  $this
    ->drupalGet('node/9999');
  $settings = $this
    ->getDrupalSettings();
  $this
    ->assertNoPattern($expected_library, 'Statistics library JS not found on non-existent node page.');
  $this
    ->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');

  // Verify that logging scripts are found on a valid node page.
  $this
    ->drupalGet($path);
  $settings = $this
    ->getDrupalSettings();
  $this
    ->assertPattern($expected_library, 'Found statistics library JS on node page.');
  $this
    ->assertIdentical($this->node
    ->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on node page.');

  // Verify the same when loading the site in a non-default language.
  $this
    ->drupalGet($this->language['langcode'] . '/' . $path);
  $settings = $this
    ->getDrupalSettings();
  $this
    ->assertPattern($expected_library, 'Found statistics library JS on a valid node page in a non-default language.');
  $this
    ->assertIdentical($this->node
    ->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on valid node page in a non-default language.');

  // Manually call statistics.php to simulate ajax data collection behavior.
  global $base_root;
  $post = array(
    'nid' => $this->node
      ->id(),
  );
  $this->client
    ->post($base_root . $stats_path, array(
    'form_params' => $post,
  ));
  $node_counter = statistics_get($this->node
    ->id());
  $this
    ->assertIdentical($node_counter['totalcount'], '1');
}