Update core 8.3.0
This commit is contained in:
parent
da7a7918f8
commit
cd7a898e66
6144 changed files with 132297 additions and 87747 deletions
|
|
@ -0,0 +1,148 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\statistics;
|
||||
|
||||
use Drupal\Core\Database\Connection;
|
||||
use Drupal\Core\State\StateInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
* Provides the default database storage backend for statistics.
|
||||
*/
|
||||
class NodeStatisticsDatabaseStorage implements StatisticsStorageInterface {
|
||||
|
||||
/**
|
||||
* The database connection used.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* The state service.
|
||||
*
|
||||
* @var \Drupal\Core\State\StateInterface
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* The request stack.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* Constructs the statistics storage.
|
||||
*
|
||||
* @param \Drupal\Core\Database\Connection $connection
|
||||
* The database connection for the node view storage.
|
||||
* @param \Drupal\Core\State\StateInterface $state
|
||||
* The state service.
|
||||
*/
|
||||
public function __construct(Connection $connection, StateInterface $state, RequestStack $request_stack) {
|
||||
$this->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');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -4,8 +4,14 @@ namespace Drupal\statistics\Plugin\Block;
|
|||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
use Drupal\Core\Entity\EntityRepositoryInterface;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Render\RendererInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\statistics\StatisticsStorageInterface;
|
||||
|
||||
/**
|
||||
* Provides a 'Popular content' block.
|
||||
|
|
@ -15,17 +21,82 @@ use Drupal\Core\Session\AccountInterface;
|
|||
* admin_label = @Translation("Popular content")
|
||||
* )
|
||||
*/
|
||||
class StatisticsPopularBlock extends BlockBase {
|
||||
class StatisticsPopularBlock extends BlockBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The entity type manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||
*/
|
||||
protected $entityTypeManager;
|
||||
|
||||
/**
|
||||
* The entity repository service.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityRepositoryInterface
|
||||
*/
|
||||
protected $entityRepository;
|
||||
|
||||
/**
|
||||
* The storage for statistics.
|
||||
*
|
||||
* @var \Drupal\statistics\StatisticsStorageInterface
|
||||
*/
|
||||
protected $statisticsStorage;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Render\RendererInterface
|
||||
*/
|
||||
protected $renderer;
|
||||
|
||||
/**
|
||||
* Constructs an StatisticsPopularBlock object.
|
||||
*
|
||||
* @param array $configuration
|
||||
* A configuration array containing information about the plugin instance.
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the plugin instance.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
||||
* The entity type manager.
|
||||
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
||||
* The entity repository service
|
||||
* @param \Drupal\statistics\StatisticsStorageInterface $statistics_storage
|
||||
* The storage for statistics.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, StatisticsStorageInterface $statistics_storage, RendererInterface $renderer) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->entityTypeManager = $entity_type_manager;
|
||||
$this->entityRepository = $entity_repository;
|
||||
$this->statisticsStorage = $statistics_storage;
|
||||
$this->renderer = $renderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('entity_type.manager'),
|
||||
$container->get('entity.repository'),
|
||||
$container->get('statistics.storage.node'),
|
||||
$container->get('renderer')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return array(
|
||||
return [
|
||||
'top_day_num' => 0,
|
||||
'top_all_num' => 0,
|
||||
'top_last_num' => 0
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -40,29 +111,29 @@ class StatisticsPopularBlock extends BlockBase {
|
|||
*/
|
||||
public function blockForm($form, FormStateInterface $form_state) {
|
||||
// Popular content block settings.
|
||||
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40);
|
||||
$numbers = array('0' => $this->t('Disabled')) + array_combine($numbers, $numbers);
|
||||
$form['statistics_block_top_day_num'] = array(
|
||||
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40];
|
||||
$numbers = ['0' => $this->t('Disabled')] + array_combine($numbers, $numbers);
|
||||
$form['statistics_block_top_day_num'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t("Number of day's top views to display"),
|
||||
'#default_value' => $this->configuration['top_day_num'],
|
||||
'#options' => $numbers,
|
||||
'#description' => $this->t('How many content items to display in "day" list.'),
|
||||
);
|
||||
$form['statistics_block_top_all_num'] = array(
|
||||
];
|
||||
$form['statistics_block_top_all_num'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Number of all time views to display'),
|
||||
'#default_value' => $this->configuration['top_all_num'],
|
||||
'#options' => $numbers,
|
||||
'#description' => $this->t('How many content items to display in "all time" list.'),
|
||||
);
|
||||
$form['statistics_block_top_last_num'] = array(
|
||||
];
|
||||
$form['statistics_block_top_last_num'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Number of most recent views to display'),
|
||||
'#default_value' => $this->configuration['top_last_num'],
|
||||
'#options' => $numbers,
|
||||
'#description' => $this->t('How many content items to display in "recently viewed" list.'),
|
||||
);
|
||||
];
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
|
@ -79,31 +150,67 @@ class StatisticsPopularBlock extends BlockBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
$content = array();
|
||||
$content = [];
|
||||
|
||||
if ($this->configuration['top_day_num'] > 0) {
|
||||
$result = statistics_title_list('daycount', $this->configuration['top_day_num']);
|
||||
if ($result) {
|
||||
$content['top_day'] = node_title_list($result, $this->t("Today's:"));
|
||||
$nids = $this->statisticsStorage->fetchAll('daycount', $this->configuration['top_day_num']);
|
||||
if ($nids) {
|
||||
$content['top_day'] = $this->nodeTitleList($nids, $this->t("Today's:"));
|
||||
$content['top_day']['#suffix'] = '<br />';
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->configuration['top_all_num'] > 0) {
|
||||
$result = statistics_title_list('totalcount', $this->configuration['top_all_num']);
|
||||
if ($result) {
|
||||
$content['top_all'] = node_title_list($result, $this->t('All time:'));
|
||||
$nids = $this->statisticsStorage->fetchAll('totalcount', $this->configuration['top_all_num']);
|
||||
if ($nids) {
|
||||
$content['top_all'] = $this->nodeTitleList($nids, $this->t('All time:'));
|
||||
$content['top_all']['#suffix'] = '<br />';
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->configuration['top_last_num'] > 0) {
|
||||
$result = statistics_title_list('timestamp', $this->configuration['top_last_num']);
|
||||
$content['top_last'] = node_title_list($result, $this->t('Last viewed:'));
|
||||
$nids = $this->statisticsStorage->fetchAll('timestamp', $this->configuration['top_last_num']);
|
||||
$content['top_last'] = $this->nodeTitleList($nids, $this->t('Last viewed:'));
|
||||
$content['top_last']['#suffix'] = '<br />';
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the ordered array of node links for build().
|
||||
*
|
||||
* @param int[] $nids
|
||||
* An ordered array of node ids.
|
||||
* @param string $title
|
||||
* The title for the list.
|
||||
*
|
||||
* @return array
|
||||
* A render array for the list.
|
||||
*/
|
||||
protected function nodeTitleList(array $nids, $title) {
|
||||
$nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);
|
||||
|
||||
$items = [];
|
||||
foreach ($nids as $nid) {
|
||||
$node = $this->entityRepository->getTranslationFromContext($nodes[$nid]);
|
||||
$item = [
|
||||
'#type' => 'link',
|
||||
'#title' => $node->getTitle(),
|
||||
'#url' => $node->urlInfo('canonical'),
|
||||
];
|
||||
$this->renderer->addCacheableDependency($item, $node);
|
||||
$items[] = $item;
|
||||
}
|
||||
|
||||
return [
|
||||
'#theme' => 'item_list__node',
|
||||
'#items' => $items,
|
||||
'#title' => $title,
|
||||
'#cache' => [
|
||||
'tags' => $this->entityTypeManager->getDefinition('node')->getListCacheTags(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,17 +65,17 @@ class StatisticsSettingsForm extends ConfigFormBase {
|
|||
$config = $this->config('statistics.settings');
|
||||
|
||||
// Content counter settings.
|
||||
$form['content'] = array(
|
||||
$form['content'] = [
|
||||
'#type' => 'details',
|
||||
'#title' => t('Content viewing counter settings'),
|
||||
'#open' => TRUE,
|
||||
);
|
||||
$form['content']['statistics_count_content_views'] = array(
|
||||
];
|
||||
$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.'),
|
||||
);
|
||||
];
|
||||
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\statistics;
|
||||
|
||||
/**
|
||||
* Provides an interface defining Statistics Storage.
|
||||
*
|
||||
* Stores the views per day, total views and timestamp of last view
|
||||
* for entities.
|
||||
*/
|
||||
interface StatisticsStorageInterface {
|
||||
|
||||
/**
|
||||
* Count a entity view.
|
||||
*
|
||||
* @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 array \Drupal\statistics\StatisticsViewsResult
|
||||
*/
|
||||
public function fetchViews($ids);
|
||||
|
||||
/**
|
||||
* Returns the number of times a single entity has been viewed.
|
||||
*
|
||||
* @param int $id
|
||||
* The ID of the entity to fetch the views for.
|
||||
*
|
||||
* @return \Drupal\statistics\StatisticsViewsResult
|
||||
*/
|
||||
public function fetchView($id);
|
||||
|
||||
/**
|
||||
* Returns the number of times a entity has been viewed.
|
||||
*
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* Delete counts for a specific entity.
|
||||
*
|
||||
* @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();
|
||||
|
||||
/**
|
||||
* Returns the highest 'totalcount' value.
|
||||
*
|
||||
* @return int
|
||||
* The highest 'totalcount' value.
|
||||
*/
|
||||
public function maxTotalCount();
|
||||
|
||||
}
|
||||
60
web/core/modules/statistics/src/StatisticsViewsResult.php
Normal file
60
web/core/modules/statistics/src/StatisticsViewsResult.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\statistics;
|
||||
|
||||
/**
|
||||
* Value object for passing statistic results.
|
||||
*/
|
||||
class StatisticsViewsResult {
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $totalCount;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $dayCount;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $timestamp;
|
||||
|
||||
public function __construct($total_count, $day_count, $timestamp) {
|
||||
$this->totalCount = $total_count;
|
||||
$this->dayCount = $day_count;
|
||||
$this->timestamp = $timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Total number of times the entity has been viewed.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTotalCount() {
|
||||
return $this->totalCount;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Total number of times the entity has been viewed "today".
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getDayCount() {
|
||||
return $this->dayCount;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Timestamp of when the entity was last viewed.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTimestamp() {
|
||||
return $this->timestamp;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -16,7 +16,7 @@ class StatisticsAdminTest extends WebTestBase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'statistics');
|
||||
public static $modules = ['node', 'statistics'];
|
||||
|
||||
/**
|
||||
* A user that has permission to administer statistics.
|
||||
|
|
@ -47,18 +47,18 @@ class StatisticsAdminTest extends WebTestBase {
|
|||
|
||||
// Create Basic page node type.
|
||||
if ($this->profile != 'standard') {
|
||||
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
|
||||
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
|
||||
}
|
||||
$this->privilegedUser = $this->drupalCreateUser(array('administer statistics', 'view post access counter', 'create page content'));
|
||||
$this->privilegedUser = $this->drupalCreateUser(['administer statistics', 'view post access counter', 'create page content']);
|
||||
$this->drupalLogin($this->privilegedUser);
|
||||
$this->testNode = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->privilegedUser->id()));
|
||||
$this->testNode = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->privilegedUser->id()]);
|
||||
$this->client = \Drupal::httpClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the statistics settings page works.
|
||||
*/
|
||||
function testStatisticsSettings() {
|
||||
public function testStatisticsSettings() {
|
||||
$config = $this->config('statistics.settings');
|
||||
$this->assertFalse($config->get('count_content_views'), 'Count content view log is disabled by default.');
|
||||
|
||||
|
|
@ -72,19 +72,19 @@ class StatisticsAdminTest extends WebTestBase {
|
|||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
// Manually calling statistics.php, simulating ajax behavior.
|
||||
$nid = $this->testNode->id();
|
||||
$post = array('nid' => $nid);
|
||||
$post = ['nid' => $nid];
|
||||
global $base_url;
|
||||
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
||||
$this->client->post($stats_path, array('form_params' => $post));
|
||||
$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, array('form_params' => $post));
|
||||
$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, array('form_params' => $post));
|
||||
$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
|
||||
|
|
@ -93,7 +93,7 @@ class StatisticsAdminTest extends WebTestBase {
|
|||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
$this->assertText('3 views', 'Node is viewed 3 times.');
|
||||
|
||||
$this->client->post($stats_path, array('form_params' => $post));
|
||||
$this->client->post($stats_path, ['form_params' => $post]);
|
||||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
$this->assertText('3 views', 'Views counter was not updated.');
|
||||
}
|
||||
|
|
@ -101,19 +101,19 @@ class StatisticsAdminTest extends WebTestBase {
|
|||
/**
|
||||
* Tests that when a node is deleted, the node counter is deleted too.
|
||||
*/
|
||||
function testDeleteNode() {
|
||||
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 = array('nid' => $nid);
|
||||
$post = ['nid' => $nid];
|
||||
global $base_url;
|
||||
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
||||
$this->client->post($stats_path, array('form_params' => $post));
|
||||
$this->client->post($stats_path, ['form_params' => $post]);
|
||||
|
||||
$result = db_select('node_counter', 'n')
|
||||
->fields('n', array('nid'))
|
||||
->fields('n', ['nid'])
|
||||
->condition('n.nid', $this->testNode->id())
|
||||
->execute()
|
||||
->fetchAssoc();
|
||||
|
|
@ -122,7 +122,7 @@ class StatisticsAdminTest extends WebTestBase {
|
|||
$this->testNode->delete();
|
||||
|
||||
$result = db_select('node_counter', 'n')
|
||||
->fields('n', array('nid'))
|
||||
->fields('n', ['nid'])
|
||||
->condition('n.nid', $this->testNode->id())
|
||||
->execute()
|
||||
->fetchAssoc();
|
||||
|
|
@ -132,7 +132,7 @@ class StatisticsAdminTest extends WebTestBase {
|
|||
/**
|
||||
* Tests that cron clears day counts and expired access logs.
|
||||
*/
|
||||
function testExpiredLogs() {
|
||||
public function testExpiredLogs() {
|
||||
$this->config('statistics.settings')
|
||||
->set('count_content_views', 1)
|
||||
->save();
|
||||
|
|
@ -141,12 +141,12 @@ class StatisticsAdminTest extends WebTestBase {
|
|||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
// Manually calling statistics.php, simulating ajax behavior.
|
||||
$nid = $this->testNode->id();
|
||||
$post = array('nid' => $nid);
|
||||
$post = ['nid' => $nid];
|
||||
global $base_url;
|
||||
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
||||
$this->client->post($stats_path, array('form_params' => $post));
|
||||
$this->client->post($stats_path, ['form_params' => $post]);
|
||||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
$this->client->post($stats_path, array('form_params' => $post));
|
||||
$this->client->post($stats_path, ['form_params' => $post]);
|
||||
$this->assertText('1 view', 'Node is viewed once.');
|
||||
|
||||
// statistics_cron() will subtract
|
||||
|
|
@ -160,7 +160,7 @@ class StatisticsAdminTest extends WebTestBase {
|
|||
$this->assertNoText('node/' . $this->testNode->id(), 'No hit URL found.');
|
||||
|
||||
$result = db_select('node_counter', 'nc')
|
||||
->fields('nc', array('daycount'))
|
||||
->fields('nc', ['daycount'])
|
||||
->condition('nid', $this->testNode->id(), '=')
|
||||
->execute()
|
||||
->fetchField();
|
||||
|
|
|
|||
|
|
@ -1,57 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\statistics\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
use Drupal\node\Entity\Node;
|
||||
|
||||
/**
|
||||
* Tests if statistics.js is loaded when content is not printed.
|
||||
*
|
||||
* @group statistics
|
||||
*/
|
||||
class StatisticsAttachedTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'statistics');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalCreateContentType(['type' => 'page']);
|
||||
|
||||
// Install "statistics_test_attached" and set it as the default theme.
|
||||
$theme = 'statistics_test_attached';
|
||||
\Drupal::service('theme_handler')->install(array($theme));
|
||||
$this->config('system.theme')
|
||||
->set('default', $theme)
|
||||
->save();
|
||||
// Installing a theme will cause the kernel terminate event to rebuild the
|
||||
// router. Simulate that here.
|
||||
\Drupal::service('router.builder')->rebuildIfNeeded();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if statistics.js is loaded when content is not printed.
|
||||
*/
|
||||
public function testAttached() {
|
||||
|
||||
$node = Node::create([
|
||||
'type' => 'page',
|
||||
'title' => 'Page node',
|
||||
'body' => 'body text'
|
||||
]);
|
||||
$node->save();
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
|
||||
$this->assertRaw('core/modules/statistics/statistics.js', 'Statistics library is available');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ class StatisticsLoggingTest extends WebTestBase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'statistics', 'block', 'locale');
|
||||
public static $modules = ['node', 'statistics', 'block', 'locale'];
|
||||
|
||||
/**
|
||||
* User with permissions to create and edit pages.
|
||||
|
|
@ -47,10 +47,10 @@ class StatisticsLoggingTest extends WebTestBase {
|
|||
|
||||
// Create Basic page node type.
|
||||
if ($this->profile != 'standard') {
|
||||
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
|
||||
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
|
||||
}
|
||||
|
||||
$this->authUser = $this->drupalCreateUser(array(
|
||||
$this->authUser = $this->drupalCreateUser([
|
||||
// For node creation.
|
||||
'access content',
|
||||
'create page content',
|
||||
|
|
@ -58,21 +58,21 @@ class StatisticsLoggingTest extends WebTestBase {
|
|||
// For language negotiation administration.
|
||||
'administer languages',
|
||||
'access administration pages',
|
||||
));
|
||||
]);
|
||||
|
||||
// Ensure we have a node page to access.
|
||||
$this->node = $this->drupalCreateNode(array('title' => $this->randomMachineName(255), 'uid' => $this->authUser->id()));
|
||||
$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 = array(
|
||||
$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', array('language_interface[enabled][language-url]' => 1), t('Save settings'));
|
||||
$this->drupalPostForm('admin/config/regional/language/detection', ['language_interface[enabled][language-url]' => 1], t('Save settings'));
|
||||
$this->drupalLogout();
|
||||
|
||||
// Enable access logging.
|
||||
|
|
@ -88,7 +88,7 @@ class StatisticsLoggingTest extends WebTestBase {
|
|||
/**
|
||||
* Verifies node hit counter logging and script placement.
|
||||
*/
|
||||
function testLogging() {
|
||||
public function testLogging() {
|
||||
$path = 'node/' . $this->node->id();
|
||||
$module_path = drupal_get_path('module', 'statistics');
|
||||
$stats_path = base_path() . $module_path . '/statistics.php';
|
||||
|
|
@ -121,8 +121,8 @@ class StatisticsLoggingTest extends WebTestBase {
|
|||
|
||||
// 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));
|
||||
$post = ['nid' => $this->node->id()];
|
||||
$this->client->post($base_root . $stats_path, ['form_params' => $post]);
|
||||
$node_counter = statistics_get($this->node->id());
|
||||
$this->assertIdentical($node_counter['totalcount'], '1');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
namespace Drupal\statistics\Tests;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
|
||||
|
||||
/**
|
||||
* Tests display of statistics report blocks.
|
||||
*
|
||||
|
|
@ -9,32 +12,34 @@ namespace Drupal\statistics\Tests;
|
|||
*/
|
||||
class StatisticsReportsTest extends StatisticsTestBase {
|
||||
|
||||
use AssertPageCacheContextsAndTagsTrait;
|
||||
|
||||
/**
|
||||
* Tests the "popular content" block.
|
||||
*/
|
||||
function testPopularContentBlock() {
|
||||
public function testPopularContentBlock() {
|
||||
// Clear the block cache to load the Statistics module's block definitions.
|
||||
$this->container->get('plugin.manager.block')->clearCachedDefinitions();
|
||||
|
||||
// Visit a node to have something show up in the block.
|
||||
$node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->blockingUser->id()));
|
||||
$node = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->blockingUser->id()]);
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
// Manually calling statistics.php, simulating ajax behavior.
|
||||
$nid = $node->id();
|
||||
$post = http_build_query(array('nid' => $nid));
|
||||
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
|
||||
$post = http_build_query(['nid' => $nid]);
|
||||
$headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
|
||||
global $base_url;
|
||||
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
||||
$client = \Drupal::httpClient();
|
||||
$client->post($stats_path, array('headers' => $headers, 'body' => $post));
|
||||
$client->post($stats_path, ['headers' => $headers, 'body' => $post]);
|
||||
|
||||
// Configure and save the block.
|
||||
$this->drupalPlaceBlock('statistics_popular_block', array(
|
||||
$block = $this->drupalPlaceBlock('statistics_popular_block', [
|
||||
'label' => 'Popular content',
|
||||
'top_day_num' => 3,
|
||||
'top_all_num' => 3,
|
||||
'top_last_num' => 3,
|
||||
));
|
||||
]);
|
||||
|
||||
// Get some page and check if the block is displayed.
|
||||
$this->drupalGet('user');
|
||||
|
|
@ -43,9 +48,16 @@ class StatisticsReportsTest extends StatisticsTestBase {
|
|||
$this->assertText('All time', 'Found the all time popular content.');
|
||||
$this->assertText('Last viewed', 'Found the last viewed popular content.');
|
||||
|
||||
// statistics.module doesn't use node entities, prevent the node language
|
||||
// from being added to the options.
|
||||
$this->assertRaw(\Drupal::l($node->label(), $node->urlInfo('canonical', ['language' => NULL])), 'Found link to visited node.');
|
||||
$tags = Cache::mergeTags($node->getCacheTags(), $block->getCacheTags());
|
||||
$tags = Cache::mergeTags($tags, $this->blockingUser->getCacheTags());
|
||||
$tags = Cache::mergeTags($tags, ['block_view', 'config:block_list', 'node_list', 'rendered', 'user_view']);
|
||||
$this->assertCacheTags($tags);
|
||||
$contexts = Cache::mergeContexts($node->getCacheContexts(), $block->getCacheContexts());
|
||||
$contexts = Cache::mergeContexts($contexts, ['url.query_args:_wrapper_format']);
|
||||
$this->assertCacheContexts($contexts);
|
||||
|
||||
// Check if the node link is displayed.
|
||||
$this->assertRaw(\Drupal::l($node->label(), $node->urlInfo('canonical')), 'Found link to visited node.');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@ use Drupal\simpletest\WebTestBase;
|
|||
|
||||
/**
|
||||
* Defines a base class for testing the Statistics module.
|
||||
*
|
||||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use \Drupal\Tests\statistics\Functional\StatisticsTestBase instead.
|
||||
*/
|
||||
abstract class StatisticsTestBase extends WebTestBase {
|
||||
|
||||
|
|
@ -14,7 +17,7 @@ abstract class StatisticsTestBase extends WebTestBase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'block', 'ban', 'statistics');
|
||||
public static $modules = ['node', 'block', 'ban', 'statistics'];
|
||||
|
||||
/**
|
||||
* User with permissions to ban IP's.
|
||||
|
|
@ -28,18 +31,18 @@ abstract class StatisticsTestBase extends WebTestBase {
|
|||
|
||||
// Create Basic page node type.
|
||||
if ($this->profile != 'standard') {
|
||||
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
|
||||
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
|
||||
}
|
||||
|
||||
// Create user.
|
||||
$this->blockingUser = $this->drupalCreateUser(array(
|
||||
$this->blockingUser = $this->drupalCreateUser([
|
||||
'access administration pages',
|
||||
'access site reports',
|
||||
'ban IP addresses',
|
||||
'administer blocks',
|
||||
'administer statistics',
|
||||
'administer users',
|
||||
));
|
||||
]);
|
||||
$this->drupalLogin($this->blockingUser);
|
||||
|
||||
// Enable logging.
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\statistics\Tests;
|
||||
|
||||
/**
|
||||
* Generates text using placeholders for dummy content to check statistics token
|
||||
* replacement.
|
||||
*
|
||||
* @group statistics
|
||||
*/
|
||||
class StatisticsTokenReplaceTest extends StatisticsTestBase {
|
||||
/**
|
||||
* Creates a node, then tests the statistics tokens generated from it.
|
||||
*/
|
||||
function testStatisticsTokenReplacement() {
|
||||
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
|
||||
|
||||
// Create user and node.
|
||||
$user = $this->drupalCreateUser(array('create page content'));
|
||||
$this->drupalLogin($user);
|
||||
$node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $user->id()));
|
||||
|
||||
// Hit the node.
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
// Manually calling statistics.php, simulating ajax behavior.
|
||||
$nid = $node->id();
|
||||
$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';
|
||||
$client = \Drupal::httpClient();
|
||||
$client->post($stats_path, array('headers' => $headers, 'body' => $post));
|
||||
$statistics = statistics_get($node->id());
|
||||
|
||||
// Generate and test tokens.
|
||||
$tests = array();
|
||||
$tests['[node:total-count]'] = 1;
|
||||
$tests['[node:day-count]'] = 1;
|
||||
$tests['[node:last-view]'] = format_date($statistics['timestamp']);
|
||||
$tests['[node:last-view:short]'] = format_date($statistics['timestamp'], 'short');
|
||||
|
||||
// Test to make sure that we generated something for each token.
|
||||
$this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
|
||||
|
||||
foreach ($tests as $input => $expected) {
|
||||
$output = \Drupal::token()->replace($input, array('node' => $node), array('langcode' => $language_interface->getId()));
|
||||
$this->assertEqual($output, $expected, format_string('Statistics token %token replaced.', array('%token' => $input)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ class IntegrationTest extends ViewTestBase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('statistics', 'statistics_test_views', 'node');
|
||||
public static $modules = ['statistics', 'statistics_test_views', 'node'];
|
||||
|
||||
/**
|
||||
* Stores the user object that accesses the page.
|
||||
|
|
@ -40,25 +40,24 @@ class IntegrationTest extends ViewTestBase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = array('test_statistics_integration');
|
||||
public static $testViews = ['test_statistics_integration'];
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
ViewTestData::createTestViews(get_class($this), array('statistics_test_views'));
|
||||
ViewTestData::createTestViews(get_class($this), ['statistics_test_views']);
|
||||
|
||||
// Create a new user for viewing nodes and statistics.
|
||||
$this->webUser = $this->drupalCreateUser(array('access content', 'view post access counter'));
|
||||
$this->webUser = $this->drupalCreateUser(['access content', 'view post access counter']);
|
||||
|
||||
// Create a new user for viewing nodes only.
|
||||
$this->deniedUser = $this->drupalCreateUser(array('access content'));
|
||||
$this->deniedUser = $this->drupalCreateUser(['access content']);
|
||||
|
||||
$this->drupalCreateContentType(array('type' => 'page'));
|
||||
$this->node = $this->drupalCreateNode(array('type' => 'page'));
|
||||
$this->drupalCreateContentType(['type' => 'page']);
|
||||
$this->node = $this->drupalCreateNode(['type' => 'page']);
|
||||
|
||||
// Enable access logging.
|
||||
// Enable counting of content views.
|
||||
$this->config('statistics.settings')
|
||||
->set('access_log.enabled', 1)
|
||||
->set('count_content_views', 1)
|
||||
->save();
|
||||
|
||||
|
|
@ -76,7 +75,7 @@ class IntegrationTest extends ViewTestBase {
|
|||
global $base_url;
|
||||
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
||||
$client = \Drupal::httpClient();
|
||||
$client->post($stats_path, array('form_params' => array('nid' => $this->node->id())));
|
||||
$client->post($stats_path, ['form_params' => ['nid' => $this->node->id()]]);
|
||||
$this->drupalGet('test_statistics_integration');
|
||||
|
||||
$expected = statistics_get($this->node->id());
|
||||
|
|
|
|||
Reference in a new issue