1 node.test NodeTokenReplaceTestCase::testNodeTokenReplacement()

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

File

core/modules/node/tests/node.test, line 3321
Tests for node.module.

Class

NodeTokenReplaceTestCase
Test node token replacement in strings.

Code

function testNodeTokenReplacement() {
  global $language;
  $url_options = array(
    'absolute' => TRUE,
    'language' => $language,
  );

  // Create a user and a node.
  $account = $this->backdropCreateUser();
  $settings = array(
    'type' => 'post',
    'uid' => $account->uid,
    'title' => '<blink>Blinking Text</blink>',
    'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32), 'summary' => $this->randomName(16)))),
  );
  $node = $this->backdropCreateNode($settings);

  // Load node so that the body and summary fields are structured properly.
  $node = node_load($node->nid);
  $instance = field_info_instance('node', 'body', $node->type);

  // Generate and test sanitized tokens.
  $tests = array();
  $tests['[node:nid]'] = $node->nid;
  $tests['[node:vid]'] = $node->vid;
  $tests['[node:tnid]'] = $node->tnid;
  $tests['[node:type]'] = 'post';
  $tests['[node:type-name]'] = 'Post';
  $tests['[node:title]'] = check_plain($node->title);
  $tests['[node:body]'] = _text_sanitize($instance, $node->langcode, $node->body[$node->langcode][0], 'value');
  $tests['[node:summary]'] = _text_sanitize($instance, $node->langcode, $node->body[$node->langcode][0], 'summary');
  $tests['[node:langcode]'] = check_plain($node->langcode);
  $tests['[node:url]'] = url('node/' . $node->nid, $url_options);
  $tests['[node:edit-url]'] = url('node/' . $node->nid . '/edit', $url_options);
  $tests['[node:author]'] = check_plain(user_format_name($account));
  $tests['[node:author:uid]'] = $node->uid;
  $tests['[node:author:name]'] = check_plain(user_format_name($account));
  $tests['[node:created:since]'] = format_interval(REQUEST_TIME - $node->created, 2, $language->langcode);
  $tests['[node:changed:since]'] = format_interval(REQUEST_TIME - $node->changed, 2, $language->langcode);

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

  foreach ($tests as $input => $expected) {
    $output = token_replace($input, array('node' => $node), array('language' => $language));
    $this->assertEqual($output, $expected, t('Sanitized node token %token replaced.', array('%token' => $input)));
  }

  // Generate and test unsanitized tokens.
  $tests['[node:title]'] = $node->title;
  $tests['[node:body]'] = $node->body[$node->langcode][0]['value'];
  $tests['[node:summary]'] = $node->body[$node->langcode][0]['summary'];
  $tests['[node:langcode]'] = $node->langcode;
  $tests['[node:author:name]'] = user_format_name($account);

  foreach ($tests as $input => $expected) {
    $output = token_replace($input, array('node' => $node), array('language' => $language, 'sanitize' => FALSE));
    $this->assertEqual($output, $expected, t('Unsanitized node token %token replaced.', array('%token' => $input)));
  }

  // Repeat for a node without a summary.
  $settings['body'] = array(LANGUAGE_NONE => array(array('value' => $this->randomName(32), 'summary' => '')));
  $node = $this->backdropCreateNode($settings);

  // Load node (without summary) so that the body and summary fields are
  // structured properly.
  $node = node_load($node->nid);
  $instance = field_info_instance('node', 'body', $node->type);

  // Generate and test sanitized token - use full body as expected value.
  $tests = array();
  $tests['[node:summary]'] = _text_sanitize($instance, $node->langcode, $node->body[$node->langcode][0], 'value');

  // Test to make sure that we generated something for each token.
  $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated for node without a summary.');

  foreach ($tests as $input => $expected) {
    $output = token_replace($input, array('node' => $node), array('language' => $language));
    $this->assertEqual($output, $expected, format_string('Sanitized node token %token replaced for node without a summary.', array('%token' => $input)));
  }

  // Generate and test unsanitized tokens.
  $tests['[node:summary]'] = $node->body[$node->langcode][0]['value'];

  foreach ($tests as $input => $expected) {
    $output = token_replace($input, array('node' => $node), array('language' => $language, 'sanitize' => FALSE));
    $this->assertEqual($output, $expected, format_string('Unsanitized node token %token replaced for node without a summary.', array('%token' => $input)));
  }
}