comparison core/modules/statistics/tests/src/Functional/StatisticsTokenReplaceTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\statistics\Functional;
4
5 /**
6 * Generates text using placeholders for dummy content to check statistics token
7 * replacement.
8 *
9 * @group statistics
10 */
11 class StatisticsTokenReplaceTest extends StatisticsTestBase {
12 /**
13 * Creates a node, then tests the statistics tokens generated from it.
14 */
15 public function testStatisticsTokenReplacement() {
16 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
17
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()]);
22
23 // Hit the node.
24 $this->drupalGet('node/' . $node->id());
25 // Manually calling statistics.php, simulating ajax behavior.
26 $nid = $node->id();
27 $post = http_build_query(['nid' => $nid]);
28 $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
29 global $base_url;
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());
34
35 // Generate and test tokens.
36 $tests = [];
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');
41
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.');
44
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]));
48 }
49 }
50
51 }