class AgreementSubscriberTest in Agreement 8.2
Same name and namespace in other branches
- 3.0.x tests/src/Unit/EventSubscriber/AgreementSubscriberTest.php \Drupal\Tests\agreement\Unit\EventSubscriber\AgreementSubscriberTest
Tests the agreement route subscriber.
@group agreement
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\agreement\Unit\EventSubscriber\AgreementSubscriberTest
Expanded class hierarchy of AgreementSubscriberTest
File
- tests/
src/ Unit/ EventSubscriber/ AgreementSubscriberTest.php, line 18
Namespace
Drupal\Tests\agreement\Unit\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AgreementSubscriberTest:: |
public | function | Provides test arguments and expectations. | |
AgreementSubscriberTest:: |
protected | function | Get the mocked current user account object. | |
AgreementSubscriberTest:: |
protected | function | Get the mocked agreement handler. | |
AgreementSubscriberTest:: |
public | function | Asserts that check for redirection method is functional. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |