You are here

class GroupContentEnablerManagerTest in Group 8

Tests the GroupContentEnabler plugin manager.

@coversDefaultClass \Drupal\group\Plugin\GroupContentEnablerManager @group group

Hierarchy

Expanded class hierarchy of GroupContentEnablerManagerTest

File

tests/src/Unit/GroupContentEnablerManagerTest.php, line 25

Namespace

Drupal\Tests\group\Unit
View source
class GroupContentEnablerManagerTest extends UnitTestCase {

  /**
   * The group content enabler manager under test.
   *
   * @var \Drupal\group\Plugin\GroupContentEnablerManager
   */
  protected $groupContentEnablerManager;

  /**
   * The service container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $container;

  /**
   * The plugin discovery.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $discovery;

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $cacheBackend;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $moduleHandler;

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->cacheBackend = $this
      ->prophesize(CacheBackendInterface::class);
    $this->moduleHandler = $this
      ->prophesize(ModuleHandlerInterface::class);
    $this->moduleHandler
      ->getImplementations('entity_type_build')
      ->willReturn([]);
    $this->moduleHandler
      ->alter('group_content_info', Argument::type('array'))
      ->willReturn(NULL);
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $storage = $this
      ->prophesize(ContentEntityStorageInterface::class);
    $this->entityTypeManager
      ->getStorage('group')
      ->willReturn($storage
      ->reveal());
    $storage = $this
      ->prophesize(ConfigEntityStorageInterface::class);
    $this->entityTypeManager
      ->getStorage('group_type')
      ->willReturn($storage
      ->reveal());
    $this->groupContentEnablerManager = new TestGroupContentEnablerManager(new \ArrayObject(), $this->cacheBackend
      ->reveal(), $this->moduleHandler
      ->reveal(), $this->entityTypeManager
      ->reveal());
    $this->discovery = $this
      ->prophesize(DiscoveryInterface::class);
    $this->groupContentEnablerManager
      ->setDiscovery($this->discovery
      ->reveal());
    $this->container = $this
      ->prophesize(ContainerInterface::class);
    $this->groupContentEnablerManager
      ->setContainer($this->container
      ->reveal());
  }

  /**
   * Sets up the group content enabler manager to be tested.
   *
   * @param array $definitions
   *   (optional) An array of group content enabler definitions.
   */
  protected function setUpPluginDefinitions($definitions = []) {
    $this->discovery
      ->getDefinition(Argument::cetera())
      ->will(function ($args) use ($definitions) {
      $plugin_id = $args[0];
      $exception_on_invalid = $args[1];
      if (isset($definitions[$plugin_id])) {
        return $definitions[$plugin_id];
      }
      elseif (!$exception_on_invalid) {
        return NULL;
      }
      else {
        throw new PluginNotFoundException($plugin_id);
      }
    });
    $this->discovery
      ->getDefinitions()
      ->willReturn($definitions);
  }

  /**
   * Tests the hasHandler() method.
   *
   * @param string $plugin_id
   *   The ID of the plugin to check the handler for.
   * @param bool $expected
   *   Whether the handler is expected to be found.
   *
   * @covers ::hasHandler
   * @dataProvider providerTestHasHandler
   */
  public function testHasHandler($plugin_id, $expected) {
    $apple = [
      'handlers' => [
        'foo_handler' => TestGroupContentHandler::class,
      ],
    ];
    $banana = [
      'handlers' => [
        'foo_handler' => FALSE,
      ],
    ];
    $this
      ->setUpPluginDefinitions([
      'apple' => $apple,
      'banana' => $banana,
    ]);
    $this
      ->assertSame($expected, $this->groupContentEnablerManager
      ->hasHandler($plugin_id, 'foo_handler'));
  }

  /**
   * Provides test data for testHasHandler().
   *
   * @return array
   *   Test data.
   */
  public function providerTestHasHandler() {
    return [
      [
        'apple',
        TRUE,
      ],
      [
        'banana',
        FALSE,
      ],
      [
        'pear',
        FALSE,
      ],
    ];
  }

  /**
   * Tests the createHandlerInstance() method.
   *
   * @covers ::createHandlerInstance
   */
  public function testCreateHandlerInstance() {
    $handler = $this->groupContentEnablerManager
      ->createHandlerInstance(TestGroupContentHandler::class, 'some_plugin', []);
    $this
      ->assertInstanceOf(GroupContentHandlerBase::class, $handler);
    $this
      ->assertInstanceOf(ModuleHandlerInterface::class, $handler
      ->getModuleHandler());
  }

  /**
   * Tests exception thrown when a handler does not implement the interface.
   *
   * @covers ::createHandlerInstance
   */
  public function testCreateHandlerInstanceNoInterface() {
    $this
      ->expectException(InvalidPluginDefinitionException::class);
    $this
      ->expectExceptionMessage('Trying to instantiate a handler that does not implement \\Drupal\\group\\Plugin\\GroupContentHandlerInterface.');
    $this->groupContentEnablerManager
      ->createHandlerInstance(TestGroupContentHandlerWithoutInterface::class, 'some_plugin', []);
  }

  /**
   * Tests the getHandler() method.
   *
   * @covers ::getHandler
   * @depends testCreateHandlerInstance
   */
  public function testGetHandler() {
    $apple = [
      'handlers' => [
        'foo_handler' => TestGroupContentHandler::class,
      ],
    ];
    $this
      ->setUpPluginDefinitions([
      'apple' => $apple,
    ]);
    $first_call_result = $this->groupContentEnablerManager
      ->getHandler('apple', 'foo_handler');
    $second_call_result = $this->groupContentEnablerManager
      ->getHandler('apple', 'foo_handler');
    $direct_call_result = $this->groupContentEnablerManager
      ->createHandlerInstance($apple['handlers']['foo_handler'], 'apple', $apple);
    $this
      ->assertEquals($first_call_result, $direct_call_result, 'Got the same result as if createHandlerInstance() were called directly.');
    $this
      ->assertSame($first_call_result, $second_call_result, 'Got the exact same handler instance when called another time.');
    $this
      ->assertNotSame($first_call_result, $direct_call_result, 'Calling createHandlerInstance() creates a fresh copy regardless of internal cache.');
  }

  /**
   * Tests exception thrown when a plugin has not defined the requested handler.
   *
   * @covers ::getHandler
   */
  public function testGetHandlerMissingHandler() {
    $this
      ->setUpPluginDefinitions([
      'apple' => [
        'handlers' => [],
      ],
    ]);
    $this
      ->expectException(InvalidPluginDefinitionException::class);
    $this
      ->expectExceptionMessage('The "apple" plugin did not specify a foo_handler handler.');
    $this->groupContentEnablerManager
      ->getHandler('apple', 'foo_handler');
  }

  /**
   * Tests the getAccessControlHandler() method.
   *
   * @covers ::getAccessControlHandler
   */
  public function testGetAccessControlHandler() {
    $apple = [
      'handlers' => [
        'access' => TestGroupContentHandler::class,
      ],
    ];
    $this
      ->setUpPluginDefinitions([
      'apple' => $apple,
    ]);
    $this
      ->assertInstanceOf(GroupContentHandlerBase::class, $this->groupContentEnablerManager
      ->getAccessControlHandler('apple'));
  }

  /**
   * Tests the getPermissionProvider() method.
   *
   * @covers ::getPermissionProvider
   */
  public function testGetPermissionProvider() {
    $apple = [
      'handlers' => [
        'permission_provider' => TestGroupContentHandler::class,
      ],
    ];
    $this
      ->setUpPluginDefinitions([
      'apple' => $apple,
    ]);
    $this
      ->assertInstanceOf(GroupContentHandlerBase::class, $this->groupContentEnablerManager
      ->getPermissionProvider('apple'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupContentEnablerManagerTest::$cacheBackend protected property The cache backend.
GroupContentEnablerManagerTest::$container protected property The service container.
GroupContentEnablerManagerTest::$discovery protected property The plugin discovery.
GroupContentEnablerManagerTest::$entityTypeManager protected property The entity type manager.
GroupContentEnablerManagerTest::$groupContentEnablerManager protected property The group content enabler manager under test.
GroupContentEnablerManagerTest::$moduleHandler protected property The module handler.
GroupContentEnablerManagerTest::providerTestHasHandler public function Provides test data for testHasHandler().
GroupContentEnablerManagerTest::setUp protected function Overrides UnitTestCase::setUp
GroupContentEnablerManagerTest::setUpPluginDefinitions protected function Sets up the group content enabler manager to be tested.
GroupContentEnablerManagerTest::testCreateHandlerInstance public function Tests the createHandlerInstance() method.
GroupContentEnablerManagerTest::testCreateHandlerInstanceNoInterface public function Tests exception thrown when a handler does not implement the interface.
GroupContentEnablerManagerTest::testGetAccessControlHandler public function Tests the getAccessControlHandler() method.
GroupContentEnablerManagerTest::testGetHandler public function Tests the getHandler() method.
GroupContentEnablerManagerTest::testGetHandlerMissingHandler public function Tests exception thrown when a plugin has not defined the requested handler.
GroupContentEnablerManagerTest::testGetPermissionProvider public function Tests the getPermissionProvider() method.
GroupContentEnablerManagerTest::testHasHandler public function Tests the hasHandler() method.
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.