View source
<?php
namespace Drupal\field\Tests\EntityReference;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\simpletest\WebTestBase;
class EntityReferenceFieldTranslatedReferenceViewTest extends WebTestBase {
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;
public static $modules = array(
'language',
'content_translation',
'node',
);
protected function setUp() {
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();
}
public function testEntityReferenceDisplay() {
$this->referrerEntity = $this
->createReferrerEntity();
$this
->assertEntityReferenceDisplay();
$this
->drupalLogin($this->rootUser);
$this
->drupalPostForm('admin/config/regional/content-language', [
'settings[node][referrer][translatable]' => FALSE,
], t('Save configuration'));
$this->referrerEntity = $this
->createReferrerEntity(FALSE);
$this
->assertEntityReferenceDisplay();
}
protected function assertEntityReferenceDisplay() {
$url = $this->referrerEntity
->urlInfo();
$translation_url = $this->referrerEntity
->urlInfo('canonical', [
'language' => ConfigurableLanguage::load($this->translateToLangcode),
]);
$this
->drupalGet($url);
$this
->assertText($this->labelOfNotTranslatedReference, 'The label of not translated reference is displayed.');
$this
->assertText($this->originalLabel, 'The default label of translated reference is displayed.');
$this
->assertNoText($this->translatedLabel, 'The translated label of translated reference is not displayed.');
$this
->drupalGet($translation_url);
$this
->assertText($this->labelOfNotTranslatedReference, 'The label of not translated reference is displayed.');
$this
->assertNoText($this->originalLabel, 'The default label of translated reference is not displayed.');
$this
->assertText($this->translatedLabel, 'The translated label of translated reference is displayed.');
}
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);
drupal_static_reset();
\Drupal::entityManager()
->clearCachedDefinitions();
\Drupal::service('router.builder')
->rebuild();
\Drupal::service('entity.definition_update_manager')
->applyUpdates();
}
protected function setUpEntityReferenceField() {
entity_create('field_storage_config', array(
'field_name' => $this->referenceFieldName,
'entity_type' => $this->testEntityTypeName,
'type' => 'entity_reference',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'translatable' => $this->translatable,
'settings' => array(
'allowed_values' => array(
array(
'target_type' => $this->testEntityTypeName,
),
),
),
))
->save();
entity_create('field_config', array(
'field_name' => $this->referenceFieldName,
'bundle' => $this->referrerType
->id(),
'entity_type' => $this->testEntityTypeName,
))
->save();
entity_get_form_display($this->testEntityTypeName, $this->referrerType
->id(), 'default')
->setComponent($this->referenceFieldName, array(
'type' => 'entity_reference_autocomplete',
))
->save();
entity_get_display($this->testEntityTypeName, $this->referrerType
->id(), 'default')
->setComponent($this->referenceFieldName, array(
'type' => 'entity_reference_label',
))
->save();
}
protected function setUpContentTypes() {
$this->referrerType = $this
->drupalCreateContentType(array(
'type' => 'referrer',
'name' => 'Referrer',
));
$this->referencedType = $this
->drupalCreateContentType(array(
'type' => 'referenced_page',
'name' => 'Referenced Page',
));
}
protected function createReferencedEntityWithTranslation() {
$node = entity_create($this->testEntityTypeName, array(
'title' => $this->originalLabel,
'type' => $this->referencedType
->id(),
'description' => array(
'value' => $this
->randomMachineName(),
'format' => 'basic_html',
),
'langcode' => $this->baseLangcode,
));
$node
->save();
$node
->addTranslation($this->translateToLangcode, array(
'title' => $this->translatedLabel,
));
$node
->save();
return $node;
}
protected function createNotTranslatedReferencedEntity() {
$node = entity_create($this->testEntityTypeName, array(
'title' => $this->labelOfNotTranslatedReference,
'type' => $this->referencedType
->id(),
'description' => array(
'value' => $this
->randomMachineName(),
'format' => 'basic_html',
),
'langcode' => $this->baseLangcode,
));
$node
->save();
return $node;
}
protected function createReferrerEntity($translatable = TRUE) {
$node = entity_create($this->testEntityTypeName, array(
'title' => $this
->randomMachineName(),
'type' => $this->referrerType
->id(),
'description' => array(
'value' => $this
->randomMachineName(),
'format' => 'basic_html',
),
$this->referenceFieldName => array(
array(
'target_id' => $this->referencedEntityWithTranslation
->id(),
),
array(
'target_id' => $this->referencedEntityWithoutTranslation
->id(),
),
),
'langcode' => $this->baseLangcode,
));
if ($translatable) {
$node
->addTranslation($this->translateToLangcode, $node
->toArray());
}
$node
->save();
return $node;
}
}