You are here

function ContentTmgmtEntitySourceUiTest::testEntitySourcePreview in Translation Management Tool 8

Test content entity source preview.

File

sources/content/tests/src/Functional/ContentTmgmtEntitySourceUiTest.php, line 782

Class

ContentTmgmtEntitySourceUiTest
Content entity source UI tests.

Namespace

Drupal\Tests\tmgmt_content\Functional

Code

function testEntitySourcePreview() {

  // Create the basic block type.
  $bundle = BlockContentType::create([
    'id' => 'basic',
    'label' => 'basic',
  ]);
  $bundle
    ->save();

  // Enable translation for basic blocks.
  $edit = [
    'entity_types[block_content]' => 'block_content',
    'settings[block_content][basic][translatable]' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
  $this
    ->assertText(t('Settings successfully updated.'));

  // Create a custom block.
  $custom_block = BlockContent::create([
    'type' => 'basic',
    'info' => 'Custom Block',
    'langcode' => 'en',
  ]);
  $custom_block
    ->save();

  // Translate the custom block and assert the preview.
  $this
    ->drupalPostForm('admin/tmgmt/sources/content/block_content', [
    'items[1]' => 1,
  ], t('Request translation'));
  $this
    ->drupalPostForm(NULL, [
    'target_language' => 'de',
    'translator' => 'test_translator',
  ], t('Submit to provider'));
  $this
    ->clickLink(t('reviewed'));
  $this
    ->clickLink(t('Preview'));
  $this
    ->assertText(t('Preview of Custom Block for German'));

  // Create a node and translation job.
  $node = $this
    ->createTranslatableNode('page', 'en');
  $this
    ->drupalPostForm('admin/tmgmt/sources', [
    'items[1]' => 1,
  ], t('Request translation'));
  $this
    ->drupalPostForm(NULL, [
    'target_language' => 'de',
    'translator' => 'test_translator',
  ], t('Submit to provider'));

  // Delete the node.
  $node
    ->delete();

  // Review the translation.
  $this
    ->clickLink(t('reviewed'));
  $review_url = $this
    ->getSession()
    ->getCurrentUrl();

  // Assert that preview page is not available for non-existing entities.
  $this
    ->clickLink(t('Preview'));
  $this
    ->assertResponse(404);

  // Assert translation message for the non-existing translated entity.
  $this
    ->drupalPostForm($review_url, [
    'title|0|value[translation]' => 'test_translation',
  ], t('Save'));
  $this
    ->assertText(t('The translation has been saved successfully.'));

  // Create translatable node.
  $node = $this
    ->createTranslatableNode('page', 'en');
  $job = $this
    ->createJob('en', 'de');
  $job->translator = $this->default_translator
    ->id();
  $job->settings->action = 'submit';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', $node
    ->getEntityTypeId(), $node
    ->id(), array(
    'tjid' => $job
      ->id(),
  ));
  $job_item
    ->save();

  // At this point job is state 0 (STATE_UNPROCESSED) or "cart job", we don't
  // want a preview link available.
  $this
    ->drupalGet(URL::fromRoute('entity.tmgmt_job_item.canonical', [
    'tmgmt_job_item' => $job
      ->id(),
  ])
    ->setAbsolute()
    ->toString());
  $this
    ->assertNoLink(t('Preview'));

  // Changing job state to active.
  $job
    ->requestTranslation();

  // Visit preview route without key.
  $this
    ->drupalGet(URL::fromRoute('tmgmt_content.job_item_preview', [
    'tmgmt_job_item' => $job
      ->id(),
  ])
    ->setAbsolute()
    ->toString());
  $this
    ->assertResponse(403);

  // Visit preview by clicking the preview button.
  $this
    ->drupalGet(URL::fromRoute('entity.tmgmt_job_item.canonical', [
    'tmgmt_job_item' => $job
      ->id(),
  ])
    ->setAbsolute()
    ->toString());
  $this
    ->clickLink(t('Preview'));
  $this
    ->assertResponse(200);

  // Translate job.
  $job->settings->action = 'translate';
  $job
    ->save();
  $job
    ->requestTranslation();
  $this
    ->assertTitle(t("Preview of @title for @target_language | Drupal", [
    '@title' => $node
      ->getTitle(),
    '@target_language' => $job
      ->getTargetLanguage()
      ->getName(),
  ]));

  // Test if anonymous user can access preview without key.
  $this
    ->drupalLogout();
  $this
    ->drupalGet(URL::fromRoute('tmgmt_content.job_item_preview', [
    'tmgmt_job_item' => $job
      ->id(),
  ])
    ->setAbsolute()
    ->toString());
  $this
    ->assertResponse(403);

  // Test if anonymous user can access preview with key.
  $key = \Drupal::service('tmgmt_content.key_access')
    ->getKey($job_item);
  $this
    ->drupalGet(URL::fromRoute('tmgmt_content.job_item_preview', [
    'tmgmt_job_item' => $job_item
      ->id(),
  ], [
    'query' => [
      'key' => $key,
    ],
  ]));
  $this
    ->assertResponse(200);
  $this
    ->assertTitle(t("Preview of @title for @target_language | Drupal", [
    '@title' => $node
      ->getTitle(),
    '@target_language' => $job
      ->getTargetLanguage()
      ->getName(),
  ]));
  $this
    ->loginAsAdmin([
    'accept translation jobs',
  ]);

  // Test preview if we edit translation.
  $this
    ->drupalGet('admin/tmgmt/items/' . $job_item
    ->id());
  $edit = [
    'title|0|value[translation]' => 'de(de-ch): Test title for preview translation from en to de.',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->drupalGet('admin/tmgmt/items/' . $job_item
    ->id());
  $this
    ->clickLink(t('Preview'));
  $this
    ->assertText('de(de-ch): Test title for preview translation from en to de.');

  // Test if anonymous user can see also the changes.
  $this
    ->drupalLogout();
  $key = \Drupal::service('tmgmt_content.key_access')
    ->getKey($job_item);
  $this
    ->drupalGet(Url::fromRoute('tmgmt_content.job_item_preview', [
    'tmgmt_job_item' => $job_item
      ->id(),
  ], [
    'query' => [
      'key' => $key,
    ],
  ]));
  $this
    ->assertResponse(200);
  $this
    ->assertText('de(de-ch): Test title for preview translation from en to de.');
  $items = $job
    ->getItems();
  $item = reset($items);
  $item
    ->acceptTranslation();

  // There should be no link if the job item is accepted.
  $this
    ->drupalGet('admin/tmgmt/items/' . $node
    ->id(), array(
    'query' => array(
      'destination' => 'admin/tmgmt/items/' . $node
        ->id(),
    ),
  ));
  $this
    ->assertNoLink(t('Preview'));
}