Chris@0: config('statistics.settings')->set('display_max_age', 0)->save(); Chris@0: Chris@0: // Create Basic page node type. Chris@0: if ($this->profile != 'standard') { Chris@0: $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); Chris@0: } Chris@0: $this->privilegedUser = $this->drupalCreateUser(['administer statistics', 'view post access counter', 'create page content']); Chris@0: $this->drupalLogin($this->privilegedUser); Chris@0: $this->testNode = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->privilegedUser->id()]); Chris@0: $this->client = \Drupal::httpClient(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies that the statistics settings page works. Chris@0: */ Chris@0: public function testStatisticsSettings() { Chris@0: $config = $this->config('statistics.settings'); Chris@0: $this->assertFalse($config->get('count_content_views'), 'Count content view log is disabled by default.'); Chris@0: Chris@0: // Enable counter on content view. Chris@0: $edit['statistics_count_content_views'] = 1; Chris@0: $this->drupalPostForm('admin/config/system/statistics', $edit, t('Save configuration')); Chris@0: $config = $this->config('statistics.settings'); Chris@0: $this->assertTrue($config->get('count_content_views'), 'Count content view log is enabled.'); Chris@0: Chris@0: // Hit the node. Chris@0: $this->drupalGet('node/' . $this->testNode->id()); Chris@0: // Manually calling statistics.php, simulating ajax behavior. Chris@0: $nid = $this->testNode->id(); Chris@0: $post = ['nid' => $nid]; Chris@0: global $base_url; Chris@0: $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'; Chris@0: $this->client->post($stats_path, ['form_params' => $post]); Chris@0: Chris@0: // Hit the node again (the counter is incremented after the hit, so Chris@0: // "1 view" will actually be shown when the node is hit the second time). Chris@0: $this->drupalGet('node/' . $this->testNode->id()); Chris@0: $this->client->post($stats_path, ['form_params' => $post]); Chris@0: $this->assertText('1 view', 'Node is viewed once.'); Chris@0: Chris@0: $this->drupalGet('node/' . $this->testNode->id()); Chris@0: $this->client->post($stats_path, ['form_params' => $post]); Chris@0: $this->assertText('2 views', 'Node is viewed 2 times.'); Chris@0: Chris@0: // Increase the max age to test that nodes are no longer immediately Chris@0: // updated, visit the node once more to populate the cache. Chris@0: $this->config('statistics.settings')->set('display_max_age', 3600)->save(); Chris@0: $this->drupalGet('node/' . $this->testNode->id()); Chris@0: $this->assertText('3 views', 'Node is viewed 3 times.'); Chris@0: Chris@0: $this->client->post($stats_path, ['form_params' => $post]); Chris@0: $this->drupalGet('node/' . $this->testNode->id()); Chris@0: $this->assertText('3 views', 'Views counter was not updated.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that when a node is deleted, the node counter is deleted too. Chris@0: */ Chris@0: public function testDeleteNode() { Chris@0: $this->config('statistics.settings')->set('count_content_views', 1)->save(); Chris@0: Chris@0: $this->drupalGet('node/' . $this->testNode->id()); Chris@0: // Manually calling statistics.php, simulating ajax behavior. Chris@0: $nid = $this->testNode->id(); Chris@0: $post = ['nid' => $nid]; Chris@0: global $base_url; Chris@0: $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'; Chris@0: $this->client->post($stats_path, ['form_params' => $post]); Chris@0: Chris@18: $connection = Database::getConnection(); Chris@18: $result = $connection->select('node_counter', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->condition('n.nid', $this->testNode->id()) Chris@0: ->execute() Chris@0: ->fetchAssoc(); Chris@0: $this->assertEqual($result['nid'], $this->testNode->id(), 'Verifying that the node counter is incremented.'); Chris@0: Chris@0: $this->testNode->delete(); Chris@0: Chris@18: $result = $connection->select('node_counter', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->condition('n.nid', $this->testNode->id()) Chris@0: ->execute() Chris@0: ->fetchAssoc(); Chris@0: $this->assertFalse($result, 'Verifying that the node counter is deleted.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that cron clears day counts and expired access logs. Chris@0: */ Chris@0: public function testExpiredLogs() { Chris@0: $this->config('statistics.settings') Chris@0: ->set('count_content_views', 1) Chris@0: ->save(); Chris@0: \Drupal::state()->set('statistics.day_timestamp', 8640000); Chris@0: Chris@0: $this->drupalGet('node/' . $this->testNode->id()); Chris@0: // Manually calling statistics.php, simulating ajax behavior. Chris@0: $nid = $this->testNode->id(); Chris@0: $post = ['nid' => $nid]; Chris@0: global $base_url; Chris@0: $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'; Chris@0: $this->client->post($stats_path, ['form_params' => $post]); Chris@0: $this->drupalGet('node/' . $this->testNode->id()); Chris@0: $this->client->post($stats_path, ['form_params' => $post]); Chris@0: $this->assertText('1 view', 'Node is viewed once.'); Chris@0: Chris@0: // statistics_cron() will subtract Chris@0: // statistics.settings:accesslog.max_lifetime config from REQUEST_TIME in Chris@0: // the delete query, so wait two secs here to make sure the access log will Chris@0: // be flushed for the node just hit. Chris@0: sleep(2); Chris@0: $this->cronRun(); Chris@0: Chris@0: $this->drupalGet('admin/reports/pages'); Chris@0: $this->assertNoText('node/' . $this->testNode->id(), 'No hit URL found.'); Chris@0: Chris@18: $result = Database::getConnection()->select('node_counter', 'nc') Chris@0: ->fields('nc', ['daycount']) Chris@0: ->condition('nid', $this->testNode->id(), '=') Chris@0: ->execute() Chris@0: ->fetchField(); Chris@0: $this->assertFalse($result, 'Daycounter is zero.'); Chris@0: } Chris@0: Chris@0: }