3 namespace Drupal\Tests\statistics\Functional;
6 * Generates text using placeholders for dummy content to check statistics token
11 class StatisticsTokenReplaceTest extends StatisticsTestBase {
13 * Creates a node, then tests the statistics tokens generated from it.
15 public function testStatisticsTokenReplacement() {
16 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
18 // Create user and node.
19 $user = $this->drupalCreateUser(['create page content']);
20 $this->drupalLogin($user);
21 $node = $this->drupalCreateNode(['type' => 'page', 'uid' => $user->id()]);
24 $this->drupalGet('node/' . $node->id());
25 // Manually calling statistics.php, simulating ajax behavior.
27 $post = http_build_query(['nid' => $nid]);
28 $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
30 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
31 $client = \Drupal::httpClient();
32 $client->post($stats_path, ['headers' => $headers, 'body' => $post]);
33 $statistics = statistics_get($node->id());
35 // Generate and test tokens.
37 $tests['[node:total-count]'] = 1;
38 $tests['[node:day-count]'] = 1;
39 $tests['[node:last-view]'] = format_date($statistics['timestamp']);
40 $tests['[node:last-view:short]'] = format_date($statistics['timestamp'], 'short');
42 // Test to make sure that we generated something for each token.
43 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
45 foreach ($tests as $input => $expected) {
46 $output = \Drupal::token()->replace($input, ['node' => $node], ['langcode' => $language_interface->getId()]);
47 $this->assertEqual($output, $expected, format_string('Statistics token %token replaced.', ['%token' => $input]));