You are here

class BlockManagerTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php \Drupal\Tests\Core\Block\BlockManagerTest
  2. 10 core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php \Drupal\Tests\Core\Block\BlockManagerTest

@coversDefaultClass \Drupal\Core\Block\BlockManager

@group block

Hierarchy

Expanded class hierarchy of BlockManagerTest

File

core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php, line 18

Namespace

Drupal\Tests\Core\Block
View source
class BlockManagerTest extends UnitTestCase {

  /**
   * The block manager under test.
   *
   * @var \Drupal\Core\Block\BlockManager
   */
  protected $blockManager;

  /**
   * The logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $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' => 'Broken/Missing',
        'category' => 'Block',
        'class' => Broken::class,
        'provider' => 'core',
      ],
      'block1' => [
        'admin_label' => 'Coconut',
        'category' => 'Group 2',
      ],
      'block2' => [
        'admin_label' => 'Apple',
        'category' => 'Group 1',
      ],
      'block3' => [
        'admin_label' => 'Banana',
        'category' => 'Group 2',
      ],
    ]);

    // Force the discovery object onto the block manager.
    $property = new \ReflectionProperty(BlockManager::class, 'discovery');
    $property
      ->setAccessible(TRUE);
    $property
      ->setValue($this->blockManager, $discovery
      ->reveal());
  }

  /**
   * @covers ::getDefinitions
   */
  public function testDefinitions() {
    $definitions = $this->blockManager
      ->getDefinitions();
    $this
      ->assertSame([
      'broken',
      'block1',
      'block2',
      'block3',
    ], array_keys($definitions));
  }

  /**
   * @covers ::getSortedDefinitions
   */
  public function testSortedDefinitions() {
    $definitions = $this->blockManager
      ->getSortedDefinitions();
    $this
      ->assertSame([
      'block2',
      'block3',
      'block1',
    ], array_keys($definitions));
  }

  /**
   * @covers ::getGroupedDefinitions
   */
  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']));
  }

  /**
   * @covers ::handlePluginNotFound
   */
  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());
  }

}

Members

Namesort descending Modifiers Type Description 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
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::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.