class FilteredPluginManagerTraitTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php \Drupal\Tests\Core\Plugin\FilteredPluginManagerTraitTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php \Drupal\Tests\Core\Plugin\FilteredPluginManagerTraitTest
  3. 10 core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php \Drupal\Tests\Core\Plugin\FilteredPluginManagerTraitTest

@coversDefaultClass \Drupal\Core\Plugin\FilteredPluginManagerTrait
@group Plugin

Hierarchy

Expanded class hierarchy of FilteredPluginManagerTraitTest

File

core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php, line 19

Namespace

Drupal\Tests\Core\Plugin
View source
class FilteredPluginManagerTraitTest extends UnitTestCase {
  
  /**
   * @covers ::getFilteredDefinitions
   * @dataProvider providerTestGetFilteredDefinitions
   */
  public function testGetFilteredDefinitions($contexts, $expected) : void {
    // Start with two plugins.
    $definitions = [];
    $definitions['plugin1'] = [
      'id' => 'plugin1',
    ];
    $definitions['plugin2'] = [
      'id' => 'plugin2',
    ];
    $type = 'the_type';
    $consumer = 'the_consumer';
    $extra = [
      'foo' => 'bar',
    ];
    $context_handler = $this->prophesize(ContextHandlerInterface::class);
    // Remove the second plugin when context1 is provided.
    $context_handler->filterPluginDefinitionsByContexts([
      'context1' => 'fake context',
    ], $definitions)
      ->willReturn([
      'plugin1' => $definitions['plugin1'],
    ]);
    // Remove the first plugin when no contexts are provided.
    $context_handler->filterPluginDefinitionsByContexts([], $definitions)
      ->willReturn([
      'plugin2' => $definitions['plugin2'],
    ]);
    // After context filtering, the alter hook will be invoked.
    $module_handler = $this->prophesize(ModuleHandlerInterface::class);
    $hooks = [
      "plugin_filter_{$type}",
      "plugin_filter_{$type}__{$consumer}",
    ];
    $module_handler->alter($hooks, $expected, $extra, $consumer)
      ->shouldBeCalled();
    $theme_manager = $this->prophesize(ThemeManagerInterface::class);
    $theme_manager->alter($hooks, $expected, $extra, $consumer)
      ->shouldBeCalled();
    $plugin_manager = new TestFilteredPluginManager($definitions, $module_handler->reveal(), $theme_manager->reveal(), $context_handler->reveal());
    $result = $plugin_manager->getFilteredDefinitions($consumer, $contexts, $extra);
    $this->assertSame($expected, $result);
  }
  
  /**
   * Provides test data for ::testGetFilteredDefinitions().
   */
  public static function providerTestGetFilteredDefinitions() {
    $data = [];
    $data['populated context'] = [
      [
        'context1' => 'fake context',
      ],
      [
        'plugin1' => [
          'id' => 'plugin1',
        ],
      ],
    ];
    $data['empty context'] = [
      [],
      [
        'plugin2' => [
          'id' => 'plugin2',
        ],
      ],
    ];
    $data['null context'] = [
      NULL,
      [
        'plugin1' => [
          'id' => 'plugin1',
        ],
        'plugin2' => [
          'id' => 'plugin2',
        ],
      ],
    ];
    return $data;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
FilteredPluginManagerTraitTest::providerTestGetFilteredDefinitions public static function Provides test data for ::testGetFilteredDefinitions().
FilteredPluginManagerTraitTest::testGetFilteredDefinitions public function @covers ::getFilteredDefinitions[[api-linebreak]]
@dataProvider providerTestGetFilteredDefinitions
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 375
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.