View source
<?php
namespace Drupal\content_translation\Tests;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
abstract class ContentTranslationUITestBase extends ContentTranslationTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected $entityId;
protected $testLanguageSelector = TRUE;
protected $testHTMLEscapeForAllLanguages = FALSE;
protected $defaultCacheContexts = [
'languages:language_interface',
'theme',
'url.query_args:_wrapper_format',
'user.permissions',
];
function testTranslationUI() {
$this
->doTestBasicTranslation();
$this
->doTestTranslationOverview();
$this
->doTestOutdatedStatus();
$this
->doTestPublishedStatus();
$this
->doTestAuthoringInfo();
$this
->doTestTranslationEdit();
$this
->doTestTranslationChanged();
$this
->doTestChangedTimeAfterSaveWithoutChanges();
$this
->doTestTranslationDeletion();
}
protected function doTestBasicTranslation() {
$default_langcode = $this->langcodes[0];
$values[$default_langcode] = $this
->getNewEntityValues($default_langcode);
$this
->drupalLogin($this->editor);
$this->entityId = $this
->createEntity($values[$default_langcode], $default_langcode);
$this
->drupalLogin($this->translator);
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this
->assertTrue($entity, 'Entity found in the database.');
$this
->drupalGet($entity
->urlInfo());
$this
->assertResponse(200, 'Entity URL is valid.');
$this
->assertCacheContexts($this->defaultCacheContexts);
$this
->drupalGet($entity
->urlInfo('drupal:content-translation-overview'));
$this
->assertNoText('Source language', 'Source language column correctly hidden.');
$translation = $this
->getTranslation($entity, $default_langcode);
foreach ($values[$default_langcode] as $property => $value) {
$stored_value = $this
->getValue($translation, $property, $default_langcode);
$value = is_array($value) ? $value[0]['value'] : $value;
$message = format_string('@property correctly stored in the default language.', array(
'@property' => $property,
));
$this
->assertEqual($stored_value, $value, $message);
}
$langcode = 'it';
$language = ConfigurableLanguage::load($langcode);
$values[$langcode] = $this
->getNewEntityValues($langcode);
$entity_type_id = $entity
->getEntityTypeId();
$add_url = Url::fromRoute("entity.{$entity_type_id}.content_translation_add", [
$entity
->getEntityTypeId() => $entity
->id(),
'source' => $default_langcode,
'target' => $langcode,
], array(
'language' => $language,
));
$this
->drupalPostForm($add_url, $this
->getEditValues($values, $langcode), $this
->getFormSubmitActionForNewTranslation($entity, $langcode));
if ($this->testHTMLEscapeForAllLanguages) {
$this
->assertNoRaw('<span class="translation-entity-all-languages">(all languages)</span>');
$this
->assertRaw('<span class="translation-entity-all-languages">(all languages)</span>');
}
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this
->drupalGet($entity
->urlInfo());
$this
->assertCacheContexts(Cache::mergeContexts([
'languages:language_content',
], $this->defaultCacheContexts));
$metadata_source_translation = $this->manager
->getTranslationMetadata($entity
->getTranslation($default_langcode));
$metadata_target_translation = $this->manager
->getTranslationMetadata($entity
->getTranslation($langcode));
$author_field_name = $entity
->hasField('content_translation_uid') ? 'content_translation_uid' : 'uid';
if ($entity
->getFieldDefinition($author_field_name)
->isTranslatable()) {
$this
->assertEqual($metadata_target_translation
->getAuthor()
->id(), $this->translator
->id(), SafeMarkup::format('Author of the target translation @langcode correctly stored for translatable owner field.', array(
'@langcode' => $langcode,
)));
$this
->assertNotEqual($metadata_target_translation
->getAuthor()
->id(), $metadata_source_translation
->getAuthor()
->id(), SafeMarkup::format('Author of the target translation @target different from the author of the source translation @source for translatable owner field.', array(
'@target' => $langcode,
'@source' => $default_langcode,
)));
}
else {
$this
->assertEqual($metadata_target_translation
->getAuthor()
->id(), $this->editor
->id(), 'Author of the entity remained untouched after translation for non translatable owner field.');
}
$created_field_name = $entity
->hasField('content_translation_created') ? 'content_translation_created' : 'created';
if ($entity
->getFieldDefinition($created_field_name)
->isTranslatable()) {
$this
->assertTrue($metadata_target_translation
->getCreatedTime() > $metadata_source_translation
->getCreatedTime(), SafeMarkup::format('Translation creation timestamp of the target translation @target is newer than the creation timestamp of the source translation @source for translatable created field.', array(
'@target' => $langcode,
'@source' => $default_langcode,
)));
}
else {
$this
->assertEqual($metadata_target_translation
->getCreatedTime(), $metadata_source_translation
->getCreatedTime(), 'Creation timestamp of the entity remained untouched after translation for non translatable created field.');
}
if ($this->testLanguageSelector) {
$this
->assertNoFieldByXPath('//select[@id="edit-langcode-0-value"]', NULL, 'Language selector correctly disabled on translations.');
}
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this
->drupalGet($entity
->urlInfo('drupal:content-translation-overview'));
$this
->assertNoText('Source language', 'Source language column correctly hidden.');
$langcode = 'fr';
$language = ConfigurableLanguage::load($langcode);
$source_langcode = 'it';
$edit = array(
'source_langcode[source]' => $source_langcode,
);
$entity_type_id = $entity
->getEntityTypeId();
$add_url = Url::fromRoute("entity.{$entity_type_id}.content_translation_add", [
$entity
->getEntityTypeId() => $entity
->id(),
'source' => $default_langcode,
'target' => $langcode,
], array(
'language' => $language,
));
$this
->drupalPostForm($add_url, $edit, t('Change'));
$this
->assertFieldByXPath("//input[@name=\"{$this->fieldName}[0][value]\"]", $values[$source_langcode][$this->fieldName][0]['value'], 'Source language correctly switched.');
$values[$langcode] = $this
->getNewEntityValues($langcode);
$edit = $this
->getEditValues($values, $langcode) + array(
'content_translation[retranslate]' => TRUE,
);
$entity_type_id = $entity
->getEntityTypeId();
$add_url = Url::fromRoute("entity.{$entity_type_id}.content_translation_add", [
$entity
->getEntityTypeId() => $entity
->id(),
'source' => $source_langcode,
'target' => $langcode,
], array(
'language' => $language,
));
$this
->drupalPostForm($add_url, $edit, $this
->getFormSubmitActionForNewTranslation($entity, $langcode));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this
->drupalGet($entity
->urlInfo('drupal:content-translation-overview'));
$this
->assertText('Source language', 'Source language column correctly shown.');
foreach ($values as $langcode => $property_values) {
$translation = $this
->getTranslation($entity, $langcode);
foreach ($property_values as $property => $value) {
$stored_value = $this
->getValue($translation, $property, $langcode);
$value = is_array($value) ? $value[0]['value'] : $value;
$message = format_string('%property correctly stored with language %language.', array(
'%property' => $property,
'%language' => $langcode,
));
$this
->assertEqual($stored_value, $value, $message);
}
}
}
protected function doTestTranslationOverview() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$translate_url = $entity
->urlInfo('drupal:content-translation-overview');
$this
->drupalGet($translate_url);
$translate_url
->setAbsolute(FALSE);
foreach ($this->langcodes as $langcode) {
if ($entity
->hasTranslation($langcode)) {
$language = new Language(array(
'id' => $langcode,
));
$view_url = $entity
->url('canonical', [
'language' => $language,
]);
$elements = $this
->xpath('//table//a[@href=:href]', [
':href' => $view_url,
]);
$this
->assertEqual((string) $elements[0], $entity
->getTranslation($langcode)
->label(), format_string('Label correctly shown for %language translation.', array(
'%language' => $langcode,
)));
$edit_path = $entity
->url('edit-form', array(
'language' => $language,
));
$elements = $this
->xpath('//table//ul[@class="dropbutton"]/li/a[@href=:href]', array(
':href' => $edit_path,
));
$this
->assertEqual((string) $elements[0], t('Edit'), format_string('Edit link correct for %language translation.', array(
'%language' => $langcode,
)));
}
}
}
protected function doTestOutdatedStatus() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$langcode = 'fr';
$languages = \Drupal::languageManager()
->getLanguages();
$edit = array(
'content_translation[retranslate]' => TRUE,
);
$edit_path = $entity
->urlInfo('edit-form', array(
'language' => $languages[$langcode],
));
$this
->drupalPostForm($edit_path, $edit, $this
->getFormSubmitAction($entity, $langcode));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
foreach ($this->langcodes as $added_langcode) {
$url = $entity
->urlInfo('edit-form', array(
'language' => ConfigurableLanguage::load($added_langcode),
));
$this
->drupalGet($url);
if ($added_langcode == $langcode) {
$this
->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is not checked by default.');
$this
->assertFalse($this
->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab should be collapsed by default.');
}
else {
$this
->assertFieldByXPath('//input[@name="content_translation[outdated]"]', TRUE, 'The translate flag is checked by default.');
$this
->assertTrue($this
->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab is correctly expanded when the translation is outdated.');
$edit = array(
'content_translation[outdated]' => FALSE,
);
$this
->drupalPostForm($url, $edit, $this
->getFormSubmitAction($entity, $added_langcode));
$this
->drupalGet($url);
$this
->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is now shown.');
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this
->assertFalse($this->manager
->getTranslationMetadata($entity
->getTranslation($added_langcode))
->isOutdated(), 'The "outdated" status has been correctly stored.');
}
}
}
protected function doTestPublishedStatus() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
foreach ($this->langcodes as $index => $langcode) {
if ($index > 0) {
$url = $entity
->urlInfo('edit-form', array(
'language' => ConfigurableLanguage::load($langcode),
));
$edit = array(
'content_translation[status]' => FALSE,
);
$this
->drupalPostForm($url, $edit, $this
->getFormSubmitAction($entity, $langcode));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this
->assertFalse($this->manager
->getTranslationMetadata($entity
->getTranslation($langcode))
->isPublished(), 'The translation has been correctly unpublished.');
}
}
$this
->drupalGet($entity
->urlInfo('edit-form'));
$this
->assertFieldByXPath('//input[@name="content_translation[status]" and @disabled="disabled"]', TRUE, 'The last translation is published and cannot be unpublished.');
}
protected function doTestAuthoringInfo() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$values = array();
foreach ($this->langcodes as $index => $langcode) {
$user = $this
->drupalCreateUser();
$values[$langcode] = array(
'uid' => $user
->id(),
'created' => REQUEST_TIME - mt_rand(0, 1000),
);
$edit = array(
'content_translation[uid]' => $user
->getUsername(),
'content_translation[created]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
);
$url = $entity
->urlInfo('edit-form', array(
'language' => ConfigurableLanguage::load($langcode),
));
$this
->drupalPostForm($url, $edit, $this
->getFormSubmitAction($entity, $langcode));
}
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
foreach ($this->langcodes as $langcode) {
$metadata = $this->manager
->getTranslationMetadata($entity
->getTranslation($langcode));
$this
->assertEqual($metadata
->getAuthor()
->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
$this
->assertEqual($metadata
->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
}
$langcode = 'en';
$edit = array(
'content_translation[uid]' => $this
->randomMachineName(12),
'content_translation[created]' => '19/11/1978',
);
$this
->drupalPostForm($entity
->urlInfo('edit-form'), $edit, $this
->getFormSubmitAction($entity, $langcode));
$this
->assertTrue($this
->xpath('//div[contains(@class, "error")]//ul'), 'Invalid values generate a list of form errors.');
$metadata = $this->manager
->getTranslationMetadata($entity
->getTranslation($langcode));
$this
->assertEqual($metadata
->getAuthor()
->id(), $values[$langcode]['uid'], 'Translation author correctly kept.');
$this
->assertEqual($metadata
->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly kept.');
}
protected function doTestTranslationDeletion() {
$this
->drupalLogin($this->translator);
$langcode = 'fr';
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$language = ConfigurableLanguage::load($langcode);
$url = $entity
->urlInfo('edit-form', array(
'language' => $language,
));
$this
->drupalPostForm($url, array(), t('Delete translation'));
$this
->drupalPostForm(NULL, array(), t('Delete @language translation', array(
'@language' => $language
->getName(),
)));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
if ($this
->assertTrue(is_object($entity), 'Entity found')) {
$translations = $entity
->getTranslationLanguages();
$this
->assertTrue(count($translations) == 2 && empty($translations[$langcode]), 'Translation successfully deleted.');
}
$args = [
$this->entityTypeId => $entity
->id(),
'language' => 'en',
];
$this
->drupalGet(Url::fromRoute("entity.{$this->entityTypeId}.content_translation_delete", $args));
$this
->assertResponse(403);
}
protected function getNewEntityValues($langcode) {
return array(
$this->fieldName => array(
array(
'value' => $this
->randomMachineName(16),
),
),
);
}
protected function getEditValues($values, $langcode, $new = FALSE) {
$edit = $values[$langcode];
$langcode = $new ? LanguageInterface::LANGCODE_NOT_SPECIFIED : $langcode;
foreach ($values[$langcode] as $property => $value) {
if (is_array($value)) {
$edit["{$property}[0][value]"] = $value[0]['value'];
unset($edit[$property]);
}
}
return $edit;
}
protected function getFormSubmitActionForNewTranslation(EntityInterface $entity, $langcode) {
$entity
->addTranslation($langcode, $entity
->toArray());
return $this
->getFormSubmitAction($entity, $langcode);
}
protected function getFormSubmitAction(EntityInterface $entity, $langcode) {
return t('Save') . $this
->getFormSubmitSuffix($entity, $langcode);
}
protected function getFormSubmitSuffix(EntityInterface $entity, $langcode) {
return '';
}
protected function getTranslation(EntityInterface $entity, $langcode) {
return $entity
->getTranslation($langcode);
}
protected function getValue(EntityInterface $translation, $property, $langcode) {
$key = $property == 'user_id' ? 'target_id' : 'value';
return $translation
->get($property)->{$key};
}
protected function getChangedFieldName($entity) {
return $entity
->hasField('content_translation_changed') ? 'content_translation_changed' : 'changed';
}
protected function doTestTranslationEdit() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$languages = $this->container
->get('language_manager')
->getLanguages();
foreach ($this->langcodes as $langcode) {
if ($langcode != 'en') {
$options = array(
'language' => $languages[$langcode],
);
$url = $entity
->urlInfo('edit-form', $options);
$this
->drupalGet($url);
$this
->assertRaw($entity
->getTranslation($langcode)
->label());
}
}
}
protected function doTestTranslationChanged() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$changed_field_name = $this
->getChangedFieldName($entity);
$definition = $entity
->getFieldDefinition($changed_field_name);
$config = $definition
->getConfig($entity
->bundle());
foreach ([
FALSE,
TRUE,
] as $translatable_changed_field) {
if ($definition
->isTranslatable()) {
$config
->setTranslatable($translatable_changed_field);
$config
->save();
}
elseif ($translatable_changed_field) {
break;
}
foreach ($entity
->getTranslationLanguages() as $language) {
sleep(1);
$langcode = $language
->getId();
$edit = array(
$this->fieldName . '[0][value]' => $this
->randomString(),
);
$edit_path = $entity
->urlInfo('edit-form', array(
'language' => $language,
));
$this
->drupalPostForm($edit_path, $edit, $this
->getFormSubmitAction($entity, $langcode));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this
->assertEqual($entity
->getChangedTimeAcrossTranslations(), $entity
->getTranslation($langcode)
->getChangedTime(), format_string('Changed time for language %language is the latest change over all languages.', array(
'%language' => $language
->getName(),
)));
}
$timestamps = array();
foreach ($entity
->getTranslationLanguages() as $language) {
$next_timestamp = $entity
->getTranslation($language
->getId())
->getChangedTime();
if (!in_array($next_timestamp, $timestamps)) {
$timestamps[] = $next_timestamp;
}
}
if ($translatable_changed_field) {
$this
->assertEqual(count($timestamps), count($entity
->getTranslationLanguages()), 'All timestamps from all languages are different.');
}
else {
$this
->assertEqual(count($timestamps), 1, 'All timestamps from all languages are identical.');
}
}
}
public function doTestChangedTimeAfterSaveWithoutChanges() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
if ($entity
->getEntityType()
->isSubclassOf('Drupal\\Core\\Entity\\EntityChangedInterface')) {
$changed_timestamp = $entity
->getChangedTime();
$entity
->save();
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this
->assertEqual($changed_timestamp, $entity
->getChangedTime(), 'The entity\'s changed time wasn\'t updated after API save without changes.');
sleep(1);
$language = $entity
->language();
$edit_path = $entity
->urlInfo('edit-form', array(
'language' => $language,
));
$this
->drupalPostForm($edit_path, [], $this
->getFormSubmitAction($entity, $language
->getId()));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this
->assertNotEqual($changed_timestamp, $entity
->getChangedTime(), 'The entity\'s changed time was updated after form save without changes.');
}
}
}