function ContentTmgmtEntitySourceUiTest::testEntitySourcePreview

Test content entity source preview.

File

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

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.
  $this
    ->drupalGet('admin/config/regional/content-language');
  $edit = [
    'entity_types[block_content]' => 'block_content',
    'settings[block_content][basic][translatable]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->assertSession()
    ->pageTextContains('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
    ->drupalGet('admin/tmgmt/sources/content/block_content');
  $this
    ->submitForm([
    'items[1]' => 1,
  ], 'Request translation');
  $this
    ->submitForm([
    'target_language' => 'de',
    'translator' => 'test_translator',
  ], 'Submit to provider');
  $this
    ->clickLink('reviewed');
  $this
    ->clickLink('Preview');
  $this
    ->assertSession()
    ->pageTextContains('Preview of Custom Block for German');

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

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

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

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

  // Assert translation message for the non-existing translated entity.
  $this
    ->drupalGet($review_url);
  $this
    ->submitForm([
    'title|0|value[translation]' => 'test_translation',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('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
    ->assertSession()
    ->linkNotExists('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
    ->assertSession()
    ->statusCodeEquals(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('Preview');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Translate job.
  $job->settings->action = 'translate';
  $job
    ->save();
  $job
    ->requestTranslation();
  $this
    ->assertSession()
    ->titleEquals((string) 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
    ->assertSession()
    ->statusCodeEquals(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
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->titleEquals((string) 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
    ->submitForm($edit, 'Save');
  $this
    ->drupalGet('admin/tmgmt/items/' . $job_item
    ->id());
  $this
    ->clickLink('Preview');
  $this
    ->assertSession()
    ->pageTextContains('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
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains('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
    ->assertSession()
    ->linkNotExists('Preview');
}