class BlockManagerTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php \Drupal\Tests\Core\Block\BlockManagerTest
- 8.9.x core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php \Drupal\Tests\Core\Block\BlockManagerTest
- 10 core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php \Drupal\Tests\Core\Block\BlockManagerTest
@coversDefaultClass \Drupal\Core\Block\BlockManager
@group block
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Block\BlockManagerTest extends \Drupal\Tests\UnitTestCase uses \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of BlockManagerTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Block/ BlockManagerTest.php, line 23
Namespace
Drupal\Tests\Core\BlockView source
class BlockManagerTest extends UnitTestCase {
use StringTranslationTrait;
/**
* The block manager under test.
*
* @var \Drupal\Core\Block\BlockManager
*/
protected $blockManager;
/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* {@inheritdoc}
*/
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);
// Specify the 'broken' block, as well as 3 other blocks with admin labels
// that are purposefully not in alphabetical order.
$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'),
],
]);
// Force the discovery object onto the block manager.
$property = new \ReflectionProperty(BlockManager::class, 'discovery');
$property->setValue($this->blockManager, $discovery->reveal());
}
/**
* @covers ::getDefinitions
*/
public function testDefinitions() : void {
$definitions = $this->blockManager
->getDefinitions();
$this->assertSame([
'broken',
'block1',
'block2',
'block3',
], array_keys($definitions));
}
/**
* @covers ::getSortedDefinitions
*/
public function testSortedDefinitions() : void {
$definitions = $this->blockManager
->getSortedDefinitions();
$this->assertSame([
'block2',
'block3',
'block1',
], array_keys($definitions));
}
/**
* @covers ::getGroupedDefinitions
*/
public function testGroupedDefinitions() : void {
$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']));
}
/**
* @covers ::handlePluginNotFound
*/
public function testHandlePluginNotFound() : void {
$this->logger
->warning('The "%plugin_id" block plugin was not found', [
'%plugin_id' => 'invalid',
])
->shouldBeCalled();
$plugin = $this->blockManager
->createInstance('invalid');
$this->assertSame('broken', $plugin->getPluginId());
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
BlockManagerTest::$blockManager | protected | property | The block manager under test. | ||
BlockManagerTest::$logger | protected | property | The logger. | ||
BlockManagerTest::setUp | protected | function | Overrides UnitTestCase::setUp | ||
BlockManagerTest::testDefinitions | public | function | @covers ::getDefinitions | ||
BlockManagerTest::testGroupedDefinitions | public | function | @covers ::getGroupedDefinitions | ||
BlockManagerTest::testHandlePluginNotFound | public | function | @covers ::handlePluginNotFound | ||
BlockManagerTest::testSortedDefinitions | public | function | @covers ::getSortedDefinitions | ||
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | ||
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | ||
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | ||
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | ||
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. | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | ||
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::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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.