You are here

public function LanguageSwitchLinksAlterEventTest::testSetLinkForLanguage in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/core_event_dispatcher/tests/src/Unit/Language/LanguageSwitchLinksAlterEventTest.php \Drupal\Tests\core_event_dispatcher\Unit\Language\LanguageSwitchLinksAlterEventTest::testSetLinkForLanguage()

Test adding a new language link.

File

modules/core_event_dispatcher/tests/src/Unit/Language/LanguageSwitchLinksAlterEventTest.php, line 89

Class

LanguageSwitchLinksAlterEventTest
Class LanguageSwitchLinksAlterEventTest.

Namespace

Drupal\Tests\core_event_dispatcher\Unit\Language

Code

public function testSetLinkForLanguage() : void {
  $currentLinks = $expectedLinks = [
    'nl_nl' => [
      'url' => new Url('<current>'),
      'title' => 'Nederlands - Dutch',
      'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'attributes' => [],
      'query' => [],
    ],
  ];
  $currentPath = new Url('<current>');
  $currentType = 'language_interface';
  $newLink = [
    'url' => new Url('<current>'),
    'title' => 'Deutsch - German',
    'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    'attributes' => [],
    'query' => [],
  ];
  $expectedLinks['de_de'] = $newLink;
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::LANGUAGE_SWITCH_LINKS_ALTER => static function (LanguageSwitchLinksAlterEvent $event) use ($newLink) {
      $event
        ->setLinkForLanguage('de_de', $newLink);
    },
  ]);
  core_event_dispatcher_language_switch_links_alter($currentLinks, $currentType, $currentPath);

  /** @var \Drupal\core_event_dispatcher\Event\Language\LanguageSwitchLinksAlterEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::LANGUAGE_SWITCH_LINKS_ALTER);
  self::assertSame($expectedLinks, $currentLinks);
  self::assertSame($expectedLinks, $event
    ->getLinks());
}