You are here

protected function GroupMembershipManagerTest::createTestMemberships in Organic groups 8

Creates a number of users that are members of the test groups.

1 call to GroupMembershipManagerTest::createTestMemberships()
GroupMembershipManagerTest::doTestGetGroupMembershipsByRoleNames in tests/src/Kernel/Entity/GroupMembershipManagerTest.php
Tests retrieval of group memberships or their IDs filtered by role names.

File

tests/src/Kernel/Entity/GroupMembershipManagerTest.php, line 632

Class

GroupMembershipManagerTest
Tests retrieving groups associated with a given group content.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

protected function createTestMemberships() {

  // Create a 'moderator' role in each of the test group types.
  foreach ([
    'node',
    'entity_test',
  ] as $entity_type_id) {
    for ($i = 0; $i < 2; $i++) {
      $bundle = "{$entity_type_id}_{$i}";
      $og_role = OgRole::create();
      $og_role
        ->setName('moderator')
        ->setGroupType($entity_type_id)
        ->setGroupBundle($bundle)
        ->save();
    }
  }

  // Create test users with different membership states in the various groups.
  // Note that the group admin (test user 0) is also a member of all groups.
  $matrix = [
    // A user which is an active member of the first node group.
    1 => [
      'node' => [
        0 => [
          'state' => OgMembershipInterface::STATE_ACTIVE,
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
        ],
      ],
    ],
    // A user which is a pending administrator of the second node group.
    2 => [
      'node' => [
        1 => [
          'state' => OgMembershipInterface::STATE_PENDING,
          'roles' => [
            OgRoleInterface::ADMINISTRATOR,
          ],
        ],
      ],
    ],
    // A user which is an active member of both test entity groups.
    3 => [
      'entity_test' => [
        0 => [
          'state' => OgMembershipInterface::STATE_ACTIVE,
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
        ],
        1 => [
          'state' => OgMembershipInterface::STATE_ACTIVE,
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
        ],
      ],
    ],
    // A user which is a pending member of the first node group and a blocked
    // moderator in the second test entity group.
    4 => [
      'node' => [
        0 => [
          'state' => OgMembershipInterface::STATE_PENDING,
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
        ],
      ],
      'entity_test' => [
        1 => [
          'state' => OgMembershipInterface::STATE_BLOCKED,
          'roles' => [
            'moderator',
          ],
        ],
      ],
    ],
    // A user which is not subscribed to any of the groups.
    5 => [],
    // A user which is both an administrator and a moderator in the second
    // node group.
    6 => [
      'node' => [
        1 => [
          'state' => OgMembershipInterface::STATE_ACTIVE,
          'roles' => [
            OgRoleInterface::ADMINISTRATOR,
            'moderator',
          ],
        ],
      ],
    ],
    // A troll who is banned everywhere.
    7 => [
      'node' => [
        0 => [
          'state' => OgMembershipInterface::STATE_BLOCKED,
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
        ],
        1 => [
          'state' => OgMembershipInterface::STATE_BLOCKED,
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
        ],
      ],
      'entity_test' => [
        0 => [
          'state' => OgMembershipInterface::STATE_BLOCKED,
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
        ],
        1 => [
          'state' => OgMembershipInterface::STATE_BLOCKED,
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
        ],
      ],
    ],
    // A user which is a pending administrator of the second test entity
    // group.
    8 => [
      'entity_test' => [
        1 => [
          'state' => OgMembershipInterface::STATE_PENDING,
          'roles' => [
            OgRoleInterface::ADMINISTRATOR,
          ],
        ],
      ],
    ],
  ];
  foreach ($matrix as $user_key => $entity_types) {
    $user = User::create([
      'name' => $this
        ->randomString(),
    ]);
    $user
      ->save();
    $this->users[$user_key] = $user;
    foreach ($entity_types as $entity_type_id => $groups) {
      foreach ($groups as $group_key => $membership_info) {
        $group = $this->groups[$entity_type_id][$group_key];
        $this
          ->createOgMembership($group, $user, $membership_info['roles'], $membership_info['state']);
      }
    }
  }
}