function ContentTmgmtEntitySourceUiTest::testCommentTranslateTab

Test translating comments.

File

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

Class

ContentTmgmtEntitySourceUiTest
Content entity source UI tests.

Namespace

Drupal\Tests\tmgmt_content\Functional

Code

function testCommentTranslateTab() {

  // Allow auto-accept.
  $default_translator = Translator::load('test_translator');
  $default_translator
    ->setAutoAccept(TRUE)
    ->save();

  // Add default comment type.
  $this
    ->addDefaultCommentField('node', 'article');

  // Enable comment translation.

  /** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
  $content_translation_manager = \Drupal::service('content_translation.manager');
  $content_translation_manager
    ->setEnabled('comment', 'comment', TRUE);

  // Change comment_body field to be translatable.
  $comment_body = FieldConfig::loadByName('comment', 'comment', 'comment_body');
  $comment_body
    ->setTranslatable(TRUE)
    ->save();

  // Create a user that is allowed to translate comments.
  $permissions = array_merge($this->translator_permissions, array(
    'translate comment',
    'post comments',
    'skip comment approval',
    'edit own comments',
    'access comments',
    'administer comments',
    'bypass node access',
  ));
  $this
    ->loginAsTranslator($permissions, TRUE);

  // Create an english source article.
  $node = $this
    ->createTranslatableNode('article', 'en');

  // Add a comment.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $edit = array(
    'subject[0][value]' => $this
      ->randomMachineName(),
    'comment_body[0][value]' => $this
      ->randomMachineName(),
  );
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your comment has been posted.');

  // Go to the translate tab.
  $this
    ->assertSession()
    ->elementExists('css', '.comment')
    ->clickLink('Edit');
  $this
    ->assertNotEmpty(preg_match('|comment/(\\d+)/edit$|', $this
    ->getUrl(), $matches), 'Comment found');
  $comment = Comment::load($matches[1]);
  $this
    ->clickLink('Translate');

  // Assert some basic strings on that page.
  $this
    ->assertSession()
    ->pageTextContains(t('Translations of @title', array(
    '@title' => $comment
      ->getSubject(),
  )));
  $this
    ->assertSession()
    ->pageTextContains('Pending Translations');

  // Request translations.
  $edit = array(
    'languages[de]' => TRUE,
    'languages[es]' => TRUE,
  );
  $this
    ->submitForm($edit, 'Request translation');

  // Verify that we are on the translate tab.
  $this
    ->assertSession()
    ->pageTextContains('2 jobs need to be checked out.');

  // Submit all jobs.
  $this
    ->assertSession()
    ->pageTextContains($comment
    ->getSubject());
  $this
    ->submitForm([], 'Submit to provider and continue');
  $this
    ->assertSession()
    ->pageTextContains($comment
    ->getSubject());
  $this
    ->submitForm([], 'Submit to provider');

  // Make sure that we're back on the translate tab.
  $this
    ->assertSession()
    ->addressEquals($comment
    ->toUrl('canonical', array(
    'absolute' => TRUE,
  ))
    ->toString() . '/translations');
  $this
    ->assertSession()
    ->pageTextContains('Test translation created.');
  $this
    ->assertSession()
    ->pageTextNotContains(t('The translation of @title to @language is finished and can now be reviewed.', array(
    '@title' => $comment
      ->getSubject(),
    '@language' => t('Spanish'),
  )));
  $this
    ->assertSession()
    ->pageTextContains(t('The translation for @title has been accepted as es: @target.', array(
    '@title' => $comment
      ->getSubject(),
    '@target' => $comment
      ->getSubject(),
  )));

  // The translated content should be in place.
  $this
    ->clickLink('de(de-ch): ' . $comment
    ->getSubject());
  $this
    ->assertSession()
    ->pageTextContains('de(de-ch): ' . $comment
    ->get('comment_body')->value);
  $this
    ->drupalGet('comment/1/translations');
  $this
    ->clickLink('es: ' . $comment
    ->getSubject());
  $this
    ->drupalGet('es/node/' . $comment
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('es: ' . $comment
    ->get('comment_body')->value);

  // Disable auto-accept.
  $default_translator
    ->setAutoAccept(FALSE)
    ->save();

  // Request translation to Italian.
  $this
    ->drupalGet('comment/' . $comment
    ->id() . '/translations');
  $edit = [
    'languages[it]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Request translation');
  $this
    ->submitForm([], 'Submit to provider');
  $this
    ->clickLink('reviewed');
  $this
    ->assertSession()
    ->pageTextContains('Translation publish status');
  $this
    ->assertSession()
    ->checkboxChecked('edit-status-published');

  // Do not publish the Italian translation.
  $edit = [
    'status[published]' => FALSE,
  ];
  $this
    ->submitForm($edit, 'Save as completed');
  $this
    ->drupalGet('it/comment/' . $comment
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('it: ' . $comment
    ->getSubject());

  // Original entity and other translations are not affected.
  $this
    ->drupalGet('comment/' . $comment
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains($comment
    ->getSubject());
  $this
    ->drupalGet('de/comment/' . $comment
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalLogout();
  $this
    ->drupalGet('it/comment/' . $comment
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(403);
}