annotate core/modules/statistics/tests/src/Functional/StatisticsTokenReplaceTest.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
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@0 12 /**
Chris@0 13 * Creates a node, then tests the statistics tokens generated from it.
Chris@0 14 */
Chris@0 15 public function testStatisticsTokenReplacement() {
Chris@0 16 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
Chris@0 17
Chris@0 18 // Create user and node.
Chris@0 19 $user = $this->drupalCreateUser(['create page content']);
Chris@0 20 $this->drupalLogin($user);
Chris@0 21 $node = $this->drupalCreateNode(['type' => 'page', 'uid' => $user->id()]);
Chris@0 22
Chris@0 23 // Hit the node.
Chris@0 24 $this->drupalGet('node/' . $node->id());
Chris@0 25 // Manually calling statistics.php, simulating ajax behavior.
Chris@0 26 $nid = $node->id();
Chris@0 27 $post = http_build_query(['nid' => $nid]);
Chris@0 28 $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
Chris@0 29 global $base_url;
Chris@0 30 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
Chris@0 31 $client = \Drupal::httpClient();
Chris@0 32 $client->post($stats_path, ['headers' => $headers, 'body' => $post]);
Chris@0 33 $statistics = statistics_get($node->id());
Chris@0 34
Chris@0 35 // Generate and test tokens.
Chris@0 36 $tests = [];
Chris@0 37 $tests['[node:total-count]'] = 1;
Chris@0 38 $tests['[node:day-count]'] = 1;
Chris@0 39 $tests['[node:last-view]'] = format_date($statistics['timestamp']);
Chris@0 40 $tests['[node:last-view:short]'] = format_date($statistics['timestamp'], 'short');
Chris@0 41
Chris@0 42 // Test to make sure that we generated something for each token.
Chris@0 43 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
Chris@0 44
Chris@0 45 foreach ($tests as $input => $expected) {
Chris@0 46 $output = \Drupal::token()->replace($input, ['node' => $node], ['langcode' => $language_interface->getId()]);
Chris@0 47 $this->assertEqual($output, $expected, format_string('Statistics token %token replaced.', ['%token' => $input]));
Chris@0 48 }
Chris@0 49 }
Chris@0 50
Chris@0 51 }