class TopBarItemManagerTest

@coversDefaultClass \Drupal\navigation\TopBarItemManager

@group navigation

Hierarchy

Expanded class hierarchy of TopBarItemManagerTest

File

core/modules/navigation/tests/src/Unit/TopBarItemManagerTest.php, line 22

Namespace

Drupal\Tests\navigation\Unit
View source
class TopBarItemManagerTest extends UnitTestCase {
  use StringTranslationTrait;
  
  /**
   * The top bar item manager under test.
   *
   * @var \Drupal\navigation\TopBarItemManagerInterface
   */
  protected TopBarItemManagerInterface $manager;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $container = new ContainerBuilder();
    $container->set('string_translation', $this->getStringTranslationStub());
    \Drupal::setContainer($container);
    $cache_backend = $this->prophesize(CacheBackendInterface::class);
    $module_handler = $this->prophesize(ModuleHandlerInterface::class);
    $this->manager = new TopBarItemManager(new \ArrayObject(), $cache_backend->reveal(), $module_handler->reveal());
    $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([
      'tools' => [
        'label' => 'Tools',
        'region' => TopBarRegion::Tools,
      ],
      'context' => [
        'admin_label' => 'Context',
        'region' => TopBarRegion::Context,
      ],
      'actions' => [
        'label' => 'Actions',
        'region' => TopBarRegion::Actions,
      ],
      'more_actions' => [
        'label' => 'More Actions',
        'region' => TopBarRegion::Actions,
      ],
    ]);
    // Force the discovery object onto the block manager.
    $property = new \ReflectionProperty(TopBarItemManager::class, 'discovery');
    $property->setValue($this->manager, $discovery->reveal());
  }
  
  /**
   * @covers ::getDefinitions
   */
  public function testDefinitions() : void {
    $definitions = $this->manager
      ->getDefinitions();
    $this->assertSame([
      'tools',
      'context',
      'actions',
      'more_actions',
    ], array_keys($definitions));
  }
  
  /**
   * @covers ::getDefinitionsByRegion
   */
  public function testGetDefinitionsByRegion() : void {
    $tools = $this->manager
      ->getDefinitionsByRegion(TopBarRegion::Tools);
    $this->assertSame([
      'tools',
    ], array_keys($tools));
    $context = $this->manager
      ->getDefinitionsByRegion(TopBarRegion::Context);
    $this->assertSame([
      'context',
    ], array_keys($context));
    $actions = $this->manager
      ->getDefinitionsByRegion(TopBarRegion::Actions);
    $this->assertSame([
      'actions',
      'more_actions',
    ], array_keys($actions));
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title 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.
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. 1
TopBarItemManagerTest::$manager protected property The top bar item manager under test.
TopBarItemManagerTest::setUp protected function Overrides UnitTestCase::setUp
TopBarItemManagerTest::testDefinitions public function @covers ::getDefinitions[[api-linebreak]]
TopBarItemManagerTest::testGetDefinitionsByRegion public function @covers ::getDefinitionsByRegion[[api-linebreak]]
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::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.