You are here

class GroupRoleSynchronizerTest in Group 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/GroupRoleSynchronizerTest.php \Drupal\Tests\group\Unit\GroupRoleSynchronizerTest

Tests the outsider group role synchronizer service.

@coversDefaultClass \Drupal\group\GroupRoleSynchronizer @group group

Hierarchy

Expanded class hierarchy of GroupRoleSynchronizerTest

File

tests/src/Unit/GroupRoleSynchronizerTest.php, line 19

Namespace

Drupal\Tests\group\Unit
View source
class GroupRoleSynchronizerTest extends UnitTestCase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $entityTypeManager;

  /**
   * The group role synchronizer service.
   *
   * @var \Drupal\group\GroupRoleSynchronizer
   */
  protected $groupRoleSynchronizer;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->groupRoleSynchronizer = new GroupRoleSynchronizer($this->entityTypeManager
      ->reveal());
  }

  /**
   * @covers ::getGroupRoleId
   */
  public function testGetGroupRoleId() {
    $expected = 'foo-' . substr(md5('group_role_sync.bar'), 0, 9);
    $this
      ->assertEquals($expected, $this->groupRoleSynchronizer
      ->getGroupRoleId('foo', 'bar'));
  }

  /**
   * @covers ::getGroupRoleIdsByGroupType
   * @depends testGetGroupRoleId
   */
  public function testGetGroupRoleIdsByGroupType() {
    $this
      ->setUpConfigEntityStorage('user_role', [
      'bar',
      'baz',
    ]);
    $expected = [
      $this->groupRoleSynchronizer
        ->getGroupRoleId('foo', 'bar'),
      $this->groupRoleSynchronizer
        ->getGroupRoleId('foo', 'baz'),
    ];
    $this
      ->assertEquals($expected, $this->groupRoleSynchronizer
      ->getGroupRoleIdsByGroupType('foo'));
  }

  /**
   * @covers ::getGroupRoleIdsByGroupTypes
   * @depends testGetGroupRoleId
   */
  public function testGetGroupRoleIdsByGroupTypes() {
    $this
      ->setUpConfigEntityStorage('user_role', [
      'bar',
      'baz',
    ]);
    $expected = [
      $this->groupRoleSynchronizer
        ->getGroupRoleId('foo', 'bar'),
      $this->groupRoleSynchronizer
        ->getGroupRoleId('bee', 'bar'),
      $this->groupRoleSynchronizer
        ->getGroupRoleId('foo', 'baz'),
      $this->groupRoleSynchronizer
        ->getGroupRoleId('bee', 'baz'),
    ];
    $this
      ->assertEquals($expected, $this->groupRoleSynchronizer
      ->getGroupRoleIdsByGroupTypes([
      'foo',
      'bee',
    ]));
  }

  /**
   * @covers ::getGroupRoleIdsByUserRole
   * @depends testGetGroupRoleId
   */
  public function testGetGroupRoleIdsByUserRole() {
    $this
      ->setUpConfigEntityStorage('group_type', [
      'foo',
      'bar',
    ]);
    $expected = [
      $this->groupRoleSynchronizer
        ->getGroupRoleId('foo', 'baz'),
      $this->groupRoleSynchronizer
        ->getGroupRoleId('bar', 'baz'),
    ];
    $this
      ->assertEquals($expected, $this->groupRoleSynchronizer
      ->getGroupRoleIdsByUserRole('baz'));
  }

  /**
   * @covers ::getGroupRoleIdsByUserRoles
   * @depends testGetGroupRoleId
   */
  public function testGetGroupRoleIdsByUserRoles() {
    $this
      ->setUpConfigEntityStorage('group_type', [
      'foo',
      'bar',
    ]);
    $expected = [
      $this->groupRoleSynchronizer
        ->getGroupRoleId('foo', 'baz'),
      $this->groupRoleSynchronizer
        ->getGroupRoleId('foo', 'ook'),
      $this->groupRoleSynchronizer
        ->getGroupRoleId('bar', 'baz'),
      $this->groupRoleSynchronizer
        ->getGroupRoleId('bar', 'ook'),
    ];
    $this
      ->assertEquals($expected, $this->groupRoleSynchronizer
      ->getGroupRoleIdsByUserRoles([
      'baz',
      'ook',
    ]));
  }

  /**
   * Mock and set up a config entity type's storage handler.
   *
   * @param string $entity_type_id
   *   The ID of the config entity type to mock the storage for.
   * @param string[] $entity_ids
   *   The IDs of the config entities to return from an entity query.
   * @param \Drupal\Core\Config\Entity\ConfigEntityInterface[] $entities
   *   The config entities to return from a loadMultiple() call.
   */
  protected function setUpConfigEntityStorage($entity_type_id, $entity_ids = [], $entities = []) {
    $storage = $this
      ->prophesize(ConfigEntityStorageInterface::class);
    if (!empty($entity_ids)) {
      $query = $this
        ->prophesize(QueryInterface::class);
      $query
        ->execute()
        ->willReturn($entity_ids);
      $storage
        ->getQuery()
        ->willReturn($query
        ->reveal());
    }
    if (!empty($entities)) {
      $storage
        ->loadMultiple(NULL)
        ->willReturn($entities);
      $storage
        ->loadMultiple(array_keys($entities))
        ->willReturn($entities);
    }
    $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->willReturn($storage
      ->reveal());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupRoleSynchronizerTest::$entityTypeManager protected property The entity type manager.
GroupRoleSynchronizerTest::$groupRoleSynchronizer protected property The group role synchronizer service.
GroupRoleSynchronizerTest::setUp public function Overrides UnitTestCase::setUp
GroupRoleSynchronizerTest::setUpConfigEntityStorage protected function Mock and set up a config entity type's storage handler.
GroupRoleSynchronizerTest::testGetGroupRoleId public function @covers ::getGroupRoleId
GroupRoleSynchronizerTest::testGetGroupRoleIdsByGroupType public function @covers ::getGroupRoleIdsByGroupType @depends testGetGroupRoleId
GroupRoleSynchronizerTest::testGetGroupRoleIdsByGroupTypes public function @covers ::getGroupRoleIdsByGroupTypes @depends testGetGroupRoleId
GroupRoleSynchronizerTest::testGetGroupRoleIdsByUserRole public function @covers ::getGroupRoleIdsByUserRole @depends testGetGroupRoleId
GroupRoleSynchronizerTest::testGetGroupRoleIdsByUserRoles public function @covers ::getGroupRoleIdsByUserRoles @depends testGetGroupRoleId
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.