public function MenuLinkFieldKernelTest::testTitleAndDescriptionTranslation in Menu Link (Field) 8
Test the title/description translation.
File
- tests/
src/ Kernel/ MenuLinkFieldKernelTest.php, line 134
Class
- MenuLinkFieldKernelTest
- Tests the menu link field.
Namespace
Drupal\Tests\menu_link\KernelCode
public function testTitleAndDescriptionTranslation() : void {
$this
->enableMultilingual();
$entity_test_mul = EntityTestMul::create([
'type' => 'entity_test_mul',
'name' => 'test',
'field_menu_link' => [
'menu_name' => 'test_menu',
'title' => 'test title EN',
'description' => 'test description EN',
],
]);
$entity_test_mul
->save();
/** @var \Drupal\Core\Menu\MenuLinkTree $menu_tree */
$menu_tree = \Drupal::service('menu.link_tree');
$parameters = new MenuTreeParameters();
$result = $menu_tree
->load('test_menu', $parameters);
self::assertCount(1, $result);
$menu_link = reset($result);
self::assertEquals(1, $menu_link->depth);
self::assertFalse($menu_link->hasChildren);
self::assertEquals('test title EN', $menu_link->link
->getTitle());
self::assertEquals('', $menu_link->link
->getParent());
self::assertEquals('test description EN', $menu_link->link
->getDescription());
self::assertEquals('test_menu', $menu_link->link
->getMenuName());
self::assertTrue($menu_link->link
->getUrlObject()
->isRouted());
self::assertEquals('entity.entity_test_mul.canonical', $menu_link->link
->getUrlObject()
->getRouteName());
self::assertEquals([
'entity_test_mul' => $entity_test_mul
->id(),
], $menu_link->link
->getUrlObject()
->getRouteParameters());
$entity_test_mul_es = $entity_test_mul
->addTranslation('es');
$entity_test_mul_es
->set('field_menu_link', [
'menu_name' => 'test_menu',
'title' => 'test title ES',
'description' => 'test description ES',
]);
$entity_test_mul_es
->save();
// Load the default language aka. EN.
$parameters = new MenuTreeParameters();
$result = $menu_tree
->load('test_menu', $parameters);
self::assertCount(1, $result);
$menu_link = reset($result);
self::assertEquals(1, $menu_link->depth);
self::assertFalse($menu_link->hasChildren);
self::assertEquals('test title EN', $menu_link->link
->getTitle());
self::assertEquals('', $menu_link->link
->getParent());
self::assertEquals('test description EN', $menu_link->link
->getDescription());
self::assertEquals('test_menu', $menu_link->link
->getMenuName());
self::assertTrue($menu_link->link
->getUrlObject()
->isRouted());
self::assertEquals('entity.entity_test_mul.canonical', $menu_link->link
->getUrlObject()
->getRouteName());
self::assertEquals([
'entity_test_mul' => $entity_test_mul
->id(),
], $menu_link->link
->getUrlObject()
->getRouteParameters());
self::assertEquals('test description EN', $menu_link->link
->getUrlObject()
->getOptions()['attributes']['title']);
// Load the ES version.
$reflection = new \ReflectionClass(\Drupal::languageManager());
$property = $reflection
->getProperty('negotiatedLanguages');
$property
->setAccessible(TRUE);
$property
->setValue(\Drupal::languageManager(), [
LanguageInterface::TYPE_CONTENT => ConfigurableLanguage::load('es'),
]);
self::assertEquals('es', \Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId());
$parameters = new MenuTreeParameters();
$result = $menu_tree
->load('test_menu', $parameters);
self::assertCount(1, $result);
$menu_link_es = reset($result);
self::assertEquals('test title ES', $menu_link_es->link
->getTitle());
self::assertEquals('', $menu_link_es->link
->getParent());
self::assertEquals('test description ES', $menu_link_es->link
->getDescription());
self::assertEquals('test_menu', $menu_link_es->link
->getMenuName());
self::assertTrue($menu_link_es->link
->getUrlObject()
->isRouted());
self::assertEquals('entity.entity_test_mul.canonical', $menu_link_es->link
->getUrlObject()
->getRouteName());
self::assertEquals([
'entity_test_mul' => $entity_test_mul
->id(),
], $menu_link_es->link
->getUrlObject()
->getRouteParameters());
self::assertEquals('test description ES', $menu_link_es->link
->getUrlObject()
->getOptions()['attributes']['title']);
self::assertEquals('es', $menu_link_es->link
->getUrlObject()
->getOptions()['language']
->getId());
}