View source
<?php
namespace Drupal\tmgmt_extension_suit\Tests;
use Drupal\Core\Language\Language;
use Drupal\node\Entity\Node;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Form\SourceOverviewForm;
class RequestBulkTranslationTest extends TmgmtExtensionSuitTestBase {
function testBulkSubmitTranslationPreselectTargetLanguage() {
$settings = [
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => filter_default_format(),
],
],
'title' => $this
->randomMachineName(8),
'type' => 'translatable_node',
'uid' => \Drupal::currentUser()
->id(),
'langcode' => 'fr',
];
$node = Node::create($settings);
$node
->save();
$this
->drupalPostForm('admin/tmgmt/sources', [
'items[1]' => 'item[1]',
'target_language' => Language::LANGCODE_NOT_SPECIFIED,
], t('Request translation in a batch'));
$this
->assertFalse($this
->isFieldChecked('edit-target-language-de'));
$this
->assertFalse($this
->isFieldChecked('edit-target-language-fr'));
$this
->assertNoField('edit-target-language-en');
$this
->drupalPostForm('admin/tmgmt/sources', [
'items[1]' => 'item[1]',
'target_language' => SourceOverviewForm::MULTIPLE,
'target_languages[en]' => 'en',
'target_languages[fr]' => 'fr',
], t('Request translation in a batch'));
$this
->assertFalse($this
->isFieldChecked('edit-target-language-de'));
$this
->assertTrue($this
->isFieldChecked('edit-target-language-fr'));
$this
->assertNoField('edit-target-language-en');
$this
->drupalPostForm('admin/tmgmt/sources', [
'items[1]' => 'item[1]',
'target_language' => SourceOverviewForm::ALL,
], t('Request translation in a batch'));
$this
->assertTrue($this
->isFieldChecked('edit-target-language-de'));
$this
->assertTrue($this
->isFieldChecked('edit-target-language-fr'));
$this
->assertNoField('edit-target-language-en');
$this
->drupalPostForm('admin/tmgmt/sources', [
'items[1]' => 'item[1]',
'target_language' => 'de',
], t('Request translation in a batch'));
$this
->assertTrue($this
->isFieldChecked('edit-target-language-de'));
$this
->assertFalse($this
->isFieldChecked('edit-target-language-fr'));
$this
->assertNoField('edit-target-language-en');
}
function testRequestBulkTranslationValidation() {
$settings = [
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => filter_default_format(),
],
],
'title' => $this
->randomMachineName(8),
'type' => 'translatable_node',
'uid' => \Drupal::currentUser()
->id(),
'langcode' => 'fr',
];
$node = Node::create($settings);
$node
->save();
$this
->requestBulkTranslation([], []);
$this
->assertText("You didn't select any source items.");
$this
->requestBulkTranslation([
3,
$node
->id(),
], []);
$this
->assertText("You can't request translation in a batch for items in different source languages.");
$this
->drupalPostForm('admin/tmgmt/sources', [
'items[1]' => 'item[1]',
'source_language' => 'de',
], t('Request translation in a batch'));
$this
->assertText('From the selection you made it was not possible to create any translation job.');
$this
->requestBulkTranslation([
2,
3,
], []);
$this
->assertTitle("Request translation | Drupal");
}
function testRequestBulkTranslation() {
$this
->requestBulkTranslation([
2,
3,
], [
'fr',
'de',
], 'local', [
'settings[translator]' => 1,
'settings[job_comment]' => 'Comment',
]);
$this
->assertText('Translation request queued for 4 jobs. Return to "Sources" page.');
$this
->assertEqual($this
->getCountOfItemsInQueue('tmgmt_extension_suit_upload'), 4);
$this
->assertEqual($this
->isItemAddedToQueue('tmgmt_extension_suit_upload', 1), 1);
$this
->assertEqual($this
->isItemAddedToQueue('tmgmt_extension_suit_upload', 2), 1);
$this
->assertEqual($this
->isItemAddedToQueue('tmgmt_extension_suit_upload', 3), 1);
$this
->assertEqual($this
->isItemAddedToQueue('tmgmt_extension_suit_upload', 4), 1);
$jobs = Job::loadMultiple([
1,
2,
3,
4,
]);
$this
->assertEqual($jobs[1]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[2]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[3]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[4]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[1]
->getTargetLangcode(), 'de');
$this
->assertEqual($jobs[2]
->getTargetLangcode(), 'fr');
$this
->assertEqual($jobs[3]
->getTargetLangcode(), 'de');
$this
->assertEqual($jobs[4]
->getTargetLangcode(), 'fr');
$this
->assertEqual($jobs[1]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
$this
->assertEqual($jobs[2]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
$this
->assertEqual($jobs[3]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
$this
->assertEqual($jobs[4]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
}
public function testRequestBulkTranslationSkippingNodeInOneLanguage() {
$this
->requestTranslation([
1,
], 'fr', 1);
$this
->requestBulkTranslation([
1,
2,
3,
], [
'fr',
], 'local', [
'settings[translator]' => 1,
'settings[job_comment]' => 'Comment',
]);
$this
->assertText('1 item conflicted with pending item and was skipped. Existing job item:');
$this
->assertText('Job item with source language = "en" and target language = "fr" for entity "node" with id = "1".');
$this
->assertText('Translation request queued for 2 jobs. Return to "Sources" page.');
$this
->assertEqual($this
->getCountOfItemsInQueue('tmgmt_extension_suit_upload'), 2);
$jobs = Job::loadMultiple([
1,
2,
3,
]);
$this
->assertEqual($jobs[1]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[2]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[3]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[1]
->getTargetLangcode(), 'fr');
$this
->assertEqual($jobs[2]
->getTargetLangcode(), 'fr');
$this
->assertEqual($jobs[3]
->getTargetLangcode(), 'fr');
$this
->assertEqual($jobs[1]
->get('settings')
->getValue(), [
[
'translator' => '',
'job_comment' => '',
],
]);
$this
->assertEqual($jobs[2]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
$this
->assertEqual($jobs[3]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
}
public function testRequestBulkTranslationSkippingNodeInTwoLanguages() {
$this
->requestTranslation([
1,
], 'fr', 1);
$this
->requestTranslation([
1,
], 'de', 2);
$this
->requestBulkTranslation([
1,
2,
3,
], [
'fr',
'de',
], 'local', [
'settings[translator]' => 1,
'settings[job_comment]' => 'Comment',
]);
$this
->assertText('2 items conflicted with pending items and were skipped. Existing job items:');
$this
->assertText('Job item with source language = "en" and target language = "fr" for entity "node" with id = "1".');
$this
->assertText('Job item with source language = "en" and target language = "de" for entity "node" with id = "1".');
$this
->assertText('Translation request queued for 4 jobs. Return to "Sources" page.');
$this
->assertEqual($this
->getCountOfItemsInQueue('tmgmt_extension_suit_upload'), 4);
$jobs = Job::loadMultiple([
1,
2,
3,
4,
5,
6,
]);
$this
->assertEqual($jobs[1]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[2]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[3]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[4]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[5]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[6]
->getSourceLangcode(), 'en');
$this
->assertEqual($jobs[1]
->getTargetLangcode(), 'fr');
$this
->assertEqual($jobs[2]
->getTargetLangcode(), 'de');
$this
->assertEqual($jobs[3]
->getTargetLangcode(), 'de');
$this
->assertEqual($jobs[4]
->getTargetLangcode(), 'fr');
$this
->assertEqual($jobs[5]
->getTargetLangcode(), 'de');
$this
->assertEqual($jobs[6]
->getTargetLangcode(), 'fr');
$this
->assertEqual($jobs[1]
->get('settings')
->getValue(), [
[
'translator' => '',
'job_comment' => '',
],
]);
$this
->assertEqual($jobs[2]
->get('settings')
->getValue(), [
[
'translator' => '',
'job_comment' => '',
],
]);
$this
->assertEqual($jobs[3]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
$this
->assertEqual($jobs[4]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
$this
->assertEqual($jobs[5]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
$this
->assertEqual($jobs[6]
->get('settings')
->getValue(), [
[
'translator' => 1,
'job_comment' => 'Comment',
],
]);
}
}