You are here

public function AgreementHandlerTest::testGetAgreementByUserAndPath in Agreement 8.2

Same name and namespace in other branches
  1. 3.0.x tests/src/Unit/AgreementHandlerTest.php \Drupal\Tests\agreement\Unit\AgreementHandlerTest::testGetAgreementByUserAndPath()

Asserts agreement discovery.

@dataProvider getAgreementProvider

Parameters

\Drupal\agreement\Entity\Agreement|false $expected: The expected return value for this test.

\Drupal\agreement\Entity\Agreement[] $agreements: A list of agreements.

array $roles: An indexed array of user roles to apply to the mock user.

int|null $agreed: The agreement state for the user.

string $path: The path to test.

File

tests/src/Unit/AgreementHandlerTest.php, line 129

Class

AgreementHandlerTest
Tests logic in the agreement handler service.

Namespace

Drupal\Tests\agreement\Unit

Code

public function testGetAgreementByUserAndPath($expected, array $agreements, array $roles, $agreed, $path) {

  // Mocks Config, ConfigFactory, and RouteMatch for PathMatcher.
  $siteConfigProphet = $this
    ->prophesize('\\Drupal\\Core\\Config\\ImmutableConfig');
  $siteConfigProphet
    ->get('page.front')
    ->willReturn('/');
  $configFactoryProphet = $this
    ->prophesize('\\Drupal\\Core\\Config\\ConfigFactoryInterface');
  $configFactoryProphet
    ->get('system.site')
    ->willReturn($siteConfigProphet
    ->reveal());
  $routeMatchProphet = $this
    ->prophesize('\\Drupal\\Core\\Routing\\RouteMatchInterface');

  // Mocks account interface with configurable roles based on data.
  $accountProphet = $this
    ->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
  $accountProphet
    ->isAnonymous()
    ->willReturn(FALSE);
  $accountProphet
    ->id()
    ->willReturn(5);
  $accountProphet
    ->getRoles()
    ->willReturn($roles);
  $statementProphet = $this
    ->prophesize('\\Drupal\\Core\\Database\\StatementInterface');
  $statementProphet
    ->fetchField()
    ->willReturn($agreed);

  // Mocks select query using mock object because prophecy.
  $select = $this
    ->getMockBuilder('\\Drupal\\Core\\Database\\Query\\SelectInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $select
    ->expects($this
    ->any())
    ->method('fields')
    ->willReturnSelf();
  $select
    ->expects($this
    ->any())
    ->method('condition')
    ->willReturnSelf();
  $select
    ->expects($this
    ->any())
    ->method('range')
    ->willReturnSelf();
  $select
    ->expects($this
    ->any())
    ->method('execute')
    ->willReturn($statementProphet
    ->reveal());
  $connectionProphet = $this
    ->prophesize('\\Drupal\\Core\\Database\\Connection');
  $connectionProphet
    ->select('agreement')
    ->willReturn($select);

  // Mocks storage and entity type manager.
  $storageProphet = $this
    ->prophesize('\\Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
  $storageProphet
    ->loadMultiple()
    ->willReturn($agreements);
  $entityTypeManagerProphet = $this
    ->prophesize('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
  $entityTypeManagerProphet
    ->getStorage('agreement')
    ->willReturn($storageProphet
    ->reveal());

  // Creates an actual PathMatcher dependency because the logic needs to be
  // tested as part of this. This could be an indication that this needs to
  // be its own class/service.
  $pathMatcher = new PathMatcher($configFactoryProphet
    ->reveal(), $routeMatchProphet
    ->reveal());
  $timeProphet = $this
    ->prophesize('\\Drupal\\Component\\Datetime\\TimeInterface');
  $timeProphet
    ->getRequestTime()
    ->willReturn(time());
  $requestStack = new RequestStack();
  $requestStack
    ->push(Request::create('/'));
  $handler = new AgreementHandler($connectionProphet
    ->reveal(), $entityTypeManagerProphet
    ->reveal(), $pathMatcher, $timeProphet
    ->reveal(), $requestStack);
  $agreement = $handler
    ->getAgreementByUserAndPath($accountProphet
    ->reveal(), $path);
  $this
    ->assertEquals($expected, $agreement);
}