You are here

protected function GetMembershipsTest::setUp in Organic groups 8

Overrides KernelTestBase::setUp

File

tests/src/Kernel/Entity/GetMembershipsTest.php, line 61

Class

GetMembershipsTest
Tests retrieving OgMembership entities associated with a given user.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installConfig([
    'og',
  ]);
  $this
    ->installEntitySchema('node');
  $this
    ->installEntitySchema('og_membership');
  $this
    ->installEntitySchema('user');
  $this
    ->installSchema('system', 'sequences');
  $this
    ->installSchema('user', [
    'users_data',
  ]);
  $this->entityTypeManager = $this->container
    ->get('entity_type.manager');

  // Create group admin user.
  $group_admin = User::create([
    'name' => $this
      ->randomString(),
  ]);
  $group_admin
    ->save();

  // Create two groups.
  for ($i = 0; $i < 2; $i++) {
    $bundle = "node_{$i}";
    NodeType::create([
      'name' => $this
        ->randomString(),
      'type' => $bundle,
    ])
      ->save();
    Og::groupTypeManager()
      ->addGroup('node', $bundle);
    $group = Node::create([
      'title' => $this
        ->randomString(),
      'type' => $bundle,
      'uid' => $group_admin
        ->id(),
    ]);
    $group
      ->save();
    $this->groups[] = $group;
  }

  // Create test users with different membership states in the two groups.
  $matrix = [
    // A user which is an active member of the first group.
    [
      OgMembershipInterface::STATE_ACTIVE,
      NULL,
    ],
    // A user which is a pending member of the second group.
    [
      NULL,
      OgMembershipInterface::STATE_PENDING,
    ],
    // A user which is an active member of both groups.
    [
      OgMembershipInterface::STATE_ACTIVE,
      OgMembershipInterface::STATE_ACTIVE,
    ],
    // A user which is a pending member of the first group and blocked in the
    // second group.
    [
      OgMembershipInterface::STATE_PENDING,
      OgMembershipInterface::STATE_BLOCKED,
    ],
    // A user which is not subscribed to either of the two groups.
    [
      NULL,
      NULL,
    ],
  ];
  foreach ($matrix as $user_key => $states) {
    $user = User::create([
      'name' => $this
        ->randomString(),
    ]);
    $user
      ->save();
    $this->users[$user_key] = $user;
    foreach ($states as $group_key => $state) {
      $group = $this->groups[$group_key];
      if ($state) {
        $this
          ->createOgMembership($group, $user, NULL, $state);
      }
    }
  }
}