Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\statistics\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Generates text using placeholders for dummy content to check statistics token
|
Chris@0
|
7 * replacement.
|
Chris@0
|
8 *
|
Chris@0
|
9 * @group statistics
|
Chris@0
|
10 */
|
Chris@0
|
11 class StatisticsTokenReplaceTest extends StatisticsTestBase {
|
Chris@17
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Creates a node, then tests the statistics tokens generated from it.
|
Chris@0
|
15 */
|
Chris@0
|
16 public function testStatisticsTokenReplacement() {
|
Chris@0
|
17 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
|
Chris@0
|
18
|
Chris@0
|
19 // Create user and node.
|
Chris@0
|
20 $user = $this->drupalCreateUser(['create page content']);
|
Chris@0
|
21 $this->drupalLogin($user);
|
Chris@0
|
22 $node = $this->drupalCreateNode(['type' => 'page', 'uid' => $user->id()]);
|
Chris@0
|
23
|
Chris@0
|
24 // Hit the node.
|
Chris@0
|
25 $this->drupalGet('node/' . $node->id());
|
Chris@0
|
26 // Manually calling statistics.php, simulating ajax behavior.
|
Chris@0
|
27 $nid = $node->id();
|
Chris@0
|
28 $post = http_build_query(['nid' => $nid]);
|
Chris@0
|
29 $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
|
Chris@0
|
30 global $base_url;
|
Chris@0
|
31 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
Chris@0
|
32 $client = \Drupal::httpClient();
|
Chris@0
|
33 $client->post($stats_path, ['headers' => $headers, 'body' => $post]);
|
Chris@0
|
34 $statistics = statistics_get($node->id());
|
Chris@0
|
35
|
Chris@0
|
36 // Generate and test tokens.
|
Chris@0
|
37 $tests = [];
|
Chris@0
|
38 $tests['[node:total-count]'] = 1;
|
Chris@0
|
39 $tests['[node:day-count]'] = 1;
|
Chris@18
|
40 /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
|
Chris@18
|
41 $date_formatter = $this->container->get('date.formatter');
|
Chris@18
|
42 $tests['[node:last-view]'] = $date_formatter->format($statistics['timestamp']);
|
Chris@18
|
43 $tests['[node:last-view:short]'] = $date_formatter->format($statistics['timestamp'], 'short');
|
Chris@0
|
44
|
Chris@0
|
45 // Test to make sure that we generated something for each token.
|
Chris@0
|
46 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
|
Chris@0
|
47
|
Chris@0
|
48 foreach ($tests as $input => $expected) {
|
Chris@0
|
49 $output = \Drupal::token()->replace($input, ['node' => $node], ['langcode' => $language_interface->getId()]);
|
Chris@0
|
50 $this->assertEqual($output, $expected, format_string('Statistics token %token replaced.', ['%token' => $input]));
|
Chris@0
|
51 }
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 }
|