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