You are here

public function RequestQueryArgumentResolverTest::testResolve in Organic groups 8

@covers ::resolve @dataProvider resolveProvider

Parameters

string $group_type: The group type that is passed as a query argument.

string $group_id: The group ID that is passed as a query argument.

array $previously_added_groups: An array of test entity IDs that were added to the collection by plugins that ran previously.

string $expected_added_group: The group that is expected to be added by the plugin. If left empty it is explicitly expected that the plugin will not add any group to the collection.

Overrides OgGroupResolverTestBase::testResolve

File

tests/src/Unit/Plugin/OgGroupResolver/RequestQueryArgumentResolverTest.php, line 67

Class

RequestQueryArgumentResolverTest
Tests the RequestQueryArgumentResolver plugin.

Namespace

Drupal\Tests\og\Unit\Plugin\OgGroupResolver

Code

public function testResolve($group_type = NULL, $group_id = NULL, array $previously_added_groups = [], $expected_added_group = NULL) {

  // It is expected that the plugin will retrieve the current request from the
  // request stack.
  $request = $this
    ->prophesize(Request::class)
    ->reveal();
  $this->requestStack
    ->getCurrentRequest()
    ->willReturn($request)
    ->shouldBeCalled();

  // It will retrieve the query object from the request.

  /** @var \Symfony\Component\HttpFoundation\ParameterBag|\Prophecy\Prophecy\ObjectProphecy $query */
  $query = $this
    ->prophesize(ParameterBag::class);

  // Mock methods to check for the existence and value of the query arguments
  // for the group entity type and ID. The plugin is allowed to call these.
  $query
    ->has(RequestQueryArgumentResolver::GROUP_ID_ARGUMENT)
    ->willReturn(!empty($group_id));
  $query
    ->has(RequestQueryArgumentResolver::GROUP_TYPE_ARGUMENT)
    ->willReturn(!empty($group_type));
  $query
    ->get(RequestQueryArgumentResolver::GROUP_ID_ARGUMENT)
    ->willReturn($group_id);
  $query
    ->get(RequestQueryArgumentResolver::GROUP_TYPE_ARGUMENT)
    ->willReturn($group_type);
  $request->query = $query
    ->reveal();

  // The plugin may try to load the entity that is described in the query
  // arguments.
  if (!empty($group_type) && !empty($group_id)) {

    // The plugin is allowed to request the entity storage for the group.
    $storage = $this
      ->prophesize(EntityStorageInterface::class);

    // The entity may be loaded from storage so the plugin can check whether
    // it is a group entity. This should only happen if it is a valid entity.
    if (!empty($this->testEntities[$group_id])) {
      $group = $this->testEntities[$group_id];
      if ($group
        ->id() === $group_id && $group
        ->getEntityTypeId() === $group_type) {
        $storage
          ->load($group_id)
          ->willReturn($group);
      }
    }
    $this->entityTypeManager
      ->getStorage($group_type)
      ->willReturn($storage);
  }

  // Construct a collection of groups that were discovered by other plugins.

  /** @var \Drupal\og\OgResolvedGroupCollectionInterface|\Prophecy\Prophecy\ObjectProphecy $collection */
  $collection = $this
    ->prophesize(OgResolvedGroupCollectionInterface::class);

  // Set expectations for investigations the plugin may launch into the nature
  // of our test entities.
  foreach ($this
    ->getTestEntityProperties() as $test_entity_id => $properties) {

    // The plugin may request if any of the entities are already discovered by
    // a previous plugin.
    $collection
      ->hasGroup($this->testEntities[$test_entity_id])
      ->willReturn(in_array($test_entity_id, $previously_added_groups));

    // The plugin may ask whether this entity is a group.
    $this->groupTypeManager
      ->isGroup($properties['type'], $properties['bundle'])
      ->willReturn(!empty($properties['group']));
  }

  // Add an expectation if the plugin should add a vote for a group or not,
  // depending on the provided test data.
  if ($expected_added_group) {
    $collection
      ->addGroup($this->testEntities[$expected_added_group], [
      'url',
    ])
      ->shouldBeCalled();
  }
  else {
    $collection
      ->addGroup()
      ->shouldNotBeCalled();
  }

  // Launch the test. Any unmet expectation will cause a failure.
  $plugin = $this
    ->getPluginInstance();
  $plugin
    ->resolve($collection
    ->reveal());
}