You are here

public function LingotekNodeCohesionRepeaterTranslationTest::testNodeWithCohesionLayoutCanvasTranslation in Lingotek Translation 3.5.x

Same name and namespace in other branches
  1. 4.0.x tests/src/Functional/LingotekNodeCohesionRepeaterTranslationTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeCohesionRepeaterTranslationTest::testNodeWithCohesionLayoutCanvasTranslation()
  2. 3.6.x tests/src/Functional/LingotekNodeCohesionRepeaterTranslationTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeCohesionRepeaterTranslationTest::testNodeWithCohesionLayoutCanvasTranslation()
  3. 3.7.x tests/src/Functional/LingotekNodeCohesionRepeaterTranslationTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeCohesionRepeaterTranslationTest::testNodeWithCohesionLayoutCanvasTranslation()
  4. 3.8.x tests/src/Functional/LingotekNodeCohesionRepeaterTranslationTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeCohesionRepeaterTranslationTest::testNodeWithCohesionLayoutCanvasTranslation()

Tests that a node can be translated.

File

tests/src/Functional/LingotekNodeCohesionRepeaterTranslationTest.php, line 133

Class

LingotekNodeCohesionRepeaterTranslationTest
Tests translating a node with multiple locales including paragraphs.

Namespace

Drupal\Tests\lingotek\Functional

Code

public function testNodeWithCohesionLayoutCanvasTranslation() {

  // Login as admin.
  $this
    ->drupalLogin($this->rootUser);

  // Add a site studio powered content.
  $this
    ->drupalGet('node/add/article');
  $edit = [];
  $edit['title[0][value]'] = 'Llamas are cool';
  $edit['langcode[0][value]'] = 'en';
  $edit['layout_canvas[0][target_id][json_values]'] = <<<'JSON'
{"canvas":[{"uid":"cpt_myrepeater","type":"component","title":"MyRepeater","enabled":true,"category":"category-10","componentId":"cpt_myrepeater","componentType":"component-pattern-repeater","uuid":"004a5c2f-1b9e-44d5-a59d-da42e45d9a3d","parentUid":"root","isContainer":0,"children":[]}],"mapper":{},"model":{"004a5c2f-1b9e-44d5-a59d-da42e45d9a3d":{"settings":{"title":"MyRepeater"},"a2ec3944-3dc0-476d-b84d-a7974ddf6ceb":[{"ee4da1c5-e659-46c4-8a22-8969134fcfa7":"Llamas are very cool"},{"ee4da1c5-e659-46c4-8a22-8969134fcfa7":"Dogs are very cool"},{"ee4da1c5-e659-46c4-8a22-8969134fcfa7":"Cats are very cool"}]}},"previewModel":{"004a5c2f-1b9e-44d5-a59d-da42e45d9a3d":{}},"variableFields":{"004a5c2f-1b9e-44d5-a59d-da42e45d9a3d":[]},"meta":{"fieldHistory":[]}}
JSON;
  $edit['moderation_state[0][state]'] = 'published';

  // Mink does not "see" hidden elements, so we need to set the value of the
  // hidden element directly.
  $this
    ->assertSession()
    ->elementExists('css', 'input[name="layout_canvas[0][target_id][json_values]"]')
    ->setValue($edit['layout_canvas[0][target_id][json_values]']);
  unset($edit['layout_canvas[0][target_id][json_values]']);
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this->node = Node::load(1);

  /** @var \Drupal\cohesion_elements\Entity\CohesionLayout $layout */
  $layout = CohesionLayout::load(1);
  $jsonValues = $layout
    ->get('json_values')->value;
  $jsonValuesData = Json::decode($jsonValues);
  $modelUuid = $jsonValuesData['canvas'][0]['uuid'];
  $innerKeyUuid = 'ee4da1c5-e659-46c4-8a22-8969134fcfa7';
  $components = array_keys($jsonValuesData['model'][$modelUuid]);

  // The first element is settings, the second one is our component uuid.
  $componentUuid = $components[1];

  // Check that only the configured fields have been uploaded, including metatags.
  $data = Json::decode(\Drupal::state()
    ->get('lingotek.uploaded_content', '[]'));

  // As those uuids are generated, we don't know them in our document fake
  // responses. We need a token replacement system for fixing that.
  \Drupal::state()
    ->set('lingotek.data_replacements', [
    '###MODEL_UUID###' => $modelUuid,
    '###COMPONENT_UUID###' => $componentUuid,
    '###INNER_KEY###' => $innerKeyUuid,
  ]);
  $this
    ->assertUploadedDataFieldCount($data, 2);
  $this
    ->assertEquals($data['title'][0]['value'], 'Llamas are cool');
  $this
    ->assertTrue(isset($data['layout_canvas'][0]['json_values'][$modelUuid]));
  $this
    ->assertTrue(isset($data['layout_canvas'][0]['json_values'][$modelUuid][$componentUuid]));
  $this
    ->assertCount(3, $data['layout_canvas'][0]['json_values'][$modelUuid][$componentUuid]);
  $this
    ->assertEquals($data['layout_canvas'][0]['json_values'][$modelUuid][$componentUuid][0][$innerKeyUuid], "Llamas are very cool");
  $this
    ->assertEquals($data['layout_canvas'][0]['json_values'][$modelUuid][$componentUuid][1][$innerKeyUuid], "Dogs are very cool");
  $this
    ->assertEquals($data['layout_canvas'][0]['json_values'][$modelUuid][$componentUuid][2][$innerKeyUuid], "Cats are very cool");

  // Check that the url used was the right one.
  $uploaded_url = \Drupal::state()
    ->get('lingotek.uploaded_url');
  $this
    ->assertIdentical(\Drupal::request()
    ->getUriForPath('/node/1'), $uploaded_url, 'The node url was used.');

  // Check that the profile used was the right one.
  $used_profile = \Drupal::state()
    ->get('lingotek.used_profile');
  $this
    ->assertIdentical('automatic', $used_profile, 'The automatic profile was used.');

  // Check that the translate tab is in the node.
  $this
    ->drupalGet('node/1');
  $this
    ->clickLink('Translate');

  // The document should have been automatically uploaded, so let's check
  // the upload status.
  $this
    ->clickLink('Check Upload Status');
  $this
    ->assertText('The import for node Llamas are cool is complete.');

  // Request translation.
  $link = $this
    ->xpath('//a[normalize-space()="Request translation" and contains(@href,"es_AR")]');
  $link[0]
    ->click();
  $this
    ->assertText("Locale 'es_AR' was added as a translation target for node Llamas are cool.");

  // Check translation status.
  $this
    ->clickLink('Check translation status');
  $this
    ->assertText('The es_AR translation for node Llamas are cool is ready for download.');

  // Check that the Edit link points to the workbench and it is opened in a new tab.
  $this
    ->assertLingotekWorkbenchLink('es_AR');

  // Download translation.
  $this
    ->clickLink('Download completed translation');
  $this
    ->assertText('The translation of node Llamas are cool into es_AR has been downloaded.');

  // The content is translated and published.
  $this
    ->clickLink('Las llamas son chulas');
  $this
    ->assertText('Las llamas son chulas');
  $this
    ->assertText('Las llamas son muy chulas');
  $this
    ->assertText('Los perros son muy chulos');
  $this
    ->assertText('Los gatos son muy chulos');

  /** @var \Drupal\Core\Entity\EntityStorageInterface $cohesionLayoutStorage */
  $cohesionLayoutStorage = $this->container
    ->get('entity_type.manager')
    ->getStorage('cohesion_layout');
  $cohesionLayoutStorage
    ->resetCache([
    1,
  ]);

  /** @var \Drupal\cohesion_elements\Entity\CohesionLayout $layout */
  $layout = CohesionLayout::load(1);
  $layout = $layout
    ->getTranslation('es-ar');
  $jsonValues = $layout
    ->get('json_values')->value;
  $jsonValuesData = Json::decode($jsonValues);
  $this
    ->assertEquals($jsonValuesData['model'][$modelUuid][$componentUuid][0][$innerKeyUuid], "Las llamas son muy chulas");
  $this
    ->assertEquals($jsonValuesData['model'][$modelUuid][$componentUuid][1][$innerKeyUuid], "Los perros son muy chulos");
  $this
    ->assertEquals($jsonValuesData['model'][$modelUuid][$componentUuid][2][$innerKeyUuid], "Los gatos son muy chulos");

  // The original content didn't change.
  $this
    ->drupalGet('node/1');
  $this
    ->assertText('Llamas are cool');
  $this
    ->assertText('Llamas are very cool');
  $this
    ->assertText('Dogs are very cool');
  $this
    ->assertText('Cats are very cool');
}