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 /modules/comment/comment.test | |
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
Diffstat (limited to 'modules/comment/comment.test')
-rw-r--r-- | modules/comment/comment.test | 100 |
1 files changed, 100 insertions, 0 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))); + } + } +} |