Commit ae1a458d authored by webchick's avatar webchick

Issue #2172859 by iamEAP: Statistics JS and settings are not included on node pages.

parent 531fd593
......@@ -52,12 +52,6 @@ function setUp() {
// Ensure we have a node page to access.
$this->node = $this->drupalCreateNode(array('title' => $this->randomName(255), 'uid' => $this->auth_user->id()));
// Enable page caching.
$config = \Drupal::config('system.performance');
$config->set('cache.page.use_internal', 1);
$config->set('cache.page.max_age', 300);
$config->save();
// Enable access logging.
\Drupal::config('statistics.settings')
->set('count_content_views', 1)
......@@ -71,38 +65,36 @@ function setUp() {
}
/**
* Verifies request logging for cached and uncached pages.
* Verifies node hit counter logging and script placement.
*/
function testLogging() {
global $base_url;
$path = 'node/' . $this->node->id();
// Verify logging of an uncached page.
$module_path = drupal_get_path('module', 'statistics');
$stats_path = $base_url . '/' . $module_path . '/statistics.php';
$expected_library = $module_path . '/statistics.js';
$expected_settings = '"statistics":{"data":{"nid":"' . $this->node->id() . '"}';
// Verify that logging scripts are not found on a non-node page.
$this->drupalGet('node');
$this->assertNoRaw($expected_library, 'Statistics library JS not found on node page.');
$this->assertNoRaw($expected_settings, 'Statistics settings not found on node page.');
// Verify that logging scripts are not found on a non-existent node page.
$this->drupalGet('node/9999');
$this->assertNoRaw($expected_library, 'Statistics library JS not found on non-existent node page.');
$this->assertNoRaw($expected_settings, 'Statistics settings not found on non-existent node page.');
// Verify that logging scripts are found on a valid node page.
$this->drupalGet($path);
// Manually calling statistics.php, simulating ajax behavior.
$this->assertRaw($expected_library, 'Found statistics library JS on node page.');
$this->assertRaw($expected_settings, 'Found statistics settings on node page.');
// Manually call statistics.php to simulate ajax data collection behavior.
$nid = $this->node->id();
$post = array('nid' => $nid);
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
$this->client->post($stats_path, array(), $post)->send();
$this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Testing an uncached page.');
$node_counter = statistics_get($this->node->id());
$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, array(), $post)->send();
$this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Testing a cached page.');
$node_counter = statistics_get($this->node->id());
$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, array(), $post)->send();
$node_counter = statistics_get($this->node->id());
$this->assertIdentical($node_counter['totalcount'], '3');
}
}
......@@ -50,9 +50,9 @@ function statistics_permission() {
*/
function statistics_node_view(EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
$node->content['#attached']['library'][] = array('statistics', 'drupal.statistics');
$node->content['statistics_content_counter']['#attached']['library'][] = array('statistics', 'drupal.statistics');
$settings = array('data' => array('nid' => $node->id()), 'url' => url(drupal_get_path('module', 'statistics') . '/statistics.php'));
$node->content['#attached']['js'][] = array(
$node->content['statistics_content_counter']['#attached']['js'][] = array(
'data' => array('statistics' => $settings),
'type' => 'setting',
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment