diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-04-20 09:48:06 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-04-20 09:48:06 +0000 |
commit | 7bb6753e9fc8d89472ecc2b6d5ab670dde27b935 (patch) | |
tree | 79eae1caf8b93351560fa36202d46913ac06e1ef | |
parent | 8e94b5d6d400d33c0f840a7ae97ff8a715272a79 (diff) | |
download | brdo-7bb6753e9fc8d89472ecc2b6d5ab670dde27b935.tar.gz brdo-7bb6753e9fc8d89472ecc2b6d5ab670dde27b935.tar.bz2 |
#701818 by mcarbone: Test coverage of every core token, and bug fixes to make them work. AWESOME! :D
-rw-r--r-- | modules/comment/comment.test | 100 | ||||
-rw-r--r-- | modules/comment/comment.tokens.inc | 22 | ||||
-rw-r--r-- | modules/file/tests/file.test | 76 | ||||
-rw-r--r-- | modules/node/node.test | 76 | ||||
-rw-r--r-- | modules/node/node.tokens.inc | 18 | ||||
-rw-r--r-- | modules/poll/poll.test | 89 | ||||
-rw-r--r-- | modules/poll/poll.tokens.inc | 17 | ||||
-rw-r--r-- | modules/statistics/statistics.test | 44 | ||||
-rw-r--r-- | modules/statistics/statistics.tokens.inc | 12 | ||||
-rw-r--r-- | modules/system/system.test | 71 | ||||
-rw-r--r-- | modules/system/system.tokens.inc | 53 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.test | 105 | ||||
-rw-r--r-- | modules/user/user.test | 64 | ||||
-rw-r--r-- | modules/user/user.tokens.inc | 1 |
14 files changed, 636 insertions, 112 deletions
diff --git a/modules/comment/comment.test b/modules/comment/comment.test index 43f3c26ae..b04618724 100644 --- a/modules/comment/comment.test +++ b/modules/comment/comment.test @@ -1263,3 +1263,103 @@ class CommentContentRebuild extends CommentHelperCase { $this->assertFalse(isset($built_content['test_property']), t('Comment content was emptied before being built.')); } } + +/** + * Test comment token replacement in strings. + */ +class CommentTokenReplaceTestCase extends CommentHelperCase { + public static function getInfo() { + return array( + 'name' => 'Comment token replacement', + 'description' => 'Generates text using placeholders for dummy content to check comment token replacement.', + 'group' => 'Comment', + ); + } + + /** + * Creates a comment, then tests the tokens generated from it. + */ + function testCommentTokenReplacement() { + global $language; + $url_options = array( + 'absolute' => TRUE, + 'language' => $language, + ); + + $this->drupalLogin($this->admin_user); + + // Set comment variables. + $this->setCommentSubject(TRUE); + + // Create a node and a comment. + $node = $this->drupalCreateNode(array('type' => 'article')); + $parent_comment = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE); + + // Post a reply to the comment. + $this->drupalGet('comment/reply/' . $node->nid . '/' . $parent_comment->id); + $child_comment = $this->postComment(NULL, $this->randomName(), $this->randomName()); + $comment = comment_load($child_comment->id); + $comment->homepage = 'http://example.org/'; + + // Add HTML to ensure that sanitation of some fields tested directly. + $comment->subject = '<blink>Blinking Comment</blink>'; + $instance = field_info_instance('comment', 'body', 'comment_body'); + + // Generate and test sanitized tokens. + $tests = array(); + $tests['[comment:cid]'] = $comment->cid; + $tests['[comment:pid]'] = $comment->pid; + $tests['[comment:nid]'] = $comment->nid; + $tests['[comment:uid]'] = $comment->uid; + $tests['[comment:hostname]'] = check_plain($comment->hostname); + $tests['[comment:name]'] = filter_xss($comment->name); + $tests['[comment:mail]'] = check_plain($this->admin_user->mail); + $tests['[comment:homepage]'] = filter_xss_bad_protocol($comment->homepage); + $tests['[comment:title]'] = filter_xss($comment->subject); + $tests['[comment:body]'] = _text_sanitize($instance, LANGUAGE_NONE, $comment->comment_body[LANGUAGE_NONE][0], 'value'); + $tests['[comment:url]'] = url('comment/' . $comment->cid, $url_options + array('fragment' => 'comment-' . $comment->cid)); + $tests['[comment:edit-url]'] = url('comment/' . $comment->cid . '/edit', $url_options); + $tests['[comment:created:since]'] = format_interval(REQUEST_TIME - $comment->created, 2, $language->language); + $tests['[comment:changed:since]'] = format_interval(REQUEST_TIME - $comment->changed, 2, $language->language); + $tests['[comment:parent:title]'] = check_plain($parent_comment->subject); + $tests['[comment:node:title]'] = check_plain($node->title); + $tests['[comment:author:name]'] = check_plain($this->admin_user->name); + + // 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('comment' => $comment), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Sanitized comment token %token replaced.', array('%token' => $input))); + } + + // Generate and test unsanitized tokens. + $tests['[comment:hostname]'] = $comment->hostname; + $tests['[comment:name]'] = $comment->name; + $tests['[comment:mail]'] = $this->admin_user->mail; + $tests['[comment:homepage]'] = $comment->homepage; + $tests['[comment:title]'] = $comment->subject; + $tests['[comment:body]'] = $comment->comment_body[LANGUAGE_NONE][0]['value']; + $tests['[comment:parent:title]'] = $parent_comment->subject; + $tests['[comment:node:title]'] = $node->title; + $tests['[comment:author:name]'] = $this->admin_user->name; + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('comment' => $comment), array('language' => $language, 'sanitize' => FALSE)); + $this->assertFalse(strcmp($output, $expected), t('Unsanitized comment token %token replaced.', array('%token' => $input))); + } + + // Load node so comment_count gets computed. + $node = node_load($node->nid); + + // Generate comment tokens for the node (it has 2 comments, both new). + $tests = array(); + $tests['[node:comment-count]'] = 2; + $tests['[node:comment-count-new]'] = 2; + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('node' => $node), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Node comment token %token replaced.', array('%token' => $input))); + } + } +} diff --git a/modules/comment/comment.tokens.inc b/modules/comment/comment.tokens.inc index d16eabda6..cf26d74c9 100644 --- a/modules/comment/comment.tokens.inc +++ b/modules/comment/comment.tokens.inc @@ -82,6 +82,11 @@ function comment_token_info() { 'description' => t("The date the comment was posted."), 'type' => 'date', ); + $comment['changed'] = array( + 'name' => t("Date changed"), + 'description' => t("The date the comment was most recently updated."), + 'type' => 'date', + ); $comment['parent'] = array( 'name' => t("Parent"), 'description' => t("The comment's parent, if comment threading is active."), @@ -133,6 +138,10 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = $replacements[$original] = $comment->cid; break; + case 'pid': + $replacements[$original] = $comment->pid; + break; + case 'nid': $replacements[$original] = $comment->nid; break; @@ -141,10 +150,6 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = $replacements[$original] = $comment->uid; break; - case 'pid': - $replacements[$original] = $comment->pid; - break; - // Poster identity information for comments case 'hostname': $replacements[$original] = $sanitize ? check_plain($comment->hostname) : $comment->hostname; @@ -175,7 +180,9 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = break; case 'body': - $replacements[$original] = $sanitize ? check_markup($comment->comment, $comment->format, '', TRUE) : $comment->comment; + $item = $comment->comment_body[LANGUAGE_NONE][0]; + $instance = field_info_instance('comment', 'body', 'comment_body'); + $replacements[$original] = $sanitize ? _text_sanitize($instance, LANGUAGE_NONE, $item, 'value') : $item['value']; break; // Comment related URLs. @@ -185,6 +192,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = break; case 'edit-url': + $url_options['fragment'] = NULL; $replacements[$original] = url('comment/' . $comment->cid . '/edit', $url_options); break; @@ -226,6 +234,10 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = $replacements += token_generate('date', $date_tokens, array('date' => $comment->created), $options); } + if ($date_tokens = token_find_with_prefix($tokens, 'changed')) { + $replacements += token_generate('date', $date_tokens, array('date' => $comment->changed), $options); + } + if (($parent_tokens = token_find_with_prefix($tokens, 'parent')) && $parent = comment_load($comment->pid)) { $replacements += token_generate('comment', $parent_tokens, array('comment' => $parent), $options); } diff --git a/modules/file/tests/file.test b/modules/file/tests/file.test index 7a9a14095..f99d30bba 100644 --- a/modules/file/tests/file.test +++ b/modules/file/tests/file.test @@ -568,3 +568,79 @@ class FileFieldPathTestCase extends FileFieldTestCase { $this->assertTrue($result, $message); } } + +/** + * Test file token replacement in strings. + */ +class FileTokenReplaceTestCase extends FileFieldTestCase { + public static function getInfo() { + return array( + 'name' => 'File token replacement', + 'description' => 'Generates text using placeholders for dummy content to check file token replacement.', + 'group' => 'File', + ); + } + + /** + * Creates a file, then tests the tokens generated from it. + */ + function testFileTokenReplacement() { + global $language; + $url_options = array( + 'absolute' => TRUE, + 'language' => $language, + ); + + // Create file field. + $type_name = 'article'; + $field_name = 'field_' . strtolower($this->randomName()); + $this->createFileField($field_name, $type_name); + $field = field_info_field($field_name); + $instance = field_info_instance('node', $field_name, $type_name); + + $test_file = $this->getTestFile('text'); + + // Create a new node with the uploaded file. + $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); + + // Load the node and the file. + $node = node_load($nid); + $file = (object) $node->{$field_name}[LANGUAGE_NONE][0]; + $file->description = 'File description.'; + + // Generate and test sanitized tokens. + $tests = array(); + $tests['[file:fid]'] = $file->fid; + $tests['[file:uid]'] = $file->uid; + $tests['[file:name]'] = check_plain($file->filename); + $tests['[file:description]'] = filter_xss($file->description); + $tests['[file:path]'] = filter_xss($file->uri); + $tests['[file:mime]'] = filter_xss($file->filemime); + $tests['[file:size]'] = format_size($file->filesize); + $tests['[file:url]'] = url(file_create_url($file->uri), $url_options); + $tests['[file:timestamp]'] = format_date($file->timestamp, 'medium', '', NULL, $language->language); + $tests['[file:timestamp:short]'] = format_date($file->timestamp, 'short', '', NULL, $language->language); + $tests['[file:owner]'] = $this->admin_user->name; + $tests['[file:owner:uid]'] = $this->admin_user->uid; + + // 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('file' => $file), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Sanitized file token %token replaced.', array('%token' => $input))); + } + + // Generate and test unsanitized tokens. + $tests['[file:name]'] = $file->filename; + $tests['[file:description]'] = $file->description; + $tests['[file:path]'] = $file->uri; + $tests['[file:mime]'] = $file->filemime; + $tests['[file:size]'] = format_size($file->filesize); + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('file' => $file), array('language' => $language, 'sanitize' => FALSE)); + $this->assertFalse(strcmp($output, $expected), t('Unsanitized file token %token replaced.', array('%token' => $input))); + } + } +} 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; diff --git a/modules/poll/poll.test b/modules/poll/poll.test index e4a7db848..35d5ed49e 100644 --- a/modules/poll/poll.test +++ b/modules/poll/poll.test @@ -457,3 +457,92 @@ class PollVoteCheckHostname extends PollTestCase { $this->assertTrue(empty($elements), t("%user is not able to vote again.", array('%user' => $this->web_user1->name))); } } + +/** + * Test poll token replacement in strings. + */ +class PollTokenReplaceTestCase extends PollTestCase { + public static function getInfo() { + return array( + 'name' => 'Poll token replacement', + 'description' => 'Generates text using placeholders for dummy content to check poll token replacement.', + 'group' => 'Poll', + ); + } + + function setUp() { + parent::setUp('poll'); + } + + /** + * Creates a poll, then tests the tokens generated from it. + */ + function testPollTokenReplacement() { + global $language; + + // Craete a poll with three choices. + $title = $this->randomName(); + $choices = $this->_generateChoices(3); + $poll_nid = $this->pollCreate($title, $choices, FALSE); + $this->drupalLogout(); + + // Create four users and have each of them vote. + $vote_user1 = $this->drupalCreateUser(array('vote on polls', 'access content')); + $this->drupalLogin($vote_user1); + $edit = array( + 'choice' => '1', + ); + $this->drupalPost('node/' . $poll_nid, $edit, t('Vote')); + $this->drupalLogout(); + + $vote_user2 = $this->drupalCreateUser(array('vote on polls', 'access content')); + $this->drupalLogin($vote_user2); + $edit = array( + 'choice' => '1', + ); + $this->drupalPost('node/' . $poll_nid, $edit, t('Vote')); + $this->drupalLogout(); + + $vote_user3 = $this->drupalCreateUser(array('vote on polls', 'access content')); + $this->drupalLogin($vote_user3); + $edit = array( + 'choice' => '2', + ); + $this->drupalPost('node/' . $poll_nid, $edit, t('Vote')); + $this->drupalLogout(); + + $vote_user4 = $this->drupalCreateUser(array('vote on polls', 'access content')); + $this->drupalLogin($vote_user4); + $edit = array( + 'choice' => '3', + ); + $this->drupalPost('node/' . $poll_nid, $edit, t('Vote')); + $this->drupalLogout(); + + $poll = node_load($poll_nid, NULL, TRUE); + + // Generate and test sanitized tokens. + $tests = array(); + $tests['[node:poll-votes]'] = 4; + $tests['[node:poll-winner]'] = filter_xss($poll->choice[1]['chtext']); + $tests['[node:poll-winner-votes]'] = 2; + $tests['[node:poll-winner-percent]'] = 50; + $tests['[node:poll-duration]'] = format_interval($poll->runtime, 1, $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' => $poll), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Sanitized poll token %token replaced.', array('%token' => $input))); + } + + // Generate and test unsanitized tokens. + $tests['[node:poll-winner]'] = $poll->choice[1]['chtext']; + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('node' => $poll), array('language' => $language, 'sanitize' => FALSE)); + $this->assertFalse(strcmp($output, $expected), t('Unsanitized poll token %token replaced.', array('%token' => $input))); + } + } +} diff --git a/modules/poll/poll.tokens.inc b/modules/poll/poll.tokens.inc index 6affdb74a..6f931ac51 100644 --- a/modules/poll/poll.tokens.inc +++ b/modules/poll/poll.tokens.inc @@ -41,6 +41,14 @@ function poll_token_info() { */ function poll_tokens($type, $tokens, array $data = array(), array $options = array()) { $sanitize = !empty($options['sanitize']); + if (isset($options['language'])) { + $url_options['language'] = $options['language']; + $language_code = $options['language']->language; + } + else { + $language_code = NULL; + } + $replacements = array(); if ($type == 'node' && !empty($data['node']) && $data['node']->type == 'poll') { @@ -65,12 +73,18 @@ function poll_tokens($type, $tokens, array $data = array(), array $options = arr if (isset($winner)) { $replacements[$original] = $sanitize ? filter_xss($winner['chtext']) : $winner['chtext']; } + else { + $replacements[$original] = ''; + } break; case 'poll-winner-votes': if (isset($winner)) { $replacements[$original] = $winner['chvotes']; } + else { + $replacements[$original] = ''; + } break; case 'poll-winner-percent': @@ -78,6 +92,9 @@ function poll_tokens($type, $tokens, array $data = array(), array $options = arr $percent = ($winner['chvotes'] / $total_votes) * 100; $replacements[$original] = number_format($percent, 0); } + else { + $replacements[$original] = ''; + } break; case 'poll-duration': diff --git a/modules/statistics/statistics.test b/modules/statistics/statistics.test index cd312f679..283c2d9da 100644 --- a/modules/statistics/statistics.test +++ b/modules/statistics/statistics.test @@ -312,3 +312,47 @@ class StatisticsAdminTestCase extends DrupalWebTestCase { $this->assertFalse($result, t('Daycounter is zero.')); } } + +/** + * Test statistics token replacement in strings. + */ +class StatisticsTokenReplaceTestCase extends StatisticsTestCase { + public static function getInfo() { + return array( + 'name' => 'Statistics token replacement', + 'description' => 'Generates text using placeholders for dummy content to check statistics token replacement.', + 'group' => 'Statistics', + ); + } + + /** + * Creates a node, then tests the statistics tokens generated from it. + */ + 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)), 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('Statistics token %token replaced.', array('%token' => $input))); + } + } +} diff --git a/modules/statistics/statistics.tokens.inc b/modules/statistics/statistics.tokens.inc index 5261c4b16..1eb5b5d32 100644 --- a/modules/statistics/statistics.tokens.inc +++ b/modules/statistics/statistics.tokens.inc @@ -10,11 +10,11 @@ * Implements hook_token_info(). */ function statistics_token_info() { - $node['views'] = array( + $node['total-count'] = array( 'name' => t("Number of views"), 'description' => t("The number of visitors who have read the node."), ); - $node['day-views'] = array( + $node['day-count'] = array( 'name' => t("Views today"), 'description' => t("The number of visitors who have read the node today."), ); @@ -40,13 +40,13 @@ function statistics_tokens($type, $tokens, array $data = array(), array $options $node = $data['node']; foreach ($tokens as $name => $original) { - if ($name == 'views') { + if ($name == 'total-count') { $statistics = statistics_get($node->nid); - $replacements[$original] = $statistics['totalviews']; + $replacements[$original] = $statistics['totalcount']; } - elseif ($name == 'views-today') { + elseif ($name == 'day-count') { $statistics = statistics_get($node->nid); - $replacements[$original] = $statistics['dayviews']; + $replacements[$original] = $statistics['daycount']; } elseif ($name == 'last-view') { $statistics = statistics_get($node->nid); diff --git a/modules/system/system.test b/modules/system/system.test index e20fd769e..d94f114a2 100644 --- a/modules/system/system.test +++ b/modules/system/system.test @@ -1490,6 +1490,77 @@ class TokenReplaceTestCase extends DrupalWebTestCase { $generated = token_generate('node', $raw_tokens, array('node' => $node), array('sanitize' => FALSE)); $this->assertFalse(strcmp($generated['[node:title]'], $node->title), t('Unsanitized token generated properly.')); } + + /** + * Tests the generation of all system site information tokens. + */ + function testSystemSiteTokenReplacement() { + global $language; + $url_options = array( + 'absolute' => TRUE, + 'language' => $language, + ); + + // Set a few site variables. + variable_set('site_name', '<strong>Drupal<strong>'); + variable_set('site_slogan', '<blink>Slogan</blink>'); + variable_set('site_mission', '<em>Mission</em>'); + + // Generate and test sanitized tokens. + $tests = array(); + $tests['[site:name]'] = check_plain(variable_get('site_name', 'Drupal')); + $tests['[site:slogan]'] = check_plain(variable_get('site_slogan', '')); + $tests['[site:mission]'] = filter_xss(variable_get('site_mission', '')); + $tests['[site:mail]'] = 'simpletest@example.com'; + $tests['[site:url]'] = url('<front>', $url_options); + $tests['[site:url-brief]'] = preg_replace('!^https?://!', '', url('<front>', $url_options)); + $tests['[site:login-url]'] = url('user', $url_options); + + // 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(), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Sanitized system site information token %token replaced.', array('%token' => $input))); + } + + // Generate and test unsanitized tokens. + $tests['[site:name]'] = variable_get('site_name', 'Drupal'); + $tests['[site:slogan]'] = variable_get('site_slogan', ''); + $tests['[site:mission]'] = variable_get('site_mission', ''); + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array(), array('language' => $language, 'sanitize' => FALSE)); + $this->assertFalse(strcmp($output, $expected), t('Unsanitized system site information token %token replaced.', array('%token' => $input))); + } + } + + /** + * Tests the generation of all system date tokens. + */ + function testSystemDateTokenReplacement() { + global $language; + + // Set time to one hour before request. + $date = REQUEST_TIME - 3600; + + // Generate and test tokens. + $tests = array(); + $tests['[date:short]'] = format_date($date, 'short', '', NULL, $language->language); + $tests['[date:medium]'] = format_date($date, 'medium', '', NULL, $language->language); + $tests['[date:long]'] = format_date($date, 'long', '', NULL, $language->language); + $tests['[date:custom:m/j/Y]'] = format_date($date, 'custom', 'm/j/Y', NULL, $language->language); + $tests['[date:since]'] = format_interval((REQUEST_TIME - $date), 2, $language->language); + $tests['[date:raw]'] = filter_xss($date); + + // 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('date' => $date), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Date token %token replaced.', array('%token' => $input))); + } + } } class InfoFileParserTestCase extends DrupalUnitTestCase { diff --git a/modules/system/system.tokens.inc b/modules/system/system.tokens.inc index e86bf8490..501ab8fd5 100644 --- a/modules/system/system.tokens.inc +++ b/modules/system/system.tokens.inc @@ -76,7 +76,7 @@ function system_token_info() { ); $date['since'] = array( 'name' => t("Time-since"), - 'description' => t("A data in 'time-since' format. (%date)", array('%date' => format_interval(REQUEST_TIME - 360, 2))), + 'description' => t("A date in 'time-since' format. (%date)", array('%date' => format_interval(REQUEST_TIME - 360, 2))), ); $date['raw'] = array( 'name' => t("Raw timestamp"), @@ -93,10 +93,6 @@ function system_token_info() { 'name' => t("User ID"), 'description' => t("The unique ID of the user who owns the file."), ); - $file['nid'] = array( - 'name' => t("Node ID"), - 'description' => t("The unique ID of the node the file is attached to."), - ); $file['name'] = array( 'name' => t("File name"), 'description' => t("The name of the file on disk."), @@ -107,7 +103,7 @@ function system_token_info() { ); $file['path'] = array( 'name' => t("Path"), - 'description' => t("The location of the file on disk."), + 'description' => t("The location of the file relative to Drupal root."), ); $file['mime'] = array( 'name' => t("MIME type"), @@ -117,7 +113,7 @@ function system_token_info() { 'name' => t("File size"), 'description' => t("The size of the file, in kilobytes."), ); - $file['path'] = array( + $file['url'] = array( 'name' => t("URL"), 'description' => t("The web-accessible URL for the file."), ); @@ -126,11 +122,6 @@ function system_token_info() { 'description' => t("The date the file was most recently changed."), 'type' => 'date', ); - $file['node'] = array( - 'name' => t("Node"), - 'description' => t("The node the file is attached to."), - 'type' => 'date', - ); $file['owner'] = array( 'name' => t("Owner"), 'description' => t("The user who originally uploaded the file."), @@ -155,6 +146,7 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a if (isset($language)) { $url_options['language'] = $language; } + $langcode = (isset($language) ? $language->language : NULL); $sanitize = !empty($options['sanitize']); $replacements = array(); @@ -203,14 +195,9 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a else { $date = $data['date']; } - $langcode = (isset($language) ? $language->language : NULL); foreach ($tokens as $name => $original) { switch ($name) { - case 'raw': - $replacements[$original] = filter_xss($date); - break; - case 'short': $replacements[$original] = format_date($date, 'short', '', NULL, $langcode); break; @@ -226,6 +213,10 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a case 'since': $replacements[$original] = format_interval((REQUEST_TIME - $date), 2, $langcode); break; + + case 'raw': + $replacements[$original] = filter_xss($date); + break; } } @@ -250,10 +241,6 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a $replacements[$original] = $file->uid; break; - case 'nid': - $replacements[$original] = $file->nid; - break; - // Essential file data case 'name': $replacements[$original] = $sanitize ? check_plain($file->filename) : $file->filename; @@ -264,7 +251,7 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a break; case 'path': - $replacements[$original] = $sanitize ? filter_xss($file->filepath) : $file->filepath; + $replacements[$original] = $sanitize ? filter_xss($file->uri) : $file->uri; break; case 'mime': @@ -276,39 +263,27 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a break; case 'url': - $replacements[$original] = url(file_create_url($file->filepath), $url_options); + $replacements[$original] = url(file_create_url($file->uri), $url_options); break; // These tokens are default variations on the chained tokens handled below. - case 'node': - if ($nid = $file->nid) { - $node = node_load($file->nid); - $replacements[$original] = $sanitize ? filter_xss($node->title) : $node->title; - } - break; - case 'timestamp': - $replacements[$original] = format_date($file->timestamp, 'medium', '', NULL, (isset($language) ? $language->language : NULL)); + $replacements[$original] = format_date($file->timestamp, 'medium', '', NULL, $langcode); break; case 'owner': $account = user_load($file->uid); - $replacements[$original] = $sanitize ? filter_xss($user->name) : $user->name; + $replacements[$original] = $sanitize ? filter_xss($account->name) : $account->name; break; } } - if ($node_tokens = token_find_with_prefix($tokens, 'node')) { - $node = node_load($file->nid); - $replacements += token_generate('node', $node_tokens, array('node' => $node), $language, $sanitize); - } - if ($date_tokens = token_find_with_prefix($tokens, 'timestamp')) { - $replacements += token_generate('date', $date_tokens, array('date' => $file->timestamp), $language, $sanitize); + $replacements += token_generate('date', $date_tokens, array('date' => $file->timestamp), $options); } if (($owner_tokens = token_find_with_prefix($tokens, 'owner')) && $account = user_load($file->uid)) { - $replacements += token_generate('user', $owner_tokens, array('user' => $account), $language, $sanitize); + $replacements += token_generate('user', $owner_tokens, array('user' => $account), $options); } } diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test index a9b574c19..fd9df1f4f 100644 --- a/modules/taxonomy/taxonomy.test +++ b/modules/taxonomy/taxonomy.test @@ -916,12 +916,15 @@ class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase { * Creates some terms and a node, then tests the tokens generated from them. */ function testTaxonomyTokenReplacement() { + global $language; + // Create two taxonomy terms. $term1 = $this->createTerm($this->vocabulary); $term2 = $this->createTerm($this->vocabulary); // Edit $term2, setting $term1 as parent. $edit = array(); + $edit['name'] = '<blink>Blinking Text</blink>'; $edit['parent[]'] = array($term1->tid); $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save')); @@ -931,57 +934,59 @@ class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase { $edit[$this->instance['field_name'] . '[' . $this->langcode . '][]'] = $term2->tid; $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); - // Generate term token strings (before and after replacement) for term2. - $source = '[term:tid]'; - $source .= '[term:vid]'; - $source .= '[term:name]'; - $source .= '[term:description]'; - $source .= '[term:url]'; - $source .= '[term:node-count]'; - $source .= '[term:parent:name]'; - $source .= '[term:vocabulary:name]'; - - $target = $term2->tid; - $target .= $term2->vid; - $target .= check_plain($term2->name); - $target .= check_markup($term2->description, $term2->format); - $target .= url('taxonomy/term/' . $term2->tid, array('absolute' => TRUE)); - $target .= 1; - $target .= check_plain($term1->name); - $target .= check_plain($this->vocabulary->name); - - $result = token_replace($source, array('term' => $term2)); - $this->assertEqual(strcmp($target, $result), 0, t('Taxonomy term placeholder tokens replaced.')); - - // Generate vocabulary token strings (before and after replacement). - $source = '[vocabulary:vid]'; - $source .= '[vocabulary:name]'; - $source .= '[vocabulary:description]'; - $source .= '[vocabulary:node-count]'; - $source .= '[vocabulary:term-count]'; - - $target = $this->vocabulary->vid; - $target .= check_plain($this->vocabulary->name); - $target .= filter_xss($this->vocabulary->description); - $target .= 1; - $target .= 2; - - $result = token_replace($source, array('vocabulary' => $this->vocabulary)); - $this->assertEqual(strcmp($target, $result), 0, t('Taxonomy vocabulary placeholder tokens replaced.')); - - // Check that the results of token_generate are sanitized properly. This - // does NOT test the cleanliness of every token -- just that the $sanitize - // flag is being passed properly through the call stack and being handled - // correctly by a 'known' token, [term:name]. - $edit = array(); - $edit['name'] = '<blink>Blinking Text</blink>'; - $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save')); + // Generate and test sanitized tokens. + $tests = array(); + $tests['[term:tid]'] = $term2->tid; + $tests['[term:vid]'] = $term2->vid; + $tests['[term:name]'] = check_plain($term2->name); + $tests['[term:description]'] = check_markup($term2->description, $term2->format); + $tests['[term:url]'] = url('taxonomy/term/' . $term2->tid, array('absolute' => TRUE)); + $tests['[term:node-count]'] = 1; + $tests['[term:parent:name]'] = check_plain($term1->name); + $tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name); + + // 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('term' => $term2), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Sanitized taxonomy term token %token replaced.', array('%token' => $input))); + } + + // Generate and test unsanitized tokens. + $tests['[term:name]'] = $term2->name; + $tests['[term:description]'] = $term2->description; + $tests['[term:parent:name]'] = $term1->name; + $tests['[term:vocabulary:name]'] = $this->vocabulary->name; - $raw_tokens = array('name' => '[term:name]'); - $generated = token_generate('term', $raw_tokens, array('term' => $term2)); - $this->assertEqual(strcmp($generated['[term:name]'], check_plain($term2->name)), 0, t('Token sanitized.')); + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('term' => $term2), array('language' => $language, 'sanitize' => FALSE)); + $this->assertFalse(strcmp($output, $expected), t('Unsanitized taxonomy term token %token replaced.', array('%token' => $input))); + } + + // Generate and test sanitized tokens. + $tests = array(); + $tests['[vocabulary:vid]'] = $this->vocabulary->vid; + $tests['[vocabulary:name]'] = check_plain($this->vocabulary->name); + $tests['[vocabulary:description]'] = filter_xss($this->vocabulary->description); + $tests['[vocabulary:node-count]'] = 1; + $tests['[vocabulary:term-count]'] = 2; - $generated = token_generate('term', $raw_tokens, array('term' => $term2), array('sanitize' => FALSE)); - $this->assertEqual(strcmp($generated['[term:name]'], $term2->name), 0, t('Unsanitized token generated properly.')); + // 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('vocabulary' => $this->vocabulary), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Sanitized taxonomy vocabulary token %token replaced.', array('%token' => $input))); + } + + // Generate and test unsanitized tokens. + $tests['[vocabulary:name]'] = $this->vocabulary->name; + $tests['[vocabulary:description]'] = $this->vocabulary->description; + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('vocabulary' => $this->vocabulary), array('language' => $language, 'sanitize' => FALSE)); + $this->assertFalse(strcmp($output, $expected), t('Unsanitized taxonomy vocabulary token %token replaced.', array('%token' => $input))); + } } } diff --git a/modules/user/user.test b/modules/user/user.test index 5287e18f9..33d90ec1a 100644 --- a/modules/user/user.test +++ b/modules/user/user.test @@ -1521,3 +1521,67 @@ class UserRoleAdminTestCase extends DrupalWebTestCase { } } +/** + * Test user token replacement in strings. + */ +class UserTokenReplaceTestCase extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'User token replacement', + 'description' => 'Generates text using placeholders for dummy content to check user token replacement.', + 'group' => 'User', + ); + } + + /** + * Creates a user, then tests the tokens generated from it. + */ + function testUserTokenReplacement() { + global $language; + $url_options = array( + 'absolute' => TRUE, + 'language' => $language, + ); + + // Create two users and log them in one after another. + $user1 = $this->drupalCreateUser(array()); + $user2 = $this->drupalCreateUser(array()); + $this->drupalLogin($user1); + $this->drupalLogout(); + $this->drupalLogin($user2); + + $account = user_load($user1->uid); + global $user; + + // Generate and test sanitized tokens. + $tests = array(); + $tests['[user:uid]'] = $account->uid; + $tests['[user:name]'] = filter_xss($account->name); + $tests['[user:mail]'] = check_plain($account->mail); + $tests['[user:url]'] = url("user/$account->uid", $url_options); + $tests['[user:edit-url]'] = url("user/$account->uid/edit", $url_options); + $tests['[user:last-login]'] = format_date($account->login, 'medium', '', NULL, $language->language); + $tests['[user:last-login:short]'] = format_date($account->login, 'short', '', NULL, $language->language); + $tests['[user:created]'] = format_date($account->created, 'medium', '', NULL, $language->language); + $tests['[user:created:short]'] = format_date($account->created, 'short', '', NULL, $language->language); + $tests['[current-user:name]'] = check_plain($user->name); + + // 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('user' => $account), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Sanitized user token %token replaced.', array('%token' => $input))); + } + + // Generate and test unsanitized tokens. + $tests['[user:name]'] = $account->name; + $tests['[user:mail]'] = $account->mail; + $tests['[current-user:name]'] = $user->name; + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('user' => $account), array('language' => $language, 'sanitize' => FALSE)); + $this->assertFalse(strcmp($output, $expected), t('Unsanitized user token %token replaced.', array('%token' => $input))); + } + } +} diff --git a/modules/user/user.tokens.inc b/modules/user/user.tokens.inc index d6fe22d37..70d4914d5 100644 --- a/modules/user/user.tokens.inc +++ b/modules/user/user.tokens.inc @@ -63,7 +63,6 @@ function user_token_info() { * Implements hook_tokens(). */ function user_tokens($type, $tokens, array $data = array(), array $options = array()) { - global $user; $url_options = array('absolute' => TRUE); if (isset($options['language'])) { $url_options['language'] = $options['language']; |