You are here

public function OgRoleManagerTest::testCreatePerBundleRoles in Organic groups 8

Tests creation of per bundle roles.

@covers ::createPerBundleRoles

@dataProvider bundleRolesProvider

Parameters

string $role_name: The name of the role being created.

File

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

Class

OgRoleManagerTest
Tests create membership helper function.

Namespace

Drupal\Tests\og\Unit

Code

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);
}