3 namespace Drupal\Tests\Core\PageCache;
5 use Drupal\Core\PageCache\RequestPolicy\NoSessionOpen;
6 use Drupal\Core\PageCache\RequestPolicyInterface;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
11 * @coversDefaultClass \Drupal\Core\PageCache\RequestPolicy\NoSessionOpen
14 class NoSessionOpenTest extends UnitTestCase {
17 * The session configuration.
19 * @var \Drupal\Core\Session\SessionConfigurationInterface|\PHPUnit_Framework_MockObject_MockObject
21 protected $sessionConfiguration;
24 * The request policy under test.
26 * @var \Drupal\Core\PageCache\RequestPolicy\NoSessionOpen
30 protected function setUp() {
31 $this->sessionConfiguration = $this->getMock('Drupal\Core\Session\SessionConfigurationInterface');
32 $this->policy = new NoSessionOpen($this->sessionConfiguration);
36 * Asserts that caching is allowed unless there is a session cookie present.
40 public function testNoAllowUnlessSessionCookiePresent() {
41 $request_without_session = new Request();
42 $request_with_session = Request::create('/', 'GET', [], ['some-session-name' => 'some-session-id']);
44 $this->sessionConfiguration->expects($this->at(0))
45 ->method('hasSession')
46 ->with($request_without_session)
47 ->will($this->returnValue(FALSE));
49 $this->sessionConfiguration->expects($this->at(1))
50 ->method('hasSession')
51 ->with($request_with_session)
52 ->will($this->returnValue(TRUE));
54 $result = $this->policy->check($request_without_session);
55 $this->assertSame(RequestPolicyInterface::ALLOW, $result);
57 $result = $this->policy->check($request_with_session);
58 $this->assertSame(NULL, $result);