You are here

public function OgRouteGroupResolverTestBase::testResolve in Organic groups 8

@todo Update documentation.

@covers ::resolve @dataProvider resolveProvider

Overrides OgGroupResolverTestBase::testResolve

File

tests/src/Unit/Plugin/OgGroupResolver/OgRouteGroupResolverTestBase.php, line 74

Class

OgRouteGroupResolverTestBase
Base class for testing OgGroupResolver plugins that depend on the route.

Namespace

Drupal\Tests\og\Unit\Plugin\OgGroupResolver

Code

public function testResolve($path = NULL, $route_object_id = NULL, array $expected_added_groups = [], array $expected_removed_groups = []) {
  if ($path) {

    // It is expected that the plugin will retrieve the current path from the
    // route matcher.
    $this
      ->willRetrieveCurrentPathFromRouteMatcher($path);

    // It is expected that the plugin will retrieve the full list of content
    // entity paths, so it can check whether the current path is related to a
    // content entity.
    $this
      ->willRetrieveContentEntityPaths();
  }
  if ($route_object_id) {

    // The plugin might retrieve the route object. This should only happen if
    // we are on an actual entity path.
    $this
      ->mightRetrieveRouteObject($route_object_id);

    // If a route object is returned the plugin will need to inspect it to
    // check if it is a group.
    $this
      ->mightCheckIfRouteObjectIsGroup($route_object_id);
  }

  // Add expectations for the groups that are added and removed by the plugin.
  $test_entities = $this->testEntities;
  foreach ([
    &$expected_added_groups,
    &$expected_removed_groups,
  ] as &$expected_groups) {

    // Replace the entity IDs from the data provider with actual test
    // entities.
    $expected_groups = array_map(function ($item) use ($test_entities) {
      return $test_entities[$item];
    }, $expected_groups);
  }

  // Add expectations for groups that should be added or removed.

  /** @var \Drupal\og\OgResolvedGroupCollectionInterface|\Prophecy\Prophecy\ObjectProphecy $collection */
  $collection = $this
    ->prophesize(OgResolvedGroupCollectionInterface::class);
  foreach ($expected_added_groups as $expected_added_group) {
    $collection
      ->addGroup($expected_added_group, [
      'route',
    ])
      ->shouldBeCalled();
  }
  foreach ($expected_removed_groups as $expected_removed_group) {
    $collection
      ->removeGroup($expected_removed_group)
      ->shouldBeCalled();
  }

  // Set expectations for when NO groups should be added or removed.
  if (empty($expected_added_groups)) {
    $collection
      ->addGroup()
      ->shouldNotBeCalled();
  }
  if (empty($expected_removed_groups)) {
    $collection
      ->removeGroup()
      ->shouldNotBeCalled();
  }

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