<?php
namespace Drupal\Tests\tmgmt\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Entity\JobItem;
use Drupal\tmgmt\Entity\Translator;
use Drupal\filter\Entity\FilterFormat;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\JobItemInterface;
class TMGMTUiTest extends TMGMTTestBase {
use TmgmtEntityTestTrait;
function setUp() : void {
parent::setUp();
$filtered_html_format = FilterFormat::create(array(
'format' => 'filtered_html',
'name' => 'Filtered HTML',
));
$filtered_html_format
->save();
$this
->addLanguage('de');
$this
->addLanguage('es');
$this
->addLanguage('el');
$this
->loginAsTranslator(array(
'access administration pages',
'create translation jobs',
'submit translation jobs',
$filtered_html_format
->getPermissionName(),
), TRUE);
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->createNodeType('page', 'Page', TRUE);
$this
->createNodeType('article', 'Article', TRUE);
}
function testCheckoutForm() {
$job = tmgmt_job_match_item('en', '');
$job
->addItem('test_source', 'test', 1);
$this
->drupalGet($job
->toUrl());
$this
->assertSession()
->responseContains('Save job" class="button js-form-submit form-submit"');
$this
->assertSession()
->pageTextContains('test_source:test:1');
$this
->assertSession()
->pageTextNotContains('Translation Job messages');
$this
->assertSession()
->pageTextNotContains('Checkout progress');
$job
->addItem('test_source', 'test', 2);
$job
->addItem('test_source', 'test', 3);
$this
->drupalGet($job
->toUrl());
$this
->assertSession()
->pageTextContains('test_source:test:1');
$this
->assertSession()
->pageTextContains('test_source:test:2');
$this
->assertSession()
->pageTextContains('test_source:test:3');
$edit = array(
'target_language' => 'el',
'settings[action]' => 'translate',
);
$this
->submitForm($edit, 'Submit to provider');
$this
->assertSession()
->pageTextContains(t('@translator can not translate from @source to @target.', array(
'@translator' => 'Test provider',
'@source' => 'English',
'@target' => 'Greek',
)));
$job = \Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->loadUnchanged($job
->id());
$this
->assertTrue($job
->isUnprocessed());
$this
->assertTrue($job
->isAuthor());
$this
->drupalLogin($this->translator_user);
$this
->drupalGet('admin/tmgmt/jobs/' . $job
->id());
$edit = array(
'target_language' => 'es',
'settings[action]' => 'translate',
);
$this
->submitForm($edit, 'Submit to provider');
$job = \Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->loadUnchanged($job
->id());
$this
->assertTrue($job
->isAuthor());
$job = \Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->loadUnchanged($job
->id());
$this
->assertTrue($job
->isActive());
foreach ($job
->getItems() as $job_item) {
$this
->assertTrue($job_item
->isNeedsReview());
}
$this
->assertSession()
->pageTextContains('Test translation created');
$this
->assertSession()
->pageTextNotContains('Test provider called');
$this
->assertSession()
->pageTextContains('Job overview');
$previous_tjid = $job
->id();
$job = tmgmt_job_match_item('en', '');
$job
->addItem('test_source', 'test', 9);
$this
->assertNotEquals($previous_tjid, $job
->id());
$this
->drupalGet($job
->toUrl());
$this
->assertSession()
->pageTextContains('You can provide a label for this job in order to identify it easily later on.');
$this
->assertSession()
->pageTextContains('test_source:test:9');
$edit = array(
'target_language' => 'es',
'settings[action]' => 'submit',
);
$this
->submitForm($edit, 'Submit to provider');
$this
->assertSession()
->pageTextContains('Test submit');
$job = \Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->loadUnchanged($job
->id());
$this
->assertTrue($job
->isActive());
$job = tmgmt_job_match_item('en', 'es');
$item10 = $job
->addItem('test_source', 'test', 10);
$this
->drupalGet($job
->toUrl());
$this
->assertSession()
->pageTextContains('You can provide a label for this job in order to identify it easily later on.');
$this
->assertSession()
->pageTextContains('test_source:test:10');
$edit = array(
'settings[action]' => 'reject',
);
$this
->submitForm($edit, 'Submit to provider');
$this
->assertSession()
->pageTextContains('This is not supported');
$job = \Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->loadUnchanged($job
->id());
$this
->assertTrue($job
->isRejected());
$args = array(
'@view' => 'view-tmgmt-job-messages',
);
$this
->assertCount(2, $this
->xpath('//div[contains(@class, @view)]//tbody/tr', $args));
$message_authors = $this
->xpath('//div[contains(@class, @view)]//td[contains(@class, @field)]/*[self::a or self::span] ', $args + array(
'@field' => 'views-field-name',
));
$this
->assertCount(2, $message_authors);
foreach ($message_authors as $message_author) {
$this
->assertEquals($this->translator_user
->getDisplayName(), $message_author
->getText());
}
$this
->assertTrue($job
->isSubmittable());
$edit = array(
'settings[action]' => 'translate',
);
$this
->submitForm($edit, 'Submit to provider');
$this
->assertSession()
->pageTextContains('Test translation created');
$this
->drupalGet('admin/tmgmt/items/' . $item10
->id());
$this
->assertSession()
->responseContains('Save" class="button js-form-submit form-submit"');
$this
->submitForm([], 'Save');
$this
->clickLink('View');
$this
->assertSession()
->responseContains('Save as completed" class="button button--primary js-form-submit form-submit"');
$this
->submitForm([], 'Save');
$this
->assertSession()
->responseContains('Save job" class="button button--primary js-form-submit form-submit"');
$this
->submitForm([], 'Save job');
\Drupal::state()
->set('tmgmt.test_source_data', array(
'title' => array(
'deep_nesting' => array(
'#text' => '<p><em><strong>Six dummy HTML tags in the title.</strong></em></p>',
'#label' => 'Title',
),
),
'body' => array(
'deep_nesting' => array(
'#text' => '<p>Two dummy HTML tags in the body.</p>',
'#label' => 'Body',
),
),
'phantom' => array(
'deep_nesting' => array(
'#text' => 'phantom text',
'#label' => 'phantom label',
'#translate' => FALSE,
'#format' => 'filtered_html',
),
),
));
$item4 = $job
->addItem('test_source', 'test', 4);
$item4
->active();
$this
->drupalGet('admin/tmgmt/items/' . $item4
->id());
$this
->assertSession()
->responseNotContains('tmgmt-ui-element-phantom-wrapper');
$this
->drupalGet('admin/tmgmt/jobs');
$rows = $this
->getSession()
->getPage()
->findAll('css', 'table.views-table tbody tr');
$found = FALSE;
foreach ($rows as $row) {
if (trim($row
->find('css', 'td:nth-child(2)')
->getText()) == 'test_source:test:10') {
$found = TRUE;
$this
->assertEquals(8, $row
->find('css', 'td:nth-child(8)')
->getText());
}
}
$this
->assertTrue($found);
$job = tmgmt_job_match_item('en', 'es');
$job
->addItem('test_source', 'test', 11);
$this
->drupalGet($job
->toUrl());
$this
->assertSession()
->pageTextContains('You can provide a label for this job in order to identify it easily later on.');
$this
->assertSession()
->pageTextContains('test_source:test:11');
$edit = array(
'settings[action]' => 'fail',
);
$this
->submitForm($edit, 'Submit to provider');
$this
->assertSession()
->pageTextContains('Service not reachable');
\Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->resetCache();
$job = Job::load($job
->id());
$this
->assertTrue($job
->isUnprocessed());
$this
->assertSession()
->pageTextContains('You can provide a label for this job in order to identify it easily later on.');
$job = tmgmt_job_match_item('en', 'es');
$job
->addItem('test_source', 'test', 12);
$this
->drupalGet($job
->toUrl());
$this
->assertSession()
->pageTextContains('You can provide a label for this job in order to identify it easily later on.');
$this
->assertSession()
->pageTextContains('test_source:test:12');
$edit = array(
'settings[action]' => 'not_translatable',
);
$this
->submitForm($edit, 'Submit to provider');
$this
->assertSession()
->pageTextContains('Fail');
$job = \Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->loadUnchanged($job
->id());
$this
->assertTrue($job
->isUnprocessed());
$this->default_translator
->setSetting('action', 'reject');
$this->default_translator
->save();
$job = tmgmt_job_match_item('en', 'es');
$job
->addItem('test_source', 'test', 13);
$this
->drupalGet($job
->toUrl());
$this
->assertSession()
->pageTextContains('You can provide a label for this job in order to identify it easily later on.');
$this
->assertSession()
->pageTextContains('test_source:test:13');
$this
->submitForm([], 'Submit to provider');
$this
->assertSession()
->pageTextContains('This is not supported.');
$job4 = \Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->loadUnchanged($job
->id());
$this
->assertTrue($job4
->isRejected());
$this
->drupalGet('admin/tmgmt/jobs');
$sources = $this
->xpath('//table[contains(@class, "views-table")]/tbody/tr/td[@class="views-field views-field-source-language-1"][contains(., "English")]');
$this
->assertCount(4, $sources);
$targets = $this
->xpath('//table[contains(@class, "views-table")]/tbody/tr/td[@class="views-field views-field-target-language"][contains(., "Spanish") or contains(., "German")]');
$this
->assertCount(4, $targets);
$first_action = $this
->xpath('//tbody/tr[2]/td[11]/div/div/ul/li[1]/a');
$this
->assertEquals('Manage', $first_action[0]
->getText());
$this->default_translator
->setSetting('action', 'not_translatable');
$this->default_translator
->save();
$this
->drupalGet('admin/tmgmt/jobs/' . $job
->id());
$this
->submitForm([
'target_language' => 'de',
], 'Submit to provider');
$this
->assertSession()
->pageTextContains('Test provider can not translate from English to German.');
$this->default_translator
->setSetting('action', 'not_available');
$this->default_translator
->save();
$this
->drupalGet('admin/tmgmt/jobs/' . $job
->id());
$this
->assertSession()
->pageTextContains('Test provider is not available. Make sure it is properly configured.');
$this
->submitForm([], 'Submit to provider');
$this
->assertSession()
->pageTextContains(t('@translator is not available. Make sure it is properly configured.', array(
'@translator' => 'Test provider',
)));
$this
->loginAsTranslator([
'delete translation jobs',
]);
$this
->drupalGet('admin/tmgmt/jobs', array(
'query' => array(
'state' => 'All',
),
));
$start_rows = $this
->xpath('//tbody/tr');
$this
->assertCount(4, $start_rows);
$this
->drupalGet($job4
->toUrl('delete-form'));
$this
->assertSession()
->pageTextContains('Are you sure you want to delete the translation job test_source:test:11 and 2 more?');
$this
->submitForm([], 'Delete');
$this
->drupalGet('admin/tmgmt/jobs', array(
'query' => array(
'state' => 'All',
),
));
$end_rows = $this
->xpath('//tbody/tr');
$this
->assertCount(3, $end_rows);
$this
->drupalGet('admin/tmgmt/items/' . $item4
->id());
$this
->clickLink('Abort');
$this
->submitForm([], 'Confirm');
$this
->assertSession()
->pageTextContains('Aborted');
$this
->assertSession()
->linkNotExists('Abort');
$job_active = $this
->createJob();
$job_active
->save();
$job_active
->setState(Job::STATE_ACTIVE);
$this
->drupalGet($job_active
->toUrl('delete-form'));
$this
->assertSession()
->statusCodeEquals(403);
}
function testCheckoutFunction() {
$job = $this
->createJob();
$job_checkout_manager = \Drupal::service('tmgmt.job_checkout_manager');
$jobs = $job_checkout_manager
->checkoutMultiple(array(
$job,
));
$this
->assertEquals($job
->id(), $jobs[0]
->id());
$this
->assertTrue($job
->isUnprocessed());
$job
->delete();
$default_translator = Translator::load('test_translator');
$default_translator
->setSetting('expose_settings', FALSE)
->save();
$job = tmgmt_job_create('en', 'de', 0, []);
$jobs = $job_checkout_manager
->checkoutMultiple(array(
$job,
));
$this
->assertEmpty($jobs);
$this
->assertTrue($job
->isActive());
$job = $this
->createJob('en', LanguageInterface::LANGCODE_NOT_SPECIFIED);
$jobs = $job_checkout_manager
->checkoutMultiple(array(
$job,
));
$this
->assertEquals($job
->id(), $jobs[0]
->id());
$this
->assertTrue($job
->isUnprocessed());
$job = $this
->createJob();
$second_translator = $this
->createTranslator();
$second_translator
->setSetting('expose_settings', FALSE)
->save();
$jobs = $job_checkout_manager
->checkoutMultiple(array(
$job,
));
$this
->assertEquals($job
->id(), $jobs[0]
->id());
$this
->assertTrue($job
->isUnprocessed());
}
public function testSuggestions() {
$job = $this
->createJob();
$job
->addItem('test_source', 'test', 1);
$job
->addItem('test_source', 'test', 7);
$this
->drupalGet($job
->toUrl());
$this
->assertSession()
->responseContains('20');
$this
->assertSession()
->pageTextContains('test_source:test_suggestion:1');
$this
->assertSession()
->pageTextContains('test_source:test_suggestion:7');
$this
->assertSession()
->pageTextContains('Test suggestion for test source 1');
$this
->assertSession()
->pageTextContains('Test suggestion for test source 7');
$edit = array(
'suggestions_table[2]' => TRUE,
);
$this
->submitForm($edit, 'Add suggestions');
$this
->assertSession()
->responseContains('31');
$this
->assertSession()
->pageTextNotContains('Test suggestion for test source 7');
$this
->assertSession()
->pageTextContains('test_source:test_suggestion_suggestion:7');
$job = $this
->createJob();
$job
->addItem('test_html_source', 'test', 1);
$this
->drupalGet($job
->toUrl());
$this
->assertSession()
->pageTextNotContains('Suggestions');
}
function testAbortJob() {
$job = $this
->createJob();
$job
->addItem('test_source', 'test', 1);
$job
->addItem('test_source', 'test', 2);
$job
->addItem('test_source', 'test', 3);
$this
->drupalGet($job
->toUrl());
$edit = array(
'target_language' => 'es',
'settings[action]' => 'translate',
);
$this
->submitForm($edit, 'Submit to provider');
$this
->drupalGet($job
->toUrl());
$this
->submitForm([], 'Abort job');
$this
->submitForm([], 'Confirm');
$this
->assertSession()
->pageTextContains('The user ordered aborting the Job through the UI.');
$this
->assertSession()
->addressEquals('admin/tmgmt/jobs/' . $job
->id());
\Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->resetCache();
$job = Job::load($job
->id());
$this
->assertTrue($job
->isAborted());
foreach ($job
->getItems() as $item) {
$this
->assertTrue($item
->isAborted());
}
$this
->submitForm([], 'Resubmit');
$this
->submitForm([], 'Confirm');
$this
->assertSession()
->responseContains(t('This job is a duplicate of the previously aborted job <a href=":url">#@id</a>', array(
':url' => $job
->toUrl()
->toString(),
'@id' => $job
->id(),
)));
$url_parts = explode('/', $this
->getUrl());
$resubmitted_job = Job::load(array_pop($url_parts));
$this
->assertTrue($resubmitted_job
->isUnprocessed());
$this
->assertEquals($job
->getTranslator()
->id(), $resubmitted_job
->getTranslator()
->id());
$this
->assertEquals($job
->getSourceLangcode(), $resubmitted_job
->getSourceLangcode());
$this
->assertEquals($job
->getTargetLangcode(), $resubmitted_job
->getTargetLangcode());
$this
->assertEquals($job
->get('settings')
->getValue(), $resubmitted_job
->get('settings')
->getValue());
foreach ($job
->getItems() as $item) {
$_items = $resubmitted_job
->getItems(array(
'data' => array(
'value' => 'id ' . $item
->getItemId(),
'operator' => 'CONTAINS',
),
));
$_item = reset($_items);
$this
->assertNotEquals($item
->getJobId(), $_item
->getJobId());
$this
->assertEquals($item
->getPlugin(), $_item
->getPlugin());
$this
->assertEquals($item
->getItemId(), $_item
->getItemId());
$this
->assertEquals($item
->getItemType(), $_item
->getItemType());
$this
->assertTrue($_item
->getWordCount() > 0);
$this
->assertTrue($_item
->getCountPending() > 0);
$this
->assertEquals(0, $_item
->getCountTranslated());
$this
->assertEquals(0, $_item
->getCountAccepted());
$this
->assertEquals(0, $_item
->getCountReviewed());
}
$this
->loginAsAdmin();
$this
->drupalGet('admin/tmgmt/jobs/' . $job
->id());
$element = $this
->xpath('//div[@class="view-content"]/table[@class="views-table views-view-table cols-8"]/tbody//tr[1]/td[4]')[0];
$this
->assertEquals('Aborted', $element
->getText());
$this
->assertSession()
->responseContains(t('Job has been duplicated as a new job <a href=":url">#@id</a>.', array(
':url' => $resubmitted_job
->toUrl()
->toString(),
'@id' => $resubmitted_job
->id(),
)));
$this
->submitForm([], 'Delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('The translation job ' . $resubmitted_job
->label() . ' has been deleted.');
$this
->drupalGet('admin/tmgmt/jobs/2/delete');
$this
->submitForm([], 'Delete');
$this
->drupalGet('admin/tmgmt/jobs/');
$this
->assertSession()
->pageTextContains('No jobs available.');
$translator = $this
->createTranslator();
$job = $this
->createJob();
$job->translator = $translator;
$job
->save();
$job
->setState(Job::STATE_ACTIVE);
$job
->addItem('test_source', 'test', 1);
$this
->drupalGet('admin/tmgmt/jobs');
$this
->clickLink('Manage');
$this
->submitForm([], 'Abort job');
$this
->submitForm([], 'Confirm');
$this
->drupalGet('admin/tmgmt/jobs', array(
'query' => array(
'state' => JobInterface::STATE_ABORTED,
),
));
$this
->assertJobStateIcon(1, 'Aborted');
ConfigurableLanguage::load('de')
->delete();
$this
->drupalGet('admin/tmgmt/jobs/' . $job
->id());
}
function testCart() {
$this
->addLanguage('fr');
$job_items = array();
for ($i = 1; $i < 6; $i++) {
$job_item = tmgmt_job_item_create('test_source', 'test', $i);
$job_item
->save();
$job_items[$i] = $job_item;
}
$this
->loginAsTranslator();
foreach ($job_items as $job_item) {
$this
->drupalGet('tmgmt-add-to-cart/' . $job_item
->id());
}
$this
->drupalGet('admin/tmgmt/cart');
foreach ($job_items as $job_item) {
$this
->assertSession()
->pageTextContains($job_item
->label());
}
$this
->submitForm([
'items[1]' => TRUE,
'items[2]' => FALSE,
'items[3]' => FALSE,
'items[4]' => TRUE,
'items[5]' => FALSE,
], 'Remove selected item');
$this
->assertSession()
->pageTextContains($job_items[2]
->label());
$this
->assertSession()
->pageTextContains($job_items[3]
->label());
$this
->assertSession()
->pageTextContains($job_items[5]
->label());
$this
->assertSession()
->pageTextNotContains($job_items[1]
->label());
$this
->assertSession()
->pageTextNotContains($job_items[4]
->label());
$this
->assertSession()
->pageTextContains('Job items were removed from the cart.');
$existing_items = JobItem::loadMultiple();
$this
->assertTrue(!isset($existing_items[$job_items[1]
->id()]));
$this
->assertTrue(!isset($existing_items[$job_items[4]
->id()]));
$this
->submitForm([], 'Empty cart');
$this
->assertSession()
->pageTextNotContains($job_items[2]
->label());
$this
->assertSession()
->pageTextNotContains($job_items[3]
->label());
$this
->assertSession()
->pageTextNotContains($job_items[5]
->label());
$this
->assertSession()
->pageTextContains('All job items were removed from the cart.');
$existing_items = JobItem::loadMultiple();
$this
->assertTrue(empty($existing_items));
$language_sequence = array(
'en',
'en',
'fr',
'fr',
'de',
'de',
);
for ($i = 1; $i < 7; $i++) {
$job_item = tmgmt_job_item_create('test_source', 'test', $i);
$job_item
->save();
$job_items[$i] = $job_item;
$languages[$job_items[$i]
->id()] = $language_sequence[$i - 1];
}
\Drupal::state()
->set('tmgmt.test_source_languages', $languages);
foreach ($job_items as $job_item) {
$this
->drupalGet('tmgmt-add-to-cart/' . $job_item
->id());
}
$this
->drupalGet('admin/tmgmt/cart');
$this
->submitForm([
'items[' . $job_items[1]
->id() . ']' => TRUE,
'items[' . $job_items[2]
->id() . ']' => TRUE,
'items[' . $job_items[3]
->id() . ']' => TRUE,
'items[' . $job_items[4]
->id() . ']' => TRUE,
'items[' . $job_items[5]
->id() . ']' => TRUE,
'items[' . $job_items[6]
->id() . ']' => FALSE,
'target_language[]' => [
'en',
'de',
],
], 'Request translation');
$this
->assertSession()
->pageTextContains(t('@count jobs need to be checked out.', array(
'@count' => 4,
)));
$storage = \Drupal::entityTypeManager()
->getStorage('tmgmt_job');
$jobs = $storage
->loadByProperties([
'source_language' => 'fr',
'target_language' => 'en',
]);
$job = reset($jobs);
$this
->assertCount(2, $job
->getItems());
$jobs = $storage
->loadByProperties([
'source_language' => 'de',
'target_language' => 'en',
]);
$job = reset($jobs);
$this
->assertCount(1, $job
->getItems());
$jobs = $storage
->loadByProperties([
'source_language' => 'en',
'target_language' => 'de',
]);
$job = reset($jobs);
$this
->assertCount(2, $job
->getItems());
$jobs = $storage
->loadByProperties([
'source_language' => 'fr',
'target_language' => 'de',
]);
$job = reset($jobs);
$this
->assertCount(2, $job
->getItems());
$this
->drupalGet('admin/tmgmt/cart');
$this
->assertSession()
->pageTextNotContains($job_items[1]
->label());
$this
->assertSession()
->pageTextNotContains($job_items[2]
->label());
$this
->assertSession()
->pageTextNotContains($job_items[3]
->label());
$this
->assertSession()
->pageTextNotContains($job_items[4]
->label());
$this
->assertSession()
->pageTextNotContains($job_items[5]
->label());
$this
->assertSession()
->pageTextContains($job_items[6]
->label());
$this
->drupalGet('admin/tmgmt/cart');
$this
->submitForm([
'items[' . $job_items[6]
->id() . ']' => FALSE,
], 'Request translation');
$this
->assertSession()
->pageTextContainsOnce("You didn't select any source items.");
}
function testPageTitles() {
$this
->loginAsAdmin();
$translator = $this
->createTranslator();
$job = $this
->createJob();
$job->translator = $translator;
$job->settings = array();
$job
->save();
$item = $job
->addItem('test_source', 'test', 1);
$this
->drupalGet('/admin/tmgmt/settings');
$this
->assertSession()
->titleEquals('Settings | Drupal');
$this
->drupalGet('/admin/tmgmt/translators');
$this
->assertSession()
->titleEquals('Providers | Drupal');
$this
->drupalGet('/admin/tmgmt/translators/add');
$this
->assertSession()
->titleEquals('Add Provider | Drupal');
$this
->drupalGet('/admin/tmgmt/translators/manage/' . $translator
->id() . '/delete');
$this
->assertSession()
->titleEquals((string) t('Are you sure you want to delete the provider @label? | Drupal', [
'@label' => $translator
->label(),
]));
$this
->drupalGet('/admin/tmgmt/translators/manage/' . $translator
->id());
$this
->assertSession()
->titleEquals('Edit provider | Drupal');
$this
->drupalGet('/admin/tmgmt/jobs/' . $job
->id() . '/delete');
$this
->assertSession()
->titleEquals((string) t('Are you sure you want to delete the translation job @label? | Drupal', [
'@label' => $job
->label(),
]));
$this
->drupalGet('/admin/tmgmt/jobs/' . $job
->id() . '/resubmit');
$this
->assertSession()
->titleEquals('Resubmit as a new job? | Drupal');
$this
->drupalGet('/admin/tmgmt/jobs/' . $job
->id() . '/abort');
$this
->assertSession()
->titleEquals('Abort this job? | Drupal');
$this
->drupalGet('/admin/tmgmt/items/' . $job
->id());
$this
->assertSession()
->titleEquals((string) t('Job item @label | Drupal', [
'@label' => $item
->label(),
]));
$this
->assertSession()
->linkExists('Home');
$this
->assertSession()
->linkExists('Administration');
$this
->assertSession()
->linkExists('Job overview');
$this
->assertSession()
->linkExists($job
->label());
$this
->drupalGet('admin');
$this
->clickLink('Translation');
$this
->assertSession()
->titleEquals('Translation | Drupal');
$this
->clickLink('Cart');
$this
->assertSession()
->titleEquals('Cart | Drupal');
$this
->clickLink('Jobs');
$this
->assertSession()
->titleEquals('Job overview | Drupal');
$this
->clickLink('Sources');
$this
->assertSession()
->titleEquals('Translation Sources | Drupal');
}
function testJobItemDelete() {
$this
->loginAsAdmin();
$translator = $this
->createTranslator();
$job = $this
->createJob();
$job->translator = $translator;
$job->settings = array();
$job
->save();
$job
->setState(Job::STATE_ACTIVE);
$item = $job
->addItem('test_source', 'test', 1);
$item
->setState(JobItem::STATE_ACTIVE);
$this
->drupalGet('admin/tmgmt/items/' . $item
->id());
$this
->assertSession()
->fieldNotExists('edit-delete');
$this
->drupalGet('admin/tmgmt/jobs/' . $job
->id());
$this
->assertSession()
->linkNotExists('Delete');
$this
->assertSession()
->linkExists('Abort');
$this
->clickLink('Abort');
$this
->assertSession()
->pageTextContains('Are you sure you want to abort the job item test_source:test:1?');
$this
->assertSession()
->linkExists('Cancel');
$this
->submitForm([], 'Confirm');
\Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->resetCache();
$job = Job::load($job
->id());
$this
->assertTrue($job
->isFinished());
$items = $job
->getItems();
$item = reset($items);
$this
->assertTrue($item
->isAborted());
$this
->drupalGet('admin/tmgmt/items/' . $item
->id());
$this
->assertSession()
->fieldNotExists('edit-delete');
$this
->assertSession()
->fieldNotExists('edit-accept');
$this
->drupalGet('admin/tmgmt/jobs/' . $job
->id());
$this
->assertSession()
->linkNotExists('Delete');
}
public function testSettings() {
$this
->loginAsAdmin();
$settings = \Drupal::config('tmgmt.settings');
$this
->assertTrue($settings
->get('quick_checkout'));
$this
->assertTrue($settings
->get('anonymous_access'));
$this
->assertEquals('_never', $settings
->get('purge_finished'));
$this
->assertTrue($settings
->get('word_count_exclude_tags'));
$this
->assertEquals(20, $settings
->get('source_list_limit'));
$this
->assertEquals(50, $settings
->get('job_items_cron_limit'));
$this
->assertTrue($settings
->get('respect_text_format'));
$this
->assertFalse($settings
->get('submit_job_item_on_cron'));
$this
->drupalGet('admin/tmgmt/settings');
$edit = [
'tmgmt_quick_checkout' => FALSE,
'tmgmt_anonymous_access' => FALSE,
'tmgmt_purge_finished' => 0,
'respect_text_format' => FALSE,
'tmgmt_submit_job_item_on_cron' => TRUE,
];
$this
->submitForm($edit, 'Save configuration');
$settings = \Drupal::config('tmgmt.settings');
$this
->assertFalse($settings
->get('quick_checkout'));
$this
->assertFalse($settings
->get('anonymous_access'));
$this
->assertEquals(0, $settings
->get('purge_finished'));
$this
->assertFalse($settings
->get('respect_text_format'));
$this
->assertTrue($settings
->get('submit_job_item_on_cron'));
}
public function testProgress() {
$this
->drupalGet('admin/tmgmt/jobs');
$this
->assertSession()
->pageTextContains('No jobs available.');
$this
->assertSession()
->optionExists('edit-state', 'Items - In progress');
$this
->assertSession()
->optionExists('edit-state', 'Items - Needs review');
$this
->assertSession()
->optionExists('edit-state', 'Items - Translation is requested from the elders of the Internet');
$this
->assertSession()
->pageTextContains('Translation is requested from the elders of the Internet');
$this
->drupalGet('admin/tmgmt/sources');
$job1 = $this
->createJob();
$job1
->save();
$job1
->setState(Job::STATE_UNPROCESSED);
$job2 = $this
->createJob();
$job2
->save();
$job2
->setState(Job::STATE_ACTIVE);
$job3 = $this
->createJob();
$job3
->save();
$job3
->setState(Job::STATE_REJECTED);
$job4 = $this
->createJob();
$job4
->save();
$job4
->setState(Job::STATE_ABORTED);
$job5 = $this
->createJob();
$job5
->save();
$job5
->setState(Job::STATE_FINISHED);
$this
->drupalGet('admin/tmgmt/jobs', array(
'query' => array(
'state' => 'All',
),
));
$this
->assertCount(5, $this
->xpath('//tbody/tr'));
$this
->assertJobStateIcon(1, 'Unprocessed');
$this
->assertJobStateIcon(2, 'In progress');
$this
->assertJobStateIcon(3, 'Rejected');
$this
->assertJobStateIcon(4, 'Aborted');
$this
->assertJobStateIcon(5, 'Finished');
$this
->drupalGet('admin/tmgmt/jobs', [
'query' => [
'state' => 'open_jobs',
],
]);
$this
->assertCount(3, $this
->xpath('//tbody/tr'));
$this
->drupalGet('admin/tmgmt/jobs', [
'query' => [
'state' => JobInterface::STATE_UNPROCESSED,
],
]);
$this
->assertCount(1, $this
->xpath('//tbody/tr'));
$this
->drupalGet('admin/tmgmt/jobs', [
'query' => [
'state' => JobInterface::STATE_REJECTED,
],
]);
$this
->assertCount(1, $this
->xpath('//tbody/tr'));
$this
->drupalGet('admin/tmgmt/jobs', array(
'query' => array(
'state' => JobInterface::STATE_ABORTED,
),
));
$this
->assertCount(1, $this
->xpath('//tbody/tr'));
$this
->drupalGet('admin/tmgmt/jobs', array(
'query' => array(
'state' => JobInterface::STATE_FINISHED,
),
));
$this
->assertCount(1, $this
->xpath('//tbody/tr'));
\Drupal::state()
->set('tmgmt.test_source_data', array(
'title' => array(
'deep_nesting' => array(
'#text' => '<p><em><strong>Six dummy HTML tags in the title.</strong></em></p>',
'#label' => 'Title',
),
),
'body' => array(
'deep_nesting' => array(
'#text' => '<p>Two dummy HTML tags in the body.</p>',
'#label' => 'Body',
),
),
));
$item1 = $job1
->addItem('test_source', 'test', 1);
$job1
->addItem('test_source', 'test', 2);
$this
->drupalGet('admin/tmgmt/job_items', array(
'query' => array(
'state' => 'All',
),
));
$this
->assertCount(2, $this
->xpath('//tbody/tr'));
$this
->assertJobItemOverviewStateIcon(1, 'Inactive');
$this
->assertSession()
->linkExists($job1
->label());
$this
->drupalGet($job1
->toUrl());
$edit = array(
'target_language' => 'de',
'settings[action]' => 'submit',
);
$this
->submitForm($edit, 'Submit to provider');
$this
->drupalGet('admin/tmgmt/items/' . $item1
->id());
$this
->submitForm([
'body|deep_nesting[translation]' => 'translation',
], 'Save');
$this
->assertJobItemStateIcon(1, 'In progress');
$this
->drupalGet('admin/tmgmt/job_items', array(
'query' => array(
'state' => JobItemInterface::STATE_ACTIVE,
),
));
$this
->assertCount(2, $this
->xpath('//tbody/tr'));
$this
->assertJobItemOverviewStateIcon(1, 'In progress');
$this
->drupalGet('admin/tmgmt/jobs', [
'query' => [
'state' => 'job_item_' . JobItemInterface::STATE_ACTIVE,
],
]);
$this
->assertJobProgress(1, 3, 1, 0, 0);
$this
->assertJobStateIcon(1, 'In progress');
\Drupal::entityTypeManager()
->getStorage('tmgmt_job_item')
->resetCache();
$item1 = JobItem::load($item1
->id());
$item1
->setTranslatorState('tmgmt_test_generating');
$item1
->save();
$this
->drupalGet('admin/tmgmt/job_items', array(
'query' => array(
'state' => 'tmgmt_test_generating',
),
));
$this
->assertCount(1, $this
->xpath('//tbody/tr'));
$this
->assertJobItemOverviewStateIcon(1, 'Translation is requested from the elders of the Internet');
$this
->assertSession()
->responseContains('earth.svg"');
$this
->drupalGet('admin/tmgmt/jobs', [
'query' => [
'state' => 'job_item_' . JobItemInterface::STATE_ACTIVE,
],
]);
$this
->assertJobProgress(1, 3, 1, 0, 0);
$this
->assertJobStateIcon(1, 'Translation is requested from the elders of the Internet');
$this
->assertSession()
->responseContains('earth.svg"');
$this
->drupalGet('admin/tmgmt/jobs', [
'query' => [
'state' => 'job_item_tmgmt_test_generating',
],
]);
$this
->assertJobProgress(1, 3, 1, 0, 0);
$this
->assertJobStateIcon(1, 'Translation is requested from the elders of the Internet');
$this
->assertSession()
->responseContains('earth.svg"');
$this
->drupalGet('admin/tmgmt/items/' . $item1
->id());
$this
->submitForm([
'title|deep_nesting[translation]' => 'translation',
], 'Save');
$this
->assertJobItemStateIcon(1, 'Needs review');
$this
->drupalGet('admin/tmgmt/job_items', array(
'query' => array(
'state' => JobItemInterface::STATE_REVIEW,
),
));
$this
->assertCount(1, $this
->xpath('//tbody/tr'));
$this
->assertJobItemOverviewStateIcon(1, 'Needs review');
$this
->drupalGet('admin/tmgmt/jobs', [
'query' => [
'state' => 'job_item_' . JobItemInterface::STATE_REVIEW,
],
]);
$this
->assertCount(1, $this
->xpath('//tbody/tr'));
$this
->assertJobProgress(1, 2, 2, 0, 0);
$this
->assertJobStateIcon(1, 'Needs review');
$page = $this
->getSession()
->getPage();
$this
->drupalGet('admin/tmgmt/items/' . $item1
->id());
$page
->pressButton('reviewed-body|deep_nesting');
$this
->drupalGet('admin/tmgmt/jobs/' . $job1
->id());
$this
->assertJobItemStateIcon(1, 'Needs review');
$this
->drupalGet('admin/tmgmt/items/' . $item1
->id());
$page
->pressButton('reviewed-title|deep_nesting');
$this
->drupalGet('admin/tmgmt/jobs/' . $job1
->id());
$this
->assertJobItemStateIcon(1, 'Needs review');
$this
->drupalGet('admin/tmgmt/jobs', array(
'query' => array(
'state' => 'open_jobs',
),
));
$this
->assertJobProgress(1, 2, 0, 2, 0);
$this
->assertJobStateIcon(1, 'Needs review');
$this
->drupalGet($item1
->toUrl());
$this
->submitForm([], 'Save as completed');
$this
->assertJobItemStateIcon(1, 'Accepted');
$this
->drupalGet('admin/tmgmt/job_items', array(
'query' => array(
'state' => JobItemInterface::STATE_ACCEPTED,
),
));
$this
->assertCount(1, $this
->xpath('//tbody/tr'));
$this
->assertJobItemOverviewStateIcon(1, 'Accepted');
$this
->drupalGet('admin/tmgmt/jobs', array(
'query' => array(
'state' => 'open_jobs',
),
));
$this
->assertJobProgress(1, 2, 0, 0, 2);
$this
->assertJobStateIcon(1, 'In progress');
$this
->drupalGet('admin/tmgmt/items/' . $item1
->id());
$this
->assertSession()
->responseContains('class="tmgmt-color-legend');
}
private function assertJobStateIcon($row, $state) {
if ($state == 'Unprocessed' || $state == 'Rejected' || $state == 'Aborted' || $state == 'Finished') {
$result = $this
->xpath('//table/tbody/tr[' . $row . ']/td[6]')[0];
$this
->assertEquals($state, trim($result
->getHtml()));
}
else {
$result = $this
->xpath('//table/tbody/tr[' . $row . ']/td[1]/img')[0];
$this
->assertEquals($state, $result
->getAttribute('title'));
}
}
protected function assertJobItemStateIcon($row, $state) {
if ($state == 'Inactive' || $state == 'Aborted' || $state == 'Accepted') {
$result = $this
->xpath('//div[@id="edit-job-items-wrapper"]//tbody/tr[' . $row . ']/td[4]')[0];
$this
->assertEquals($state, trim($result
->getHtml()));
}
else {
$result = $this
->xpath('//div[@id="edit-job-items-wrapper"]//tbody/tr[' . $row . ']/td[1]/img')[0];
$this
->assertEquals($state, $result
->getAttribute('title'));
}
}
private function assertJobItemOverviewStateIcon($row, $state) {
if ($state == 'Inactive' || $state == 'Aborted' || $state == 'Accepted') {
$result = $this
->xpath('//table/tbody/tr[' . $row . ']/td[7]')[0];
$this
->assertEquals($state, trim($result
->getHtml()));
}
else {
$result = $this
->xpath('//table/tbody/tr[' . $row . ']/td[1]/img')[0];
$this
->assertEquals($state, $result
->getAttribute('title'));
}
}
private function assertJobProgress($row, $pending, $translated, $reviewed, $accepted) {
$result = $this
->xpath('//table/tbody/tr[' . $row . ']/td[6]')[0];
$div_number = 1;
if ($pending > 0) {
$this
->assertEquals('tmgmt-progress tmgmt-progress-pending', $result
->find('css', "div > div:nth-child({$div_number})")
->getAttribute('class'));
$div_number++;
}
else {
$this
->assertNotEquals('tmgmt-progress tmgmt-progress-pending', $result
->find('css', "div > div:nth-child({$div_number})")
->getAttribute('class'));
}
if ($translated > 0) {
$this
->assertEquals('tmgmt-progress tmgmt-progress-translated', $result
->find('css', "div > div:nth-child({$div_number})")
->getAttribute('class'));
$div_number++;
}
else {
$child = $result
->find('css', "div > div:nth-child({$div_number})");
$this
->assertTrue(!$child || $child
->getAttribute('class') != 'tmgmt-progress tmgmt-progress-translated');
}
if ($reviewed > 0) {
$this
->assertEquals('tmgmt-progress tmgmt-progress-reviewed', $result
->find('css', "div > div:nth-child({$div_number})")
->getAttribute('class'));
$div_number++;
}
else {
$child = $result
->find('css', "div > div:nth-child({$div_number})");
$this
->assertTrue(!$child || $child
->getAttribute('class') != 'tmgmt-progress tmgmt-progress-reviewed');
}
if ($accepted > 0) {
$this
->assertEquals('tmgmt-progress tmgmt-progress-accepted', $result
->find('css', "div > div:nth-child({$div_number})")
->getAttribute('class'));
}
else {
$child = $result
->find('css', "div > div:nth-child({$div_number})");
$this
->assertTrue(!$child || $child
->getAttribute('class') != 'tmgmt-progress tmgmt-progress-accepted');
}
$title = t('Pending: @pending, translated: @translated, reviewed: @reviewed, accepted: @accepted.', array(
'@pending' => $pending,
'@translated' => $translated,
'@reviewed' => $reviewed,
'@accepted' => $accepted,
));
$this
->assertEquals($title, $result
->find('css', 'div')
->getAttribute('title'));
}
}