1. 8.2.x core/modules/comment/src/Tests/CommentTokenReplaceTest.php
  2. 8.0.x core/modules/comment/src/Tests/CommentTokenReplaceTest.php
  3. 8.1.x core/modules/comment/src/Tests/CommentTokenReplaceTest.php
  4. 8.3.x core/modules/comment/src/Tests/CommentTokenReplaceTest.php

Namespace

Drupal\comment\Tests

File

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

Classes

Namesort descending Description
CommentTokenReplaceTest Generates text using placeholders for dummy content to check comment token replacement.