class LanguageSwitchLinksAlterEventTest in Hook Event Dispatcher 8
Class LanguageSwitchLinksAlterEventTest.
@package Drupal\Tests\hook_event_dispatcher\Unit\Language
@group hook_event_dispatcher
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\hook_event_dispatcher\Unit\Language\LanguageSwitchLinksAlterEventTest
Expanded class hierarchy of LanguageSwitchLinksAlterEventTest
File
- tests/
src/ Unit/ Language/ LanguageSwitchLinksAlterEventTest.php, line 21
Namespace
Drupal\Tests\hook_event_dispatcher\Unit\LanguageView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LanguageSwitchLinksAlterEventTest:: |
private | property | The manager. | |
LanguageSwitchLinksAlterEventTest:: |
public | function |
Sets up the test. Overrides UnitTestCase:: |
|
LanguageSwitchLinksAlterEventTest:: |
public | function | Test adding a new link by reference. | |
LanguageSwitchLinksAlterEventTest:: |
public | function | Test adding a new link by reference. | |
LanguageSwitchLinksAlterEventTest:: |
public | function | Test adding a new language link. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |