You are here

public function GetGroupContentTest::testBasicGroupReferences in Organic groups 8

Test retrieval of group content that references a single group.

File

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

Class

GetGroupContentTest
Tests getting the group content of a group.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

public function testBasicGroupReferences() {
  $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 4 group content types, two for each entity type referencing each
  // group. Create a group content entity for each.
  $group_content = [];
  foreach ([
    'node',
    'entity_test',
  ] as $entity_type) {
    foreach ([
      'node',
      'entity_test',
    ] as $target_group_type) {

      // Create the group content bundle if it's a node. Entity Test doesn't
      // have real bundles.
      $bundle = mb_strtolower($this
        ->randomMachineName());
      if ($entity_type === 'node') {
        NodeType::create([
          'type' => $bundle,
          'name' => $this
            ->randomString(),
        ])
          ->save();
      }

      // Create the groups audience field.
      $field_name = "og_{$target_group_type}";
      $settings = [
        'field_name' => $field_name,
        'field_storage_config' => [
          'settings' => [
            'target_type' => $groups[$target_group_type]
              ->getEntityTypeId(),
          ],
        ],
        'field_config' => [
          'settings' => [
            'handler' => 'default',
            'handler_settings' => [
              'target_bundles' => [
                $groups[$target_group_type]
                  ->bundle() => $groups[$target_group_type]
                  ->bundle(),
              ],
            ],
          ],
        ],
      ];
      Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, $entity_type, $bundle, $settings);

      // Create the group content entity.
      $label_field = $entity_type === 'node' ? 'title' : 'name';
      $entity = $this->entityTypeManager
        ->getStorage($entity_type)
        ->create([
        $label_field => $this
          ->randomString(),
        'type' => $bundle,
        $field_name => [
          [
            'target_id' => $groups[$target_group_type]
              ->id(),
          ],
        ],
      ]);
      $entity
        ->save();
      $group_content[$entity_type][$target_group_type] = $entity;
    }
  }

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

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

    // Test that the correct results are returned when filtering by entity
    // type.
    foreach ([
      'node',
      'entity_test',
    ] as $filter) {
      $result = $membership_manager
        ->getGroupContentIds($groups[$group_type], [
        $filter,
      ]);
      $this
        ->assertEquals(1, count($result), "Only one entity type is returned when getting {$group_type} results filtered by {$group_content_type} group content.");
      $this
        ->assertEquals([
        $group_content[$filter][$group_type]
          ->id(),
      ], $result[$filter], "The correct result is returned for the {$group_type} group, filtered by {$group_content_type} group content.");
    }
  }
}