NoSessionOpenTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Drupal\Tests\Core\PageCache;
  3. use Drupal\Core\PageCache\RequestPolicy\NoSessionOpen;
  4. use Drupal\Core\PageCache\RequestPolicyInterface;
  5. use Drupal\Tests\UnitTestCase;
  6. use Symfony\Component\HttpFoundation\Request;
  7. /**
  8. * @coversDefaultClass \Drupal\Core\PageCache\RequestPolicy\NoSessionOpen
  9. * @group PageCache
  10. */
  11. class NoSessionOpenTest extends UnitTestCase {
  12. /**
  13. * The session configuration.
  14. *
  15. * @var \Drupal\Core\Session\SessionConfigurationInterface|\PHPUnit\Framework\MockObject\MockObject
  16. */
  17. protected $sessionConfiguration;
  18. /**
  19. * The request policy under test.
  20. *
  21. * @var \Drupal\Core\PageCache\RequestPolicy\NoSessionOpen
  22. */
  23. protected $policy;
  24. protected function setUp() {
  25. $this->sessionConfiguration = $this->createMock('Drupal\Core\Session\SessionConfigurationInterface');
  26. $this->policy = new NoSessionOpen($this->sessionConfiguration);
  27. }
  28. /**
  29. * Asserts that caching is allowed unless there is a session cookie present.
  30. *
  31. * @covers ::check
  32. */
  33. public function testNoAllowUnlessSessionCookiePresent() {
  34. $request_without_session = new Request();
  35. $request_with_session = Request::create('/', 'GET', [], ['some-session-name' => 'some-session-id']);
  36. $this->sessionConfiguration->expects($this->at(0))
  37. ->method('hasSession')
  38. ->with($request_without_session)
  39. ->will($this->returnValue(FALSE));
  40. $this->sessionConfiguration->expects($this->at(1))
  41. ->method('hasSession')
  42. ->with($request_with_session)
  43. ->will($this->returnValue(TRUE));
  44. $result = $this->policy->check($request_without_session);
  45. $this->assertSame(RequestPolicyInterface::ALLOW, $result);
  46. $result = $this->policy->check($request_with_session);
  47. $this->assertSame(NULL, $result);
  48. }
  49. }