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