diff --git a/core/lib/Drupal/Core/Session/AnonymousUserSession.php b/core/lib/Drupal/Core/Session/AnonymousUserSession.php index edf3b14..661b0c4 100644 --- a/core/lib/Drupal/Core/Session/AnonymousUserSession.php +++ b/core/lib/Drupal/Core/Session/AnonymousUserSession.php @@ -16,10 +16,12 @@ class AnonymousUserSession extends UserSession { /** * Constructs a new anonymous user session. + * + * Intentionally don't allow parameters to be passed in like UserSession. */ public function __construct() { try { - $this->hostname = \Drupal::request()->getClientIP(); + $this->hostname = \Drupal::request()->getClientIp(); } catch (RuntimeException $e) { // We are not in a request context. diff --git a/core/lib/Drupal/Core/Session/UserSession.php b/core/lib/Drupal/Core/Session/UserSession.php index 35dfbc4..bfaefcc 100644 --- a/core/lib/Drupal/Core/Session/UserSession.php +++ b/core/lib/Drupal/Core/Session/UserSession.php @@ -241,7 +241,7 @@ public function getLastAccessedTime() { * {@inheritdoc} */ public function getHostname() { - + return $this->hostname; } } diff --git a/core/tests/Drupal/Tests/Core/Session/AnonymousUserSessionTest.php b/core/tests/Drupal/Tests/Core/Session/AnonymousUserSessionTest.php index 136b700..82b2496 100644 --- a/core/tests/Drupal/Tests/Core/Session/AnonymousUserSessionTest.php +++ b/core/tests/Drupal/Tests/Core/Session/AnonymousUserSessionTest.php @@ -7,6 +7,68 @@ namespace Drupal\Tests\Core\Session; -class AnonymousUserSessionTest { +use Drupal\Tests\UnitTestCase; +use Drupal\Core\Session\AnonymousUserSession; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; + +/** + * Tests the AnonymousUserSession class. + * + * @group Drupal + * + * @see \Drupal\Core\Session\AnonymousUserSession + */ +class AnonymousUserSessionTest extends UnitTestCase { + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'Anonymous user session object', + 'description' => 'Tests the anonymous user session object.', + 'group' => 'Session', + ); + } + + /** + * Tests creating an AnonymousUserSession when the request is available. + * + * @covers \Drupal\Core\Session\AnonymousUserSession::__construct + */ + public function testAnonymousUserSessionWithRequest() { + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); + $request->expects($this->once()) + ->method('getClientIp') + ->will($this->returnValue('test')); + $container = new ContainerBuilder(); + $container->set('request', $request); + \Drupal::setContainer($container); + + $anonymous_user = new AnonymousUserSession(); + + $this->assertSame('test', $anonymous_user->getHostname()); + } + + /** + * Tests creating an AnonymousUserSession when the request is not available. + * + * @covers \Drupal\Core\Session\AnonymousUserSession::__construct + */ + public function testAnonymousUserSessionWithNoRequest() { + $container = new ContainerBuilder(); + + // Set a synthetic 'request' definition on the container. + $definition = new Definition(); + $definition->setSynthetic(TRUE); + + $container->setDefinition('request', $definition); + \Drupal::setContainer($container); + + $anonymous_user = new AnonymousUserSession(); + + $this->assertSame('', $anonymous_user->getHostname()); + } }