function StatisticsTokenReplaceTestCase::testStatisticsTokenReplacement

Creates a node, then tests the statistics tokens generated from it.

File

modules/statistics/statistics.test, line 477

Class

StatisticsTokenReplaceTestCase
Tests statistics token replacement in strings.

Code

function testStatisticsTokenReplacement() {
  global $language;
  // Create user and node.
  $user = $this->drupalCreateUser(array(
    'create page content',
  ));
  $this->drupalLogin($user);
  $node = $this->drupalCreateNode(array(
    'type' => 'page',
    'uid' => $user->uid,
  ));
  // Hit the node.
  $this->drupalGet('node/' . $node->nid);
  $statistics = statistics_get($node->nid);
  // Generate and test tokens.
  $tests = array();
  $tests['[node:total-count]'] = 1;
  $tests['[node:day-count]'] = 1;
  $tests['[node:last-view]'] = format_date($statistics['timestamp']);
  $tests['[node:last-view:short]'] = format_date($statistics['timestamp'], 'short');
  // Test to make sure that we generated something for each token.
  $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
  foreach ($tests as $input => $expected) {
    $output = token_replace($input, array(
      'node' => $node,
    ), array(
      'language' => $language,
    ));
    $this->assertEqual($output, $expected, format_string('Statistics token %token replaced.', array(
      '%token' => $input,
    )));
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.