You are here

class UserGroupAccessResolverTest in Organic groups 8

Tests the UserGroupAccessResolver plugin.

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

Hierarchy

Expanded class hierarchy of UserGroupAccessResolverTest

File

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

Namespace

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

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

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

  /**
   * {@inheritdoc}
   *
   * @param array $previously_added_groups
   *   An array of test entity IDs that were added to the collection by plugins
   *   that ran previously.
   * @param array $expected_added_groups
   *   An array of groups that are expected to be added by the plugin. If left
   *   empty it is expected that the plugin will not add any group to the
   *   collection.
   * @param array $expected_removed_groups
   *   An array of groups that are expected to be removed by the plugin. If left
   *   empty it is expected that the plugin will not remove any group from the
   *   collection.
   *
   * @covers ::resolve
   * @dataProvider resolveProvider
   */
  public function testResolve(array $previously_added_groups = [], array $expected_added_groups = [], array $expected_removed_groups = []) {

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

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

    // It is expected that the plugin will retrieve the full set of information
    // about the groups in the collection.
    $test_entities = $this->testEntities;
    $group_info = array_map(function ($group) use ($test_entities) {
      return [
        'entity' => $test_entities[$group],
      ];
    }, $previously_added_groups);
    $collection
      ->getGroupInfo()
      ->willReturn($group_info)
      ->shouldBeCalled();

    // Add expectations for groups that should be added or removed.
    foreach ($expected_added_groups as $expected_added_group) {
      $collection
        ->addGroup($test_entities[$expected_added_group], [
        'user',
      ])
        ->shouldBeCalled();
    }
    foreach ($expected_removed_groups as $expected_removed_group) {
      $collection
        ->removeGroup($test_entities[$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());
  }

  /**
   * {@inheritdoc}
   */
  protected function createMockedEntity($id, array $properties) {
    $entity = parent::createMockedEntity($id, $properties);

    // Mock the expected result of an access check on the entity.
    $entity
      ->access('view')
      ->willReturn($properties['current_user_has_access']);
    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  protected function getTestEntityProperties() {
    return [
      // Some test groups to which the current user has access.
      'group-access-0' => [
        'type' => 'node',
        'bundle' => 'group',
        'group' => TRUE,
        'current_user_has_access' => TRUE,
      ],
      'group-access-1' => [
        'type' => 'taxonomy_term',
        'bundle' => 'assembly',
        'group' => TRUE,
        'current_user_has_access' => TRUE,
      ],
      // Some test groups to which the current user does not have access.
      'group-noaccess-0' => [
        'type' => 'entity_test',
        'bundle' => 'cluster',
        'group' => TRUE,
        'current_user_has_access' => FALSE,
      ],
      'group-noaccess-1' => [
        'type' => 'block_content',
        'bundle' => 'flock',
        'group' => TRUE,
        'current_user_has_access' => FALSE,
      ],
    ];
  }

  /**
   * Data provider for testResolve().
   *
   * @see ::testResolve()
   */
  public function resolveProvider() {
    return [
      // Test that the groups to which the user does not have access are removed
      // from a collection that has both accessible and non-accessible groups.
      // The accessible groups should get a vote added, so that the 'user' cache
      // context is correctly set on it.
      [
        // We start with a collection that has a mix of accessible and non-
        // accessible groups.
        [
          'group-access-0',
          'group-access-1',
          'group-noaccess-0',
          'group-noaccess-1',
        ],
        // A vote should be added to the accessible groups.
        [
          'group-access-0',
          'group-access-1',
        ],
        // The non-accessible groups should be removed.
        [
          'group-noaccess-0',
          'group-noaccess-1',
        ],
      ],
      // Test that no groups are removed when the collection does not contain
      // any non-accessible groups.
      [
        [
          'group-access-0',
          'group-access-1',
        ],
        [
          'group-access-0',
          'group-access-1',
        ],
        [],
      ],
      // Test that no votes are added when the collection does not contain any
      // accessible groups. The non-accessible ones should be removed.
      [
        [
          'group-noaccess-0',
          'group-noaccess-1',
        ],
        [],
        [
          'group-noaccess-0',
          'group-noaccess-1',
        ],
      ],
      // Test that nothing happens on an empty collection.
      [
        [],
        [],
        [],
      ],
    ];
  }

}

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::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::setUp protected function Overrides UnitTestCase::setUp 2
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.
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.
UserGroupAccessResolverTest::$className protected property The fully qualified class name of the plugin under test. Overrides OgGroupResolverTestBase::$className
UserGroupAccessResolverTest::$pluginId protected property The ID of the plugin under test. Overrides OgGroupResolverTestBase::$pluginId
UserGroupAccessResolverTest::createMockedEntity protected function Creates a mocked content entity to use in the test. Overrides OgGroupResolverTestBase::createMockedEntity
UserGroupAccessResolverTest::getTestEntityProperties protected function Returns properties used to create mock test entities. Overrides OgGroupResolverTestBase::getTestEntityProperties
UserGroupAccessResolverTest::resolveProvider public function Data provider for testResolve().
UserGroupAccessResolverTest::testResolve public function @covers ::resolve @dataProvider resolveProvider Overrides OgGroupResolverTestBase::testResolve