You are here

class AgreementSubscriberTest in Agreement 8.2

Same name and namespace in other branches
  1. 3.0.x tests/src/Unit/EventSubscriber/AgreementSubscriberTest.php \Drupal\Tests\agreement\Unit\EventSubscriber\AgreementSubscriberTest

Tests the agreement route subscriber.

@group agreement

Hierarchy

Expanded class hierarchy of AgreementSubscriberTest

File

tests/src/Unit/EventSubscriber/AgreementSubscriberTest.php, line 18

Namespace

Drupal\Tests\agreement\Unit\EventSubscriber
View source
class AgreementSubscriberTest extends UnitTestCase {

  /**
   * Asserts that check for redirection method is functional.
   *
   * @param bool $canBypass
   *   Permission to bypass agreement.
   * @param bool $hasAgreement
   *   Whether or not an agreement is "found" in this test.
   * @param bool $expected
   *   Whether a redirect is expected or not.
   *
   * @dataProvider checkForRedirectionProvider
   */
  public function testCheckForRedirection($canBypass, $hasAgreement, $expected) {
    $pathProphet = $this
      ->prophesize('\\Drupal\\Core\\Path\\CurrentPathStack');
    $pathProphet
      ->getPath(Argument::any())
      ->willReturn('test');
    $sessionProphet = $this
      ->prophesize('\\Drupal\\Core\\Session\\SessionManagerInterface');
    $kernelProphet = $this
      ->prophesize('\\Drupal\\Core\\DrupalKernelInterface');
    $request = new Request();
    $event = new GetResponseEvent($kernelProphet
      ->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
    $subscriber = new AgreementSubscriber($this
      ->getAgreementHandlerStub($hasAgreement), $pathProphet
      ->reveal(), $sessionProphet
      ->reveal(), $this
      ->getAccountStub($canBypass));
    $subscriber
      ->checkForRedirection($event);
    $isRedirect = $event
      ->getResponse() !== NULL ? $event
      ->getResponse()
      ->isRedirect() : FALSE;
    $this
      ->assertEquals($expected, $isRedirect);
  }

  /**
   * Get the mocked current user account object.
   *
   * @param bool $canBypass
   *   Can the user bypass agreement.
   *
   * @return object
   *   The mocked user account object.
   */
  protected function getAccountStub($canBypass = FALSE) {
    $accountProphet = $this
      ->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
    $accountProphet
      ->hasPermission('bypass agreement')
      ->willReturn($canBypass);
    return $accountProphet
      ->reveal();
  }

  /**
   * Get the mocked agreement handler.
   *
   * @param bool $willHaveAgreement
   *   Whether an agreement object should be returned or not.
   *
   * @return object
   *   The mocked agreement handler object.
   */
  protected function getAgreementHandlerStub($willHaveAgreement = FALSE) {
    $agreement = FALSE;
    if ($willHaveAgreement) {
      $agreementProphet = $this
        ->prophesize('\\Drupal\\agreement\\Entity\\Agreement');
      $agreementProphet
        ->get('path')
        ->willReturn('test');
      $agreement = $agreementProphet
        ->reveal();
    }
    $handlerProphet = $this
      ->prophesize('\\Drupal\\agreement\\AgreementHandlerInterface');
    $handlerProphet
      ->getAgreementByUserAndPath(Argument::any(), Argument::any())
      ->willReturn($agreement);
    return $handlerProphet
      ->reveal();
  }

  /**
   * Provides test arguments and expectations.
   *
   * @return array
   *   An array of test arguments.
   */
  public function checkForRedirectionProvider() {
    return [
      // Bypass, Have agreement, Expected Response.
      [
        TRUE,
        FALSE,
        FALSE,
      ],
      [
        TRUE,
        TRUE,
        FALSE,
      ],
      [
        FALSE,
        FALSE,
        FALSE,
      ],
      [
        FALSE,
        TRUE,
        TRUE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AgreementSubscriberTest::checkForRedirectionProvider public function Provides test arguments and expectations.
AgreementSubscriberTest::getAccountStub protected function Get the mocked current user account object.
AgreementSubscriberTest::getAgreementHandlerStub protected function Get the mocked agreement handler.
AgreementSubscriberTest::testCheckForRedirection public function Asserts that check for redirection method is functional.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed 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.
UnitTestCase::setUp protected function 340