You are here

public function LingotekNodeLayoutBuilderSymmetricTranslationTest::testNodeWithLayoutBuilderSTBlockTranslation in Lingotek Translation 3.5.x

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

Tests that a node can be translated.

File

tests/src/Functional/LingotekNodeLayoutBuilderSymmetricTranslationTest.php, line 121

Class

LingotekNodeLayoutBuilderSymmetricTranslationTest
Tests translating a node with multiple locales including paragraphs.

Namespace

Drupal\Tests\lingotek\Functional

Code

public function testNodeWithLayoutBuilderSTBlockTranslation() {

  // This is a hack for avoiding writing different lingotek endpoint mocks.
  \Drupal::state()
    ->set('lingotek.uploaded_content_type', 'node+layoutbuilderst');
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->drupalGet('node/add/article');
  $edit = [];
  $edit['title[0][value]'] = 'Llamas are cool';
  $edit['body[0][value]'] = 'Llamas are very cool';
  $edit['langcode[0][value]'] = 'en';
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Add a block with a custom label.
  $page
    ->clickLink('Layout');
  $assert_session
    ->elementTextContains('css', '.layout-builder__message.layout-builder__message--overrides', 'You are editing the layout for this Article content item. Edit the template for all Article content items instead.');
  $assert_session
    ->linkExists('Edit the template for all Article content items instead.');
  $page
    ->clickLink('Add block');
  $page
    ->clickLink('Powered by Drupal');
  $page
    ->fillField('settings[label]', 'Block title layout builder override');
  $page
    ->checkField('settings[label_display]');
  $page
    ->pressButton('Add block');
  $page
    ->clickLink('Add block');
  $page
    ->clickLink('Lingotek Test Rich text');
  $page
    ->fillField('settings[label]', 'Rich text label');
  $page
    ->fillField('settings[rich_text][value]', 'Rich text value');
  $page
    ->checkField('settings[label_display]');
  $page
    ->pressButton('Add block');
  $page
    ->pressButton('Save layout');
  $assert_session
    ->pageTextContains('Block title layout builder override');
  $assert_session
    ->pageTextContains('Rich text label');
  $assert_session
    ->pageTextContains('Rich text value');

  // Get the UUID of the component.
  $components = Node::load(1)
    ->get('layout_builder__layout')
    ->getSection(0)
    ->getComponents();
  $uuids = array_keys($components);
  $uuidArticleLinks = $uuids[0];
  $uuidArticleBody = $uuids[1];
  $uuidArticlePoweredBy = $uuids[2];
  $uuidArticleRichText = $uuids[3];

  // 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', [
    '###UID_LINKS###' => $uuidArticleLinks,
    '###UID_BODY###' => $uuidArticleBody,
    '###UID_POWEREDBY###' => $uuidArticlePoweredBy,
    '###UID_RICHTEXT###' => $uuidArticleRichText,
  ]);
  $this
    ->clickLink('Translate');
  $this
    ->clickLink('Upload');
  $this
    ->checkForMetaRefresh();
  $assert_session
    ->pageTextContains('Uploaded 1 document to Lingotek.');

  // Check that only the configured fields have been uploaded.
  $data = Json::decode(\Drupal::state()
    ->get('lingotek.uploaded_content', '[]'));
  $this
    ->assertUploadedDataFieldCount($data, 3);
  $this
    ->assertEquals($data['title'][0]['value'], 'Llamas are cool');
  $this
    ->assertEquals($data['body'][0]['value'], 'Llamas are very cool');
  $this
    ->assertCount(4, $data['layout_builder__translation']['components']);
  $this
    ->assertEquals($data['layout_builder__translation']['components'][$uuidArticlePoweredBy]['label'], 'Block title layout builder override');
  $this
    ->assertEquals($data['layout_builder__translation']['components'][$uuidArticleRichText]['label'], 'Rich text label');
  $this
    ->assertEquals($data['layout_builder__translation']['components'][$uuidArticleRichText]['rich_text.value'], 'Rich text value');

  // We don't include non translatable properties.
  $this
    ->assertFalse(isset($data['layout_builder__translation']['components'][$uuidArticleRichText]['rich_text.format']));

  // 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('manual', $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');
  $assert_session
    ->pageTextContains('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();
  $assert_session
    ->pageTextContains("Locale 'es_AR' was added as a translation target for node Llamas are cool.");

  // Check translation status.
  $this
    ->clickLink('Check translation status');
  $assert_session
    ->pageTextContains('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', 'dummy-document-hash-id');

  // Download translation.
  $this
    ->clickLink('Download completed translation');
  $assert_session
    ->pageTextContains('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');
  $assert_session
    ->pageTextContains('Las llamas son chulas');
  $assert_session
    ->pageTextContains('Las llamas son muy chulas');
  $assert_session
    ->pageTextContains('Título de bloque de layout builder sobreescrito');
  $assert_session
    ->pageTextContains('Título de texto enriquecido');
  $assert_session
    ->pageTextContains('Valor de texto enriquecido');

  // The original content didn't change.
  $this
    ->drupalGet('node/1');
  $assert_session
    ->pageTextContains('Llamas are cool');
  $assert_session
    ->pageTextContains('Llamas are very cool');
  $assert_session
    ->pageTextContains('Block title layout builder override');
  $assert_session
    ->pageTextContains('Rich text label');
  $assert_session
    ->pageTextContains('Rich text value');
}