ChainRequestPolicyTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Drupal\Tests\Core\PageCache;
  3. use Drupal\Core\PageCache\RequestPolicyInterface;
  4. use Drupal\Core\PageCache\ChainRequestPolicy;
  5. use Drupal\Tests\UnitTestCase;
  6. use Symfony\Component\HttpFoundation\Request;
  7. /**
  8. * @coversDefaultClass \Drupal\Core\PageCache\ChainRequestPolicy
  9. * @group PageCache
  10. */
  11. class ChainRequestPolicyTest extends UnitTestCase {
  12. /**
  13. * The chain request policy under test.
  14. *
  15. * @var \Drupal\Core\PageCache\ChainRequestPolicy
  16. */
  17. protected $policy;
  18. /**
  19. * A request object.
  20. *
  21. * @var \Symfony\Component\HttpFoundation\Request
  22. */
  23. protected $request;
  24. protected function setUp() {
  25. $this->policy = new ChainRequestPolicy();
  26. $this->request = new Request();
  27. }
  28. /**
  29. * Asserts that check() returns NULL if the chain is empty.
  30. *
  31. * @covers ::check
  32. */
  33. public function testEmptyChain() {
  34. $result = $this->policy->check($this->request);
  35. $this->assertSame(NULL, $result);
  36. }
  37. /**
  38. * Asserts that check() returns NULL if a rule returns NULL.
  39. *
  40. * @covers ::check
  41. */
  42. public function testNullRuleChain() {
  43. $rule = $this->createMock('Drupal\Core\PageCache\RequestPolicyInterface');
  44. $rule->expects($this->once())
  45. ->method('check')
  46. ->with($this->request)
  47. ->will($this->returnValue(NULL));
  48. $this->policy->addPolicy($rule);
  49. $result = $this->policy->check($this->request);
  50. $this->assertSame(NULL, $result);
  51. }
  52. /**
  53. * Asserts that check() throws an exception if a rule returns an invalid value.
  54. *
  55. * @dataProvider providerChainExceptionOnInvalidReturnValue
  56. * @covers ::check
  57. */
  58. public function testChainExceptionOnInvalidReturnValue($return_value) {
  59. $rule = $this->createMock('Drupal\Core\PageCache\RequestPolicyInterface');
  60. $rule->expects($this->once())
  61. ->method('check')
  62. ->with($this->request)
  63. ->will($this->returnValue($return_value));
  64. $this->policy->addPolicy($rule);
  65. $this->expectException(\UnexpectedValueException::class);
  66. $this->policy->check($this->request);
  67. }
  68. /**
  69. * Provides test data for testChainExceptionOnInvalidReturnValue.
  70. *
  71. * @return array
  72. * Test input and expected result.
  73. */
  74. public function providerChainExceptionOnInvalidReturnValue() {
  75. return [
  76. [FALSE],
  77. [0],
  78. [1],
  79. [TRUE],
  80. [[1, 2, 3]],
  81. [new \stdClass()],
  82. ];
  83. }
  84. /**
  85. * Asserts that check() returns ALLOW if any of the rules returns ALLOW.
  86. *
  87. * @dataProvider providerAllowIfAnyRuleReturnedAllow
  88. * @covers ::check
  89. */
  90. public function testAllowIfAnyRuleReturnedAllow($return_values) {
  91. foreach ($return_values as $return_value) {
  92. $rule = $this->createMock('Drupal\Core\PageCache\RequestPolicyInterface');
  93. $rule->expects($this->once())
  94. ->method('check')
  95. ->with($this->request)
  96. ->will($this->returnValue($return_value));
  97. $this->policy->addPolicy($rule);
  98. }
  99. $actual_result = $this->policy->check($this->request);
  100. $this->assertSame(RequestPolicyInterface::ALLOW, $actual_result);
  101. }
  102. /**
  103. * Provides test data for testAllowIfAnyRuleReturnedAllow.
  104. *
  105. * @return array
  106. * Test input and expected result.
  107. */
  108. public function providerAllowIfAnyRuleReturnedAllow() {
  109. return [
  110. [[RequestPolicyInterface::ALLOW]],
  111. [[NULL, RequestPolicyInterface::ALLOW]],
  112. ];
  113. }
  114. /**
  115. * Asserts that check() returns immediately when a rule returned DENY.
  116. */
  117. public function testStopChainOnFirstDeny() {
  118. $rule1 = $this->createMock('Drupal\Core\PageCache\RequestPolicyInterface');
  119. $rule1->expects($this->once())
  120. ->method('check')
  121. ->with($this->request)
  122. ->will($this->returnValue(RequestPolicyInterface::ALLOW));
  123. $this->policy->addPolicy($rule1);
  124. $deny_rule = $this->createMock('Drupal\Core\PageCache\RequestPolicyInterface');
  125. $deny_rule->expects($this->once())
  126. ->method('check')
  127. ->with($this->request)
  128. ->will($this->returnValue(RequestPolicyInterface::DENY));
  129. $this->policy->addPolicy($deny_rule);
  130. $ignored_rule = $this->createMock('Drupal\Core\PageCache\RequestPolicyInterface');
  131. $ignored_rule->expects($this->never())
  132. ->method('check');
  133. $this->policy->addPolicy($ignored_rule);
  134. $actual_result = $this->policy->check($this->request);
  135. $this->assertsame(RequestPolicyInterface::DENY, $actual_result);
  136. }
  137. }