82715c28c293c003856607bdece19764dc9d478e
[yaffs-website] / web / core / modules / content_translation / tests / src / Functional / ContentTranslationTestBase.php
1 <?php
2
3 namespace Drupal\Tests\content_translation\Functional;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\language\Entity\ConfigurableLanguage;
9 use Drupal\Tests\BrowserTestBase;
10 use Drupal\field\Entity\FieldStorageConfig;
11
12 /**
13  * Base class for content translation tests.
14  */
15 abstract class ContentTranslationTestBase extends BrowserTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['text'];
23
24   /**
25    * The entity type being tested.
26    *
27    * @var string
28    */
29   protected $entityTypeId = 'entity_test_mul';
30
31   /**
32    * The bundle being tested.
33    *
34    * @var string
35    */
36   protected $bundle;
37
38   /**
39    * The added languages.
40    *
41    * @var array
42    */
43   protected $langcodes;
44
45   /**
46    * The account to be used to test translation operations.
47    *
48    * @var \Drupal\user\UserInterface
49    */
50   protected $translator;
51
52   /**
53    * The account to be used to test multilingual entity editing.
54    *
55    * @var \Drupal\user\UserInterface
56    */
57   protected $editor;
58
59   /**
60    * The account to be used to test access to both workflows.
61    *
62    * @var \Drupal\user\UserInterface
63    */
64   protected $administrator;
65
66   /**
67    * The name of the field used to test translation.
68    *
69    * @var string
70    */
71   protected $fieldName;
72
73   /**
74    * The translation controller for the current entity type.
75    *
76    * @var \Drupal\content_translation\ContentTranslationHandlerInterface
77    */
78   protected $controller;
79
80   /**
81    * @var \Drupal\content_translation\ContentTranslationManagerInterface
82    */
83   protected $manager;
84
85   protected function setUp() {
86     parent::setUp();
87
88     $this->setupLanguages();
89     $this->setupBundle();
90     $this->enableTranslation();
91     $this->setupUsers();
92     $this->setupTestFields();
93
94     $this->manager = $this->container->get('content_translation.manager');
95     $this->controller = $this->manager->getTranslationHandler($this->entityTypeId);
96
97     // Rebuild the container so that the new languages are picked up by services
98     // that hold a list of languages.
99     $this->rebuildContainer();
100   }
101
102   /**
103    * Adds additional languages.
104    */
105   protected function setupLanguages() {
106     $this->langcodes = ['it', 'fr'];
107     foreach ($this->langcodes as $langcode) {
108       ConfigurableLanguage::createFromLangcode($langcode)->save();
109     }
110     array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId());
111   }
112
113   /**
114    * Returns an array of permissions needed for the translator.
115    */
116   protected function getTranslatorPermissions() {
117     return array_filter([$this->getTranslatePermission(), 'create content translations', 'update content translations', 'delete content translations']);
118   }
119
120   /**
121    * Returns the translate permissions for the current entity and bundle.
122    */
123   protected function getTranslatePermission() {
124     $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
125     if ($permission_granularity = $entity_type->getPermissionGranularity()) {
126       return $permission_granularity == 'bundle' ? "translate {$this->bundle} {$this->entityTypeId}" : "translate {$this->entityTypeId}";
127     }
128   }
129
130   /**
131    * Returns an array of permissions needed for the editor.
132    */
133   protected function getEditorPermissions() {
134     // Every entity-type-specific test needs to define these.
135     return [];
136   }
137
138   /**
139    * Returns an array of permissions needed for the administrator.
140    */
141   protected function getAdministratorPermissions() {
142     return array_merge($this->getEditorPermissions(), $this->getTranslatorPermissions(), ['administer languages', 'administer content translation']);
143   }
144
145   /**
146    * Creates and activates translator, editor and admin users.
147    */
148   protected function setupUsers() {
149     $this->translator = $this->drupalCreateUser($this->getTranslatorPermissions(), 'translator');
150     $this->editor = $this->drupalCreateUser($this->getEditorPermissions(), 'editor');
151     $this->administrator = $this->drupalCreateUser($this->getAdministratorPermissions(), 'administrator');
152     $this->drupalLogin($this->translator);
153   }
154
155   /**
156    * Creates or initializes the bundle date if needed.
157    */
158   protected function setupBundle() {
159     if (empty($this->bundle)) {
160       $this->bundle = $this->entityTypeId;
161     }
162   }
163
164   /**
165    * Enables translation for the current entity type and bundle.
166    */
167   protected function enableTranslation() {
168     // Enable translation for the current entity type and ensure the change is
169     // picked up.
170     \Drupal::service('content_translation.manager')->setEnabled($this->entityTypeId, $this->bundle, TRUE);
171     drupal_static_reset();
172     \Drupal::entityManager()->clearCachedDefinitions();
173     \Drupal::service('router.builder')->rebuild();
174     \Drupal::service('entity.definition_update_manager')->applyUpdates();
175   }
176
177   /**
178    * Creates the test fields.
179    */
180   protected function setupTestFields() {
181     if (empty($this->fieldName)) {
182       $this->fieldName = 'field_test_et_ui_test';
183     }
184     FieldStorageConfig::create([
185       'field_name' => $this->fieldName,
186       'type' => 'string',
187       'entity_type' => $this->entityTypeId,
188       'cardinality' => 1,
189     ])->save();
190     FieldConfig::create([
191       'entity_type' => $this->entityTypeId,
192       'field_name' => $this->fieldName,
193       'bundle' => $this->bundle,
194       'label' => 'Test translatable text-field',
195     ])->save();
196     entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
197       ->setComponent($this->fieldName, [
198         'type' => 'string_textfield',
199         'weight' => 0,
200       ])
201       ->save();
202   }
203
204   /**
205    * Creates the entity to be translated.
206    *
207    * @param array $values
208    *   An array of initial values for the entity.
209    * @param string $langcode
210    *   The initial language code of the entity.
211    * @param string $bundle_name
212    *   (optional) The entity bundle, if the entity uses bundles. Defaults to
213    *   NULL. If left NULL, $this->bundle will be used.
214    *
215    * @return string
216    *   The entity id.
217    */
218   protected function createEntity($values, $langcode, $bundle_name = NULL) {
219     $entity_values = $values;
220     $entity_values['langcode'] = $langcode;
221     $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
222     if ($bundle_key = $entity_type->getKey('bundle')) {
223       $entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
224     }
225     $controller = $this->container->get('entity.manager')->getStorage($this->entityTypeId);
226     if (!($controller instanceof SqlContentEntityStorage)) {
227       foreach ($values as $property => $value) {
228         if (is_array($value)) {
229           $entity_values[$property] = [$langcode => $value];
230         }
231       }
232     }
233     $entity = $this->container->get('entity_type.manager')
234       ->getStorage($this->entityTypeId)
235       ->create($entity_values);
236     $entity->save();
237     return $entity->id();
238   }
239
240   /**
241    * Returns the edit URL for the specified entity.
242    *
243    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
244    *   The entity being edited.
245    *
246    * @return \Drupal\Core\Url
247    *   The edit URL.
248    */
249   protected function getEditUrl(ContentEntityInterface $entity) {
250     if ($entity->access('update', $this->loggedInUser)) {
251       $url = $entity->toUrl('edit-form');
252     }
253     else {
254       $url = $entity->toUrl('drupal:content-translation-edit');
255       $url->setRouteParameter('language', $entity->language()->getId());
256     }
257     return $url;
258   }
259
260 }