83bf47a49535cf296279028d65abd0267d72e847
[yaffs-website] / web / core / modules / shortcut / tests / src / Functional / ShortcutTranslationUITest.php
1 <?php
2
3 namespace Drupal\Tests\shortcut\Functional;
4
5 use Drupal\content_translation\Tests\ContentTranslationUITestBase;
6 use Drupal\Core\Entity\EntityChangedInterface;
7 use Drupal\Core\Language\Language;
8
9 /**
10  * Tests the shortcut translation UI.
11  *
12  * @group Shortcut
13  */
14 class ShortcutTranslationUITest extends ContentTranslationUITestBase {
15
16   /**
17    * {inheritdoc}
18    */
19   protected $defaultCacheContexts = ['languages:language_interface', 'session', 'theme', 'user', 'url.path', 'url.query_args', 'url.site'];
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = [
27     'language',
28     'content_translation',
29     'link',
30     'shortcut',
31     'toolbar'
32   ];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     $this->entityTypeId = 'shortcut';
39     $this->bundle = 'default';
40     parent::setUp();
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   protected function getTranslatorPermissions() {
47     return array_merge(parent::getTranslatorPermissions(), ['access shortcuts', 'administer shortcuts', 'access toolbar']);
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function createEntity($values, $langcode, $bundle_name = NULL) {
54     $values['link']['uri'] = 'internal:/user';
55     return parent::createEntity($values, $langcode, $bundle_name);
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function getNewEntityValues($langcode) {
62     return ['title' => [['value' => $this->randomMachineName()]]] + parent::getNewEntityValues($langcode);
63   }
64
65   protected function doTestBasicTranslation() {
66     parent::doTestBasicTranslation();
67
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()]));
82       }
83     }
84   }
85
86   /**
87    * {@inheritdoc}
88    */
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();
95
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);
102
103         $title = t('@title [%language translation]', [
104           '@title' => $entity->getTranslation($langcode)->label(),
105           '%language' => $languages[$langcode]->getName(),
106         ]);
107         $this->assertRaw($title);
108       }
109     }
110   }
111
112   /**
113    * Tests the basic translation workflow.
114    */
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);
120
121     $this->assertFalse(
122       $entity instanceof EntityChangedInterface,
123       format_string('%entity is not implementing EntityChangedInterface.', ['%entity' => $this->entityTypeId])
124     );
125   }
126
127 }