3 namespace Drupal\Tests\token\Kernel;
5 use Drupal\comment\Entity\Comment;
6 use Drupal\comment\Tests\CommentTestTrait;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
12 * Tests comment tokens.
16 class CommentTest extends KernelTestBase {
25 public static $modules = ['node', 'comment', 'field', 'text', 'entity_reference'];
30 protected function setUp() {
33 $this->installEntitySchema('node');
34 $this->installEntitySchema('user');
35 $this->installEntitySchema('comment');
36 $this->installSchema('comment', ['comment_entity_statistics']);
38 $node_type = NodeType::create(['type' => 'page', 'name' => t('Page')]);
41 $this->installConfig(['comment']);
43 $this->addDefaultCommentField('node', 'page');
46 function testCommentTokens() {
47 $node = Node::create([
49 'title' => $this->randomMachineName()
53 $parent_comment = Comment::create([
54 'entity_id' => $node->id(),
55 'entity_type' => 'node',
56 'field_name' => 'comment',
57 'name' => 'anonymous user',
58 'mail' => 'anonymous@example.com',
59 'subject' => $this->randomMachineName(),
60 'body' => $this->randomMachineName(),
62 $parent_comment->save();
64 // Fix http://example.com/index.php/comment/1 fails 'url:path' test.
65 $parent_comment_path = $parent_comment->url();
68 'url' => $parent_comment->urlInfo('canonical', ['fragment' => "comment-{$parent_comment->id()}"])->setAbsolute()->toString(),
69 'url:absolute' => $parent_comment->urlInfo('canonical', ['fragment' => "comment-{$parent_comment->id()}"])->setAbsolute()->toString(),
70 'url:relative' => $parent_comment->urlInfo('canonical', ['fragment' => "comment-{$parent_comment->id()}"])->toString(),
71 'url:path' => $parent_comment_path,
72 'parent:url:absolute' => NULL,
74 $this->assertTokens('comment', array('comment' => $parent_comment), $tokens);
76 $comment = Comment::create([
77 'entity_id' => $node->id(),
78 'pid' => $parent_comment->id(),
79 'entity_type' => 'node',
80 'field_name' => 'comment',
81 'name' => 'anonymous user',
82 'mail' => 'anonymous@example.com',
83 'subject' => $this->randomMachineName(),
84 'body' => $this->randomMachineName(),
88 // Fix http://example.com/index.php/comment/1 fails 'url:path' test.
89 $comment_path = Url::fromRoute('entity.comment.canonical', array('comment' => $comment->id()))->toString();
92 'url' => $comment->urlInfo('canonical', ['fragment' => "comment-{$comment->id()}"])->setAbsolute()->toString(),
93 'url:absolute' => $comment->urlInfo('canonical', ['fragment' => "comment-{$comment->id()}"])->setAbsolute()->toString(),
94 'url:relative' => $comment->urlInfo('canonical', ['fragment' => "comment-{$comment->id()}"])->toString(),
95 'url:path' => $comment_path,
96 'parent:url:absolute' => $parent_comment->urlInfo('canonical', ['fragment' => "comment-{$parent_comment->id()}"])->setAbsolute()->toString(),
98 $this->assertTokens('comment', array('comment' => $comment), $tokens);