View source
<?php
namespace Drupal\Tests\default_content\Kernel;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\KernelTests\KernelTestBase;
class TranslationormalizerTest extends KernelTestBase {
use EntityReferenceTestTrait;
public static $modules = [
'system',
'user',
'field',
'serialization',
'hal',
'default_content',
'content_translation',
'language',
'node',
];
protected $exporter;
protected function setUp() : void {
parent::setUp();
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
NodeType::create([
'type' => 'page',
'name' => 'page',
])
->save();
$this
->installConfig([
'language',
]);
ConfigurableLanguage::createFromLangcode('de')
->save();
ConfigurableLanguage::createFromLangcode('fr')
->save();
}
public function testTranslationNormalization() {
$node = Node::create([
'type' => 'page',
'title' => 'English Title',
'langcode' => 'en',
]);
$node
->addTranslation('de', [
'title' => 'German Title',
]);
$node
->addTranslation('fr', [
'title' => 'French Title',
]);
$node
->save();
$normalizer = \Drupal::service('default_content.content_entity_normalizer');
$normalized = $normalizer
->normalize($node);
$expected = [
'_meta' => [
'version' => '1.0',
'entity_type' => 'node',
'uuid' => $node
->uuid(),
'bundle' => 'page',
'default_langcode' => 'en',
],
'default' => [
'revision_uid' => [
0 => [
'target_id' => 0,
],
],
'status' => [
0 => [
'value' => TRUE,
],
],
'uid' => [
0 => [
'target_id' => 0,
],
],
'title' => [
0 => [
'value' => 'English Title',
],
],
'created' => [
0 => [
'value' => $node
->getCreatedTime(),
],
],
'promote' => [
0 => [
'value' => TRUE,
],
],
'sticky' => [
0 => [
'value' => FALSE,
],
],
'revision_translation_affected' => [
0 => [
'value' => TRUE,
],
],
],
'translations' => [
'fr' => [
'status' => [
0 => [
'value' => TRUE,
],
],
'uid' => [
0 => [
'target_id' => 0,
],
],
'title' => [
0 => [
'value' => 'French Title',
],
],
'created' => [
0 => [
'value' => $node
->getCreatedTime(),
],
],
'promote' => [
0 => [
'value' => TRUE,
],
],
'sticky' => [
0 => [
'value' => FALSE,
],
],
'revision_translation_affected' => [
0 => [
'value' => TRUE,
],
],
],
'de' => [
'status' => [
0 => [
'value' => TRUE,
],
],
'uid' => [
0 => [
'target_id' => 0,
],
],
'title' => [
0 => [
'value' => 'German Title',
],
],
'created' => [
0 => [
'value' => $node
->getCreatedTime(),
],
],
'promote' => [
0 => [
'value' => TRUE,
],
],
'sticky' => [
0 => [
'value' => FALSE,
],
],
'revision_translation_affected' => [
0 => [
'value' => TRUE,
],
],
],
],
];
$this
->assertEquals($expected, $normalized);
$node
->delete();
$normalized['translations']['es'] = $normalized['translations']['fr'];
$recreated_node = $normalizer
->denormalize($normalized);
$this
->assertEquals('English Title', $recreated_node
->label());
$this
->assertEquals('en', $recreated_node
->language()
->getId());
$this
->assertTrue($recreated_node
->hasTranslation('de'));
$this
->assertTrue($recreated_node
->hasTranslation('fr'));
$this
->assertFalse($recreated_node
->hasTranslation('es'));
$this
->assertEquals('German Title', $recreated_node
->getTranslation('de')
->label());
$this
->assertEquals('French Title', $recreated_node
->getTranslation('fr')
->label());
$normalized['_meta']['default_langcode'] = 'it';
$recreated_node = $normalizer
->denormalize($normalized);
$this
->assertEquals('French Title', $recreated_node
->label());
$this
->assertEquals('fr', $recreated_node
->language()
->getId());
$this
->assertTrue($recreated_node
->hasTranslation('de'));
$this
->assertTrue($recreated_node
->hasTranslation('fr'));
$this
->assertFalse($recreated_node
->hasTranslation('it'));
$this
->assertEquals('German Title', $recreated_node
->getTranslation('de')
->label());
unset($normalized['translations']);
$recreated_node = $normalizer
->denormalize($normalized);
$this
->assertEquals('English Title', $recreated_node
->label());
$this
->assertEquals('en', $recreated_node
->language()
->getId());
$this
->assertFalse($recreated_node
->hasTranslation('de'));
$this
->assertFalse($recreated_node
->hasTranslation('fr'));
$this
->assertFalse($recreated_node
->hasTranslation('es'));
}
}