function CommentTokenReplaceTest::testHomepageToken
Same name and namespace in other branches
- 11.x core/modules/comment/tests/src/Kernel/CommentTokenReplaceTest.php \Drupal\Tests\comment\Kernel\CommentTokenReplaceTest::testHomepageToken()
Tests that the homepage token handles both NULL and set values.
File
-
core/
modules/ comment/ tests/ src/ Kernel/ CommentTokenReplaceTest.php, line 44
Class
- CommentTokenReplaceTest
- Tests comment token replacement.
Namespace
Drupal\Tests\comment\KernelCode
public function testHomepageToken() : void {
$account = $this->createUser();
// Create a host entity.
$entity = EntityTest::create([
'name' => 'Test entity',
]);
$entity->save();
// Create a comment without setting a homepage.
$comment = Comment::create([
'subject' => 'Test comment',
'uid' => $account->id(),
'entity_type' => 'entity_test',
'field_name' => 'comment',
'entity_id' => $entity->id(),
'comment_type' => 'comment',
'status' => 1,
'comment_body' => [
'value' => 'This is the comment body.',
'format' => 'plain_text',
],
]);
$comment->save();
// Verify that getHomepage() returns NULL when no homepage is set.
$this->assertNull($comment->getHomepage());
// Replace the homepage token. This should not produce any errors and
// should return an empty string when the homepage is NULL.
$output = $this->tokenService
->replace('[comment:homepage]', [
'comment' => $comment,
], [
'langcode' => $this->interfaceLanguage
->getId(),
]);
$this->assertSame('', $output, 'Homepage token with NULL value returns empty string.');
// Now set a homepage and verify the token returns the sanitized URL.
$comment->setHomepage('http://example.org/');
$comment->save();
$output = $this->tokenService
->replace('[comment:homepage]', [
'comment' => $comment,
], [
'langcode' => $this->interfaceLanguage
->getId(),
]);
$this->assertSame(UrlHelper::stripDangerousProtocols('http://example.org/'), $output, 'Homepage token returns sanitized URL.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.