FactoryTest.php in Drupal 10
File
core/tests/Drupal/KernelTests/Core/Plugin/FactoryTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Plugin;
use Drupal\Component\Plugin\Exception\ExceptionInterface;
class FactoryTest extends PluginTestBase {
protected static $modules = [
'node',
'user',
];
public function testDefaultFactory() {
$plugin = $this->testPluginManager
->createInstance('user_login', [
'title' => 'Please enter your login name and password',
]);
$this
->assertSame('Drupal\\plugin_test\\Plugin\\plugin_test\\mock_block\\MockUserLoginBlock', get_class($plugin), 'Correct plugin class instantiated with default factory.');
$this
->assertSame('Please enter your login name and password', $plugin
->getTitle(), 'Plugin instance correctly configured.');
try {
$this->testPluginManager
->createInstance('non_existing');
$this
->fail('Drupal\\Component\\Plugin\\Exception\\ExceptionInterface expected');
} catch (\Exception $e) {
$this
->assertInstanceOf(ExceptionInterface::class, $e);
}
}
public function testReflectionFactory() {
$plugin = $this->mockBlockManager
->createInstance('user_login', [
'title' => 'Please enter your login name and password',
]);
$this
->assertSame('Drupal\\plugin_test\\Plugin\\plugin_test\\mock_block\\MockUserLoginBlock', get_class($plugin), 'Correct plugin class instantiated.');
$this
->assertSame('Please enter your login name and password', $plugin
->getTitle(), 'Plugin instance correctly configured.');
$plugin = $this->mockBlockManager
->createInstance('menu:main_menu', [
'depth' => 2,
]);
$this
->assertSame('<ul><li>1<ul><li>1.1</li></ul></li></ul>', $plugin
->getContent(), 'Derived plugin instance correctly instantiated and configured.');
foreach ([
'non_existing',
'menu:non_existing',
'menu',
] as $invalid_id) {
try {
$this->mockBlockManager
->createInstance($invalid_id);
$this
->fail('Drupal\\Component\\Plugin\\Exception\\ExceptionInterface expected');
} catch (\Exception $e) {
$this
->assertInstanceOf(ExceptionInterface::class, $e);
}
}
}
}
Classes
Name |
Description |
FactoryTest |
Tests that plugins are correctly instantiated. |