You are here

abstract class OgGroupResolverTestBase in Organic groups 8

Base class for testing OgGroupResolver plugins.

@group og

Hierarchy

Expanded class hierarchy of OgGroupResolverTestBase

File

tests/src/Unit/Plugin/OgGroupResolver/OgGroupResolverTestBase.php, line 19

Namespace

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

  /**
   * The fully qualified class name of the plugin under test.
   *
   * @var string
   */
  protected $className;

  /**
   * The ID of the plugin under test.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * Mocked test entities.
   *
   * @var \Drupal\Core\Entity\ContentEntityInterface[]
   */
  protected $testEntities;

  /**
   * The mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $entityTypeManager;

  /**
   * The OG group audience helper.
   *
   * @var \Drupal\og\OgGroupAudienceHelperInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $groupAudienceHelper;

  /**
   * The mocked OG group type manager.
   *
   * @var \Drupal\og\GroupTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $groupTypeManager;

  /**
   * The OG membership manager.
   *
   * @var \Drupal\og\MembershipManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $membershipManager;

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

    // Instantiate mocks of the classes that the plugins rely on.
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->groupAudienceHelper = $this
      ->prophesize(OgGroupAudienceHelperInterface::class);
    $this->groupTypeManager = $this
      ->prophesize(GroupTypeManagerInterface::class);
    $this->membershipManager = $this
      ->prophesize(MembershipManagerInterface::class);

    // Create mocked test entities.

    /** @var \Drupal\Core\Entity\ContentEntityInterface[] $test_entities */
    $test_entities = [];
    foreach ($this
      ->getTestEntityProperties() as $id => $properties) {
      $entity_type_id = $properties['type'];
      $bundle_id = $properties['bundle'];
      $is_group = !empty($properties['group']);
      $is_group_content = !empty($properties['group_content']);
      $entity = $this
        ->createMockedEntity($id, $properties);
      $test_entities[$id] = $entity
        ->reveal();

      // It is not being tight lipped about whether it is a group or group
      // content.
      $this->groupTypeManager
        ->isGroup($entity_type_id, $bundle_id)
        ->willReturn($is_group);
      $this->groupAudienceHelper
        ->hasGroupAudienceField($entity_type_id, $bundle_id)
        ->willReturn($is_group_content);

      // If the entity is group content it will spill the beans on which groups
      // it belongs to.
      if ($is_group_content) {
        $groups = [];
        foreach ($properties['group_content'] as $group_id) {
          $group = $test_entities[$group_id];
          $groups[$group
            ->getEntityTypeId()][$group
            ->id()] = $group;
        }
        $this->membershipManager
          ->getGroups($entity)
          ->willReturn($groups);
      }
    }
    $this->testEntities = $test_entities;
  }

  /**
   * Tests the groups that are resolved by the plugin.
   *
   * @dataProvider resolveProvider
   * @covers ::resolve()
   */
  public abstract function testResolve();

  /**
   * Tests if the plugin is able to stop the group resolving process.
   *
   * @covers ::isPropagationStopped
   * @covers ::stopPropagation
   */
  public function testStopPropagation() {
    $plugin = $this
      ->getPluginInstance();

    // Initially propagation should not be stopped.
    $this
      ->assertFalse($plugin
      ->isPropagationStopped());

    // Test if propagation can be stopped.
    $plugin
      ->stopPropagation();
    $this
      ->assertTrue($plugin
      ->isPropagationStopped());
  }

  /**
   * Returns properties used to create mock test entities.
   *
   * This is used to facilitate referring to entities in data providers. Since a
   * data provider is called before the test setup runs, we cannot return actual
   * entities in the data provider. Instead the data provider can refer to these
   * test entities by ID, and the actual entity mocks will be generated in the
   * test setup.
   *
   * The test groups should be declared first, the group content last.
   *
   * @return array
   *   An array of entity metadata, keyed by test entity ID. Each item is an
   *   array with the following keys:
   *   - type (required): The entity type ID.
   *   - bundle (required): The entity bundle.
   *   - group (optional): Whether or not the entity is a group.
   *   - group_content (optional): An array containing IDs of groups this group
   *     content belongs to.
   */
  protected abstract function getTestEntityProperties();

  /**
   * Returns an instance of the plugin under test.
   *
   * @return \Drupal\og\OgGroupResolverInterface
   *   The plugin under test.
   */
  protected function getPluginInstance() {
    $args = array_merge([
      [],
      $this->pluginId,
      [
        'id' => $this->pluginId,
        'class' => $this->className,
        'provider' => 'og',
      ],
    ], $this
      ->getInjectedDependencies());
    return new $this->className(...$args);
  }

  /**
   * Returns the mocked classes that the plugin depends on.
   *
   * @return array
   *   The mocked dependencies.
   */
  protected function getInjectedDependencies() {
    return [];
  }

  /**
   * Creates a mocked content entity to use in the test.
   *
   * @param string $id
   *   The entity ID to assign to the mocked entity.
   * @param array $properties
   *   An associative array of properties to assign to the mocked entity, with
   *   the following keys:
   *   - type: The entity type.
   *   - bundle: The entity bundle.
   *
   * @return \Drupal\Core\Entity\ContentEntityInterface|\Prophecy\Prophecy\ObjectProphecy
   *   The mocked entity.
   */
  protected function createMockedEntity($id, array $properties) {

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

    // In case this entity is questioned about its identity, it shall
    // willingly pony up the requested information.
    $entity
      ->id()
      ->willReturn($id);
    $entity
      ->getEntityTypeId()
      ->willReturn($properties['type']);
    $entity
      ->bundle()
      ->willReturn($properties['bundle']);
    return $entity;
  }

}

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::setUp protected function Overrides UnitTestCase::setUp 2
OgGroupResolverTestBase::testResolve abstract public function Tests the groups that are resolved by the plugin. 3
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.