You are here

class LanguageSwitchLinksAlterEventTest in Hook Event Dispatcher 8

Class LanguageSwitchLinksAlterEventTest.

@package Drupal\Tests\hook_event_dispatcher\Unit\Language

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of LanguageSwitchLinksAlterEventTest

File

tests/src/Unit/Language/LanguageSwitchLinksAlterEventTest.php, line 21

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Language
View source
class LanguageSwitchLinksAlterEventTest extends UnitTestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * Sets up the test.
   */
  public function setUp() {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    Drupal::setContainer($builder);
  }

  /**
   * Test adding a new link by reference.
   */
  public function testAddLinksByReference() {
    $currentLinks = [
      'nl_nl' => [
        'url' => new Url('<current>'),
        'title' => 'Nederlands - Dutch',
        'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'attributes' => [],
        'query' => [],
      ],
    ];
    $currentPath = new Url('<current>');
    $currentType = 'language_interface';
    $testLink = [
      'url' => new Url('<current>'),
      'title' => 'Deutsch - German',
      'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'attributes' => [],
      'query' => [],
    ];
    $expectedLinks = $currentLinks;
    $expectedLinks['de_de'] = $testLink;
    $expectedLinks['nl_nl']['attributes'] = [
      'some attr',
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::LANGUAGE_SWITCH_LINKS_ALTER => static function (LanguageSwitchLinksAlterEvent $event) use ($testLink) {
        $links =& $event
          ->getLinks();
        $links['de_de'] = $testLink;
        $links['nl_nl']['attributes'] = [
          'some attr',
        ];
      },
    ]);
    hook_event_dispatcher_language_switch_links_alter($currentLinks, $currentType, $currentPath);

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

  /**
   * Test adding a new link by reference.
   */
  public function testAddLinksBySet() {
    $currentLinks = [
      'nl_nl' => [
        'url' => new Url('<current>'),
        'title' => 'Nederlands - Dutch',
        'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'attributes' => [],
        'query' => [],
      ],
    ];
    $currentPath = new Url('<current>');
    $currentType = 'language_interface';
    $testLink = [
      'url' => new Url('<current>'),
      'title' => 'Deutsch - German',
      'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'attributes' => [],
      'query' => [],
    ];
    $expectedLinks = $currentLinks;
    $expectedLinks['de_de'] = $testLink;
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::LANGUAGE_SWITCH_LINKS_ALTER => static function (LanguageSwitchLinksAlterEvent $event) use ($testLink) {
        $links = $event
          ->getLinks();
        $links['de_de'] = $testLink;
        $event
          ->setLinks($links);
      },
    ]);
    hook_event_dispatcher_language_switch_links_alter($currentLinks, $currentType, $currentPath);

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

  /**
   * Test adding a new language link.
   */
  public function testSetLinkForLanguage() {
    $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);
      },
    ]);
    hook_event_dispatcher_language_switch_links_alter($currentLinks, $currentType, $currentPath);

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

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageSwitchLinksAlterEventTest::$manager private property The manager.
LanguageSwitchLinksAlterEventTest::setUp public function Sets up the test. Overrides UnitTestCase::setUp
LanguageSwitchLinksAlterEventTest::testAddLinksByReference public function Test adding a new link by reference.
LanguageSwitchLinksAlterEventTest::testAddLinksBySet public function Test adding a new link by reference.
LanguageSwitchLinksAlterEventTest::testSetLinkForLanguage public function Test adding a new language link.
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.