/web/core/modules/content_translation/tests/src/Functional/ContentTranslationTestBase.php

https://gitlab.com/mohamed_hussein/prodt · PHP · 258 lines · 122 code · 29 blank · 107 comment · 9 complexity · e7757632c5cc0d4816eded9f7dbb4d55 MD5 · raw file

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