StatisticsTokenReplaceTest.php 2.9 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\statistics\Functional;
4

5
6
use Drupal\Component\Render\FormattableMarkup;

7
/**
8
9
10
11
 * Generates text using placeholders for dummy content to check statistics token
 * replacement.
 *
 * @group statistics
12
13
 */
class StatisticsTokenReplaceTest extends StatisticsTestBase {
14

15
16
17
18
19
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

20
21
22
  /**
   * Creates a node, then tests the statistics tokens generated from it.
   */
23
  public function testStatisticsTokenReplacement() {
24
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
25
26

    // Create user and node.
27
    $user = $this->drupalCreateUser(['create page content']);
28
    $this->drupalLogin($user);
29
    $node = $this->drupalCreateNode(['type' => 'page', 'uid' => $user->id()]);
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = $this->container->get('date.formatter');
    $request_time = \Drupal::time()->getRequestTime();

    // Generate and test tokens.
    $tests = [];
    $tests['[node:total-count]'] = 0;
    $tests['[node:day-count]'] = 0;
    $tests['[node:last-view]'] = t('never');
    $tests['[node:last-view:short]'] = $date_formatter->format($request_time, 'short');

    foreach ($tests as $input => $expected) {
      $output = \Drupal::token()->replace($input, ['node' => $node], ['langcode' => $language_interface->getId()]);
      $this->assertEqual($output, $expected, new FormattableMarkup('Statistics token %token replaced.', ['%token' => $input]));
    }

47
    // Hit the node.
48
    $this->drupalGet('node/' . $node->id());
49
    // Manually calling statistics.php, simulating ajax behavior.
50
    $nid = $node->id();
51
52
    $post = http_build_query(['nid' => $nid]);
    $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
53
    global $base_url;
54
    $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
55
    $client = \Drupal::httpClient();
56
    $client->post($stats_path, ['headers' => $headers, 'body' => $post]);
57
58
    /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
    $statistics = \Drupal::service('statistics.storage.node')->fetchView($node->id());
59
60

    // Generate and test tokens.
61
    $tests = [];
62
63
    $tests['[node:total-count]'] = 1;
    $tests['[node:day-count]'] = 1;
64
65
    $tests['[node:last-view]'] = $date_formatter->format($statistics->getTimestamp());
    $tests['[node:last-view:short]'] = $date_formatter->format($statistics->getTimestamp(), 'short');
66
67

    // Test to make sure that we generated something for each token.
68
    $this->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');
69
70

    foreach ($tests as $input => $expected) {
71
      $output = \Drupal::token()->replace($input, ['node' => $node], ['langcode' => $language_interface->getId()]);
72
      $this->assertEqual($expected, $output, new FormattableMarkup('Statistics token %token replaced.', ['%token' => $input]));
73
74
    }
  }
75

76
}