AgreementSubscriberTest.php in Agreement 3.0.x
File
tests/src/Unit/EventSubscriber/AgreementSubscriberTest.php
View source
<?php
namespace Drupal\Tests\agreement\Unit\EventSubscriber;
use Drupal\agreement\EventSubscriber\AgreementSubscriber;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class AgreementSubscriberTest extends UnitTestCase {
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 RequestEvent($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);
}
protected function getAccountStub($canBypass = FALSE) {
$accountProphet = $this
->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
$accountProphet
->hasPermission('bypass agreement')
->willReturn($canBypass);
return $accountProphet
->reveal();
}
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();
}
public function checkForRedirectionProvider() {
return [
[
TRUE,
FALSE,
FALSE,
],
[
TRUE,
TRUE,
FALSE,
],
[
FALSE,
FALSE,
FALSE,
],
[
FALSE,
TRUE,
TRUE,
],
];
}
}