Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / TermTranslationUITest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\taxonomy\Entity\Vocabulary;
8
9 /**
10  * Tests the Term Translation UI.
11  *
12  * @group taxonomy
13  */
14 class TermTranslationUITest extends ContentTranslationUITestBase {
15
16   /**
17    * The vocabulary used for creating terms.
18    *
19    * @var \Drupal\taxonomy\VocabularyInterface
20    */
21   protected $vocabulary;
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = ['language', 'content_translation', 'taxonomy'];
29
30   protected function setUp() {
31     $this->entityTypeId = 'taxonomy_term';
32     $this->bundle = 'tags';
33     parent::setUp();
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setupBundle() {
40     parent::setupBundle();
41
42     // Create a vocabulary.
43     $this->vocabulary = Vocabulary::create([
44       'name' => $this->bundle,
45       'description' => $this->randomMachineName(),
46       'vid' => $this->bundle,
47       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
48       'weight' => mt_rand(0, 10),
49     ]);
50     $this->vocabulary->save();
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   protected function getTranslatorPermissions() {
57     return array_merge(parent::getTranslatorPermissions(), ['administer taxonomy']);
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   protected function getNewEntityValues($langcode) {
64     return ['name' => $this->randomMachineName()] + parent::getNewEntityValues($langcode);
65   }
66
67   /**
68    * Returns an edit array containing the values to be posted.
69    */
70   protected function getEditValues($values, $langcode, $new = FALSE) {
71     $edit = parent::getEditValues($values, $langcode, $new);
72
73     // To be able to post values for the configurable base fields (name,
74     // description) have to be suffixed with [0][value].
75     foreach ($edit as $property => $value) {
76       foreach (['name', 'description'] as $key) {
77         if ($property == $key) {
78           $edit[$key . '[0][value]'] = $value;
79           unset($edit[$property]);
80         }
81       }
82     }
83     return $edit;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function testTranslationUI() {
90     parent::testTranslationUI();
91
92     // Make sure that no row was inserted for taxonomy vocabularies which do
93     // not have translations enabled.
94     $rows = db_query('SELECT tid, count(tid) AS count FROM {taxonomy_term_field_data} WHERE vid <> :vid GROUP BY tid', [':vid' => $this->bundle])->fetchAll();
95     foreach ($rows as $row) {
96       $this->assertTrue($row->count < 2, 'Term does not have translations.');
97     }
98   }
99
100   /**
101    * Tests translate link on vocabulary term list.
102    */
103   public function testTranslateLinkVocabularyAdminPage() {
104     $this->drupalLogin($this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer taxonomy'])));
105
106     $values = [
107       'name' => $this->randomMachineName(),
108     ];
109     $translatable_tid = $this->createEntity($values, $this->langcodes[0], $this->vocabulary->id());
110
111     // Create an untranslatable vocabulary.
112     $untranslatable_vocabulary = Vocabulary::create([
113       'name' => 'untranslatable_voc',
114       'description' => $this->randomMachineName(),
115       'vid' => 'untranslatable_voc',
116       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
117       'weight' => mt_rand(0, 10),
118     ]);
119     $untranslatable_vocabulary->save();
120
121     $values = [
122       'name' => $this->randomMachineName(),
123     ];
124     $untranslatable_tid = $this->createEntity($values, $this->langcodes[0], $untranslatable_vocabulary->id());
125
126     // Verify translation links.
127     $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
128     $this->assertResponse(200, 'The translatable vocabulary page was found.');
129     $this->assertLinkByHref('term/' . $translatable_tid . '/translations', 0, 'The translations link exists for a translatable vocabulary.');
130     $this->assertLinkByHref('term/' . $translatable_tid . '/edit', 0, 'The edit link exists for a translatable vocabulary.');
131
132     $this->drupalGet('admin/structure/taxonomy/manage/' . $untranslatable_vocabulary->id() . '/overview');
133     $this->assertResponse(200);
134     $this->assertLinkByHref('term/' . $untranslatable_tid . '/edit');
135     $this->assertNoLinkByHref('term/' . $untranslatable_tid . '/translations');
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   protected function doTestTranslationEdit() {
142     $storage = $this->container->get('entity_type.manager')
143       ->getStorage($this->entityTypeId);
144     $storage->resetCache([$this->entityId]);
145     $entity = $storage->load($this->entityId);
146     $languages = $this->container->get('language_manager')->getLanguages();
147
148     foreach ($this->langcodes as $langcode) {
149       // We only want to test the title for non-english translations.
150       if ($langcode != 'en') {
151         $options = ['language' => $languages[$langcode]];
152         $url = $entity->urlInfo('edit-form', $options);
153         $this->drupalGet($url);
154
155         $title = t('@title [%language translation]', [
156           '@title' => $entity->getTranslation($langcode)->label(),
157           '%language' => $languages[$langcode]->getName(),
158         ]);
159         $this->assertRaw($title);
160       }
161     }
162   }
163
164 }