function CommentHostnameTest::testGetDefaultHostname

Same name in other branches
  1. 9 core/modules/comment/tests/src/Kernel/CommentHostnameTest.php \Drupal\Tests\comment\Kernel\CommentHostnameTest::testGetDefaultHostname()
  2. 10 core/modules/comment/tests/src/Kernel/CommentHostnameTest.php \Drupal\Tests\comment\Kernel\CommentHostnameTest::testGetDefaultHostname()
  3. 11.x core/modules/comment/tests/src/Kernel/CommentHostnameTest.php \Drupal\Tests\comment\Kernel\CommentHostnameTest::testGetDefaultHostname()

Tests hostname default value callback.

@covers ::getDefaultHostname

File

core/modules/comment/tests/src/Kernel/CommentHostnameTest.php, line 29

Class

CommentHostnameTest
Tests the hostname base field.

Namespace

Drupal\Tests\comment\Kernel

Code

public function testGetDefaultHostname() {
    // Create a fake request to be used for testing.
    $request = Request::create('/', 'GET', [], [], [], [
        'REMOTE_ADDR' => '203.0.113.1',
    ]);
    
    /** @var \Symfony\Component\HttpFoundation\RequestStack $stack */
    $stack = $this->container
        ->get('request_stack');
    $stack->push($request);
    CommentType::create([
        'id' => 'foo',
        'target_entity_type_id' => 'entity_test',
    ])->save();
    // Check that the hostname is empty by default.
    $comment = Comment::create([
        'comment_type' => 'foo',
    ]);
    $this->assertEquals('', $comment->getHostname());
    \Drupal::configFactory()->getEditable('comment.settings')
        ->set('log_ip_addresses', TRUE)
        ->save(TRUE);
    // Check that the hostname was set correctly.
    $comment = Comment::create([
        'comment_type' => 'foo',
    ]);
    $this->assertEquals('203.0.113.1', $comment->getHostname());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.