890d18e2574f3b41b68175d47fb2aa76975f0b38
[yaffs-website] / web / modules / contrib / token / tests / src / Kernel / CommentTest.php
1 <?php
2
3 namespace Drupal\Tests\token\Kernel;
4
5 use Drupal\comment\Entity\Comment;
6 use Drupal\comment\Tests\CommentTestTrait;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\Core\Url;
10
11 /**
12  * Tests comment tokens.
13  *
14  * @group token
15  */
16 class CommentTest extends KernelTestBase {
17
18   use CommentTestTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['node', 'comment', 'field', 'text', 'entity_reference'];
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     $this->installEntitySchema('node');
34     $this->installEntitySchema('user');
35     $this->installEntitySchema('comment');
36     $this->installSchema('comment', ['comment_entity_statistics']);
37
38     $node_type = NodeType::create(['type' => 'page', 'name' => t('Page')]);
39     $node_type->save();
40
41     $this->installConfig(['comment']);
42
43     $this->addDefaultCommentField('node', 'page');
44   }
45
46   function testCommentTokens() {
47     $node = Node::create([
48       'type' => 'page',
49       'title' => $this->randomMachineName()
50     ]);
51     $node->save();
52
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(),
61     ]);
62     $parent_comment->save();
63
64     // Fix http://example.com/index.php/comment/1 fails 'url:path' test.
65     $parent_comment_path = $parent_comment->url();
66
67     $tokens = array(
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,
73     );
74     $this->assertTokens('comment', array('comment' => $parent_comment), $tokens);
75
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(),
85     ]);
86     $comment->save();
87
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();
90
91     $tokens = array(
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(),
97     );
98     $this->assertTokens('comment', array('comment' => $comment), $tokens);
99   }
100
101 }