You are here

class OgRoleManagerTest in Organic groups 8

Same name in this branch
  1. 8 tests/src/Unit/OgRoleManagerTest.php \Drupal\Tests\og\Unit\OgRoleManagerTest
  2. 8 tests/src/Kernel/OgRoleManagerTest.php \Drupal\Tests\og\Kernel\OgRoleManagerTest

Tests create membership helper function.

@group og @coversDefaultClass \Drupal\og\OgRoleManager

Hierarchy

Expanded class hierarchy of OgRoleManagerTest

File

tests/src/Unit/OgRoleManagerTest.php, line 22

Namespace

Drupal\Tests\og\Unit
View source
class OgRoleManagerTest extends UnitTestCase {

  /**
   * The entity type ID of the test group.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The bundle ID of the test group.
   *
   * @var string
   */
  protected $bundle;

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

  /**
   * The entity storage service.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $entityStorage;

  /**
   * The event dispatcher service.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $eventDispatcher;

  /**
   * The created OG role.
   *
   * @var \Drupal\og\Entity\OgRole|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $ogRole;

  /**
   * The entity storage for OgRole entities.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $ogRoleStorage;

  /**
   * The permissions manager service.
   *
   * @var \Drupal\og\PermissionManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $permissionManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entityTypeId = $this
      ->randomMachineName();
    $this->bundle = $this
      ->randomMachineName();
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->entityStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $this->eventDispatcher = $this
      ->prophesize(EventDispatcherInterface::class);
    $this->ogRole = $this
      ->prophesize(OgRole::class);
    $this->ogRoleStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $this->permissionManager = $this
      ->prophesize(PermissionManagerInterface::class);
    $this->entityTypeManager
      ->getStorage('og_role')
      ->willReturn($this->ogRoleStorage
      ->reveal());
  }

  /**
   * Tests creation of per bundle roles.
   *
   * @param string $role_name
   *   The name of the role being created.
   *
   * @covers ::createPerBundleRoles
   *
   * @dataProvider bundleRolesProvider
   */
  public function testCreatePerBundleRoles($role_name) {
    $entity_type_id = $this->entityTypeId;
    $bundle = $this->bundle;

    // The Prophecy mocking framework uses 'promises' for dynamically generating
    // mocks that return context dependent data. This works by dynamically
    // setting the expected behaviors in an anonymous function. Make sure the
    // mocks are available in the local scope so they can be passed to the
    // anonymous functions.
    $permission_manager = $this->permissionManager;

    // We have to use OgRole and not OgRoleInterface, due to inheritance issues,
    // where PHP doesn't allow OgRoleInterface to extend RoleInterface.
    $og_role = $this->ogRole;
    foreach ($this
      ->getDefaultRoleProperties() as $properties) {

      // It is expected that the role will be created with default properties.
      $this->ogRoleStorage
        ->create($properties)
        ->will(function () use ($entity_type_id, $bundle, $role_name, $og_role, $permission_manager) {

        // It is expected that the OG permissions that need to be populated on
        // the new role will be requested. We are not testing permissions here
        // so we can just return an empty array.
        $permission_manager
          ->getDefaultGroupPermissions($entity_type_id, $bundle, $role_name)
          ->willReturn([])
          ->shouldBeCalled();

        // For each role that is created it is expected that the role name
        // will be retrieved, so that the role name can be used to filter the
        // permissions.
        $og_role
          ->getName()
          ->willReturn($role_name)
          ->shouldBeCalled();

        // The group type, bundle and permissions will have to be set on the
        // new role.
        $og_role
          ->setGroupType($entity_type_id)
          ->shouldBeCalled();
        $og_role
          ->setGroupBundle($bundle)
          ->shouldBeCalled();
        return $og_role
          ->reveal();
      })
        ->shouldBeCalled();

      // The role is expected to be saved.
      $og_role
        ->save()
        ->willReturn(1)
        ->shouldBeCalled();
    }
    $og_role_manager = $this
      ->getOgRoleManager();
    $og_roles = $og_role_manager
      ->createPerBundleRoles($this->entityTypeId, $this->bundle);
    $this
      ->assertCount(2, $og_roles);
  }

  /**
   * Provides test data to test bundle roles creation.
   *
   * @return array
   *   Array with the OG Role machine names.
   */
  public function bundleRolesProvider() {
    return [
      [
        OgRoleInterface::ANONYMOUS,
      ],
      [
        OgRoleInterface::AUTHENTICATED,
      ],
    ];
  }

  /**
   * Returns the expected properties of the default role with the given name.
   *
   * @return array
   *   The default properties.
   */
  protected function getDefaultRoleProperties() {
    return [
      OgRoleInterface::ANONYMOUS => [
        'role_type' => OgRoleInterface::ROLE_TYPE_REQUIRED,
        'label' => 'Non-member',
        'name' => OgRoleInterface::ANONYMOUS,
      ],
      OgRoleInterface::AUTHENTICATED => [
        'role_type' => OgRoleInterface::ROLE_TYPE_REQUIRED,
        'label' => 'Member',
        'name' => OgRoleInterface::AUTHENTICATED,
      ],
    ];
  }

  /**
   * Tests role removal.
   *
   * @covers ::removeRoles
   */
  public function testRoleRemoval() {
    $properties = [
      'group_type' => $this->entityTypeId,
      'group_bundle' => $this->bundle,
    ];
    $this->ogRoleStorage
      ->loadByProperties($properties)
      ->willReturn([
      $this->ogRole
        ->reveal(),
      $this->ogRole
        ->reveal(),
      $this->ogRole
        ->reveal(),
    ])
      ->shouldBeCalled();

    // It is expected that all roles will be deleted, so three delete() calls
    // will be made.
    $this->ogRole
      ->delete()
      ->shouldBeCalledTimes(3);
    $og_role_manager = $this
      ->getOgRoleManager();
    $og_role_manager
      ->removeRoles($this->entityTypeId, $this->bundle);
  }

  /**
   * Return a new OG role manager object.
   *
   * @return \Drupal\og\OgRoleManagerInterface
   *   The initialized OG role manager.
   */
  protected function getOgRoleManager() {
    return new OgRoleManager($this->entityTypeManager
      ->reveal(), $this->eventDispatcher
      ->reveal(), $this->permissionManager
      ->reveal());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgRoleManagerTest::$bundle protected property The bundle ID of the test group.
OgRoleManagerTest::$entityStorage protected property The entity storage service.
OgRoleManagerTest::$entityTypeId protected property The entity type ID of the test group.
OgRoleManagerTest::$entityTypeManager protected property The entity type manager service.
OgRoleManagerTest::$eventDispatcher protected property The event dispatcher service.
OgRoleManagerTest::$ogRole protected property The created OG role.
OgRoleManagerTest::$ogRoleStorage protected property The entity storage for OgRole entities.
OgRoleManagerTest::$permissionManager protected property The permissions manager service.
OgRoleManagerTest::bundleRolesProvider public function Provides test data to test bundle roles creation.
OgRoleManagerTest::getDefaultRoleProperties protected function Returns the expected properties of the default role with the given name.
OgRoleManagerTest::getOgRoleManager protected function Return a new OG role manager object.
OgRoleManagerTest::setUp protected function Overrides UnitTestCase::setUp
OgRoleManagerTest::testCreatePerBundleRoles public function Tests creation of per bundle roles.
OgRoleManagerTest::testRoleRemoval public function Tests role removal.
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.