View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
class LingotekSaveTargetDataTest extends LingotekTestBase {
public static $modules = [
'node',
];
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this->rootUser);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_ES')
->save();
ConfigurableLanguage::createFromLangcode('de')
->setThirdPartySetting('lingotek', 'locale', 'de_DE')
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
drupal_static_reset();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
$this
->applyEntityUpdates();
$this
->rebuildContainer();
$this
->saveLingotekContentTranslationSettingsForNodeTypes();
}
public function testRightNodeIsSavedIfThereIsNoRevisionInMetadata() {
$node1 = $this
->createNode([
'type' => 'article',
'title' => 'Node 1',
]);
$node1
->save();
$node2 = $this
->createNode([
'type' => 'article',
'title' => 'Node 2',
]);
$node2
->save();
$translation_service = \Drupal::service('lingotek.content_translation');
$es_data = [
'title' => [
0 => [
'value' => 'Nodo 2 ES',
],
],
'body' => [
0 => [
'value' => 'es body',
],
],
'_lingotek_metadata' => [
'_entity_type_id' => 'node',
'_entity_id' => 2,
],
];
$translation_service
->saveTargetData($node2, 'es', $es_data);
$nodeUntranslated = \Drupal::entityTypeManager()
->getStorage('node')
->load(1);
$this
->assertFalse($nodeUntranslated
->hasTranslation('es'));
$nodeTranslated = \Drupal::entityTypeManager()
->getStorage('node')
->load(2);
$this
->assertTrue($nodeTranslated
->hasTranslation('es'));
}
public function testRightRevisionsAreSavedIfThereIsMetadata() {
$node = $this
->createNode([
'type' => 'article',
'title' => 'Revision 1',
]);
$node
->setTitle('Revision 2');
$node
->setNewRevision();
$node
->save();
$node
->setTitle('Revision 3');
$node
->setNewRevision();
$node
->save();
$translation_service = \Drupal::service('lingotek.content_translation');
$es_data = [
'title' => [
0 => [
'value' => 'Revision 2 ES',
],
],
'body' => [
0 => [
'value' => 'es body',
],
],
'_lingotek_metadata' => [
'_entity_type_id' => 'node',
'_entity_id' => 1,
'_entity_revision' => 2,
],
];
$translation_service
->saveTargetData($node, 'es', $es_data);
$node = \Drupal::entityTypeManager()
->getStorage('node')
->load(1);
$node = $node
->getTranslation('es');
$this
->assertEqual('es body', $node->body->value, 'The body is translated correctly.');
$this
->assertEqual('Revision 2 ES', $node
->getTitle(), 'The title in the revision translation is the one given.');
$this
->assertEqual(3, $node
->getRevisionId(), 'The translation is saved in the newest revision.');
}
public function testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable() {
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage
->setTranslatable(TRUE)
->save();
$field = FieldConfig::loadByName('node', 'article', 'body');
$field
->setTranslatable(TRUE)
->save();
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field = FieldConfig::loadByName('node', 'article', 'body');
$this
->assertTrue($field_storage
->isTranslatable(), 'Field storage is translatable.');
$this
->assertTrue($field
->isTranslatable(), 'Field instance is translatable.');
$this
->createNode([
'type' => 'article',
]);
$node = Node::load(1);
$title = $node
->getTranslation('en')
->getTitle();
$body = $node
->getTranslation('en')->body->value;
$this
->verbose($body);
$translation_service = \Drupal::service('lingotek.content_translation');
$es_data = [
'title' => [
0 => [
'value' => 'es title',
],
],
'body' => [
0 => [
'value' => 'es body',
],
],
];
$node = $translation_service
->saveTargetData($node, 'es', $es_data);
$this
->assertEqual('es body', $node
->getTranslation('es')->body->value, 'The body is translated if the field is translatable.');
$this
->assertEqual($body, $node
->getTranslation('en')->body->value, 'The body in the original language is not overridden.');
$this
->assertEqual('es title', $node
->getTranslation('es')
->getTitle(), 'The title in the translation is the one given.');
$this
->assertEqual($title, $node
->getTranslation('en')
->getTitle(), 'The title in the original language is not overridden.');
$field
->setTranslatable(FALSE)
->save();
$this
->assertTrue($field_storage
->isTranslatable(), 'Field storage is translatable.');
$this
->assertFalse($field
->isTranslatable(), 'Field instance is not translatable.');
$de_data = [
'title' => [
0 => [
'value' => 'de title',
],
],
'body' => [
0 => [
'value' => 'de body',
],
],
];
$node = $translation_service
->saveTargetData($node, 'de', $de_data);
$this
->assertEqual($body, $node
->getTranslation('de')->body->value, 'The body is not written if the field is not translatable.');
$this
->assertEqual($body, $node
->getTranslation('en')->body->value, 'The body is not overridden if the field is not translatable.');
$this
->assertEqual('de title', $node
->getTranslation('de')
->getTitle(), 'The title in the translation is the one given.');
$this
->assertEqual($title, $node
->getTranslation('en')
->getTitle(), 'The title in the original language is not overridden.');
}
}