You are here

class DefaultPluginManagerTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php \Drupal\Tests\Core\Plugin\DefaultPluginManagerTest

Tests the DefaultPluginManager.

@group Plugin

@coversDefaultClass \Drupal\Core\Plugin\DefaultPluginManager

Hierarchy

Expanded class hierarchy of DefaultPluginManagerTest

File

core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php, line 19
Contains \Drupal\Tests\Core\Plugin\DefaultPluginManagerTest.

Namespace

Drupal\Tests\Core\Plugin
View source
class DefaultPluginManagerTest extends UnitTestCase {

  /**
   * The expected plugin definitions.
   *
   * @var array
   */
  protected $expectedDefinitions;

  /**
   * The namespaces to look for plugin definitions.
   *
   * @var \Traversable
   */
  protected $namespaces;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->expectedDefinitions = array(
      'apple' => array(
        'id' => 'apple',
        'label' => 'Apple',
        'color' => 'green',
        'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Apple',
      ),
      'banana' => array(
        'id' => 'banana',
        'label' => 'Banana',
        'color' => 'yellow',
        'uses' => array(
          'bread' => 'Banana bread',
        ),
        'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Banana',
      ),
    );
    $this->namespaces = new \ArrayObject();
    $this->namespaces['Drupal\\plugin_test'] = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
  }

  /**
   * Tests the plugin manager with a disabled module.
   */
  public function testDefaultPluginManagerWithDisabledModule() {
    $definitions = $this->expectedDefinitions;
    $definitions['cherry'] = array(
      'id' => 'cherry',
      'label' => 'Cherry',
      'color' => 'red',
      'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Cherry',
      'provider' => 'disabled_module',
    );
    $module_handler = $this
      ->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $module_handler
      ->expects($this
      ->once())
      ->method('moduleExists')
      ->with('disabled_module')
      ->will($this
      ->returnValue(FALSE));
    $plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler, 'test_alter_hook', '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    $this
      ->assertEmpty($plugin_manager
      ->getDefinition('cherry', FALSE), 'Plugin information of a disabled module is not available');
  }

  /**
   * Tests the plugin manager and object plugin definitions.
   */
  public function testDefaultPluginManagerWithObjects() {
    $definitions = $this->expectedDefinitions;
    $definitions['cherry'] = (object) array(
      'id' => 'cherry',
      'label' => 'Cherry',
      'color' => 'red',
      'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Cherry',
      'provider' => 'disabled_module',
    );
    $module_handler = $this
      ->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $module_handler
      ->expects($this
      ->once())
      ->method('moduleExists')
      ->with('disabled_module')
      ->will($this
      ->returnValue(FALSE));
    $plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler, 'test_alter_hook', '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    $this
      ->assertEmpty($plugin_manager
      ->getDefinition('cherry', FALSE), 'Plugin information is available');
  }

  /**
   * Tests the plugin manager with no cache and altering.
   */
  public function testDefaultPluginManager() {
    $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    $this
      ->assertEquals($this->expectedDefinitions, $plugin_manager
      ->getDefinitions());
    $this
      ->assertEquals($this->expectedDefinitions['banana'], $plugin_manager
      ->getDefinition('banana'));
  }

  /**
   * Tests the plugin manager with no cache and altering.
   */
  public function testDefaultPluginManagerWithAlter() {
    $module_handler = $this
      ->getMockBuilder('Drupal\\Core\\Extension\\ModuleHandler')
      ->disableOriginalConstructor()
      ->getMock();

    // Configure the stub.
    $alter_hook_name = $this
      ->randomMachineName();
    $module_handler
      ->expects($this
      ->once())
      ->method('alter')
      ->with($this
      ->equalTo($alter_hook_name), $this
      ->equalTo($this->expectedDefinitions));
    $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, $alter_hook_name, '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    $this
      ->assertEquals($this->expectedDefinitions, $plugin_manager
      ->getDefinitions());
    $this
      ->assertEquals($this->expectedDefinitions['banana'], $plugin_manager
      ->getDefinition('banana'));
  }

  /**
   * Tests the plugin manager with caching and altering.
   */
  public function testDefaultPluginManagerWithEmptyCache() {
    $cid = $this
      ->randomMachineName();
    $cache_backend = $this
      ->getMockBuilder('Drupal\\Core\\Cache\\MemoryBackend')
      ->disableOriginalConstructor()
      ->getMock();
    $cache_backend
      ->expects($this
      ->once())
      ->method('get')
      ->with($cid)
      ->will($this
      ->returnValue(FALSE));
    $cache_backend
      ->expects($this
      ->once())
      ->method('set')
      ->with($cid, $this->expectedDefinitions);
    $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    $plugin_manager
      ->setCacheBackend($cache_backend, $cid);
    $this
      ->assertEquals($this->expectedDefinitions, $plugin_manager
      ->getDefinitions());
    $this
      ->assertEquals($this->expectedDefinitions['banana'], $plugin_manager
      ->getDefinition('banana'));
  }

  /**
   * Tests the plugin manager with caching and altering.
   */
  public function testDefaultPluginManagerWithFilledCache() {
    $cid = $this
      ->randomMachineName();
    $cache_backend = $this
      ->getMockBuilder('Drupal\\Core\\Cache\\MemoryBackend')
      ->disableOriginalConstructor()
      ->getMock();
    $cache_backend
      ->expects($this
      ->once())
      ->method('get')
      ->with($cid)
      ->will($this
      ->returnValue((object) array(
      'data' => $this->expectedDefinitions,
    )));
    $cache_backend
      ->expects($this
      ->never())
      ->method('set');
    $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    $plugin_manager
      ->setCacheBackend($cache_backend, $cid);
    $this
      ->assertEquals($this->expectedDefinitions, $plugin_manager
      ->getDefinitions());
  }

  /**
   * Tests the plugin manager with caching disabled.
   */
  public function testDefaultPluginManagerNoCache() {
    $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    $cid = $this
      ->randomMachineName();
    $cache_backend = $this
      ->getMockBuilder('Drupal\\Core\\Cache\\MemoryBackend')
      ->disableOriginalConstructor()
      ->getMock();
    $cache_backend
      ->expects($this
      ->never())
      ->method('get');
    $cache_backend
      ->expects($this
      ->never())
      ->method('set');
    $plugin_manager
      ->setCacheBackend($cache_backend, $cid);
    $plugin_manager
      ->useCaches(FALSE);
    $this
      ->assertEquals($this->expectedDefinitions, $plugin_manager
      ->getDefinitions());
    $this
      ->assertEquals($this->expectedDefinitions['banana'], $plugin_manager
      ->getDefinition('banana'));
  }

  /**
   * Tests the plugin manager cache clear with tags.
   */
  public function testCacheClearWithTags() {
    $cid = $this
      ->randomMachineName();
    $cache_backend = $this
      ->getMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $cache_tags_invalidator = $this
      ->getMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
    $cache_tags_invalidator
      ->expects($this
      ->once())
      ->method('invalidateTags')
      ->with(array(
      'tag',
    ));
    $cache_backend
      ->expects($this
      ->never())
      ->method('deleteMultiple');
    $this
      ->getContainerWithCacheTagsInvalidator($cache_tags_invalidator);
    $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    $plugin_manager
      ->setCacheBackend($cache_backend, $cid, array(
      'tag',
    ));
    $plugin_manager
      ->clearCachedDefinitions();
  }

  /**
   * Tests plugins with the proper interface.
   *
   * @covers ::createInstance
   */
  public function testCreateInstanceWithJustValidInterfaces() {
    $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    foreach ($this->expectedDefinitions as $plugin_id => $definition) {
      $this
        ->assertNotNull($plugin_manager
        ->createInstance($plugin_id));
    }
  }

  /**
   * Tests plugins without the proper interface.
   *
   * @covers ::createInstance
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   * @expectedExceptionMessage Plugin "kale" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface \Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface
   */
  public function testCreateInstanceWithInvalidInterfaces() {
    $module_handler = $this
      ->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $module_handler
      ->expects($this
      ->any())
      ->method('moduleExists')
      ->with('plugin_test')
      ->willReturn(TRUE);
    $this->expectedDefinitions['kale'] = array(
      'id' => 'kale',
      'label' => 'Kale',
      'color' => 'green',
      'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Kale',
      'provider' => 'plugin_test',
    );
    $this->expectedDefinitions['apple']['provider'] = 'plugin_test';
    $this->expectedDefinitions['banana']['provider'] = 'plugin_test';
    $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, NULL, '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\FruitInterface');
    $plugin_manager
      ->createInstance('kale');
  }

  /**
   * Tests plugins without a required interface.
   *
   * @covers ::getDefinitions
   */
  public function testGetDefinitionsWithoutRequiredInterface() {
    $module_handler = $this
      ->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $module_handler
      ->expects($this
      ->any())
      ->method('moduleExists')
      ->with('plugin_test')
      ->willReturn(FALSE);
    $this->expectedDefinitions['kale'] = array(
      'id' => 'kale',
      'label' => 'Kale',
      'color' => 'green',
      'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Kale',
      'provider' => 'plugin_test',
    );
    $this->expectedDefinitions['apple']['provider'] = 'plugin_test';
    $this->expectedDefinitions['banana']['provider'] = 'plugin_test';
    $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, NULL);
    $this
      ->assertInternalType('array', $plugin_manager
      ->getDefinitions());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultPluginManagerTest::$expectedDefinitions protected property The expected plugin definitions.
DefaultPluginManagerTest::$namespaces protected property The namespaces to look for plugin definitions.
DefaultPluginManagerTest::setUp protected function Overrides UnitTestCase::setUp
DefaultPluginManagerTest::testCacheClearWithTags public function Tests the plugin manager cache clear with tags.
DefaultPluginManagerTest::testCreateInstanceWithInvalidInterfaces public function Tests plugins without the proper interface.
DefaultPluginManagerTest::testCreateInstanceWithJustValidInterfaces public function Tests plugins with the proper interface.
DefaultPluginManagerTest::testDefaultPluginManager public function Tests the plugin manager with no cache and altering.
DefaultPluginManagerTest::testDefaultPluginManagerNoCache public function Tests the plugin manager with caching disabled.
DefaultPluginManagerTest::testDefaultPluginManagerWithAlter public function Tests the plugin manager with no cache and altering.
DefaultPluginManagerTest::testDefaultPluginManagerWithDisabledModule public function Tests the plugin manager with a disabled module.
DefaultPluginManagerTest::testDefaultPluginManagerWithEmptyCache public function Tests the plugin manager with caching and altering.
DefaultPluginManagerTest::testDefaultPluginManagerWithFilledCache public function Tests the plugin manager with caching and altering.
DefaultPluginManagerTest::testDefaultPluginManagerWithObjects public function Tests the plugin manager and object plugin definitions.
DefaultPluginManagerTest::testGetDefinitionsWithoutRequiredInterface public function Tests plugins without a required interface.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.