You are here

public function EntityTokenTestCase::testTokenSupport in Entity API 7

Tests whether token support is basically working.

File

./entity.test, line 1444
Entity CRUD API tests.

Class

EntityTokenTestCase
Tests provided entity property info of the core modules.

Code

public function testTokenSupport() {

  // Test basic tokens.
  $node = $this
    ->drupalCreateNode(array(
    'sticky' => TRUE,
    'promote' => FALSE,
  ));
  $text = "Sticky: [node:sticky] Promote: [node:promote] User: [site:current-user:name]";
  $true = t('true');
  $false = t('false');
  $user_name = $GLOBALS['user']->name;
  $target = "Sticky: {$true} Promote: {$false} User: {$user_name}";
  $replace = token_replace($text, array(
    'node' => $node,
  ));
  $this
    ->assertEqual($replace, $target, 'Provided tokens basically work.');

  // Test multiple-value tokens using the tags field of articles.
  for ($i = 0; $i < 4; $i++) {
    $tags[$i] = entity_property_values_create_entity('taxonomy_term', array(
      'name' => $this
        ->randomName(),
      'vocabulary' => 1,
    ))
      ->save();
    $field_value[LANGUAGE_NONE][$i]['tid'] = $tags[$i]
      ->getIdentifier();
    $labels[$i] = $tags[$i]
      ->label();
  }
  $node = $this
    ->drupalCreateNode(array(
    'title' => 'foo',
    'type' => 'article',
    'field_tags' => $field_value,
  ));
  $text = "Tags: [node:field-tags] First: [node:field-tags:0] 2nd name: [node:field-tags:1:name] 1st vocab [node:field-tags:0:vocabulary]";
  $tag_labels = implode(', ', $labels);
  $target = "Tags: {$tag_labels} First: {$labels[0]} 2nd name: {$labels[1]} 1st vocab {$tags[0]->vocabulary->label()}";
  $replace = token_replace($text, array(
    'node' => $node,
  ));
  $this
    ->assertEqual($replace, $target, 'Multiple-value token replacements have been replaced.');

  // Make sure not existing values are not handled.
  $replace = token_replace("[node:field-tags:43]", array(
    'node' => $node,
  ));
  $this
    ->assertEqual($replace, "[node:field-tags:43]", 'Not existing values are not replaced.');

  // Test data-structure tokens like [site:current-page:url].
  $replace = token_replace("[site:current-page:url]", array());
  $this
    ->assertEqual($replace, $GLOBALS['base_root'] . request_uri(), 'Token replacements of data structure properties replaced.');

  // Test chaining of data-structure tokens using an image-field.
  $file = $this
    ->createFile('image');
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $wrapper = entity_metadata_wrapper('node', $node);
  $wrapper->field_image = array(
    'fid' => $file->fid,
  );
  $replace = token_replace("[node:field-image:file:name]", array(
    'node' => $node,
  ));
  $this
    ->assertEqual($replace, $wrapper->field_image->file->name
    ->value(), 'Token replacements of an image field have been replaced.');
}