View source
<?php
namespace Drupal\Tests\field\Functional\EntityReference;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldStorageConfig;
class EntityReferenceFieldTranslatedReferenceViewTest extends BrowserTestBase {
protected $translatable = TRUE;
protected $baseLangcode = 'en';
protected $translateToLangcode = 'hu';
protected $testEntityTypeName = 'node';
protected $referrerType;
protected $referencedType;
protected $referrerEntity;
protected $referencedEntityWithoutTranslation;
protected $referencedEntityWithTranslation;
protected $referenceFieldName = 'test_reference_field';
protected $labelOfNotTranslatedReference;
protected $originalLabel;
protected $translatedLabel;
protected $webUser;
protected static $modules = [
'language',
'content_translation',
'node',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->labelOfNotTranslatedReference = $this
->randomMachineName();
$this->originalLabel = $this
->randomMachineName();
$this->translatedLabel = $this
->randomMachineName();
$this
->setUpLanguages();
$this
->rebuildContainer();
$this
->setUpContentTypes();
$this
->enableTranslation();
$this
->setUpEntityReferenceField();
$this
->createContent();
$this->webUser = $this
->drupalCreateUser([
'edit any ' . $this->referrerType
->id() . ' content',
]);
}
public function testEntityReferenceDisplay() {
$this->referrerEntity = $this
->createReferrerEntity();
$this
->assertEntityReferenceDisplay();
$this
->assertEntityReferenceFormDisplay();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('admin/config/regional/content-language');
$this
->submitForm([
'settings[node][referrer][translatable]' => FALSE,
], 'Save configuration');
$this
->drupalLogout();
$this->referrerEntity = $this
->createReferrerEntity(FALSE);
$this
->assertEntityReferenceDisplay();
$this
->assertEntityReferenceFormDisplay();
}
protected function assertEntityReferenceDisplay() : void {
$url = $this->referrerEntity
->toUrl();
$translation_url = $this->referrerEntity
->toUrl('canonical', [
'language' => ConfigurableLanguage::load($this->translateToLangcode),
]);
$this
->drupalGet($url);
$this
->assertSession()
->pageTextContains($this->labelOfNotTranslatedReference);
$this
->assertSession()
->pageTextContains($this->originalLabel);
$this
->assertSession()
->pageTextNotContains($this->translatedLabel);
$this
->drupalGet($translation_url);
$this
->assertSession()
->pageTextContains($this->labelOfNotTranslatedReference);
$this
->assertSession()
->pageTextNotContains($this->originalLabel);
$this
->assertSession()
->pageTextContains($this->translatedLabel);
}
protected function assertEntityReferenceFormDisplay() : void {
$this
->drupalLogin($this->webUser);
$url = $this->referrerEntity
->toUrl('edit-form');
$translation_url = $this->referrerEntity
->toUrl('edit-form', [
'language' => ConfigurableLanguage::load($this->translateToLangcode),
]);
$this
->drupalGet($url);
$this
->assertSession()
->fieldValueEquals('test_reference_field[0][target_id]', $this->originalLabel . ' (1)');
$this
->assertSession()
->fieldValueEquals('test_reference_field[1][target_id]', $this->labelOfNotTranslatedReference . ' (2)');
$this
->drupalGet($translation_url);
$this
->assertSession()
->fieldValueEquals('test_reference_field[0][target_id]', $this->translatedLabel . ' (1)');
$this
->assertSession()
->fieldValueEquals('test_reference_field[1][target_id]', $this->labelOfNotTranslatedReference . ' (2)');
$this
->drupalLogout();
}
protected function setUpLanguages() {
ConfigurableLanguage::createFromLangcode($this->translateToLangcode)
->save();
}
protected function createContent() {
$this->referencedEntityWithTranslation = $this
->createReferencedEntityWithTranslation();
$this->referencedEntityWithoutTranslation = $this
->createNotTranslatedReferencedEntity();
}
protected function enableTranslation() {
\Drupal::service('content_translation.manager')
->setEnabled($this->testEntityTypeName, $this->referrerType
->id(), TRUE);
\Drupal::service('content_translation.manager')
->setEnabled($this->testEntityTypeName, $this->referencedType
->id(), TRUE);
}
protected function setUpEntityReferenceField() {
FieldStorageConfig::create([
'field_name' => $this->referenceFieldName,
'entity_type' => $this->testEntityTypeName,
'type' => 'entity_reference',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'translatable' => $this->translatable,
'settings' => [
'allowed_values' => [
[
'target_type' => $this->testEntityTypeName,
],
],
],
])
->save();
FieldConfig::create([
'field_name' => $this->referenceFieldName,
'bundle' => $this->referrerType
->id(),
'entity_type' => $this->testEntityTypeName,
])
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay($this->testEntityTypeName, $this->referrerType
->id())
->setComponent($this->referenceFieldName, [
'type' => 'entity_reference_autocomplete',
])
->save();
$display_repository
->getViewDisplay($this->testEntityTypeName, $this->referrerType
->id())
->setComponent($this->referenceFieldName, [
'type' => 'entity_reference_label',
])
->save();
}
protected function setUpContentTypes() {
$this->referrerType = $this
->drupalCreateContentType([
'type' => 'referrer',
'name' => 'Referrer',
]);
$this->referencedType = $this
->drupalCreateContentType([
'type' => 'referenced_page',
'name' => 'Referenced Page',
]);
}
protected function createReferencedEntityWithTranslation() {
$node = \Drupal::entityTypeManager()
->getStorage($this->testEntityTypeName)
->create([
'title' => $this->originalLabel,
'type' => $this->referencedType
->id(),
'description' => [
'value' => $this
->randomMachineName(),
'format' => 'basic_html',
],
'langcode' => $this->baseLangcode,
]);
$node
->save();
$node
->addTranslation($this->translateToLangcode, [
'title' => $this->translatedLabel,
]);
$node
->save();
return $node;
}
protected function createNotTranslatedReferencedEntity() {
$node = \Drupal::entityTypeManager()
->getStorage($this->testEntityTypeName)
->create([
'title' => $this->labelOfNotTranslatedReference,
'type' => $this->referencedType
->id(),
'description' => [
'value' => $this
->randomMachineName(),
'format' => 'basic_html',
],
'langcode' => $this->baseLangcode,
]);
$node
->save();
return $node;
}
protected function createReferrerEntity($translatable = TRUE) {
$node = \Drupal::entityTypeManager()
->getStorage($this->testEntityTypeName)
->create([
'title' => $this
->randomMachineName(),
'type' => $this->referrerType
->id(),
'description' => [
'value' => $this
->randomMachineName(),
'format' => 'basic_html',
],
$this->referenceFieldName => [
[
'target_id' => $this->referencedEntityWithTranslation
->id(),
],
[
'target_id' => $this->referencedEntityWithoutTranslation
->id(),
],
],
'langcode' => $this->baseLangcode,
]);
if ($translatable) {
$node
->addTranslation($this->translateToLangcode, $node
->toArray());
}
$node
->save();
return $node;
}
}