You are here

protected function GroupMembershipManagerTest::setUp in Organic groups 8

Overrides KernelTestBase::setUp

File

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

Class

GroupMembershipManagerTest
Tests retrieving groups associated with a given group content.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installConfig([
    'og',
  ]);
  $this
    ->installEntitySchema('entity_test');
  $this
    ->installEntitySchema('entity_test_rev');
  $this
    ->installEntitySchema('entity_test_with_bundle');
  $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');
  $this->membershipManager = $this->container
    ->get('og.membership_manager');
  $this->groups = [];

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

  // Create four groups of two different entity types.
  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['node'][] = $group;

    // The Entity Test entity doesn't have 'real' bundles, so we don't need to
    // create one, we can just add the group to the fake bundle.
    $bundle = "entity_test_{$i}";
    Og::groupTypeManager()
      ->addGroup('entity_test', $bundle);
    $group = EntityTest::create([
      'type' => $bundle,
      'name' => $this
        ->randomString(),
      'uid' => $group_admin
        ->id(),
    ]);
    $group
      ->save();
    $this->groups['entity_test'][] = $group;
  }

  // Create a group content type with two group audience fields, one for each
  // group.
  $bundle = mb_strtolower($this
    ->randomMachineName());
  foreach ([
    'entity_test',
    'node',
  ] as $target_type) {
    $settings = [
      'field_name' => 'group_audience_' . $target_type,
      'field_storage_config' => [
        'settings' => [
          'target_type' => $target_type,
        ],
      ],
    ];
    Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'entity_test', $bundle, $settings);
  }

  // Create a group content entity that references all four groups.
  $values = [
    'name' => $this
      ->randomString(),
    'type' => $bundle,
  ];
  foreach ([
    'entity_test',
    'node',
  ] as $target_type) {
    foreach ($this->groups[$target_type] as $group) {
      $values['group_audience_' . $target_type][] = [
        'target_id' => $group
          ->id(),
      ];
    }
  }
  $this->groupContent = $this->entityTypeManager
    ->getStorage('entity_test')
    ->create($values);
  $this->groupContent
    ->save();
}