You are here

protected function AccessByOgMembershipTest::setUp in Organic groups 8

Overrides KernelTestBase::setUp

File

tests/src/Kernel/Access/AccessByOgMembershipTest.php, line 70

Class

AccessByOgMembershipTest
Tests access to content by OgMembership.

Namespace

Drupal\Tests\og\Kernel\Access

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installConfig([
    'og',
  ]);
  $this
    ->installEntitySchema('block_content');
  $this
    ->installEntitySchema('node');
  $this
    ->installEntitySchema('og_membership');
  $this
    ->installEntitySchema('user');
  $this
    ->installSchema('system', 'sequences');

  // Create a user role for a standard authenticated user.
  $role = Role::create([
    'id' => 'authenticated',
    'label' => 'authenticated',
  ]);
  $role
    ->grantPermission('access content');
  $role
    ->save();

  // Create a test user for each membership type.
  $membership_types = [
    // The group owner.
    'owner',
    // A regular member of the group.
    'member',
    // A user that is not a member of the group.
    'non-member',
    // A blocked user.
    'blocked',
  ];
  foreach ($membership_types as $membership_type) {
    $user = User::create([
      'name' => $membership_type,
    ]);
    $user
      ->save();
    $this->users[$membership_type] = $user;
  }

  // Create a "group" bundle on the Custom Block entity type and turn it into
  // a group. Note we're not using the Entity Test entity for this since it
  // does not have real support for multiple bundles.
  BlockContentType::create([
    'id' => 'group',
  ])
    ->save();
  Og::groupTypeManager()
    ->addGroup('block_content', 'group');

  // Create a group.
  $this->group = BlockContent::create([
    'title' => $this
      ->randomString(),
    'type' => 'group',
    'uid' => $this->users['owner']
      ->id(),
  ]);
  $this->group
    ->save();

  // Create a group content type.
  $type = NodeType::create([
    'type' => 'group_content',
    'name' => $this
      ->randomString(),
  ]);
  $type
    ->save();
  $settings = [
    'field_storage_config' => [
      'settings' => [
        'target_type' => 'block_content',
      ],
    ],
  ];
  Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'node', 'group_content', $settings);

  // Grant both members and non-members permission to edit any group content.
  foreach ([
    OgRoleInterface::AUTHENTICATED,
    OgRoleInterface::ANONYMOUS,
  ] as $role_name) {

    /** @var \Drupal\og\Entity\OgRole $role */
    $role = OgRole::getRole('block_content', 'group', $role_name);
    $role
      ->grantPermission('edit any group_content content')
      ->save();
  }
  $role = OgRole::getRole('block_content', 'group', OgRoleInterface::AUTHENTICATED);

  // Subscribe the normal member and the blocked member to the group.
  foreach ([
    'member',
    'blocked',
  ] as $membership_type) {
    $state = $membership_type === 'member' ? OgMembershipInterface::STATE_ACTIVE : OgMembershipInterface::STATE_BLOCKED;
    $this
      ->createOgMembership($this->group, $this->users[$membership_type], NULL, $state);
  }

  // Create three group content items, one owned by the group owner, one by
  // the member, and one by the blocked user.
  foreach ([
    'owner',
    'member',
    'blocked',
  ] as $membership_type) {
    $this->groupContent[$membership_type] = Node::create([
      'title' => $this
        ->randomString(),
      'type' => 'group_content',
      'uid' => $this->users[$membership_type]
        ->id(),
      OgGroupAudienceHelperInterface::DEFAULT_FIELD => [
        [
          'target_id' => $this->group
            ->id(),
        ],
      ],
    ]);
    $this->groupContent[$membership_type]
      ->save();
  }
}