You are here

public function UserSectionStorageUnitTest::testGetUserSectionsShouldBeStaticallyCached in Workbench Access 8

Tests that ::getUserSections is statically cached.

@covers ::getUserSections

File

tests/src/Unit/UserSectionStorageUnitTest.php, line 28

Class

UserSectionStorageUnitTest
Unit tests for user section storage service.

Namespace

Drupal\Tests\workbench_access\Unit

Code

public function testGetUserSectionsShouldBeStaticallyCached() {
  $user = $this
    ->prophesize(UserInterface::class);
  $testUserId = 37;
  $entity_type_manager = $this
    ->prophesize(EntityTypeManagerInterface::class);
  $section_storage = $this
    ->prophesize(EntityStorageInterface::class);
  $query = $this
    ->prophesize(QueryAggregateInterface::class);
  $section_storage
    ->getAggregateQuery()
    ->willReturn($query
    ->reveal());
  $query
    ->condition('access_scheme', 'editorial_section')
    ->willReturn($query
    ->reveal());
  $query
    ->condition('user_id', 37)
    ->willReturn($query
    ->reveal());
  $query
    ->groupBy('section_id')
    ->willReturn($query
    ->reveal());
  $query
    ->execute()
    ->willReturn([
    37 => [
      'section_id' => 3,
    ],
  ])
    ->shouldBeCalledTimes(1);
  $entity_type_manager
    ->getStorage('section_association')
    ->willReturn($section_storage
    ->reveal());
  $scheme = $this
    ->prophesize(AccessSchemeInterface::class);
  $scheme
    ->id()
    ->willReturn('editorial_section');
  $user
    ->id()
    ->willReturn($testUserId);
  $role_section_storage = $this
    ->prophesize(RoleSectionStorageInterface::class);
  $role_section_storage
    ->getRoleSections($scheme
    ->reveal(), $user
    ->reveal())
    ->willReturn([]);
  $user_section_storage = new UserSectionStorage($entity_type_manager
    ->reveal(), $user
    ->reveal(), $role_section_storage
    ->reveal());

  // First time, prime the cache.
  $first = $user_section_storage
    ->getUserSections($scheme
    ->reveal(), $user
    ->reveal());

  // Second time, just return the result.
  $second = $user_section_storage
    ->getUserSections($scheme
    ->reveal(), $user
    ->reveal());
  $this
    ->assertEquals($first, $second);
}