StatisticsTokenReplaceTest.php in Zircon Profile 8
File
core/modules/statistics/src/Tests/StatisticsTokenReplaceTest.php
View source
<?php
namespace Drupal\statistics\Tests;
class StatisticsTokenReplaceTest extends StatisticsTestBase {
function testStatisticsTokenReplacement() {
$language_interface = \Drupal::languageManager()
->getCurrentLanguage();
$user = $this
->drupalCreateUser(array(
'create page content',
));
$this
->drupalLogin($user);
$node = $this
->drupalCreateNode(array(
'type' => 'page',
'uid' => $user
->id(),
));
$this
->drupalGet('node/' . $node
->id());
$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::service('http_client_factory')
->fromOptions([
'config/curl' => [
CURLOPT_TIMEOUT => 10,
],
]);
$client
->post($stats_path, array(
'headers' => $headers,
'body' => $post,
));
$statistics = statistics_get($node
->id());
$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');
$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,
)));
}
}
}