You are here

protected function FootermapBlockTest::setUp in footermap: a footer site map 8

Overrides UnitTestCase::setUp

File

tests/src/Unit/Plugin/Block/FootermapBlockTest.php, line 35

Class

FootermapBlockTest
Test footermap block methods.

Namespace

Drupal\Tests\footermap\Unit\Plugin\Block

Code

protected function setUp() {
  parent::setUp();

  // Create some menu entities.
  $menu1 = new Menu([
    'id' => 'menu1',
    'description' => 'menu1',
    'label' => 'menu1',
  ], 'menu');
  $menu2 = new Menu([
    'id' => 'menu2',
    'description' => 'menu2',
    'label' => 'menu2',
  ], 'menu');
  $link1 = [
    'title' => 'Link1',
  ];
  $link1_definition = [
    'id' => 'menu_link1',
    'title' => 'Link1',
    'description' => 'A link',
    'enabled' => TRUE,
    'options' => [],
    'weight' => 0,
  ];
  $link1_child = [
    'title' => 'Child Link 1',
  ];
  $link1_child_definition = [
    'id' => 'menu_link1_child',
    'title' => 'Child Link 1',
    'description' => 'A child link',
    'enabled' => TRUE,
    'options' => [],
    'weight' => 5,
  ];
  $link1_child2 = [
    'title' => 'Child Link 2',
  ];
  $link1_child2_definition = [
    'id' => 'menu_link2',
    'title' => 'Child Link 2',
    'description' => 'A second link',
    'enabled' => TRUE,
    'options' => [],
    'weight' => -5,
  ];
  $configEntityType = new ConfigEntityType([
    'id' => 'menu',
    'label' => 'Menu',
    'handlers' => [
      'access' => 'Drupal\\system\\MenuAccessControlHandler',
    ],
    'entity_keys' => [
      'id' => 'id',
      'label' => 'label',
    ],
    'admin_permission' => 'administer menu',
  ]);

  // Mock Static Menu link overrides.
  $staticMenuLinkProphet = $this
    ->prophesize('\\Drupal\\Core\\Menu\\StaticMenuLinkOverridesInterface');
  $staticMenuLinkOverrides = $staticMenuLinkProphet
    ->reveal();

  // Create menu link plugin instances.
  $menu1_link1 = new MenuLinkDefault($link1, 'menu_link1', $link1_definition, $staticMenuLinkOverrides);
  $menu1_link1_child = new MenuLinkDefault($link1_child, 'menu_link1_child', $link1_child_definition, $staticMenuLinkOverrides);
  $menu1_link2_child = new MenuLinkDefault($link1_child2, 'menu_link1_child2', $link1_child2_definition, $staticMenuLinkOverrides);

  // Create menu link tree for menu1 with menu routes.
  $menu1_subtree = new MenuLinkTreeElement($menu1_link1_child, FALSE, 2, FALSE, []);
  $menu1_subtree2 = new MenuLinkTreeElement($menu1_link2_child, FALSE, 2, FALSE, []);
  $menu1_tree = new MenuLinkTreeElement($menu1_link1, TRUE, 1, FALSE, [
    $menu1_subtree,
    $menu1_subtree2,
  ]);

  // Mock config entity storage.
  $configStorageProphet = $this
    ->prophesize('\\Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
  $configStorageProphet
    ->loadMultiple(Argument::any())
    ->willReturn([
    'menu1' => $menu1,
    'menu2' => $menu2,
  ]);

  // Mock the entity_type manager.
  $entityTypeProphet = $this
    ->prophesize('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
  $entityTypeProphet
    ->getStorage('menu')
    ->willReturn($configStorageProphet
    ->reveal());
  $entityTypeProphet
    ->getDefinition('menu')
    ->willReturn($configEntityType);

  // Mock the entity repository.
  $entityRepositoryProphet = $this
    ->prophesize('\\Drupal\\Core\\Entity\\EntityRepositoryInterface');
  $entityRepositoryProphet
    ->loadEntityByUuid('menu_link_content', Argument::any());

  // Mock the menu link tree manager.
  $menuLinkTreeProphet = $this
    ->prophesize('\\Drupal\\Core\\Menu\\MenuLinkTreeInterface');
  $menuLinkTreeProphet
    ->load('menu1', $this
    ->getMenuParameters())
    ->willReturn([
    $menu1_tree,
  ]);
  $menuLinkTreeProphet
    ->transform(Argument::any(), Argument::any())
    ->willReturn([
    $menu1_tree,
  ]);

  // Mock the plugin manager for menu link.
  $menuLinkPluginProphet = $this
    ->prophesize('\\Drupal\\Core\\Menu\\MenuLinkManagerInterface');

  // Mock the Logger Channel factory.
  $loggerChannelProphet = $this
    ->prophesize('\\Drupal\\Core\\Logger\\LoggerChannelFactoryInterface');
  $loggerChannelProphet
    ->get('footermap')
    ->willReturn(new LoggerChannel('footermap'));

  // Mock the Access Manager.
  $accessManager = $this
    ->prophesize('\\Drupal\\Core\\Access\\AccessManagerInterface')
    ->reveal();

  // Mock an anonymous user session.
  $account = $this
    ->prophesize('\\Drupal\\Core\\Session\\AnonymousUserSession')
    ->reveal();

  // Mock the entity_type.manager service.
  $entityTypeManager = $entityTypeProphet
    ->reveal();
  $anonymousTreeManipulator = new AnonymousMenuLinkTreeManipulator($accessManager, $account, $entityTypeManager);
  $this->container = new ContainerBuilder();
  $this->container
    ->set('entity_type.manager', $entityTypeManager);
  $this->container
    ->set('entity.repository', $entityRepositoryProphet
    ->reveal());
  $this->container
    ->set('menu.link_tree', $menuLinkTreeProphet
    ->reveal());
  $this->container
    ->set('plugin.manager.menu.link', $menuLinkPluginProphet
    ->reveal());
  $this->container
    ->set('string_translation', $this
    ->getStringTranslationStub());
  $this->container
    ->set('footermap.anonymous_tree_manipulator', $anonymousTreeManipulator);
  $this->container
    ->set('footermap.anonymous_user', $account);
  $this->container
    ->set('logger.factory', $loggerChannelProphet
    ->reveal());
  \Drupal::setContainer($this->container);
}