You are here

class ChainRequestPolicyTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/PageCache/ChainRequestPolicyTest.php \Drupal\Tests\Core\PageCache\ChainRequestPolicyTest

@coversDefaultClass \Drupal\Core\PageCache\ChainRequestPolicy @group PageCache

Hierarchy

Expanded class hierarchy of ChainRequestPolicyTest

File

core/tests/Drupal/Tests/Core/PageCache/ChainRequestPolicyTest.php, line 19
Contains \Drupal\Tests\Core\PageCache\ChainRequestPolicyTest.

Namespace

Drupal\Tests\Core\PageCache
View source
class ChainRequestPolicyTest extends UnitTestCase {

  /**
   * The chain request policy under test.
   *
   * @var \Drupal\Core\PageCache\ChainRequestPolicy
   */
  protected $policy;

  /**
   * A request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;
  public function setUp() {
    $this->policy = new ChainRequestPolicy();
    $this->request = new Request();
  }

  /**
   * Asserts that check() returns NULL if the chain is empty.
   *
   * @covers ::check
   */
  public function testEmptyChain() {
    $result = $this->policy
      ->check($this->request);
    $this
      ->assertSame(NULL, $result);
  }

  /**
   * Asserts that check() returns NULL if a rule returns NULL.
   *
   * @covers ::check
   */
  public function testNullRuleChain() {
    $rule = $this
      ->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
    $rule
      ->expects($this
      ->once())
      ->method('check')
      ->with($this->request)
      ->will($this
      ->returnValue(NULL));
    $this->policy
      ->addPolicy($rule);
    $result = $this->policy
      ->check($this->request);
    $this
      ->assertSame(NULL, $result);
  }

  /**
   * Asserts that check() throws an exception if a rule returns an invalid value.
   *
   * @expectedException \UnexpectedValueException
   * @dataProvider providerChainExceptionOnInvalidReturnValue
   * @covers ::check
   */
  public function testChainExceptionOnInvalidReturnValue($return_value) {
    $rule = $this
      ->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
    $rule
      ->expects($this
      ->once())
      ->method('check')
      ->with($this->request)
      ->will($this
      ->returnValue($return_value));
    $this->policy
      ->addPolicy($rule);
    $this->policy
      ->check($this->request);
  }

  /**
   * Provides test data for testChainExceptionOnInvalidReturnValue.
   *
   * @return array
   *   Test input and expected result.
   */
  public function providerChainExceptionOnInvalidReturnValue() {
    return [
      [
        FALSE,
      ],
      [
        0,
      ],
      [
        1,
      ],
      [
        TRUE,
      ],
      [
        [
          1,
          2,
          3,
        ],
      ],
      [
        new \stdClass(),
      ],
    ];
  }

  /**
   * Asserts that check() returns ALLOW if any of the rules returns ALLOW.
   *
   * @dataProvider providerAllowIfAnyRuleReturnedAllow
   * @covers ::check
   */
  public function testAllowIfAnyRuleReturnedAllow($return_values) {
    foreach ($return_values as $return_value) {
      $rule = $this
        ->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
      $rule
        ->expects($this
        ->once())
        ->method('check')
        ->with($this->request)
        ->will($this
        ->returnValue($return_value));
      $this->policy
        ->addPolicy($rule);
    }
    $actual_result = $this->policy
      ->check($this->request);
    $this
      ->assertSame(RequestPolicyInterface::ALLOW, $actual_result);
  }

  /**
   * Provides test data for testAllowIfAnyRuleReturnedAllow.
   *
   * @return array
   *   Test input and expected result.
   */
  public function providerAllowIfAnyRuleReturnedAllow() {
    return [
      [
        [
          RequestPolicyInterface::ALLOW,
        ],
      ],
      [
        [
          NULL,
          RequestPolicyInterface::ALLOW,
        ],
      ],
    ];
  }

  /**
   * Asserts that check() returns immediately when a rule returned DENY.
   */
  public function testStopChainOnFirstDeny() {
    $rule1 = $this
      ->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
    $rule1
      ->expects($this
      ->once())
      ->method('check')
      ->with($this->request)
      ->will($this
      ->returnValue(RequestPolicyInterface::ALLOW));
    $this->policy
      ->addPolicy($rule1);
    $deny_rule = $this
      ->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
    $deny_rule
      ->expects($this
      ->once())
      ->method('check')
      ->with($this->request)
      ->will($this
      ->returnValue(RequestPolicyInterface::DENY));
    $this->policy
      ->addPolicy($deny_rule);
    $ignored_rule = $this
      ->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
    $ignored_rule
      ->expects($this
      ->never())
      ->method('check');
    $this->policy
      ->addPolicy($ignored_rule);
    $actual_result = $this->policy
      ->check($this->request);
    $this
      ->assertsame(RequestPolicyInterface::DENY, $actual_result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChainRequestPolicyTest::$policy protected property The chain request policy under test.
ChainRequestPolicyTest::$request protected property A request object.
ChainRequestPolicyTest::providerAllowIfAnyRuleReturnedAllow public function Provides test data for testAllowIfAnyRuleReturnedAllow.
ChainRequestPolicyTest::providerChainExceptionOnInvalidReturnValue public function Provides test data for testChainExceptionOnInvalidReturnValue.
ChainRequestPolicyTest::setUp public function Overrides UnitTestCase::setUp
ChainRequestPolicyTest::testAllowIfAnyRuleReturnedAllow public function Asserts that check() returns ALLOW if any of the rules returns ALLOW.
ChainRequestPolicyTest::testChainExceptionOnInvalidReturnValue public function Asserts that check() throws an exception if a rule returns an invalid value.
ChainRequestPolicyTest::testEmptyChain public function Asserts that check() returns NULL if the chain is empty.
ChainRequestPolicyTest::testNullRuleChain public function Asserts that check() returns NULL if a rule returns NULL.
ChainRequestPolicyTest::testStopChainOnFirstDeny public function Asserts that check() returns immediately when a rule returned DENY.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.