3 namespace Drupal\comment\Tests;
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Component\Utility\Html;
8 use Drupal\Component\Utility\UrlHelper;
9 use Drupal\comment\Entity\Comment;
10 use Drupal\Core\Render\BubbleableMetadata;
11 use Drupal\node\Entity\Node;
12 use Drupal\taxonomy\Entity\Term;
13 use Drupal\taxonomy\Entity\Vocabulary;
14 use Drupal\user\Entity\User;
17 * Generates text using placeholders for dummy content to check comment token
22 class CommentTokenReplaceTest extends CommentTestBase {
27 public static $modules = ['taxonomy'];
30 * Creates a comment, then tests the tokens generated from it.
32 public function testCommentTokenReplacement() {
33 $token_service = \Drupal::token();
34 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
37 'language' => $language_interface,
46 // Change the title of the admin user.
47 $this->adminUser->name->value = 'This is a title with some special & > " stuff.';
48 $this->adminUser->save();
49 $this->drupalLogin($this->adminUser);
51 // Set comment variables.
52 $this->setCommentSubject(TRUE);
54 // Create a node and a comment.
55 $node = $this->drupalCreateNode(['type' => 'article', 'title' => '<script>alert("123")</script>']);
56 $parent_comment = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
58 // Post a reply to the comment.
59 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $parent_comment->id());
60 $child_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
61 $comment = Comment::load($child_comment->id());
62 $comment->setHomepage('http://example.org/');
64 // Add HTML to ensure that sanitation of some fields tested directly.
65 $comment->setSubject('<blink>Blinking Comment</blink>');
67 // Generate and test tokens.
69 $tests['[comment:cid]'] = $comment->id();
70 $tests['[comment:hostname]'] = $comment->getHostname();
71 $tests['[comment:author]'] = Html::escape($comment->getAuthorName());
72 $tests['[comment:mail]'] = $this->adminUser->getEmail();
73 $tests['[comment:homepage]'] = UrlHelper::filterBadProtocol($comment->getHomepage());
74 $tests['[comment:title]'] = Html::escape($comment->getSubject());
75 $tests['[comment:body]'] = $comment->comment_body->processed;
76 $tests['[comment:langcode]'] = $comment->language()->getId();
77 $tests['[comment:url]'] = $comment->url('canonical', $url_options + ['fragment' => 'comment-' . $comment->id()]);
78 $tests['[comment:edit-url]'] = $comment->url('edit-form', $url_options);
79 $tests['[comment:created]'] = \Drupal::service('date.formatter')->format($comment->getCreatedTime(), 'medium', ['langcode' => $language_interface->getId()]);
80 $tests['[comment:created:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getCreatedTime(), ['langcode' => $language_interface->getId()]);
81 $tests['[comment:changed:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getChangedTimeAcrossTranslations(), ['langcode' => $language_interface->getId()]);
82 $tests['[comment:parent:cid]'] = $comment->hasParentComment() ? $comment->getParentComment()->id() : NULL;
83 $tests['[comment:parent:title]'] = $parent_comment->getSubject();
84 $tests['[comment:entity]'] = Html::escape($node->getTitle());
85 // Test node specific tokens.
86 $tests['[comment:entity:nid]'] = $comment->getCommentedEntityId();
87 $tests['[comment:entity:title]'] = Html::escape($node->getTitle());
88 $tests['[comment:author:uid]'] = $comment->getOwnerId();
89 $tests['[comment:author:name]'] = Html::escape($this->adminUser->getDisplayName());
91 $base_bubbleable_metadata = BubbleableMetadata::createFromObject($comment);
93 $metadata_tests['[comment:cid]'] = $base_bubbleable_metadata;
94 $metadata_tests['[comment:hostname]'] = $base_bubbleable_metadata;
95 $bubbleable_metadata = clone $base_bubbleable_metadata;
96 $bubbleable_metadata->addCacheableDependency($this->adminUser);
97 $metadata_tests['[comment:author]'] = $bubbleable_metadata;
98 $bubbleable_metadata = clone $base_bubbleable_metadata;
99 $bubbleable_metadata->addCacheableDependency($this->adminUser);
100 $metadata_tests['[comment:mail]'] = $bubbleable_metadata;
101 $metadata_tests['[comment:homepage]'] = $base_bubbleable_metadata;
102 $metadata_tests['[comment:title]'] = $base_bubbleable_metadata;
103 $metadata_tests['[comment:body]'] = $base_bubbleable_metadata;
104 $metadata_tests['[comment:langcode]'] = $base_bubbleable_metadata;
105 $metadata_tests['[comment:url]'] = $base_bubbleable_metadata;
106 $metadata_tests['[comment:edit-url]'] = $base_bubbleable_metadata;
107 $bubbleable_metadata = clone $base_bubbleable_metadata;
108 $metadata_tests['[comment:created]'] = $bubbleable_metadata->addCacheTags(['rendered']);
109 $bubbleable_metadata = clone $base_bubbleable_metadata;
110 $metadata_tests['[comment:created:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
111 $bubbleable_metadata = clone $base_bubbleable_metadata;
112 $metadata_tests['[comment:changed:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
113 $bubbleable_metadata = clone $base_bubbleable_metadata;
114 $metadata_tests['[comment:parent:cid]'] = $bubbleable_metadata->addCacheTags(['comment:1']);
115 $metadata_tests['[comment:parent:title]'] = $bubbleable_metadata;
116 $bubbleable_metadata = clone $base_bubbleable_metadata;
117 $metadata_tests['[comment:entity]'] = $bubbleable_metadata->addCacheTags(['node:2']);
118 // Test node specific tokens.
119 $metadata_tests['[comment:entity:nid]'] = $bubbleable_metadata;
120 $metadata_tests['[comment:entity:title]'] = $bubbleable_metadata;
121 $bubbleable_metadata = clone $base_bubbleable_metadata;
122 $metadata_tests['[comment:author:uid]'] = $bubbleable_metadata->addCacheTags(['user:2']);
123 $metadata_tests['[comment:author:name]'] = $bubbleable_metadata;
125 // Test to make sure that we generated something for each token.
126 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
128 foreach ($tests as $input => $expected) {
129 $bubbleable_metadata = new BubbleableMetadata();
130 $output = $token_service->replace($input, ['comment' => $comment], ['langcode' => $language_interface->getId()], $bubbleable_metadata);
131 $this->assertEqual($output, $expected, new FormattableMarkup('Comment token %token replaced.', ['%token' => $input]));
132 $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
135 // Test anonymous comment author.
136 $author_name = 'This is a random & " > string';
137 $comment->setOwnerId(0)->setAuthorName($author_name);
138 $input = '[comment:author]';
139 $output = $token_service->replace($input, ['comment' => $comment], ['langcode' => $language_interface->getId()]);
140 $this->assertEqual($output, Html::escape($author_name), format_string('Comment author token %token replaced.', ['%token' => $input]));
141 // Add comment field to user and term entities.
142 $this->addDefaultCommentField('user', 'user', 'comment', CommentItemInterface::OPEN, 'comment_user');
143 $this->addDefaultCommentField('taxonomy_term', 'tags', 'comment', CommentItemInterface::OPEN, 'comment_term');
145 // Create a user and a comment.
146 $user = User::create(['name' => 'alice']);
148 $this->postComment($user, 'user body', 'user subject', TRUE);
150 // Create a term and a comment.
151 $term = Term::create([
156 $this->postComment($term, 'term body', 'term subject', TRUE);
158 // Load node, user and term again so comment_count gets computed.
159 $node = Node::load($node->id());
160 $user = User::load($user->id());
161 $term = Term::load($term->id());
163 // Generate comment tokens for node (it has 2 comments, both new),
166 $tests['[entity:comment-count]'] = 2;
167 $tests['[entity:comment-count-new]'] = 2;
168 $tests['[node:comment-count]'] = 2;
169 $tests['[node:comment-count-new]'] = 2;
170 $tests['[user:comment-count]'] = 1;
171 $tests['[user:comment-count-new]'] = 1;
172 $tests['[term:comment-count]'] = 1;
173 $tests['[term:comment-count-new]'] = 1;
175 foreach ($tests as $input => $expected) {
176 $output = $token_service->replace($input, ['entity' => $node, 'node' => $node, 'user' => $user, 'term' => $term], ['langcode' => $language_interface->getId()]);
177 $this->assertEqual($output, $expected, format_string('Comment token %token replaced.', ['%token' => $input]));