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