summaryrefslogtreecommitdiff
path: root/modules/node
diff options
context:
space:
mode:
Diffstat (limited to 'modules/node')
-rw-r--r--modules/node/node.test76
-rw-r--r--modules/node/node.tokens.inc18
2 files changed, 83 insertions, 11 deletions
diff --git a/modules/node/node.test b/modules/node/node.test
index 053a5968c..5f215d321 100644
--- a/modules/node/node.test
+++ b/modules/node/node.test
@@ -1666,3 +1666,79 @@ class NodeQueryAlter extends DrupalWebTestCase {
}
}
}
+
+/**
+ * Test node token replacement in strings.
+ */
+class NodeTokenReplaceTestCase extends DrupalWebTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Node token replacement',
+ 'description' => 'Generates text using placeholders for dummy content to check node token replacement.',
+ 'group' => 'Node',
+ );
+ }
+
+ /**
+ * Creates a node, then tests the tokens generated from it.
+ */
+ function testNodeTokenReplacement() {
+ global $language;
+ $url_options = array(
+ 'absolute' => TRUE,
+ 'language' => $language,
+ );
+
+ // Create a user and a node.
+ $account = $this->drupalCreateUser();
+ $settings = array(
+ 'type' => 'article',
+ 'uid' => $account->uid,
+ 'title' => '<blink>Blinking Text</blink>',
+ 'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32), 'summary' => $this->randomName(16)))),
+ );
+ $node = $this->drupalCreateNode($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:uid]'] = $node->uid;
+ $tests['[node:type]'] = 'article';
+ $tests['[node:type-name]'] = 'Article';
+ $tests['[node:title]'] = check_plain($node->title);
+ $tests['[node:body]'] = _text_sanitize($instance, $node->language, $node->body[$node->language][0], 'value');
+ $tests['[node:summary]'] = _text_sanitize($instance, $node->language, $node->body[$node->language][0], 'summary');
+ $tests['[node:language]'] = check_plain($node->language);
+ $tests['[node:url]'] = url('node/' . $node->nid, $url_options);
+ $tests['[node:edit-url]'] = url('node/' . $node->nid . '/edit', $url_options);
+ $tests['[node:author:name]'] = check_plain($account->name);
+ $tests['[node:created:since]'] = format_interval(REQUEST_TIME - $node->created, 2, $language->language);
+ $tests['[node:changed:since]'] = format_interval(REQUEST_TIME - $node->changed, 2, $language->language);
+
+ // 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->assertFalse(strcmp($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->language][0]['value'];
+ $tests['[node:summary]'] = $node->body[$node->language][0]['summary'];
+ $tests['[node:language]'] = $node->language;
+ $tests['[node:author:name]'] = $account->name;
+
+ foreach ($tests as $input => $expected) {
+ $output = token_replace($input, array('node' => $node), array('language' => $language, 'sanitize' => FALSE));
+ $this->assertFalse(strcmp($output, $expected), t('Unsanitized node token %token replaced.', array('%token' => $input)));
+ }
+ }
+}
diff --git a/modules/node/node.tokens.inc b/modules/node/node.tokens.inc
index 4473088eb..c28e5347e 100644
--- a/modules/node/node.tokens.inc
+++ b/modules/node/node.tokens.inc
@@ -129,8 +129,13 @@ function node_tokens($type, $tokens, array $data = array(), array $options = arr
$replacements[$original] = $node->uid;
break;
- case 'name':
- $replacements[$original] = $sanitize ? check_plain($node->name) : $node->name;
+ case 'type':
+ $replacements[$original] = $sanitize ? check_plain($node->type) : $node->type;
+ break;
+
+ case 'type-name':
+ $type_name = node_type_get_name($node);
+ $replacements[$original] = $sanitize ? check_plain($type_name) : $type_name;
break;
case 'title':
@@ -147,15 +152,6 @@ function node_tokens($type, $tokens, array $data = array(), array $options = arr
}
break;
- case 'type':
- $replacements[$original] = $sanitize ? check_plain($node->type) : $node->type;
- break;
-
- case 'type-name':
- $type_name = node_type_get_name($node);
- $replacements[$original] = $sanitize ? check_plain($type_name) : $type_name;
- break;
-
case 'language':
$replacements[$original] = $sanitize ? check_plain($node->language) : $node->language;
break;