Issue #3068060 by alexpott, timmillwood, Berdir: Properly deprecate statistics_get()

merge-requests/55/head
catch 2019-07-23 20:46:19 +01:00
parent 3a795eb086
commit af4492f97c
6 changed files with 62 additions and 31 deletions

View File

@ -123,10 +123,13 @@ function statistics_title_list($dbfield, $dbrows) {
/**
* Retrieves a node's "view statistics".
*
* @deprecated in Drupal 8.2.x, will be removed before Drupal 9.0.0.
* Use \Drupal::service('statistics.storage.node')->fetchView($id).
* @deprecated in drupal:8.2.0 and is removed from drupal:9.0.0. Use
* \Drupal::service('statistics.storage.node')->fetchView($id) instead.
*
* @see https://www.drupal.org/node/2778245
*/
function statistics_get($id) {
@trigger_error("statistics_get() is deprecated in drupal:8.2.0 and is removed from drupal:9.0.0. Use Drupal::service('statistics.storage.node')->fetchView() instead. See https://www.drupal.org/node/2778245", E_USER_DEPRECATED);
if ($id > 0) {
/** @var \Drupal\statistics\StatisticsViewsResult $statistics */
$statistics = \Drupal::service('statistics.storage.node')->fetchView($id);

View File

@ -40,25 +40,22 @@ function statistics_tokens($type, $tokens, array $data, array $options, Bubbleab
if ($type == 'node' & !empty($data['node'])) {
$node = $data['node'];
/** @var \Drupal\statistics\StatisticsStorageInterface $stats_storage */
$stats_storage = \Drupal::service('statistics.storage.node');
foreach ($tokens as $name => $original) {
if ($name == 'total-count') {
$statistics = statistics_get($node->id());
$replacements[$original] = $statistics['totalcount'];
$replacements[$original] = $stats_storage->fetchView($node->id())->getTotalCount();
}
elseif ($name == 'day-count') {
$statistics = statistics_get($node->id());
$replacements[$original] = $statistics['daycount'];
$replacements[$original] = $stats_storage->fetchView($node->id())->getDayCount();
}
elseif ($name == 'last-view') {
$statistics = statistics_get($node->id());
$replacements[$original] = \Drupal::service('date.formatter')->format($statistics['timestamp']);
$replacements[$original] = \Drupal::service('date.formatter')->format($stats_storage->fetchView($node->id())->getTimestamp());
}
}
if ($created_tokens = $token_service->findWithPrefix($tokens, 'last-view')) {
$statistics = statistics_get($node->id());
$replacements += $token_service->generate('date', $created_tokens, ['date' => $statistics['timestamp']], $options, $bubbleable_metadata);
$replacements += $token_service->generate('date', $created_tokens, ['date' => $stats_storage->fetchView($node->id())->getTimestamp()], $options, $bubbleable_metadata);
}
}

View File

@ -125,8 +125,8 @@ class StatisticsLoggingTest extends BrowserTestBase {
global $base_root;
$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);
$node_counter = \Drupal::service('statistics.storage.node')->fetchView($this->node->id());
$this->assertIdentical(1, $node_counter->getTotalCount());
// Try fetching statistics for an invalid node ID and verify it returns
// FALSE.
@ -136,7 +136,7 @@ class StatisticsLoggingTest extends BrowserTestBase {
// This is a test specifically for the deprecated statistics_get() function
// and so should remain unconverted until that function is removed.
$result = statistics_get($node_id);
$result = \Drupal::service('statistics.storage.node')->fetchView($node_id);
$this->assertIdentical($result, FALSE);
}

View File

@ -33,7 +33,8 @@ class StatisticsTokenReplaceTest extends StatisticsTestBase {
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
$client = \Drupal::httpClient();
$client->post($stats_path, ['headers' => $headers, 'body' => $post]);
$statistics = statistics_get($node->id());
/** @var \Drupal\statistics\StatisticsViewsResult $statistics */
$statistics = \Drupal::service('statistics.storage.node')->fetchView($node->id());
// Generate and test tokens.
$tests = [];
@ -41,8 +42,8 @@ class StatisticsTokenReplaceTest extends StatisticsTestBase {
$tests['[node:day-count]'] = 1;
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = $this->container->get('date.formatter');
$tests['[node:last-view]'] = $date_formatter->format($statistics['timestamp']);
$tests['[node:last-view:short]'] = $date_formatter->format($statistics['timestamp'], 'short');
$tests['[node:last-view]'] = $date_formatter->format($statistics->getTimestamp());
$tests['[node:last-view:short]'] = $date_formatter->format($statistics->getTimestamp(), '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.');

View File

@ -78,26 +78,20 @@ class IntegrationTest extends ViewTestBase {
$client->post($stats_path, ['form_params' => ['nid' => $this->node->id()]]);
$this->drupalGet('test_statistics_integration');
$expected = statistics_get($this->node->id());
// Convert the timestamp to year, to match the expected output of the date
// handler.
$expected['timestamp'] = date('Y', $expected['timestamp']);
foreach ($expected as $field => $value) {
$xpath = "//div[contains(@class, views-field-$field)]/span[@class = 'field-content']";
$this->assertFieldByXpath($xpath, $value, "The $field output matches the expected.");
}
/** @var \Drupal\statistics\StatisticsViewsResult $statistics */
$statistics = \Drupal::service('statistics.storage.node')->fetchView($this->node->id());
$this->assertSession()->pageTextContains('Total views: 1');
$this->assertSession()->pageTextContains('Views today: 1');
$this->assertSession()->pageTextContains('Most recent view: ' . date('Y', $statistics->getTimestamp()));
$this->drupalLogout();
$this->drupalLogin($this->deniedUser);
$this->drupalGet('test_statistics_integration');
$this->assertResponse(200);
foreach ($expected as $field => $value) {
$xpath = "//div[contains(@class, views-field-$field)]/span[@class = 'field-content']";
$this->assertNoFieldByXpath($xpath, $value, "The $field output is not displayed.");
}
$this->assertSession()->pageTextNotContains('Total views:');
$this->assertSession()->pageTextNotContains('Views today:');
$this->assertSession()->pageTextNotContains('Most recent view:');
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Drupal\Tests\statistics\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests deprecations in the Statistics module.
*
* @group statistics
* @group legacy
*/
class StatisticsDeprecationsTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['statistics'];
/**
* @expectedDeprecation statistics_get() is deprecated in drupal:8.2.0 and is removed from drupal:9.0.0. Use Drupal::service('statistics.storage.node')->fetchView() instead. See https://www.drupal.org/node/2778245
*/
public function testStatisticsGetDeprecation() {
$this->installSchema('statistics', 'node_counter');
$this->container->get('statistics.storage.node')->recordView(1);
$expected_timestamp = $this->container->get('datetime.time')->getRequestTime();
$this->assertSame([
'totalcount' => 1,
'daycount' => 1,
'timestamp' => $expected_timestamp,
], statistics_get(1));
}
}