EntityLanguageTestBase.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\Component\Utility\Unicode;
  4. use Drupal\language\Entity\ConfigurableLanguage;
  5. use Drupal\field\Entity\FieldConfig;
  6. use Drupal\field\Entity\FieldStorageConfig;
  7. /**
  8. * Base class for language-aware entity tests.
  9. */
  10. abstract class EntityLanguageTestBase extends EntityKernelTestBase {
  11. /**
  12. * The language manager service.
  13. *
  14. * @var \Drupal\Core\Language\LanguageManagerInterface
  15. */
  16. protected $languageManager;
  17. /**
  18. * The available language codes.
  19. *
  20. * @var array
  21. */
  22. protected $langcodes;
  23. /**
  24. * The test field name.
  25. *
  26. * @var string
  27. */
  28. protected $fieldName;
  29. /**
  30. * The untranslatable test field name.
  31. *
  32. * @var string
  33. */
  34. protected $untranslatableFieldName;
  35. public static $modules = ['language', 'entity_test'];
  36. protected function setUp() {
  37. parent::setUp();
  38. $this->languageManager = $this->container->get('language_manager');
  39. foreach (entity_test_entity_types() as $entity_type_id) {
  40. // The entity_test schema is installed by the parent.
  41. if ($entity_type_id != 'entity_test') {
  42. $this->installEntitySchema($entity_type_id);
  43. }
  44. }
  45. $this->installConfig(['language']);
  46. // Create the test field.
  47. module_load_install('entity_test');
  48. entity_test_install();
  49. // Enable translations for the test entity type.
  50. $this->state->set('entity_test.translation', TRUE);
  51. // Create a translatable test field.
  52. $this->fieldName = Unicode::strtolower($this->randomMachineName() . '_field_name');
  53. // Create an untranslatable test field.
  54. $this->untranslatableFieldName = Unicode::strtolower($this->randomMachineName() . '_field_name');
  55. // Create field fields in all entity variations.
  56. foreach (entity_test_entity_types() as $entity_type) {
  57. FieldStorageConfig::create([
  58. 'field_name' => $this->fieldName,
  59. 'entity_type' => $entity_type,
  60. 'type' => 'text',
  61. 'cardinality' => 4,
  62. ])->save();
  63. FieldConfig::create([
  64. 'field_name' => $this->fieldName,
  65. 'entity_type' => $entity_type,
  66. 'bundle' => $entity_type,
  67. 'translatable' => TRUE,
  68. ])->save();
  69. FieldStorageConfig::create([
  70. 'field_name' => $this->untranslatableFieldName,
  71. 'entity_type' => $entity_type,
  72. 'type' => 'text',
  73. 'cardinality' => 4,
  74. ])->save();
  75. FieldConfig::create([
  76. 'field_name' => $this->untranslatableFieldName,
  77. 'entity_type' => $entity_type,
  78. 'bundle' => $entity_type,
  79. 'translatable' => FALSE,
  80. ])->save();
  81. }
  82. // Create the default languages.
  83. $this->installConfig(['language']);
  84. // Create test languages.
  85. $this->langcodes = [];
  86. for ($i = 0; $i < 3; ++$i) {
  87. $language = ConfigurableLanguage::create([
  88. 'id' => 'l' . $i,
  89. 'label' => $this->randomString(),
  90. 'weight' => $i,
  91. ]);
  92. $this->langcodes[$i] = $language->getId();
  93. $language->save();
  94. }
  95. }
  96. /**
  97. * Toggles field storage translatability.
  98. *
  99. * @param string $entity_type
  100. * The type of the entity fields are attached to.
  101. */
  102. protected function toggleFieldTranslatability($entity_type, $bundle) {
  103. $fields = [$this->fieldName, $this->untranslatableFieldName];
  104. foreach ($fields as $field_name) {
  105. $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
  106. $translatable = !$field->isTranslatable();
  107. $field->set('translatable', $translatable);
  108. $field->save();
  109. $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
  110. $this->assertEqual($field->isTranslatable(), $translatable, 'Field translatability changed.');
  111. }
  112. \Drupal::cache('entity')->deleteAll();
  113. }
  114. }