You are here

class UserSectionStorageUnitTest in Workbench Access 8

Unit tests for user section storage service.

@group workbench_access

@coversDefaultClass \Drupal\workbench_access\UserSectionStorage

Hierarchy

Expanded class hierarchy of UserSectionStorageUnitTest

File

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

Namespace

Drupal\Tests\workbench_access\Unit
View source
class UserSectionStorageUnitTest extends UnitTestCase {

  /**
   * Tests that ::getUserSections is statically cached.
   *
   * @covers ::getUserSections
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
UserSectionStorageUnitTest::testGetUserSectionsShouldBeStaticallyCached public function Tests that ::getUserSections is statically cached.