View source
<?php
namespace Drupal\Tests\shortcut\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Language\Language;
class ShortcutTranslationUITest extends ContentTranslationUITestBase {
protected $defaultCacheContexts = [
'languages:language_interface',
'session',
'theme',
'user',
'url.path',
'url.query_args',
'url.site',
];
protected static $modules = [
'language',
'content_translation',
'link',
'shortcut',
'toolbar',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
$this->entityTypeId = 'shortcut';
$this->bundle = 'default';
parent::setUp();
}
protected function getTranslatorPermissions() {
return array_merge(parent::getTranslatorPermissions(), [
'access shortcuts',
'administer shortcuts',
'access toolbar',
]);
}
protected function createEntity($values, $langcode, $bundle_name = NULL) {
$values['link']['uri'] = 'internal:/user';
return parent::createEntity($values, $langcode, $bundle_name);
}
protected function getNewEntityValues($langcode) {
return [
'title' => [
[
'value' => $this
->randomMachineName(),
],
],
] + parent::getNewEntityValues($langcode);
}
protected function doTestBasicTranslation() {
parent::doTestBasicTranslation();
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
$storage
->resetCache([
$this->entityId,
]);
$entity = $storage
->load($this->entityId);
foreach ($this->langcodes as $langcode) {
if ($entity
->hasTranslation($langcode)) {
$language = new Language([
'id' => $langcode,
]);
$this
->drupalGet('<front>', [
'language' => $language,
]);
$expected_path = \Drupal::urlGenerator()
->generateFromRoute('user.page', [], [
'language' => $language,
]);
$label = $entity
->getTranslation($langcode)
->label();
$this
->assertSession()
->elementExists('xpath', "//nav[contains(@class, 'toolbar-lining')]/ul[@class='toolbar-menu']/li/a[contains(@href, '{$expected_path}') and normalize-space(text())='{$label}']");
}
}
}
protected function doTestTranslationEdit() {
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
$storage
->resetCache([
$this->entityId,
]);
$entity = $storage
->load($this->entityId);
$languages = $this->container
->get('language_manager')
->getLanguages();
foreach ($this->langcodes as $langcode) {
if ($langcode != 'en') {
$options = [
'language' => $languages[$langcode],
];
$url = $entity
->toUrl('edit-form', $options);
$this
->drupalGet($url);
$this
->assertSession()
->pageTextContains("{$entity->getTranslation($langcode)->label()} [{$languages[$langcode]->getName()} translation]");
}
}
}
protected function doTestTranslationChanged() {
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
$storage
->resetCache([
$this->entityId,
]);
$entity = $storage
->load($this->entityId);
$this
->assertFalse($entity instanceof EntityChangedInterface, new FormattableMarkup('%entity is not implementing EntityChangedInterface.', [
'%entity' => $this->entityTypeId,
]));
}
}