CommentTokenReplaceTest.php 8.64 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\comment\Tests\CommentTokenReplaceTest.
6 7 8 9
 */

namespace Drupal\comment\Tests;

10
use Drupal\Component\Utility\Html;
11
use Drupal\Component\Utility\UrlHelper;
12
use Drupal\Component\Utility\Xss;
13
use Drupal\comment\Entity\Comment;
14
use Drupal\Core\Render\BubbleableMetadata;
15
use Drupal\node\Entity\Node;
16

17
/**
18 19 20 21
 * Generates text using placeholders for dummy content to check comment token
 * replacement.
 *
 * @group comment
22 23 24 25 26 27
 */
class CommentTokenReplaceTest extends CommentTestBase {
  /**
   * Creates a comment, then tests the tokens generated from it.
   */
  function testCommentTokenReplacement() {
28
    $token_service = \Drupal::token();
29
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
30 31 32 33 34
    $url_options = array(
      'absolute' => TRUE,
      'language' => $language_interface,
    );

35
    $this->drupalLogin($this->adminUser);
36 37 38 39 40 41

    // Set comment variables.
    $this->setCommentSubject(TRUE);

    // Create a node and a comment.
    $node = $this->drupalCreateNode(array('type' => 'article'));
42
    $parent_comment = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
43 44

    // Post a reply to the comment.
45
    $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $parent_comment->id());
46
    $child_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
47
    $comment = Comment::load($child_comment->id());
48
    $comment->setHomepage('http://example.org/');
49 50

    // Add HTML to ensure that sanitation of some fields tested directly.
51
    $comment->setSubject('<blink>Blinking Comment</blink>');
52 53 54

    // Generate and test sanitized tokens.
    $tests = array();
55
    $tests['[comment:cid]'] = $comment->id();
56
    $tests['[comment:hostname]'] = Html::escape($comment->getHostname());
57
    $tests['[comment:author]'] = Xss::filter($comment->getAuthorName());
58
    $tests['[comment:mail]'] = Html::escape($this->adminUser->getEmail());
59
    $tests['[comment:homepage]'] = UrlHelper::filterBadProtocol($comment->getHomepage());
60
    $tests['[comment:title]'] = Xss::filter($comment->getSubject());
61
    $tests['[comment:body]'] = $comment->comment_body->processed;
62
    $tests['[comment:langcode]'] = Html::escape($comment->language()->getId());
63 64
    $tests['[comment:url]'] = $comment->url('canonical', $url_options + array('fragment' => 'comment-' . $comment->id()));
    $tests['[comment:edit-url]'] = $comment->url('edit-form', $url_options);
65
    $tests['[comment:created]'] = \Drupal::service('date.formatter')->format($comment->getCreatedTime(), 'medium', array('langcode' => $language_interface->getId()));
66 67
    $tests['[comment:created:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getCreatedTime(), array('langcode' => $language_interface->getId()));
    $tests['[comment:changed:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getChangedTimeAcrossTranslations(), array('langcode' => $language_interface->getId()));
68
    $tests['[comment:parent:cid]'] = $comment->hasParentComment() ? $comment->getParentComment()->id() : NULL;
69 70
    $tests['[comment:parent:title]'] = Html::escape($parent_comment->getSubject());
    $tests['[comment:entity]'] = Html::escape($node->getTitle());
71 72
    // Test node specific tokens.
    $tests['[comment:entity:nid]'] = $comment->getCommentedEntityId();
73
    $tests['[comment:entity:title]'] = Html::escape($node->getTitle());
74
    $tests['[comment:author:uid]'] = $comment->getOwnerId();
75
    $tests['[comment:author:name]'] = Html::escape($this->adminUser->getUsername());
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    $base_bubbleable_metadata = BubbleableMetadata::createFromObject($comment);
    $metadata_tests = [];
    $metadata_tests['[comment:cid]'] = $base_bubbleable_metadata;
    $metadata_tests['[comment:hostname]'] = $base_bubbleable_metadata;
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $bubbleable_metadata->addCacheableDependency($this->adminUser);
    $metadata_tests['[comment:author]'] = $bubbleable_metadata;
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $bubbleable_metadata->addCacheableDependency($this->adminUser);
    $metadata_tests['[comment:mail]'] = $bubbleable_metadata;
    $metadata_tests['[comment:homepage]'] = $base_bubbleable_metadata;
    $metadata_tests['[comment:title]'] = $base_bubbleable_metadata;
    $metadata_tests['[comment:body]'] = $base_bubbleable_metadata;
    $metadata_tests['[comment:langcode]'] = $base_bubbleable_metadata;
    $metadata_tests['[comment:url]'] = $base_bubbleable_metadata;
    $metadata_tests['[comment:edit-url]'] = $base_bubbleable_metadata;
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $metadata_tests['[comment:created]'] = $bubbleable_metadata->addCacheTags(['rendered']);
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $metadata_tests['[comment:created:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $metadata_tests['[comment:changed:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $metadata_tests['[comment:parent:cid]'] = $bubbleable_metadata->addCacheTags(['comment:1']);
    $metadata_tests['[comment:parent:title]'] = $bubbleable_metadata;
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $metadata_tests['[comment:entity]'] = $bubbleable_metadata->addCacheTags(['node:2']);
    // Test node specific tokens.
    $metadata_tests['[comment:entity:nid]'] = $bubbleable_metadata;
    $metadata_tests['[comment:entity:title]'] = $bubbleable_metadata;
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $metadata_tests['[comment:author:uid]'] = $bubbleable_metadata->addCacheTags(['user:2']);
    $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 114

    foreach ($tests as $input => $expected) {
115 116
      $bubbleable_metadata = new BubbleableMetadata();
      $output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId()), $bubbleable_metadata);
117
      $this->assertEqual($output, $expected, format_string('Sanitized comment token %token replaced.', array('%token' => $input)));
118
      $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
119 120 121
    }

    // Generate and test unsanitized tokens.
122 123
    $tests['[comment:hostname]'] = $comment->getHostname();
    $tests['[comment:author]'] = $comment->getAuthorName();
124
    $tests['[comment:mail]'] = $this->adminUser->getEmail();
125 126
    $tests['[comment:homepage]'] = $comment->getHomepage();
    $tests['[comment:title]'] = $comment->getSubject();
127
    $tests['[comment:body]'] = $comment->comment_body->value;
128
    $tests['[comment:langcode]'] = $comment->language()->getId();
129
    $tests['[comment:parent:title]'] = $parent_comment->getSubject();
130
    $tests['[comment:entity]'] = $node->getTitle();
131
    $tests['[comment:author:name]'] = $this->adminUser->getUsername();
132 133

    foreach ($tests as $input => $expected) {
134
      $output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId(), 'sanitize' => FALSE));
135
      $this->assertEqual($output, $expected, format_string('Unsanitized comment token %token replaced.', array('%token' => $input)));
136 137
    }

138 139 140 141 142 143 144 145 146
    // Test anonymous comment author.
    $author_name = $this->randomString();
    $comment->setOwnerId(0)->setAuthorName($author_name);
    $input = '[comment:author]';
    $output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId()));
    $this->assertEqual($output, Xss::filter($author_name), format_string('Sanitized comment author token %token replaced.', array('%token' => $input)));
    $output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId(), 'sanitize' => FALSE));
    $this->assertEqual($output, $author_name, format_string('Unsanitized comment author token %token replaced.', array('%token' => $input)));

147
    // Load node so comment_count gets computed.
148
    $node = Node::load($node->id());
149 150 151

    // Generate comment tokens for the node (it has 2 comments, both new).
    $tests = array();
152 153
    $tests['[entity:comment-count]'] = 2;
    $tests['[entity:comment-count-new]'] = 2;
154 155

    foreach ($tests as $input => $expected) {
156
      $output = $token_service->replace($input, array('entity' => $node, 'node' => $node), array('langcode' => $language_interface->getId()));
157
      $this->assertEqual($output, $expected, format_string('Node comment token %token replaced.', array('%token' => $input)));
158 159
    }
  }
160

161
}