You are here

public function ContentEntityLinkTest::testLinkField in Translation Management Tool 8

Tests the link field integration.

File

sources/content/tests/src/Kernel/ContentEntityLinkTest.php, line 56

Class

ContentEntityLinkTest
Tests for the link integration.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

public function testLinkField() {

  // Create an entity with a link.
  $values = [
    'langcode' => 'en',
    'uid' => 1,
    'name' => 'Llama',
    'link' => [
      'title' => 'Llama title',
      'uri' => 'https://www.google.com',
    ],
  ];
  $entity = EntityTestMul::create($values);
  $entity
    ->save();
  $job = tmgmt_job_create('en', 'de');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', $this->entityTypeId, $entity
    ->id(), [
    'tjid' => $job
      ->id(),
  ]);
  $job_item
    ->save();
  $source_plugin = $this->container
    ->get('plugin.manager.tmgmt.source')
    ->createInstance('content');
  $data = $source_plugin
    ->getData($job_item);

  // Test the expected structure of the link field.
  $expected_field_data = [
    '#label' => 'Link',
    0 => [
      'uri' => [
        '#label' => 'URI',
        '#text' => 'https://www.google.com',
        '#translate' => FALSE,
      ],
      'title' => [
        '#label' => 'Link text',
        '#text' => 'Llama title',
        '#translate' => TRUE,
      ],
    ],
  ];
  $this
    ->assertEquals($expected_field_data, $data['link']);

  // Now request a translation and save it back.
  $job
    ->requestTranslation();
  $items = $job
    ->getItems();
  $item = reset($items);
  $item
    ->acceptTranslation();

  // Check that the translations were saved correctly.
  $entity = EntityTestMul::load($entity
    ->id());
  $translation = $entity
    ->getTranslation('de');
  $this
    ->assertEquals('https://www.google.com', $translation
    ->get('link')->uri);
  $this
    ->assertEquals('de(de-ch): Llama title', $translation
    ->get('link')->title);

  // Update the URI of the translation.
  $entity = EntityTestMul::load($entity
    ->id());
  $translation = $entity
    ->getTranslation('de');
  $translation
    ->get('link')->uri = 'https://www.google.de';

  // Set a new title to the link in the English translation.
  $entity
    ->get('link')->title = 'Super llama title';
  $entity
    ->save();

  // Create a new job to refresh the German translation.
  $job = tmgmt_job_create('en', 'de');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', $this->entityTypeId, $entity
    ->id(), [
    'tjid' => $job
      ->id(),
  ]);
  $job_item
    ->save();

  // Now request a translation and save it back.
  $job
    ->requestTranslation();
  $items = $job
    ->getItems();
  $item = reset($items);
  $item
    ->acceptTranslation();

  // Check that the translations were saved correctly and the customized
  // link URI was not overwritten.
  $entity = EntityTestMul::load($entity
    ->id());
  $translation = $entity
    ->getTranslation('de');
  $this
    ->assertEquals('https://www.google.de', $translation
    ->get('link')->uri);
  $this
    ->assertEquals('de(de-ch): Super llama title', $translation
    ->get('link')->title);
}