You are here

function CommentTest::testCommentTokens in Token 8

File

tests/src/Kernel/CommentTest.php, line 46

Class

CommentTest
Tests comment tokens.

Namespace

Drupal\Tests\token\Kernel

Code

function testCommentTokens() {
  $node = Node::create([
    'type' => 'page',
    'title' => $this
      ->randomMachineName(),
  ]);
  $node
    ->save();
  $parent_comment = Comment::create([
    'entity_id' => $node
      ->id(),
    'entity_type' => 'node',
    'field_name' => 'comment',
    'name' => 'anonymous user',
    'mail' => 'anonymous@example.com',
    'subject' => $this
      ->randomMachineName(),
    'body' => $this
      ->randomMachineName(),
  ]);
  $parent_comment
    ->save();

  // Fix http://example.com/index.php/comment/1 fails 'url:path' test.
  $parent_comment_path = $parent_comment
    ->toUrl()
    ->toString();
  $tokens = [
    'url' => $parent_comment
      ->toUrl('canonical', [
      'fragment' => "comment-{$parent_comment->id()}",
    ])
      ->setAbsolute()
      ->toString(),
    'url:absolute' => $parent_comment
      ->toUrl('canonical', [
      'fragment' => "comment-{$parent_comment->id()}",
    ])
      ->setAbsolute()
      ->toString(),
    'url:relative' => $parent_comment
      ->toUrl('canonical', [
      'fragment' => "comment-{$parent_comment->id()}",
    ])
      ->toString(),
    'url:path' => $parent_comment_path,
    'parent:url:absolute' => NULL,
  ];
  $this
    ->assertTokens('comment', [
    'comment' => $parent_comment,
  ], $tokens);
  $comment = Comment::create([
    'entity_id' => $node
      ->id(),
    'pid' => $parent_comment
      ->id(),
    'entity_type' => 'node',
    'field_name' => 'comment',
    'name' => 'anonymous user',
    'mail' => 'anonymous@example.com',
    'subject' => $this
      ->randomMachineName(),
    'body' => $this
      ->randomMachineName(),
  ]);
  $comment
    ->save();

  // Fix http://example.com/index.php/comment/1 fails 'url:path' test.
  $comment_path = Url::fromRoute('entity.comment.canonical', [
    'comment' => $comment
      ->id(),
  ])
    ->toString();
  $tokens = [
    'url' => $comment
      ->toUrl('canonical', [
      'fragment' => "comment-{$comment->id()}",
    ])
      ->setAbsolute()
      ->toString(),
    'url:absolute' => $comment
      ->toUrl('canonical', [
      'fragment' => "comment-{$comment->id()}",
    ])
      ->setAbsolute()
      ->toString(),
    'url:relative' => $comment
      ->toUrl('canonical', [
      'fragment' => "comment-{$comment->id()}",
    ])
      ->toString(),
    'url:path' => $comment_path,
    'parent:url:absolute' => $parent_comment
      ->toUrl('canonical', [
      'fragment' => "comment-{$parent_comment->id()}",
    ])
      ->setAbsolute()
      ->toString(),
  ];
  $this
    ->assertTokens('comment', [
    'comment' => $comment,
  ], $tokens);
}