3 namespace Drupal\Tests\field\Functional\EntityReference;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
12 * Tests the translation of entity reference field display on nodes.
14 * @group entity_reference
16 class EntityReferenceFieldTranslatedReferenceViewTest extends BrowserTestBase {
19 * Flag indicating whether the field is translatable.
23 protected $translatable = TRUE;
26 * The langcode of the source language.
30 protected $baseLangcode = 'en';
33 * Target langcode for translation.
37 protected $translateToLangcode = 'hu';
40 * The test entity type name.
44 protected $testEntityTypeName = 'node';
47 * Entity type which have the entity reference field.
49 * @var \Drupal\node\Entity\NodeType
51 protected $referrerType;
54 * Entity type which can be referenced.
56 * @var \Drupal\node\Entity\NodeType
58 protected $referencedType;
61 * The referrer entity.
63 * @var \Drupal\node\Entity\Node
65 protected $referrerEntity;
68 * The entity to refer.
70 * @var \Drupal\node\Entity\Node
72 protected $referencedEntityWithoutTranslation;
75 * The entity to refer.
77 * @var \Drupal\node\Entity\Node
79 protected $referencedEntityWithTranslation;
82 * The machine name of the entity reference field.
86 protected $referenceFieldName = 'test_reference_field';
89 * The label of the untranslated referenced entity, used in assertions.
93 protected $labelOfNotTranslatedReference;
96 * The original label of the referenced entity, used in assertions.
100 protected $originalLabel;
103 * The translated label of the referenced entity, used in assertions.
107 protected $translatedLabel;
110 * An user with permission to edit the referrer entity.
112 * @var \Drupal\user\UserInterface
121 public static $modules = [
123 'content_translation',
127 protected function setUp() {
130 $this->labelOfNotTranslatedReference = $this->randomMachineName();
131 $this->originalLabel = $this->randomMachineName();
132 $this->translatedLabel = $this->randomMachineName();
134 $this->setUpLanguages();
136 // We setup languages, so we need to ensure that the language manager
137 // and language path processor is updated.
138 $this->rebuildContainer();
140 $this->setUpContentTypes();
141 $this->enableTranslation();
142 $this->setUpEntityReferenceField();
143 $this->createContent();
145 $this->webUser = $this->drupalCreateUser(['edit any ' . $this->referrerType->id() . ' content']);
149 * Tests if the entity is displayed in an entity reference field.
151 public function testEntityReferenceDisplay() {
152 // Create a translated referrer entity.
153 $this->referrerEntity = $this->createReferrerEntity();
154 $this->assertEntityReferenceDisplay();
155 $this->assertEntityReferenceFormDisplay();
157 // Disable translation for referrer content type.
158 $this->drupalLogin($this->rootUser);
159 $this->drupalPostForm('admin/config/regional/content-language', ['settings[node][referrer][translatable]' => FALSE], t('Save configuration'));
160 $this->drupalLogout();
162 // Create a referrer entity without translation.
163 $this->referrerEntity = $this->createReferrerEntity(FALSE);
164 $this->assertEntityReferenceDisplay();
165 $this->assertEntityReferenceFormDisplay();
169 * Assert entity reference display.
171 protected function assertEntityReferenceDisplay() {
172 $url = $this->referrerEntity->urlInfo();
173 $translation_url = $this->referrerEntity->urlInfo('canonical', ['language' => ConfigurableLanguage::load($this->translateToLangcode)]);
175 $this->drupalGet($url);
176 $this->assertText($this->labelOfNotTranslatedReference, 'The label of not translated reference is displayed.');
177 $this->assertText($this->originalLabel, 'The default label of translated reference is displayed.');
178 $this->assertNoText($this->translatedLabel, 'The translated label of translated reference is not displayed.');
179 $this->drupalGet($translation_url);
180 $this->assertText($this->labelOfNotTranslatedReference, 'The label of not translated reference is displayed.');
181 $this->assertNoText($this->originalLabel, 'The default label of translated reference is not displayed.');
182 $this->assertText($this->translatedLabel, 'The translated label of translated reference is displayed.');
186 * Assert entity reference form display.
188 protected function assertEntityReferenceFormDisplay() {
189 $this->drupalLogin($this->webUser);
190 $url = $this->referrerEntity->urlInfo('edit-form');
191 $translation_url = $this->referrerEntity->urlInfo('edit-form', ['language' => ConfigurableLanguage::load($this->translateToLangcode)]);
193 $this->drupalGet($url);
194 $this->assertSession()->fieldValueEquals('test_reference_field[0][target_id]', $this->originalLabel . ' (1)');
195 $this->assertSession()->fieldValueEquals('test_reference_field[1][target_id]', $this->labelOfNotTranslatedReference . ' (2)');
196 $this->drupalGet($translation_url);
197 $this->assertSession()->fieldValueEquals('test_reference_field[0][target_id]', $this->translatedLabel . ' (1)');
198 $this->assertSession()->fieldValueEquals('test_reference_field[1][target_id]', $this->labelOfNotTranslatedReference . ' (2)');
199 $this->drupalLogout();
203 * Adds additional languages.
205 protected function setUpLanguages() {
206 ConfigurableLanguage::createFromLangcode($this->translateToLangcode)->save();
210 * Creates a test subject contents, with translation.
212 protected function createContent() {
213 $this->referencedEntityWithTranslation = $this->createReferencedEntityWithTranslation();
214 $this->referencedEntityWithoutTranslation = $this->createNotTranslatedReferencedEntity();
218 * Enables translations where it needed.
220 protected function enableTranslation() {
221 // Enable translation for the entity types and ensure the change is picked
223 \Drupal::service('content_translation.manager')->setEnabled($this->testEntityTypeName, $this->referrerType->id(), TRUE);
224 \Drupal::service('content_translation.manager')->setEnabled($this->testEntityTypeName, $this->referencedType->id(), TRUE);
225 drupal_static_reset();
226 \Drupal::entityManager()->clearCachedDefinitions();
227 \Drupal::service('router.builder')->rebuild();
228 \Drupal::service('entity.definition_update_manager')->applyUpdates();
232 * Adds term reference field for the article content type.
234 protected function setUpEntityReferenceField() {
235 FieldStorageConfig::create([
236 'field_name' => $this->referenceFieldName,
237 'entity_type' => $this->testEntityTypeName,
238 'type' => 'entity_reference',
239 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
240 'translatable' => $this->translatable,
242 'allowed_values' => [
244 'target_type' => $this->testEntityTypeName,
250 FieldConfig::create([
251 'field_name' => $this->referenceFieldName,
252 'bundle' => $this->referrerType->id(),
253 'entity_type' => $this->testEntityTypeName,
256 entity_get_form_display($this->testEntityTypeName, $this->referrerType->id(), 'default')
257 ->setComponent($this->referenceFieldName, [
258 'type' => 'entity_reference_autocomplete',
261 entity_get_display($this->testEntityTypeName, $this->referrerType->id(), 'default')
262 ->setComponent($this->referenceFieldName, [
263 'type' => 'entity_reference_label',
269 * Create content types.
271 protected function setUpContentTypes() {
272 $this->referrerType = $this->drupalCreateContentType([
273 'type' => 'referrer',
274 'name' => 'Referrer',
276 $this->referencedType = $this->drupalCreateContentType([
277 'type' => 'referenced_page',
278 'name' => 'Referenced Page',
283 * Create a referenced entity with a translation.
285 protected function createReferencedEntityWithTranslation() {
286 /** @var \Drupal\node\Entity\Node $node */
287 $node = entity_create($this->testEntityTypeName, [
288 'title' => $this->originalLabel,
289 'type' => $this->referencedType->id(),
291 'value' => $this->randomMachineName(),
292 'format' => 'basic_html',
294 'langcode' => $this->baseLangcode,
297 $node->addTranslation($this->translateToLangcode, [
298 'title' => $this->translatedLabel,
307 * Create the referenced entity.
309 protected function createNotTranslatedReferencedEntity() {
310 /** @var \Drupal\node\Entity\Node $node */
311 $node = entity_create($this->testEntityTypeName, [
312 'title' => $this->labelOfNotTranslatedReference,
313 'type' => $this->referencedType->id(),
315 'value' => $this->randomMachineName(),
316 'format' => 'basic_html',
318 'langcode' => $this->baseLangcode,
326 * Create the referrer entity.
328 protected function createReferrerEntity($translatable = TRUE) {
329 /** @var \Drupal\node\Entity\Node $node */
330 $node = entity_create($this->testEntityTypeName, [
331 'title' => $this->randomMachineName(),
332 'type' => $this->referrerType->id(),
334 'value' => $this->randomMachineName(),
335 'format' => 'basic_html',
337 $this->referenceFieldName => [
338 ['target_id' => $this->referencedEntityWithTranslation->id()],
339 ['target_id' => $this->referencedEntityWithoutTranslation->id()],
341 'langcode' => $this->baseLangcode,
344 $node->addTranslation($this->translateToLangcode, $node->toArray());