You are here

public function TranslationormalizerTest::testTranslationNormalization in Default Content for D8 2.0.x

Tests menu_link_content entities.

File

tests/src/Kernel/TranslationNormalizerTest.php, line 68

Class

TranslationormalizerTest
Tests export functionality.

Namespace

Drupal\Tests\default_content\Kernel

Code

public function testTranslationNormalization() {

  /** @var \Drupal\node\NodeInterface $node */
  $node = Node::create([
    'type' => 'page',
    'title' => 'English Title',
    'langcode' => 'en',
  ]);
  $node
    ->addTranslation('de', [
    'title' => 'German Title',
  ]);
  $node
    ->addTranslation('fr', [
    'title' => 'French Title',
  ]);
  $node
    ->save();

  /** @var \Drupal\default_content\Normalizer\ContentEntityNormalizerInterface $normalizer */
  $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();

  // Denormalize it back, add an extra translation that doesn't exist, that
  // should be ignored.
  $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());

  // Change the default translation language to a language that isn't known,
  // an available language will be picked from the translations and imported
  // with those values.
  $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 all translations, then the entity is created in english.
  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'));
}