3 namespace Drupal\Tests\shortcut\Functional;
5 use Drupal\content_translation\Tests\ContentTranslationUITestBase;
6 use Drupal\Core\Entity\EntityChangedInterface;
7 use Drupal\Core\Language\Language;
10 * Tests the shortcut translation UI.
14 class ShortcutTranslationUITest extends ContentTranslationUITestBase {
19 protected $defaultCacheContexts = ['languages:language_interface', 'session', 'theme', 'user', 'url.path', 'url.query_args', 'url.site'];
26 public static $modules = [
28 'content_translation',
37 protected function setUp() {
38 $this->entityTypeId = 'shortcut';
39 $this->bundle = 'default';
46 protected function getTranslatorPermissions() {
47 return array_merge(parent::getTranslatorPermissions(), ['access shortcuts', 'administer shortcuts', 'access toolbar']);
53 protected function createEntity($values, $langcode, $bundle_name = NULL) {
54 $values['link']['uri'] = 'internal:/user';
55 return parent::createEntity($values, $langcode, $bundle_name);
61 protected function getNewEntityValues($langcode) {
62 return ['title' => [['value' => $this->randomMachineName()]]] + parent::getNewEntityValues($langcode);
65 protected function doTestBasicTranslation() {
66 parent::doTestBasicTranslation();
68 $storage = $this->container->get('entity_type.manager')
69 ->getStorage($this->entityTypeId);
70 $storage->resetCache([$this->entityId]);
71 $entity = $storage->load($this->entityId);
72 foreach ($this->langcodes as $langcode) {
73 if ($entity->hasTranslation($langcode)) {
74 $language = new Language(['id' => $langcode]);
75 // Request the front page in this language and assert that the right
76 // translation shows up in the shortcut list with the right path.
77 $this->drupalGet('<front>', ['language' => $language]);
78 $expected_path = \Drupal::urlGenerator()->generateFromRoute('user.page', [], ['language' => $language]);
79 $label = $entity->getTranslation($langcode)->label();
80 $elements = $this->xpath('//nav[contains(@class, "toolbar-lining")]/ul[@class="toolbar-menu"]/li/a[contains(@href, :href) and normalize-space(text())=:label]', [':href' => $expected_path, ':label' => $label]);
81 $this->assertTrue(!empty($elements), format_string('Translated @language shortcut link @label found.', ['@label' => $label, '@language' => $language->getName()]));
89 protected function doTestTranslationEdit() {
90 $storage = $this->container->get('entity_type.manager')
91 ->getStorage($this->entityTypeId);
92 $storage->resetCache([$this->entityId]);
93 $entity = $storage->load($this->entityId);
94 $languages = $this->container->get('language_manager')->getLanguages();
96 foreach ($this->langcodes as $langcode) {
97 // We only want to test the title for non-english translations.
98 if ($langcode != 'en') {
99 $options = ['language' => $languages[$langcode]];
100 $url = $entity->urlInfo('edit-form', $options);
101 $this->drupalGet($url);
103 $title = t('@title [%language translation]', [
104 '@title' => $entity->getTranslation($langcode)->label(),
105 '%language' => $languages[$langcode]->getName(),
107 $this->assertRaw($title);
113 * Tests the basic translation workflow.
115 protected function doTestTranslationChanged() {
116 $storage = $this->container->get('entity_type.manager')
117 ->getStorage($this->entityTypeId);
118 $storage->resetCache([$this->entityId]);
119 $entity = $storage->load($this->entityId);
122 $entity instanceof EntityChangedInterface,
123 format_string('%entity is not implementing EntityChangedInterface.', ['%entity' => $this->entityTypeId])