CommentTokenReplaceTest.php 5.29 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

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**
 * Tests comment token replacement in strings.
 */
class CommentTokenReplaceTest extends CommentTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Comment token replacement',
      'description' => 'Generates text using placeholders for dummy content to check comment token replacement.',
      'group' => 'Comment',
    );
  }

  /**
   * Creates a comment, then tests the tokens generated from it.
   */
  function testCommentTokenReplacement() {
29
    $token_service = \Drupal::token();
30
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    $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.
46
    $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $parent_comment->id());
47
    $child_comment = $this->postComment(NULL, $this->randomName(), $this->randomName());
48
    $comment = comment_load($child_comment->id());
49
    $comment->setHomepage('http://example.org/');
50 51

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

    // Generate and test sanitized tokens.
    $tests = array();
56
    $tests['[comment:cid]'] = $comment->id();
57
    $tests['[comment:hostname]'] = String::checkPlain($comment->getHostname());
58 59
    $tests['[comment:name]'] = Xss::filter($comment->getAuthorName());
    $tests['[comment:author]'] = Xss::filter($comment->getAuthorName());
60
    $tests['[comment:mail]'] = String::checkPlain($this->admin_user->getEmail());
61
    $tests['[comment:homepage]'] = check_url($comment->getHomepage());
62
    $tests['[comment:title]'] = Xss::filter($comment->getSubject());
63 64 65
    $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);
66 67 68
    $tests['[comment:created:since]'] = format_interval(REQUEST_TIME - $comment->getCreatedTime(), 2, $language_interface->id);
    $tests['[comment:changed:since]'] = format_interval(REQUEST_TIME - $comment->getChangedTime(), 2, $language_interface->id);
    $tests['[comment:parent:cid]'] = $comment->hasParentComment() ? $comment->getParentComment()->id() : NULL;
69
    $tests['[comment:parent:title]'] = String::checkPlain($parent_comment->getSubject());
70
    $tests['[comment:node:nid]'] = $comment->getCommentedEntityId();
71
    $tests['[comment:node:title]'] = String::checkPlain($node->getTitle());
72
    $tests['[comment:author:uid]'] = $comment->getOwnerId();
73
    $tests['[comment:author:name]'] = String::checkPlain($this->admin_user->getUsername());
74 75

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

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

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

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

    // Load node so comment_count gets computed.
101
    $node = node_load($node->id());
102 103 104

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

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