You are here

public function GetGroupContentTest::testMultipleGroupReferences in Organic groups 8

Test retrieval of group content that references multiple groups.

File

tests/src/Kernel/Entity/GetGroupContentTest.php, line 175

Class

GetGroupContentTest
Tests getting the group content of a group.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

public function testMultipleGroupReferences() {
  $groups = [];

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

  // Create a group content type.
  $bundle = mb_strtolower($this
    ->randomMachineName());
  $settings = [
    'field_storage_config' => [
      'settings' => [
        'target_type' => 'node',
      ],
    ],
  ];
  Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'entity_test', $bundle, $settings);

  // Create a group content entity that references both groups.
  $group_content = $this->entityTypeManager
    ->getStorage('entity_test')
    ->create([
    'name' => $this
      ->randomString(),
    'type' => $bundle,
    OgGroupAudienceHelperInterface::DEFAULT_FIELD => [
      [
        'target_id' => $groups[0]
          ->id(),
      ],
      [
        'target_id' => $groups[1]
          ->id(),
      ],
    ],
  ]);
  $group_content
    ->save();

  /** @var \Drupal\og\MembershipManagerInterface $membership_manager */
  $membership_manager = \Drupal::service('og.membership_manager');

  // Check that Og::getGroupContent() returns the group content entity for
  // both groups.
  $expected = [
    'entity_test' => [
      $group_content
        ->id(),
    ],
  ];
  foreach ($groups as $key => $groups) {
    $result = $membership_manager
      ->getGroupContentIds($groups);
    $this
      ->assertEquals($expected, $result, "The group content entity is returned for group {$key}.");
  }
}