You are here

class RequestQueryArgumentResolverTest in Organic groups 8

Tests the RequestQueryArgumentResolver plugin.

@group og @coversDefaultClass \Drupal\og\Plugin\OgGroupResolver\RequestQueryArgumentResolver

Hierarchy

Expanded class hierarchy of RequestQueryArgumentResolverTest

File

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

Namespace

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

  /**
   * {@inheritdoc}
   */
  protected $className = RequestQueryArgumentResolver::class;

  /**
   * {@inheritdoc}
   */
  protected $pluginId = 'request_query_argument';

  /**
   * The mocked request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $requestStack;

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

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

  /**
   * {@inheritdoc}
   *
   * @param string $group_type
   *   The group type that is passed as a query argument.
   * @param string $group_id
   *   The group ID that is passed as a query argument.
   * @param array $previously_added_groups
   *   An array of test entity IDs that were added to the collection by plugins
   *   that ran previously.
   * @param 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.
   *
   * @covers ::resolve
   * @dataProvider resolveProvider
   */
  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());
  }

  /**
   * {@inheritdoc}
   */
  protected function getInjectedDependencies() {
    return [
      $this->requestStack
        ->reveal(),
      $this->groupTypeManager
        ->reveal(),
      $this->entityTypeManager
        ->reveal(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getTestEntityProperties() {
    return [
      // A test group.
      'group-0' => [
        'type' => 'node',
        'bundle' => 'group',
        'group' => TRUE,
      ],
      // Another test group.
      'group-1' => [
        'type' => 'taxonomy_term',
        'bundle' => 'assembly',
        'group' => TRUE,
      ],
      // A group content entity.
      'group_content' => [
        'type' => 'entity_test',
        'bundle' => 'content',
        'group_content' => [
          'group-0',
        ],
      ],
      // An entity that is not a group nor group content.
      'non_group' => [
        'type' => 'entity_test',
        'bundle' => 'non_group',
      ],
    ];
  }

  /**
   * Data provider for testResolve().
   *
   * @see ::testResolve()
   */
  public function resolveProvider() {
    return [
      // Test that no group is added on a path that does not have a query
      // argument.
      [
        // There is no query argument for the entity ID.
        NULL,
        // There is no query argument for the bundle ID.
        NULL,
        // A group was added by another plugin.
        [
          'group-0',
        ],
        // But this plugin should not add any group since there is no query
        // argument.
        NULL,
      ],
      // Test that no group is added on a path that has an invalid entity type
      // in the query arguments.
      [
        'parmigiano-reggiano',
        'group-0',
        [
          'group-0',
        ],
        NULL,
      ],
      // Test that no groups are added on a path that has a missing entity type.
      [
        NULL,
        'group-0',
        [
          'group-0',
        ],
        NULL,
      ],
      // Test that no groups are added on a path that has an invalid entity ID.
      [
        'node',
        // Group 1 is a taxonomy term, this ID is invalid for groups of type
        // node.
        'group-1',
        [
          'group-1',
        ],
        NULL,
      ],
      // Test that no groups are added on a path that has a missing entity ID.
      [
        'node',
        NULL,
        [
          'group-0',
        ],
        NULL,
      ],
      // Test that a vote can be added for a group that is present on the query
      // argument and has been previously added by another plugin.
      [
        'node',
        'group-0',
        [
          'group-0',
        ],
        'group-0',
      ],
      // Test that a vote can be added for a group of a different entity type
      // that is present on the query argument and has been previously added by
      // another plugin.
      [
        'taxonomy_term',
        'group-1',
        [
          'group-1',
        ],
        'group-1',
      ],
      // Test that a vote can be added for a group that is present on the query
      // argument and is part of multiple groups that have been added by other
      // plugins.
      [
        'node',
        'group-0',
        [
          'group-0',
          'group-1',
        ],
        'group-0',
      ],
      // Test that a vote can not be added for a group that is present on the
      // query argument but has not been previously added by another plugin. We
      // do not want users to be able to fake a group context by messing with
      // the query arguments.
      [
        'node',
        'group-0',
        [
          'group-1',
        ],
        NULL,
      ],
      // Test that a vote can not be added for a group content entity that is
      // present on the query argument.
      [
        'entity_test',
        'group_content',
        [
          'group-0',
        ],
        NULL,
      ],
      // Test that a vote can not be added for an entity that is present on the
      // query argument but is neither a group nor group content.
      [
        'entity_test',
        'non_group',
        [
          'group-0',
          'group-1',
        ],
        NULL,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::$testEntities protected property Mocked test entities.
OgGroupResolverTestBase::createMockedEntity protected function Creates a mocked content entity to use in the test. 1
OgGroupResolverTestBase::getPluginInstance protected function Returns an instance of the plugin under test.
OgGroupResolverTestBase::testStopPropagation public function Tests if the plugin is able to stop the group resolving process.
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.
RequestQueryArgumentResolverTest::$className protected property The fully qualified class name of the plugin under test. Overrides OgGroupResolverTestBase::$className
RequestQueryArgumentResolverTest::$pluginId protected property The ID of the plugin under test. Overrides OgGroupResolverTestBase::$pluginId
RequestQueryArgumentResolverTest::$requestStack protected property The mocked request stack.
RequestQueryArgumentResolverTest::getInjectedDependencies protected function Returns the mocked classes that the plugin depends on. Overrides OgGroupResolverTestBase::getInjectedDependencies
RequestQueryArgumentResolverTest::getTestEntityProperties protected function Returns properties used to create mock test entities. Overrides OgGroupResolverTestBase::getTestEntityProperties
RequestQueryArgumentResolverTest::resolveProvider public function Data provider for testResolve().
RequestQueryArgumentResolverTest::setUp protected function Overrides OgGroupResolverTestBase::setUp
RequestQueryArgumentResolverTest::testResolve public function @covers ::resolve @dataProvider resolveProvider Overrides OgGroupResolverTestBase::testResolve
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.