CommentTokenReplaceTest.php 5.24 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\comment\Tests\CommentTokenReplaceTest.
 */

namespace Drupal\comment\Tests;

10
use Drupal\Component\Utility\String;
11
use Drupal\Component\Utility\Xss;
12
use Drupal\comment\Entity\Comment;
13

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

    $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.
42
    $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $parent_comment->id());
43
    $child_comment = $this->postComment(NULL, $this->randomName(), $this->randomName());
44
    $comment = Comment::load($child_comment->id());
45
    $comment->setHomepage('http://example.org/');
46 47

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

    // Generate and test sanitized tokens.
    $tests = array();
52
    $tests['[comment:cid]'] = $comment->id();
53
    $tests['[comment:hostname]'] = String::checkPlain($comment->getHostname());
54 55
    $tests['[comment:name]'] = Xss::filter($comment->getAuthorName());
    $tests['[comment:author]'] = Xss::filter($comment->getAuthorName());
56
    $tests['[comment:mail]'] = String::checkPlain($this->admin_user->getEmail());
57
    $tests['[comment:homepage]'] = check_url($comment->getHomepage());
58
    $tests['[comment:title]'] = Xss::filter($comment->getSubject());
59 60 61
    $tests['[comment:body]'] = $comment->comment_body->processed;
    $tests['[comment:url]'] = url('comment/' . $comment->id(), $url_options + array('fragment' => 'comment-' . $comment->id()));
    $tests['[comment:edit-url]'] = url('comment/' . $comment->id() . '/edit', $url_options);
62 63
    $tests['[comment:created:since]'] = \Drupal::service('date')->formatInterval(REQUEST_TIME - $comment->getCreatedTime(), 2, $language_interface->id);
    $tests['[comment:changed:since]'] = \Drupal::service('date')->formatInterval(REQUEST_TIME - $comment->getChangedTime(), 2, $language_interface->id);
64
    $tests['[comment:parent:cid]'] = $comment->hasParentComment() ? $comment->getParentComment()->id() : NULL;
65
    $tests['[comment:parent:title]'] = String::checkPlain($parent_comment->getSubject());
66
    $tests['[comment:node:nid]'] = $comment->getCommentedEntityId();
67
    $tests['[comment:node:title]'] = String::checkPlain($node->getTitle());
68
    $tests['[comment:author:uid]'] = $comment->getOwnerId();
69
    $tests['[comment:author:name]'] = String::checkPlain($this->admin_user->getUsername());
70 71

    // Test to make sure that we generated something for each token.
72
    $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
73 74

    foreach ($tests as $input => $expected) {
75
      $output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->id));
76
      $this->assertEqual($output, $expected, format_string('Sanitized comment token %token replaced.', array('%token' => $input)));
77 78 79
    }

    // Generate and test unsanitized tokens.
80 81 82
    $tests['[comment:hostname]'] = $comment->getHostname();
    $tests['[comment:name]'] = $comment->getAuthorName();
    $tests['[comment:author]'] = $comment->getAuthorName();
83
    $tests['[comment:mail]'] = $this->admin_user->getEmail();
84 85
    $tests['[comment:homepage]'] = $comment->getHomepage();
    $tests['[comment:title]'] = $comment->getSubject();
86
    $tests['[comment:body]'] = $comment->comment_body->value;
87
    $tests['[comment:parent:title]'] = $parent_comment->getSubject();
88
    $tests['[comment:node:title]'] = $node->getTitle();
89
    $tests['[comment:author:name]'] = $this->admin_user->getUsername();
90 91

    foreach ($tests as $input => $expected) {
92
      $output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->id, 'sanitize' => FALSE));
93
      $this->assertEqual($output, $expected, format_string('Unsanitized comment token %token replaced.', array('%token' => $input)));
94 95 96
    }

    // Load node so comment_count gets computed.
97
    $node = node_load($node->id());
98 99 100

    // Generate comment tokens for the node (it has 2 comments, both new).
    $tests = array();
101 102 103
    $tests['[entity:comment-count]'] = 2;
    $tests['[entity:comment-count-new]'] = 2;
    // Also test the deprecated legacy token.
104
    $tests['[node:comment-count]'] = 2;
105
    $tests['[node:comment-count-new]'] = 2;
106 107

    foreach ($tests as $input => $expected) {
108
      $output = $token_service->replace($input, array('entity' => $node, 'node' => $node), array('langcode' => $language_interface->id));
109
      $this->assertEqual($output, $expected, format_string('Node comment token %token replaced.', array('%token' => $input)));
110 111
    }
  }
112

113
}