You are here

public function ContentEntityUpdateParagraphTest::testUpdateParagraphTranslation in Translation Management Tool 8

Tests that paragraph items with existing translations gets new translation.

File

sources/content/tests/src/Kernel/ContentEntityUpdateParagraphTest.php, line 102

Class

ContentEntityUpdateParagraphTest
Content entity update paragraphs test.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

public function testUpdateParagraphTranslation() {

  // Create the initial paragraph in defualt langauge.
  $paragraph = Paragraph::create([
    'text' => 'Initial Source Paragraph #1',
    'type' => 'text',
  ]);
  $paragraph
    ->save();

  // Create the initial test node with a reference to the paragraph.
  $node = Node::create([
    'langcode' => 'en',
    'title' => 'Initial Source Node #1',
    'type' => 'article',
    'paragraph_reference' => $paragraph,
  ]);
  $node
    ->save();
  $this
    ->assertRevisionCount(1, 'node', $node
    ->id());
  $this
    ->assertRevisionCount(1, 'paragraph', $paragraph
    ->id());

  // Add an initial translation so that we can test that we're able to update
  // a Content source translation with an already existing translation.
  $node_storage = \Drupal::entityTypeManager()
    ->getStorage('node');
  $node_de = $node
    ->addTranslation('de', [
    'title' => 'Initial host translation DE',
  ] + $node
    ->toArray());
  $node_de = $node_storage
    ->createRevision($node_de, FALSE);
  $node_de->paragraph_reference->entity
    ->getTranslation('de')
    ->set('text', 'Initial paragraph translation DE');
  $node_de
    ->isDefaultRevision(TRUE);
  $node_de
    ->save();
  $node_de = Node::load($node_de
    ->id())
    ->getTranslation('de');
  $this
    ->assertEquals('Initial host translation DE', $node_de->title->value);
  $this
    ->assertEquals('Initial paragraph translation DE', $node_de->paragraph_reference->entity
    ->getTranslation('de')->text->value);
  $this
    ->assertRevisionCount(2, 'node', $node
    ->id());
  $this
    ->assertRevisionCount(2, 'paragraph', $paragraph
    ->id());

  // Create a new job and job_item for the node.
  $job = tmgmt_job_create('en', 'de');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', 'node', $node
    ->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.
  $node = Node::load($node
    ->id());
  $translation = $node
    ->getTranslation('de');
  $this
    ->assertEquals('de(de-ch): Initial Source Node #1', $translation->title->value);
  $this
    ->assertEquals('de(de-ch): Initial Source Paragraph #1', $translation->paragraph_reference->entity
    ->getTranslation('de')->text->value);
  $this
    ->assertRevisionCount(3, 'node', $node
    ->id());
  $this
    ->assertRevisionCount(3, 'paragraph', $paragraph
    ->id());

  // Update the node and paragraph so that we can test if the translations are
  // updated.
  $node_storage = \Drupal::entityTypeManager()
    ->getStorage('node');
  $node = $node
    ->set('title', 'Updated Source Node #2');
  $node = $node_storage
    ->createRevision($node, FALSE);
  $node->paragraph_reference->entity
    ->set('text', 'Updated Source Paragraph #2');
  $node
    ->isDefaultRevision(TRUE);
  $node
    ->save();
  $this
    ->assertRevisionCount(4, 'node', $node
    ->id());
  $this
    ->assertRevisionCount(4, 'paragraph', $paragraph
    ->id());
  $node = Node::load($node
    ->id());

  // Create a new job but this time we'll reset the entity cache before
  // accepting the translation.
  $job = tmgmt_job_create('en', 'de');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', 'node', $node
    ->id(), [
    'tjid' => $job
      ->id(),
  ]);
  $job_item
    ->save();

  // Reset the cache since we cannot know the state of the cache when
  // importing the translation.
  \Drupal::service('entity.memory_cache')
    ->reset();

  // 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.
  $node = Node::load($node
    ->id());
  $translation = $node
    ->getTranslation('de');
  $this
    ->assertEquals('de(de-ch): Updated Source Node #2', $translation->title->value);
  $this
    ->assertEquals('de(de-ch): Updated Source Paragraph #2', $translation->paragraph_reference->entity
    ->getTranslation('de')->text->value);
  $this
    ->assertRevisionCount(5, 'node', $node
    ->id());
  $this
    ->assertRevisionCount(5, 'paragraph', $paragraph
    ->id());
}