You are here

abstract class OgRouteGroupResolverTestBase in Organic groups 8

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

Hierarchy

Expanded class hierarchy of OgRouteGroupResolverTestBase

File

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

Namespace

Drupal\Tests\og\Unit\Plugin\OgGroupResolver
View source
abstract class OgRouteGroupResolverTestBase extends OgGroupResolverTestBase {

  /**
   * A list of link templates that belong to entity types used in the tests.
   *
   * This mocks the data returned by EntityTypeInterface::getLinkTemplates().
   *
   * @var array
   *   A list of link templates, keyed by entity type ID.
   *
   * @see \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates()
   */
  protected $linkTemplates = [
    'node' => [
      'canonical' => '/node/{node}',
      'delete-form' => '/node/{node}/delete',
      'edit-form' => '/node/{node}/edit',
      'version-history' => '/node/{node}/revisions',
      'revision' => '/node/{node}/revisions/{node_revision}/view',
    ],
    'entity_test' => [
      'canonical' => '/entity_test/{entity_test}',
      'add-form' => '/entity_test/add',
      'edit-form' => '/entity_test/manage/{entity_test}/edit',
      'delete-form' => '/entity_test/delete/entity_test/{entity_test}',
    ],
    'taxonomy_term' => [
      'canonical' => '/taxonomy/term/{taxonomy_term}',
      'delete-form' => '/taxonomy/term/{taxonomy_term}/delete',
      'edit-form' => '/taxonomy/term/{taxonomy_term}/edit',
    ],
  ];

  /**
   * The mocked route matcher.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $routeMatch;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();

    // Instantiate mocks of the classes that the plugins rely on.
    $this->routeMatch = $this
      ->prophesize(RouteMatchInterface::class);
  }

  /**
   * {@inheritdoc}
   *
   * @todo Update documentation.
   *
   * @covers ::resolve
   * @dataProvider resolveProvider
   */
  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());
  }

  /**
   * Returns a set of entity link templates for testing.
   *
   * This mimicks the data returned by EntityTypeInterface::getLinkTemplates().
   *
   * @param string $entity_type
   *   The entity type for which to return the link templates.
   *
   * @return array
   *   An array of link templates.
   *
   * @see \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates()
   */
  protected function getLinkTemplates($entity_type) {
    return $this->linkTemplates[$entity_type];
  }

  /**
   * Adds an expectation that the current path will be retrieved from the route.
   *
   * @param string $path
   *   The path that will be retrieved.
   */
  protected function willRetrieveCurrentPathFromRouteMatcher($path) {

    /** @var \Symfony\Component\Routing\Route|\Prophecy\Prophecy\ObjectProphecy $route */
    $route = $this
      ->prophesize(Route::class);
    $route
      ->getPath()
      ->willReturn($path)
      ->shouldBeCalled();
    $this->routeMatch
      ->getRouteObject()
      ->willReturn($route
      ->reveal())
      ->shouldBeCalled();
  }

  /**
   * Adds an expectation that the plugin will retrieve a list of entity paths.
   *
   * The plugin need to match the current path to this list of entity paths to
   * see if we are currently on an entity path of a group or group content
   * entity.
   * In order to retrieve the content entity paths, the plugin will have to
   * request a full list of all entity types, then request the "link templates"
   * from the content entities.
   */
  protected function willRetrieveContentEntityPaths() {

    // Provide some mocked content entity types.
    $entity_types = [];
    foreach (array_keys($this->linkTemplates) as $entity_type_id) {

      /** @var \Drupal\Core\Entity\EntityTypeInterface|\Prophecy\Prophecy\ObjectProphecy $entity_type */
      $entity_type = $this
        ->prophesize(EntityTypeInterface::class);

      // The plugin will need to know if this is a content entity, so we will
      // provide this information. We are not requiring this to be called since
      // there are other ways of determining this (e.g. `instanceof`).
      $entity_type
        ->entityClassImplements(ContentEntityInterface::class)
        ->willReturn(TRUE);

      // The plugin will need to inquire about the link templates that the
      // entity provides. This should be called.
      $entity_type
        ->getLinkTemplates()
        ->willReturn($this
        ->getLinkTemplates($entity_type_id))
        ->shouldBeCalled();
      $entity_types[$entity_type_id] = $entity_type
        ->reveal();
    }
    $this->entityTypeManager
      ->getDefinitions()
      ->willReturn($entity_types)
      ->shouldBeCalled();
  }

  /**
   * Adds an expectation that the plugin will (not) retrieve the route object.
   *
   * If the current path is an entity path, the plugin should retrieve the
   * entity from the route so it can check if the entity is a group. If we are
   * not, then it should not attempt to retrieve it.
   *
   * @param string|null $route_object_id
   *   The ID of the entity that is present on the current route, or NULL if we
   *   are not on a content entity path. The ID may be any of the ones created
   *   in the test setup, e.g. 'group', 'group_content', 'non_group'.
   */
  protected function mightRetrieveRouteObject($route_object_id) {

    // The route object should only be retrieved if we are on a content entity
    // path.
    if ($route_object_id) {
      $this->routeMatch
        ->getParameter($this
        ->getTestEntityProperties()[$route_object_id]['type'])
        ->willReturn($this->testEntities[$route_object_id]);
    }
  }

  /**
   * Adds an expectation that checks if the route object is a group.
   *
   * If the plugin found a content entity on the route then it should check
   * whether the entity is a group or not. If no content entity was found, it
   * should not perform this check.
   *
   * @param string|null $route_object_id
   *   The ID of the entity that is present on the current route, or NULL if we
   *   are not on a content entity path. The ID may be any of the ones created
   *   in the test setup, e.g. 'group', 'group_content', 'non_group'.
   */
  protected function mightCheckIfRouteObjectIsGroup($route_object_id) {
    $properties = $this
      ->getTestEntityProperties();
    $entity_type_id = $properties[$route_object_id]['type'];
    $bundle = $properties[$route_object_id]['bundle'];
    $this->groupTypeManager
      ->isGroup($entity_type_id, $bundle)
      ->willReturn(!empty($properties[$route_object_id]['group']));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgGroupResolverTestBase::$className protected property The fully qualified class name of the plugin under test. 4
OgGroupResolverTestBase::$entityTypeManager protected property The mocked entity type manager.
OgGroupResolverTestBase::$groupAudienceHelper protected property The OG group audience helper.
OgGroupResolverTestBase::$groupTypeManager protected property The mocked OG group type manager.
OgGroupResolverTestBase::$membershipManager protected property The OG membership manager.
OgGroupResolverTestBase::$pluginId protected property The ID of the plugin under test. 4
OgGroupResolverTestBase::$testEntities protected property Mocked test entities.
OgGroupResolverTestBase::createMockedEntity protected function Creates a mocked content entity to use in the test. 1
OgGroupResolverTestBase::getInjectedDependencies protected function Returns the mocked classes that the plugin depends on. 3
OgGroupResolverTestBase::getPluginInstance protected function Returns an instance of the plugin under test.
OgGroupResolverTestBase::getTestEntityProperties abstract protected function Returns properties used to create mock test entities. 4
OgGroupResolverTestBase::testStopPropagation public function Tests if the plugin is able to stop the group resolving process.
OgRouteGroupResolverTestBase::$linkTemplates protected property A list of link templates that belong to entity types used in the tests.
OgRouteGroupResolverTestBase::$routeMatch protected property The mocked route matcher.
OgRouteGroupResolverTestBase::getLinkTemplates protected function Returns a set of entity link templates for testing.
OgRouteGroupResolverTestBase::mightCheckIfRouteObjectIsGroup protected function Adds an expectation that checks if the route object is a group.
OgRouteGroupResolverTestBase::mightRetrieveRouteObject protected function Adds an expectation that the plugin will (not) retrieve the route object.
OgRouteGroupResolverTestBase::setUp protected function Overrides OgGroupResolverTestBase::setUp
OgRouteGroupResolverTestBase::testResolve public function @todo Update documentation. Overrides OgGroupResolverTestBase::testResolve
OgRouteGroupResolverTestBase::willRetrieveContentEntityPaths protected function Adds an expectation that the plugin will retrieve a list of entity paths.
OgRouteGroupResolverTestBase::willRetrieveCurrentPathFromRouteMatcher protected function Adds an expectation that the current path will be retrieved from the route.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.