You are here

public function GetGroupContentTest::testMultipleGroupAudienceFields in Organic groups 8

Test retrieval of group content with multiple group audience fields.

File

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

Class

GetGroupContentTest
Tests getting the group content of a group.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

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

  // Create two groups of different entity types.
  $bundle = mb_strtolower($this
    ->randomMachineName());
  NodeType::create([
    'name' => $this
      ->randomString(),
    'type' => $bundle,
  ])
    ->save();
  Og::groupTypeManager()
    ->addGroup('node', $bundle);
  $groups['node'] = Node::create([
    'title' => $this
      ->randomString(),
    'type' => $bundle,
    'uid' => $this->groupAdmin
      ->id(),
  ]);
  $groups['node']
    ->save();

  // 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 = mb_strtolower($this
    ->randomMachineName());
  Og::groupTypeManager()
    ->addGroup('entity_test', $bundle);
  $groups['entity_test'] = EntityTest::create([
    'type' => $bundle,
    'name' => $this
      ->randomString(),
    'uid' => $this->groupAdmin
      ->id(),
  ]);
  $groups['entity_test']
    ->save();

  // 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 both groups.
  $values = [
    'name' => $this
      ->randomString(),
    'type' => $bundle,
  ];
  foreach ([
    'entity_test',
    'node',
  ] as $target_type) {
    $values['group_audience_' . $target_type] = [
      [
        'target_id' => $groups[$target_type]
          ->id(),
      ],
    ];
  }
  $group_content = $this->entityTypeManager
    ->getStorage('entity_test')
    ->create($values);
  $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}.");
  }
}