diff --git a/core/modules/statistics/config/install/statistics.settings.yml b/core/modules/statistics/config/install/statistics.settings.yml index 6686062923..8c73e19338 100644 --- a/core/modules/statistics/config/install/statistics.settings.yml +++ b/core/modules/statistics/config/install/statistics.settings.yml @@ -1,2 +1,2 @@ -count_content_views: 0 +entity_type_ids: {} display_max_age: 3600 diff --git a/core/modules/statistics/config/schema/statistics.schema.yml b/core/modules/statistics/config/schema/statistics.schema.yml index c72a227264..0c2cd05008 100644 --- a/core/modules/statistics/config/schema/statistics.schema.yml +++ b/core/modules/statistics/config/schema/statistics.schema.yml @@ -4,12 +4,15 @@ statistics.settings: type: config_object label: 'Statistics settings' mapping: - count_content_views: - type: integer - label: 'Count content views' display_max_age: type: integer label: 'How long any statistics may be cached, i.e. the refresh interval' + entity_type_ids: + type: sequence + label: 'Entity Type IDs' + sequence: + type: string + label: 'Entity Type ID' block.settings.statistics_popular_block: type: block_settings diff --git a/core/modules/statistics/migration_templates/statistics_settings.yml b/core/modules/statistics/migration_templates/statistics_settings.yml index 1f5b5bbd0e..6a6bc0e4ff 100644 --- a/core/modules/statistics/migration_templates/statistics_settings.yml +++ b/core/modules/statistics/migration_templates/statistics_settings.yml @@ -10,7 +10,11 @@ source: - statistics_flush_accesslog_timer - statistics_count_content_views process: - 'count_content_views': statistics_count_content_views + entity_type_ids: + - + plugin: callback + callable: statistics_migrate_callback + source: statistics_count_content_views destination: plugin: config config_name: statistics.settings diff --git a/core/modules/statistics/src/NodeStatisticsDatabaseStorage.php b/core/modules/statistics/src/NodeStatisticsDatabaseStorage.php deleted file mode 100644 index 0cccb1700b..0000000000 --- a/core/modules/statistics/src/NodeStatisticsDatabaseStorage.php +++ /dev/null @@ -1,148 +0,0 @@ -connection = $connection; - $this->state = $state; - $this->requestStack = $request_stack; - } - - /** - * {@inheritdoc} - */ - public function recordView($id) { - return (bool) $this->connection - ->merge('node_counter') - ->key('nid', $id) - ->fields([ - 'daycount' => 1, - 'totalcount' => 1, - 'timestamp' => $this->getRequestTime(), - ]) - ->expression('daycount', 'daycount + 1') - ->expression('totalcount', 'totalcount + 1') - ->execute(); - } - - /** - * {@inheritdoc} - */ - public function fetchViews($ids) { - $views = $this->connection - ->select('node_counter', 'nc') - ->fields('nc', ['totalcount', 'daycount', 'timestamp']) - ->condition('nid', $ids, 'IN') - ->execute() - ->fetchAll(); - foreach ($views as $id => $view) { - $views[$id] = new StatisticsViewsResult($view->totalcount, $view->daycount, $view->timestamp); - } - return $views; - } - - /** - * {@inheritdoc} - */ - public function fetchView($id) { - $views = $this->fetchViews([$id]); - return reset($views); - } - - /** - * {@inheritdoc} - */ - public function fetchAll($order = 'totalcount', $limit = 5) { - assert(in_array($order, ['totalcount', 'daycount', 'timestamp']), "Invalid order argument."); - - return $this->connection - ->select('node_counter', 'nc') - ->fields('nc', ['nid']) - ->orderBy($order, 'DESC') - ->range(0, $limit) - ->execute() - ->fetchCol(); - } - - /** - * {@inheritdoc} - */ - public function deleteViews($id) { - return (bool) $this->connection - ->delete('node_counter') - ->condition('nid', $id) - ->execute(); - } - - /** - * {@inheritdoc} - */ - public function resetDayCount() { - $statistics_timestamp = $this->state->get('statistics.day_timestamp') ?: 0; - if (($this->getRequestTime() - $statistics_timestamp) >= 86400) { - $this->state->set('statistics.day_timestamp', $this->getRequestTime()); - $this->connection->update('node_counter') - ->fields(['daycount' => 0]) - ->execute(); - } - } - - /** - * {@inheritdoc} - */ - public function maxTotalCount() { - $query = $this->connection->select('node_counter', 'nc'); - $query->addExpression('MAX(totalcount)'); - $max_total_count = (int)$query->execute()->fetchField(); - return $max_total_count; - } - - /** - * Get current request time. - * - * @return int - * Unix timestamp for current server request time. - */ - protected function getRequestTime() { - return $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME'); - } - -} diff --git a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php index 1802a4569c..a0805bb327 100644 --- a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php +++ b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php @@ -83,7 +83,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->get('entity_type.manager'), $container->get('entity.repository'), - $container->get('statistics.storage.node'), + $container->get('statistics.storage'), $container->get('renderer') ); } @@ -95,7 +95,7 @@ public function defaultConfiguration() { return [ 'top_day_num' => 0, 'top_all_num' => 0, - 'top_last_num' => 0 + 'top_last_num' => 0, ]; } @@ -151,9 +151,10 @@ public function blockSubmit($form, FormStateInterface $form_state) { */ public function build() { $content = []; + $entity_type = $this->entityTypeManager->getDefinition('node'); if ($this->configuration['top_day_num'] > 0) { - $nids = $this->statisticsStorage->fetchAll('daycount', $this->configuration['top_day_num']); + $nids = $this->statisticsStorage->fetchAll($entity_type, 'daycount', $this->configuration['top_day_num']); if ($nids) { $content['top_day'] = $this->nodeTitleList($nids, $this->t("Today's:")); $content['top_day']['#suffix'] = '
'; @@ -161,7 +162,7 @@ public function build() { } if ($this->configuration['top_all_num'] > 0) { - $nids = $this->statisticsStorage->fetchAll('totalcount', $this->configuration['top_all_num']); + $nids = $this->statisticsStorage->fetchAll($entity_type, 'totalcount', $this->configuration['top_all_num']); if ($nids) { $content['top_all'] = $this->nodeTitleList($nids, $this->t('All time:')); $content['top_all']['#suffix'] = '
'; @@ -169,7 +170,7 @@ public function build() { } if ($this->configuration['top_last_num'] > 0) { - $nids = $this->statisticsStorage->fetchAll('timestamp', $this->configuration['top_last_num']); + $nids = $this->statisticsStorage->fetchAll($entity_type, 'timestamp', $this->configuration['top_last_num']); $content['top_last'] = $this->nodeTitleList($nids, $this->t('Last viewed:')); $content['top_last']['#suffix'] = '
'; } diff --git a/core/modules/statistics/src/Plugin/views/field/EntityCounterTimestamp.php b/core/modules/statistics/src/Plugin/views/field/EntityCounterTimestamp.php new file mode 100644 index 0000000000..9bc2a9c089 --- /dev/null +++ b/core/modules/statistics/src/Plugin/views/field/EntityCounterTimestamp.php @@ -0,0 +1,24 @@ +hasPermission('view post access counter'); + } + +} diff --git a/core/modules/statistics/src/Plugin/views/field/NodeCounterTimestamp.php b/core/modules/statistics/src/Plugin/views/field/NodeCounterTimestamp.php index fb0eb3049e..037526920f 100644 --- a/core/modules/statistics/src/Plugin/views/field/NodeCounterTimestamp.php +++ b/core/modules/statistics/src/Plugin/views/field/NodeCounterTimestamp.php @@ -2,9 +2,6 @@ namespace Drupal\statistics\Plugin\views\field; -use Drupal\views\Plugin\views\field\Date; -use Drupal\Core\Session\AccountInterface; - /** * Field handler to display the most recent time the node has been viewed. * @@ -12,13 +9,6 @@ * * @ViewsField("node_counter_timestamp") */ -class NodeCounterTimestamp extends Date { - - /** - * {@inheritdoc} - */ - public function access(AccountInterface $account) { - return $account->hasPermission('view post access counter'); - } +class NodeCounterTimestamp extends EntityCounterTimestamp { } diff --git a/core/modules/statistics/src/StatisticsDatabaseStorage.php b/core/modules/statistics/src/StatisticsDatabaseStorage.php new file mode 100644 index 0000000000..db4b720bf5 --- /dev/null +++ b/core/modules/statistics/src/StatisticsDatabaseStorage.php @@ -0,0 +1,198 @@ +connection = $connection; + $this->requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public function recordView($entity_type_id, $key , $id) { + $table = $entity_type_id . '_counter'; + try { + return (bool) $this->connection + ->merge($table) + ->key($key, $id) + ->fields([ + 'daycount' => 1, + 'totalcount' => 1, + 'timestamp' => $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME'), + ]) + ->expression('daycount', 'daycount + 1') + ->expression('totalcount', 'totalcount + 1') + ->execute(); + } + catch(\Exception $e) { + $database_schema = $this->connection->schema(); + if ($database_schema->tableExists($table)) { + throw $e; + } + else { + $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id); + $this->createTable($entity_type); + $this->recordView($entity_type_id, $key, $id); + } + } + } + + /** + * {@inheritdoc} + */ + public function fetchView(EntityTypeInterface $entity_type, $id) { + $entity_type_id = $entity_type->id(); + $table = $entity_type_id . '_counter'; + $view = $this->connection + ->select($table, 'c') + ->fields('c', ['totalcount', 'daycount', 'timestamp']) + ->condition($entity_type->getKey('id'), $id) + ->execute() + ->fetchObject(); + if ($view) { + return new StatisticsViewsResult($view->totalcount, $view->daycount, $view->timestamp); + } + return NULL; + } + + /** + * {@inheritdoc} + */ + public function fetchAll(EntityTypeInterface $entity_type, $order = 'totalcount', $limit = 5) { + assert(in_array($order, ['totalcount', 'daycount', 'timestamp']), "Invalid order argument."); + $table = $entity_type->id() . '_counter'; + return $this->connection + ->select($table, 'nc') + ->fields('nc', [$entity_type->getKey('id')]) + ->orderBy($order, 'DESC') + ->range(0, $limit) + ->execute() + ->fetchCol(); + } + + /** + * {@inheritdoc} + */ + public function deleteViews(EntityTypeInterface $entity_type, $id) { + $table = $entity_type->id() . '_counter'; + return (bool) $this->connection + ->delete($table) + ->condition($entity_type->getKey('id'), $id) + ->execute(); + } + + /** + * {@inheritdoc} + */ + public function maxTotalCount(EntityTypeInterface $entity_type) { + $table = $entity_type->id() . '_counter'; + $query = $this->connection->select($table, 'nc'); + $query->addExpression('MAX(totalcount)'); + $max_total_count = (int) $query->execute()->fetchField(); + return $max_total_count; + } + + /** + * {@inheritdoc} + */ + public function createTable(EntityTypeInterface $entity_type) { + $entity_type_id = $entity_type->id(); + $idKey = $entity_type->getKey('id'); + $id_definition = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($entity_type_id)[$idKey]; + if ($id_definition->getType() === 'integer') { + $id_schema = [ + 'description' => "The {{$entity_type_id}}.$idKey for these statistics.", + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ]; + } + else { + $id_schema = [ + 'description' => "The {{$entity_type_id}}.$idKey for these statistics.", + 'type' => 'varchar_ascii', + 'length' => 128, + 'not null' => TRUE, + ]; + } + $table = $entity_type_id . '_counter'; + if (!$this->connection->schema()->tableExists($table)) { + $schema = [ + 'description' => "Access statistics for {{$entity_type_id}}s.", + 'fields' => [ + $idKey => $id_schema, + 'totalcount' => [ + 'description' => "The total number of times the {{$entity_type_id}} has been viewed.", + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + 'size' => 'big', + ], + 'daycount' => [ + 'description' => "The total number of times the {{$entity_type_id}} has been viewed today.", + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + 'size' => 'medium', + ], + 'timestamp' => [ + 'description' => "The most recent time the {{$entity_type_id}} has been viewed.", + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ], + ], + 'primary key' => [$idKey], + ]; + $this->connection->schema()->createTable($table, $schema); + } + } + + /** + * {@inheritdoc} + */ + public function dropTable(EntityTypeInterface $entity_type) { + $entity_type_id = $entity_type->id(); + $table = $entity_type_id . '_counter'; + if ($this->connection->schema()->tableExists($table)) { + $this->connection->schema()->dropTable($table); + } + } + +} diff --git a/core/modules/statistics/src/StatisticsResetCount.php b/core/modules/statistics/src/StatisticsResetCount.php new file mode 100644 index 0000000000..d678939aa6 --- /dev/null +++ b/core/modules/statistics/src/StatisticsResetCount.php @@ -0,0 +1,67 @@ +connection = $connection; + $this->state = $state; + $this->time = $time; + } + + /** + * {@inheritdoc} + */ + public function resetDayCount($entity_type_id) { + $statistics_timestamp = $this->state->get('statistics.day_timestamp') ?: 0; + $time = $this->time->getRequestTime(); + $table = $entity_type_id . '_counter'; + if (($time - $statistics_timestamp) >= 86400 && $this->connection->schema()->tableExists($table)) { + $this->state->set('statistics.day_timestamp', $time); + $this->connection->update($table) + ->fields(['daycount' => 0]) + ->execute(); + } + } + +} diff --git a/core/modules/statistics/src/StatisticsResetCountInterface.php b/core/modules/statistics/src/StatisticsResetCountInterface.php new file mode 100644 index 0000000000..d4b493cfa3 --- /dev/null +++ b/core/modules/statistics/src/StatisticsResetCountInterface.php @@ -0,0 +1,18 @@ +entityTypeManager = $entity_type_manager; + $this->entityTypeRepository = $entity_type_repository; + $this->statisticsStorage = $statistics_storage; $this->moduleHandler = $module_handler; } @@ -40,7 +71,10 @@ public function __construct(ConfigFactoryInterface $config_factory, ModuleHandle public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), - $container->get('module_handler') + $container->get('module_handler'), + $container->get('entity_type.manager'), + $container->get('entity_type.repository'), + $container->get('statistics.storage') ); } @@ -63,18 +97,27 @@ protected function getEditableConfigNames() { */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('statistics.settings'); + $labels = $this->entityTypeRepository->getEntityTypeLabels(TRUE); + $labels = $labels[(string) $this->t('Content', [], ['context' => 'Entity type group'])]; + $options = []; + foreach ($labels as $entity_type_id => $label) { + $options[$entity_type_id] = $this->t('Enable statistics for @entity_type', ['@entity_type' => $label]); + } - // Content counter settings. + // Content entity counter settings. $form['content'] = [ '#type' => 'details', - '#title' => t('Content viewing counter settings'), + '#title' => $this->t('Content viewing counter settings'), + '#description' => $this->t('Increment a counter each time content is viewed.'), '#open' => TRUE, ]; - $form['content']['statistics_count_content_views'] = [ - '#type' => 'checkbox', - '#title' => t('Count content views'), - '#default_value' => $config->get('count_content_views'), - '#description' => t('Increment a counter each time content is viewed.'), + + $form['content']['entity_type_ids'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Select content'), + '#options' => $options, + '#default_value' => $config->get('entity_type_ids'), + ]; return parent::buildForm($form, $form_state); @@ -84,9 +127,19 @@ public function buildForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { + $entity_type_ids = $form_state->getValue('entity_type_ids'); $this->config('statistics.settings') - ->set('count_content_views', $form_state->getValue('statistics_count_content_views')) + ->set('entity_type_ids', $entity_type_ids) ->save(); + foreach (array_keys($entity_type_ids) as $entity_type_id) { + $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); + if ($entity_type_ids[$entity_type_id] === $entity_type_id) { + $this->statisticsStorage->createTable($entity_type); + } + else { + $this->statisticsStorage->dropTable($entity_type); + } + } // The popular statistics block is dependent on these settings, so clear the // block plugin definitions cache. diff --git a/core/modules/statistics/src/StatisticsStorageInterface.php b/core/modules/statistics/src/StatisticsStorageInterface.php index ad6d6d5dce..8b4c5dd70c 100644 --- a/core/modules/statistics/src/StatisticsStorageInterface.php +++ b/core/modules/statistics/src/StatisticsStorageInterface.php @@ -2,6 +2,8 @@ namespace Drupal\statistics; +use Drupal\Core\Entity\EntityTypeInterface; + /** * Provides an interface defining Statistics Storage. * @@ -13,78 +15,87 @@ /** * Count a entity view. * + * @param string $entity_type_id + * The entity type ID. + * @param int $key + * The ID key of the entity to count. * @param int $id * The ID of the entity to count. * * @return bool * TRUE if the entity view has been counted. */ - public function recordView($id); - - /** - * Returns the number of times entities have been viewed. - * - * @param array $ids - * An array of IDs of entities to fetch the views for. - * - * @return \Drupal\statistics\StatisticsViewsResult[] - * An array of value objects representing the number of times each entity - * has been viewed. The array is keyed by entity ID. If an ID does not - * exist, it will not be present in the array. - */ - public function fetchViews($ids); + public function recordView($entity_type_id, $key, $id); /** * Returns the number of times a single entity has been viewed. * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. * @param int $id * The ID of the entity to fetch the views for. * - * @return \Drupal\statistics\StatisticsViewsResult|false - * If the entity exists, a value object representing the number of times if - * has been viewed. If it does not exist, FALSE is returned. + * @return \Drupal\statistics\StatisticsViewsResult|null + * The StatisticsViewsResult object if the result is present NULL otherwise. */ - public function fetchView($id); + public function fetchView(EntityTypeInterface $entity_type, $id); /** - * Returns the number of times a entity has been viewed. + * Returns the number of times an entity has been viewed. * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. * @param string $order * The counter name to order by: * - 'totalcount' The total number of views. * - 'daycount' The number of views today. * - 'timestamp' The unix timestamp of the last view. - * * @param int $limit * The number of entity IDs to return. * * @return array * An ordered array of entity IDs. */ - public function fetchAll($order = 'totalcount', $limit = 5); + public function fetchAll(EntityTypeInterface $entity_type, $order = 'totalcount', $limit = 5); /** * Delete counts for a specific entity. * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. * @param int $id * The ID of the entity which views to delete. * * @return bool * TRUE if the entity views have been deleted. */ - public function deleteViews($id); - - /** - * Reset the day counter for all entities once every day. - */ - public function resetDayCount(); + public function deleteViews(EntityTypeInterface $entity_type, $id); /** * Returns the highest 'totalcount' value. * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * * @return int * The highest 'totalcount' value. */ - public function maxTotalCount(); + public function maxTotalCount(EntityTypeInterface $entity_type); + + /** + * Creates entity counter table. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + */ + public function createTable(EntityTypeInterface $entity_type); + + /** + * Drops entity counter table. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + */ + public function dropTable(EntityTypeInterface $entity_type); } diff --git a/core/modules/statistics/src/Tests/StatisticsAdminTest.php b/core/modules/statistics/src/Tests/StatisticsAdminTest.php deleted file mode 100644 index 3ce0457d43..0000000000 --- a/core/modules/statistics/src/Tests/StatisticsAdminTest.php +++ /dev/null @@ -1,170 +0,0 @@ -config('statistics.settings')->set('display_max_age', 0)->save(); - - // Create Basic page node type. - if ($this->profile != 'standard') { - $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); - } - $this->privilegedUser = $this->drupalCreateUser(['administer statistics', 'view post access counter', 'create page content']); - $this->drupalLogin($this->privilegedUser); - $this->testNode = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->privilegedUser->id()]); - $this->client = \Drupal::httpClient(); - } - - /** - * Verifies that the statistics settings page works. - */ - public function testStatisticsSettings() { - $config = $this->config('statistics.settings'); - $this->assertFalse($config->get('count_content_views'), 'Count content view log is disabled by default.'); - - // Enable counter on content view. - $edit['statistics_count_content_views'] = 1; - $this->drupalPostForm('admin/config/system/statistics', $edit, t('Save configuration')); - $config = $this->config('statistics.settings'); - $this->assertTrue($config->get('count_content_views'), 'Count content view log is enabled.'); - - // Hit the node. - $this->drupalGet('node/' . $this->testNode->id()); - // Manually calling statistics.php, simulating ajax behavior. - $nid = $this->testNode->id(); - $post = ['nid' => $nid]; - global $base_url; - $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'; - $this->client->post($stats_path, ['form_params' => $post]); - - // Hit the node again (the counter is incremented after the hit, so - // "1 view" will actually be shown when the node is hit the second time). - $this->drupalGet('node/' . $this->testNode->id()); - $this->client->post($stats_path, ['form_params' => $post]); - $this->assertText('1 view', 'Node is viewed once.'); - - $this->drupalGet('node/' . $this->testNode->id()); - $this->client->post($stats_path, ['form_params' => $post]); - $this->assertText('2 views', 'Node is viewed 2 times.'); - - // Increase the max age to test that nodes are no longer immediately - // updated, visit the node once more to populate the cache. - $this->config('statistics.settings')->set('display_max_age', 3600)->save(); - $this->drupalGet('node/' . $this->testNode->id()); - $this->assertText('3 views', 'Node is viewed 3 times.'); - - $this->client->post($stats_path, ['form_params' => $post]); - $this->drupalGet('node/' . $this->testNode->id()); - $this->assertText('3 views', 'Views counter was not updated.'); - } - - /** - * Tests that when a node is deleted, the node counter is deleted too. - */ - public function testDeleteNode() { - $this->config('statistics.settings')->set('count_content_views', 1)->save(); - - $this->drupalGet('node/' . $this->testNode->id()); - // Manually calling statistics.php, simulating ajax behavior. - $nid = $this->testNode->id(); - $post = ['nid' => $nid]; - global $base_url; - $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'; - $this->client->post($stats_path, ['form_params' => $post]); - - $result = db_select('node_counter', 'n') - ->fields('n', ['nid']) - ->condition('n.nid', $this->testNode->id()) - ->execute() - ->fetchAssoc(); - $this->assertEqual($result['nid'], $this->testNode->id(), 'Verifying that the node counter is incremented.'); - - $this->testNode->delete(); - - $result = db_select('node_counter', 'n') - ->fields('n', ['nid']) - ->condition('n.nid', $this->testNode->id()) - ->execute() - ->fetchAssoc(); - $this->assertFalse($result, 'Verifying that the node counter is deleted.'); - } - - /** - * Tests that cron clears day counts and expired access logs. - */ - public function testExpiredLogs() { - $this->config('statistics.settings') - ->set('count_content_views', 1) - ->save(); - \Drupal::state()->set('statistics.day_timestamp', 8640000); - - $this->drupalGet('node/' . $this->testNode->id()); - // Manually calling statistics.php, simulating ajax behavior. - $nid = $this->testNode->id(); - $post = ['nid' => $nid]; - global $base_url; - $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'; - $this->client->post($stats_path, ['form_params' => $post]); - $this->drupalGet('node/' . $this->testNode->id()); - $this->client->post($stats_path, ['form_params' => $post]); - $this->assertText('1 view', 'Node is viewed once.'); - - // statistics_cron() will subtract - // statistics.settings:accesslog.max_lifetime config from REQUEST_TIME in - // the delete query, so wait two secs here to make sure the access log will - // be flushed for the node just hit. - sleep(2); - $this->cronRun(); - - $this->drupalGet('admin/reports/pages'); - $this->assertNoText('node/' . $this->testNode->id(), 'No hit URL found.'); - - $result = db_select('node_counter', 'nc') - ->fields('nc', ['daycount']) - ->condition('nid', $this->testNode->id(), '=') - ->execute() - ->fetchField(); - $this->assertFalse($result, 'Daycounter is zero.'); - } - -} diff --git a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php deleted file mode 100644 index a75f89638d..0000000000 --- a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php +++ /dev/null @@ -1,142 +0,0 @@ -profile != 'standard') { - $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); - } - - $this->authUser = $this->drupalCreateUser([ - // For node creation. - 'access content', - 'create page content', - 'edit own page content', - // For language negotiation administration. - 'administer languages', - 'access administration pages', - ]); - - // Ensure we have a node page to access. - $this->node = $this->drupalCreateNode(['title' => $this->randomMachineName(255), 'uid' => $this->authUser->id()]); - - // Add a custom language and enable path-based language negotiation. - $this->drupalLogin($this->authUser); - $this->language = [ - 'predefined_langcode' => 'custom', - 'langcode' => 'xx', - 'label' => $this->randomMachineName(16), - 'direction' => 'ltr', - ]; - $this->drupalPostForm('admin/config/regional/language/add', $this->language, t('Add custom language')); - $this->drupalPostForm('admin/config/regional/language/detection', ['language_interface[enabled][language-url]' => 1], t('Save settings')); - $this->drupalLogout(); - - // Enable access logging. - $this->config('statistics.settings') - ->set('count_content_views', 1) - ->save(); - - // Clear the logs. - db_truncate('node_counter'); - $this->client = \Drupal::httpClient(); - } - - /** - * Verifies node hit counter logging and script placement. - */ - public 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 = '/