annotate core/modules/comment/tests/src/Kernel/CommentHostnameTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@4 1 <?php
Chris@4 2
Chris@4 3 namespace Drupal\Tests\comment\Kernel;
Chris@4 4
Chris@4 5 use Drupal\comment\Entity\Comment;
Chris@4 6 use Drupal\comment\Entity\CommentType;
Chris@4 7 use Drupal\KernelTests\KernelTestBase;
Chris@4 8 use Symfony\Component\HttpFoundation\Request;
Chris@4 9
Chris@4 10 /**
Chris@4 11 * Tests the hostname base field.
Chris@4 12 *
Chris@4 13 * @coversDefaultClass \Drupal\comment\Entity\Comment
Chris@4 14 *
Chris@4 15 * @group comment
Chris@4 16 */
Chris@4 17 class CommentHostnameTest extends KernelTestBase {
Chris@4 18
Chris@4 19 /**
Chris@4 20 * {@inheritdoc}
Chris@4 21 */
Chris@4 22 protected static $modules = ['comment', 'entity_test', 'user'];
Chris@4 23
Chris@4 24 /**
Chris@4 25 * Tests hostname default value callback.
Chris@4 26 *
Chris@4 27 * @covers ::getDefaultHostname
Chris@4 28 */
Chris@4 29 public function testGetDefaultHostname() {
Chris@4 30 // Create a fake request to be used for testing.
Chris@4 31 $request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '203.0.113.1']);
Chris@4 32 /** @var \Symfony\Component\HttpFoundation\RequestStack $stack */
Chris@4 33 $stack = $this->container->get('request_stack');
Chris@4 34 $stack->push($request);
Chris@4 35
Chris@4 36 CommentType::create([
Chris@4 37 'id' => 'foo',
Chris@4 38 'target_entity_type_id' => 'entity_test',
Chris@4 39 ])->save();
Chris@5 40
Chris@5 41 // Check that the hostname is empty by default.
Chris@4 42 $comment = Comment::create(['comment_type' => 'foo']);
Chris@5 43 $this->assertEquals('', $comment->getHostname());
Chris@4 44
Chris@5 45 \Drupal::configFactory()
Chris@5 46 ->getEditable('comment.settings')
Chris@5 47 ->set('log_ip_addresses', TRUE)
Chris@5 48 ->save(TRUE);
Chris@4 49 // Check that the hostname was set correctly.
Chris@5 50 $comment = Comment::create(['comment_type' => 'foo']);
Chris@4 51 $this->assertEquals('203.0.113.1', $comment->getHostname());
Chris@4 52 }
Chris@4 53
Chris@4 54 }