You are here

public function GetBundleByBundleTest::testGetBundleIdsByBundle in Organic groups 8

Tests retrieval of bundles that are referenc[ed|ing] bundles.

This tests the retrieval of the relations between groups and group content and vice versa. The retrieval of groups that are referenced by group content is done by GroupTypeManagerInterface::getGroupBundleIdsByGroupContenBundle() while GroupTypeManagerInterface::getGroupContentBundleIdsByGroupBundle() handles the opposite case.

Both methods are tested here in a single test since they are very similar, and not having to set up the entire relationship structure twice reduces the total test running time.

@covers ::getGroupBundleIdsByGroupContentBundle @covers ::getGroupContentBundleIdsByGroupBundle

@dataProvider getBundleIdsByBundleProvider

Parameters

array $relationships: An array indicating the relationships between groups and group content bundles that need to be set up in the test.

array $expected_group_by_group_content: An array containing the expected results for the call to getGroupBundleIdsByGroupContentBundle().

array $expected_group_content_by_group: An array containing the expected results for the 4 calls to getGroupContentBundleIdsByGroupBundle() that will be made in the test.

File

tests/src/Kernel/Entity/GetBundleByBundleTest.php, line 121

Class

GetBundleByBundleTest
Tests retrieving group content bundles by group bundles and vice versa.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

public function testGetBundleIdsByBundle(array $relationships, array $expected_group_by_group_content, array $expected_group_content_by_group) {

  // Set up the relations as indicated in the test.
  foreach ($relationships as $group_content_entity_type_id => $group_content_bundle_ids) {
    foreach ($group_content_bundle_ids as $group_content_bundle_id => $group_audience_fields) {
      switch ($group_content_entity_type_id) {
        case 'node':
          NodeType::create([
            'name' => $this
              ->randomString(),
            'type' => $group_content_bundle_id,
          ])
            ->save();
          break;
        case 'block_content':
          BlockContentType::create([
            'id' => $group_content_bundle_id,
          ])
            ->save();
          break;
      }
      foreach ($group_audience_fields as $group_audience_field_key => $group_audience_field_data) {
        foreach ($group_audience_field_data as $group_entity_type_id => $group_bundle_ids) {
          $settings = [
            'field_name' => 'group_audience_' . $group_audience_field_key,
            'field_storage_config' => [
              'settings' => [
                'target_type' => $group_entity_type_id,
              ],
            ],
          ];
          if (!empty($group_bundle_ids)) {
            $settings['field_config'] = [
              'settings' => [
                'handler' => 'default',
                'handler_settings' => [
                  'target_bundles' => array_combine($group_bundle_ids, $group_bundle_ids),
                ],
              ],
            ];
          }
          Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, $group_content_entity_type_id, $group_content_bundle_id, $settings);
        }
      }
    }
  }

  // Test ::getGroupBundleIdsByGroupContentBundle().
  foreach ($expected_group_by_group_content as $group_content_entity_type_id => $group_content_bundle_ids) {
    foreach ($group_content_bundle_ids as $group_content_bundle_id => $expected_result) {
      $this
        ->assertEquals($expected_result, $this->groupTypeManager
        ->getGroupBundleIdsByGroupContentBundle($group_content_entity_type_id, $group_content_bundle_id));
    }
  }

  // Test ::getGroupContentBundleIdsByGroupBundle().
  foreach ([
    'node',
    'block_content',
  ] as $group_entity_type_id) {
    for ($i = 0; $i < 2; $i++) {
      $group_bundle_id = 'group_' . $i;

      // If the expected value is omitted, we expect an empty array.
      $expected_result = !empty($expected_group_content_by_group[$group_entity_type_id][$group_bundle_id]) ? $expected_group_content_by_group[$group_entity_type_id][$group_bundle_id] : [];
      $this
        ->assertEquals($expected_result, $this->groupTypeManager
        ->getGroupContentBundleIdsByGroupBundle($group_entity_type_id, $group_bundle_id));
    }
  }
}