Same filename and directory in other branches
- 8.9.x core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php
- 9 core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php
Namespace
Drupal\Tests\Core\Block
File
core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\Core\Block;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\Block\BlockManager;
use Drupal\Core\Block\Plugin\Block\Broken;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Psr\Log\LoggerInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class BlockManagerTest extends UnitTestCase {
use StringTranslationTrait;
protected $blockManager;
protected $logger;
protected function setUp() : void {
parent::setUp();
$container = new ContainerBuilder();
$current_user = $this
->prophesize(AccountInterface::class);
$container
->set('current_user', $current_user
->reveal());
$container
->set('string_translation', $this
->getStringTranslationStub());
\Drupal::setContainer($container);
$cache_backend = $this
->prophesize(CacheBackendInterface::class);
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$this->logger = $this
->prophesize(LoggerInterface::class);
$this->blockManager = new BlockManager(new \ArrayObject(), $cache_backend
->reveal(), $module_handler
->reveal(), $this->logger
->reveal());
$this->blockManager
->setStringTranslation($this
->getStringTranslationStub());
$discovery = $this
->prophesize(DiscoveryInterface::class);
$discovery
->getDefinitions()
->willReturn([
'broken' => [
'admin_label' => $this
->t('Broken/Missing'),
'category' => $this
->t('Block'),
'class' => Broken::class,
'provider' => 'core',
],
'block1' => [
'admin_label' => $this
->t('Coconut'),
'category' => $this
->t('Group 2'),
],
'block2' => [
'admin_label' => $this
->t('Apple'),
'category' => $this
->t('Group 1'),
],
'block3' => [
'admin_label' => $this
->t('Banana'),
'category' => $this
->t('Group 2'),
],
]);
$property = new \ReflectionProperty(BlockManager::class, 'discovery');
$property
->setValue($this->blockManager, $discovery
->reveal());
}
public function testDefinitions() {
$definitions = $this->blockManager
->getDefinitions();
$this
->assertSame([
'broken',
'block1',
'block2',
'block3',
], array_keys($definitions));
}
public function testSortedDefinitions() {
$definitions = $this->blockManager
->getSortedDefinitions();
$this
->assertSame([
'block2',
'block3',
'block1',
], array_keys($definitions));
}
public function testGroupedDefinitions() {
$definitions = $this->blockManager
->getGroupedDefinitions();
$this
->assertSame([
'Group 1',
'Group 2',
], array_keys($definitions));
$this
->assertSame([
'block2',
], array_keys($definitions['Group 1']));
$this
->assertSame([
'block3',
'block1',
], array_keys($definitions['Group 2']));
}
public function testHandlePluginNotFound() {
$this->logger
->warning('The "%plugin_id" was not found', [
'%plugin_id' => 'invalid',
])
->shouldBeCalled();
$plugin = $this->blockManager
->createInstance('invalid');
$this
->assertSame('broken', $plugin
->getPluginId());
}
}
Classes