View source
<?php
namespace Drupal\Tests\tmgmt_content\Functional;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\Tests\tmgmt\Functional\TmgmtEntityTestTrait;
use Drupal\Tests\tmgmt\Functional\TMGMTTestBase;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Entity\Translator;
class ContentTmgmtEntitySourceUiTest extends TMGMTTestBase {
use TmgmtEntityTestTrait;
public static $modules = array(
'tmgmt_content',
'comment',
'ckeditor',
'block_content',
);
function setUp() : void {
parent::setUp();
$this
->addLanguage('de');
$this
->addLanguage('fr');
$this
->addLanguage('es');
$this
->addLanguage('el');
$this
->addLanguage('it');
$this
->createNodeType('page', 'Page', TRUE);
$this
->createNodeType('article', 'Article', TRUE);
$this
->loginAsAdmin(array(
'create translation jobs',
'submit translation jobs',
'accept translation jobs',
'administer blocks',
'administer content translation',
'edit any article content',
));
}
function testNodeTranslateTabSingleCheckout() {
$this
->loginAsTranslator(array(
'translate any entity',
'create content translations',
));
$node = $this
->createTranslatableNode('page', 'en');
$node_not_translated = $this
->createTranslatableNode('page', 'en');
$node_german = $this
->createTranslatableNode('page', 'de');
$this
->drupalGet('node/' . $node
->id());
$this
->clickLink('Translate');
$this
->assertText(t('Translations of @title', array(
'@title' => $node
->getTitle(),
)));
$this
->assertText(t('Pending Translations'));
$edit = array(
'languages[de]' => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Request translation'));
$this
->assertText(t('One job needs to be checked out.'));
$this
->assertText($node
->getTitle());
$this
->drupalPostForm(NULL, array(), t('Submit to provider'));
$this
->assertEqual($node
->toUrl('canonical', array(
'absolute' => TRUE,
))
->toString() . '/translations', $this
->getUrl());
$this
->assertText(t('Test translation created.'));
$this
->assertText(t('The translation of @title to @language is finished and can now be reviewed.', array(
'@title' => $node
->getTitle(),
'@language' => t('German'),
)));
$this
->clickLinkWithImageTitle('Needs review');
$this
->drupalPostForm(NULL, array(), t('Save as completed'));
$node = Node::load($node
->id());
$translation = $node
->getTranslation('de');
$this
->assertText(t('The translation for @title has been accepted as @target.', array(
'@title' => $node
->getTitle(),
'@target' => $translation
->label(),
)));
$this
->clickLink('de(de-ch): ' . $node
->label());
$this
->assertText('de(de-ch): ' . $node
->getTitle());
$this
->assertText('de(de-ch): ' . $node->body->value);
$this
->drupalGet('node/' . $node
->id());
$this
->clickLink(t('Translate'));
$edit = array(
'languages[fr]' => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Request translation'));
$this
->drupalGet('node/' . $node
->id() . '/translations', array(
'query' => array(
'destination' => 'node/' . $node
->id(),
),
));
$this
->clickLink(t('Inactive'));
$this
->assertText($node
->getTitle());
$this
->assertRaw('<div data-drupal-selector="edit-actions" class="form-actions js-form-wrapper form-wrapper" id="edit-actions">');
$this
->drupalPostForm(NULL, [], t('Validate HTML tags'));
$this
->assertText($node
->label());
$this
->assertResponse(200);
$this
->drupalGet('node/' . $node
->id() . '/translations', array(
'query' => array(
'destination' => 'node/' . $node
->id(),
),
));
$edit = array(
'languages[es]' => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Request translation'));
$this
->assertText(t('One job needs to be checked out.'));
$this
->assertText($node
->getTitle());
$this
->drupalPostForm(NULL, array(), t('Submit to provider'));
$this
->assertEqual($node
->toUrl('canonical', array(
'absolute' => TRUE,
))
->toString(), $this
->getUrl());
$this
->drupalGet('admin/tmgmt/sources/content/node');
$this
->assertText($node
->getTitle());
$this
->assertText($node_not_translated
->getTitle());
$this
->drupalPostForm(NULL, array(
'search[target_language]' => 'de',
'search[target_status]' => 'untranslated',
), t('Search'));
$this
->assertNoText($node
->getTitle());
$this
->assertNoText($node_german
->getTitle());
$this
->assertText($node_not_translated
->getTitle());
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
$node = Node::load($node
->id());
$node
->getTranslation('de')->content_translation_outdated->value = 1;
$node
->save();
$this
->drupalPostForm(NULL, array(
'search[target_language]' => 'de',
'search[target_status]' => 'outdated',
), t('Search'));
$this
->assertText($node
->getTitle());
$this
->assertNoText($node_german
->getTitle());
$this
->assertNoText($node_not_translated
->getTitle());
$this
->drupalPostForm(NULL, array(
'search[target_language]' => 'de',
'search[target_status]' => 'untranslated_or_outdated',
), t('Search'));
$this
->assertText($node
->getTitle());
$this
->assertNoText($node_german
->getTitle());
$this
->assertText($node_not_translated
->getTitle());
$xpath = $this
->xpath('//*[@id="edit-items"]/tbody/tr[2]/td[6]/a/img');
$this
->assertEqual($xpath[0]
->getAttribute('title'), t('Translation Outdated'));
$xpath_source = $this
->xpath('//*[@id="edit-items"]/tbody/tr[2]/td[4]/*[1]');
$xpath_not_translated = $this
->xpath('//*[@id="edit-items"]/tbody/tr[2]/td[5]/*[1]');
$xpath_outdated = $this
->xpath('//*[@id="edit-items"]/tbody/tr[2]/td[6]/*[1]');
$this
->assertTrue(strpos($xpath_source[0]
->getAttribute('href'), '/node/1') !== FALSE);
$this
->assertContains('node/1', $xpath_source[0]
->getAttribute('href'));
$this
->assertNotEquals('a', $xpath_not_translated[0]
->getTagName());
$this
->assertContains('/de/node/1', $xpath_outdated[0]
->getAttribute('href'));
$deleted_node = $this
->createTranslatableNode('page', 'en');
$second_node = $this
->createTranslatableNode('page', 'en');
$this
->drupalGet('node/' . $deleted_node
->id() . '/translations');
$edit = array(
'languages[de]' => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Request translation'));
$this
->drupalPostForm(NULL, array(), t('Submit to provider'));
$edit = array(
'languages[fr]' => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Request translation'));
$this
->drupalPostForm(NULL, [], t('Submit to provider'));
$job = $this
->createJob('en', 'de');
$job
->addItem('content', 'node', $deleted_node
->id());
$job
->addItem('content', 'node', $second_node
->id());
$this
->drupalGet($job
->toUrl());
$this
->drupalPostForm(NULL, [], t('Submit to provider'));
$this
->assertText(t('1 conflicting item has been dropped.'));
$this
->drupalGet('node/' . $deleted_node
->id() . '/translations');
$this
->clickLinkWithImageTitle('Needs review');
$deleted_node
->delete();
$this
->drupalPostForm(NULL, array(), t('Save as completed'));
$this
->assertText(t('@id of type @type does not exist, the job can not be completed.', array(
'@id' => $deleted_node
->id(),
'@type' => $deleted_node
->getEntityTypeId(),
)));
}
function testNodeTranslateTabMultipleCheckout() {
$default_translator = Translator::load('test_translator');
$default_translator
->setAutoAccept(TRUE)
->save();
$this
->loginAsTranslator(array(
'translate any entity',
'create content translations',
));
$node = $this
->createTranslatableNode('page', 'en');
$this
->drupalGet('node/' . $node
->id());
$this
->clickLink('Translate');
$this
->assertText(t('Translations of @title', array(
'@title' => $node
->getTitle(),
)));
$this
->assertText(t('Pending Translations'));
$edit = array(
'languages[de]' => TRUE,
'languages[es]' => TRUE,
'languages[it]' => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Request translation'));
$this
->assertText(t('3 jobs need to be checked out.'));
$this
->assertText('3 jobs pending');
$this
->assertText($node
->label() . ', English to German');
$this
->assertText($node
->label() . ', English to Spanish');
$this
->assertText($node
->label() . ', English to Italian');
$this
->assertRaw('progress__track');
$this
->assertRaw('<div class="progress__bar" style="width: 3%"></div>');
$edit = [
'label[0][value]' => 'Customized label',
'submit_all' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Submit to provider and continue'));
$this
->assertText('Test translation created.');
$this
->assertText('The translation job has been finished.');
$this
->assertText('The translation for ' . $node
->label() . ' has been accepted as de(de-ch): ' . $node
->label() . '.');
$this
->assertText('The translation for ' . $node
->label() . ' has been accepted as es: ' . $node
->label() . '.');
$this
->assertText('The translation for ' . $node
->label() . ' has been accepted as it: ' . $node
->label() . '.');
$this
->assertEqual($node
->toUrl('canonical', array(
'absolute' => TRUE,
))
->toString() . '/translations', $this
->getUrl());
$this
->assertText(t('Test translation created.'));
$this
->assertNoText(t('The translation of @title to @language is finished and can now be reviewed.', array(
'@title' => $node
->getTitle(),
'@language' => t('Spanish'),
)));
$node = Node::load($node
->id());
$translation = $node
->getTranslation('es');
$this
->assertText(t('The translation for @title has been accepted as @target.', array(
'@title' => $node
->getTitle(),
'@target' => $translation
->label(),
)));
$this
->clickLink($node
->getTitle());
$this
->drupalGet('de/node/' . $node
->id());
$this
->assertText('de(de-ch): ' . $node
->getTitle());
$this
->assertText('de(de-ch): ' . $node->body->value);
$this
->drupalGet('es/node/' . $node
->id());
$this
->assertText('es: ' . $node
->getTitle());
$this
->assertText('es: ' . $node->body->value);
foreach (Job::loadMultiple() as $job) {
$this
->assertEqual($job
->label(), 'Customized label');
}
}
function testNodeTranslateTabQuickCheckout() {
$default_translator = Translator::load('test_translator');
$default_translator
->setSetting('expose_settings', FALSE)
->setAutoAccept(TRUE)
->save();
$this
->loginAsTranslator([
'translate any entity',
'create content translations',
]);
$node = $this
->createTranslatableNode('page', 'en');
$this
->drupalGet($node
->toUrl());
$this
->clickLink('Translate');
$edit = [
'languages[de]' => TRUE,
'languages[es]' => TRUE,
'languages[it]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Request translation');
$this
->assertText('Test translation created.');
$this
->assertText('The translation job has been finished.');
$this
->assertText('The translation for ' . $node
->label() . ' has been accepted as de(de-ch): ' . $node
->label() . '.');
$this
->assertText('The translation for ' . $node
->label() . ' has been accepted as es: ' . $node
->label() . '.');
$this
->assertText('The translation for ' . $node
->label() . ' has been accepted as it: ' . $node
->label() . '.');
$this
->assertEqual($node
->toUrl('drupal:content-translation-overview', [
'absolute' => TRUE,
])
->toString(), $this
->getUrl());
$this
->assertText('Test translation created.');
$this
->assertNoText(t('The translation of @title to @language is finished and can now be reviewed.', array(
'@title' => $node
->getTitle(),
'@language' => t('Spanish'),
)));
$node = Node::load($node
->id());
$translation = $node
->getTranslation('es');
$this
->assertText(t('The translation for @title has been accepted as @target.', [
'@title' => $node
->getTitle(),
'@target' => $translation
->label(),
]));
$this
->clickLink($node
->getTitle());
$this
->clickLink('Translate');
$this
->clickLink('de(de-ch): ' . $node
->getTitle());
$this
->assertText('de(de-ch): ' . $node
->getTitle());
$this
->assertText('de(de-ch): ' . $node->body->value);
$this
->drupalGet('es/node/' . $node
->id());
$this
->assertText('es: ' . $node
->getTitle());
$this
->assertText('es: ' . $node->body->value);
}
function testNodeTranslateTabMultipleCheckoutUnsupported() {
$default_translator = Translator::load('test_translator');
$default_translator
->setAutoAccept(TRUE)
->save();
$this
->loginAsTranslator([
'translate any entity',
'create content translations',
]);
$node = $this
->createTranslatableNode('page', 'en');
$this
->drupalGet('node/' . $node
->id());
$this
->clickLink('Translate');
$this
->assertText(t('Translations of @title', [
'@title' => $node
->getTitle(),
]));
$this
->assertText(t('Pending Translations'));
$edit = [
'languages[de]' => TRUE,
'languages[es]' => TRUE,
'languages[el]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Request translation'));
$this
->assertText(t('3 jobs need to be checked out.'));
$this
->assertText('3 jobs pending');
$this
->assertText($node
->label() . ', English to German');
$this
->assertText($node
->label() . ', English to Spanish');
$this
->assertText($node
->label() . ', English to Greek');
$this
->assertRaw('progress__track');
$this
->assertRaw('<div class="progress__bar" style="width: 3%"></div>');
$edit = [
'submit_all' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Submit to provider and continue'));
$this
->assertText('Test translation created.');
$this
->assertText('The translation job has been finished.');
$this
->assertText('The translation for ' . $node
->label() . ' has been accepted as de(de-ch): ' . $node
->label() . '.');
$this
->assertText('The translation for ' . $node
->label() . ' has been accepted as es: ' . $node
->label() . '.');
$this
->assertText('Job ' . $node
->label() . ' is not translatable with the chosen settings: Test provider can not translate from English to Greek.');
$this
->assertText('1 job pending');
$this
->assertNoText($node
->label() . ', English to German');
$this
->assertNoText($node
->label() . ', English to Spanish');
$this
->assertText($node
->label() . ', English to Greek');
$this
->assertRaw('progress__track');
$this
->assertRaw('<div class="progress__bar" style="width: 67%"></div>');
}
function testCommentTranslateTab() {
$default_translator = Translator::load('test_translator');
$default_translator
->setAutoAccept(TRUE)
->save();
$this
->addDefaultCommentField('node', 'article');
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('comment', 'comment', TRUE);
$comment_body = FieldConfig::loadByName('comment', 'comment', 'comment_body');
$comment_body
->setTranslatable(TRUE)
->save();
$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);
$node = $this
->createTranslatableNode('article', 'en');
$this
->drupalGet('node/' . $node
->id());
$edit = array(
'subject[0][value]' => $this
->randomMachineName(),
'comment_body[0][value]' => $this
->randomMachineName(),
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText(t('Your comment has been posted.'));
$this
->clickLink('Edit');
$this
->assertNotEmpty(preg_match('|comment/(\\d+)/edit$|', $this
->getUrl(), $matches), 'Comment found');
$comment = Comment::load($matches[1]);
$this
->clickLink('Translate');
$this
->assertText(t('Translations of @title', array(
'@title' => $comment
->getSubject(),
)));
$this
->assertText(t('Pending Translations'));
$edit = array(
'languages[de]' => TRUE,
'languages[es]' => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Request translation'));
$this
->assertText(t('2 jobs need to be checked out.'));
$this
->assertText($comment
->getSubject());
$this
->drupalPostForm(NULL, array(), t('Submit to provider and continue'));
$this
->assertText($comment
->getSubject());
$this
->drupalPostForm(NULL, array(), t('Submit to provider'));
$this
->assertUrl($comment
->toUrl('canonical', array(
'absolute' => TRUE,
))
->toString() . '/translations');
$this
->assertText(t('Test translation created.'));
$this
->assertNoText(t('The translation of @title to @language is finished and can now be reviewed.', array(
'@title' => $comment
->getSubject(),
'@language' => t('Spanish'),
)));
$this
->assertText(t('The translation for @title has been accepted as es: @target.', array(
'@title' => $comment
->getSubject(),
'@target' => $comment
->getSubject(),
)));
$this
->clickLink('de(de-ch): ' . $comment
->getSubject());
$this
->assertText('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
->assertText('es: ' . $comment
->get('comment_body')->value);
$default_translator
->setAutoAccept(FALSE)
->save();
$edit = [
'languages[it]' => TRUE,
];
$this
->drupalPostForm('comment/' . $comment
->id() . '/translations', $edit, 'Request translation');
$this
->drupalPostForm(NULL, [], 'Submit to provider');
$this
->clickLink('reviewed');
$this
->assertText('Translation publish status');
$this
->assertFieldChecked('edit-status-published', 'Target publish status field is checked.');
$edit = [
'status[published]' => FALSE,
];
$this
->drupalPostForm(NULL, $edit, 'Save as completed');
$this
->drupalGet('it/comment/' . $comment
->id());
$this
->assertText('it: ' . $comment
->getSubject());
$this
->drupalGet('comment/' . $comment
->id());
$this
->assertResponse(200);
$this
->assertText($comment
->getSubject());
$this
->drupalGet('de/comment/' . $comment
->id());
$this
->assertResponse(200);
$this
->drupalLogout();
$this
->drupalGet('it/comment/' . $comment
->id());
$this
->assertResponse(403);
}
function testCart() {
$this
->loginAsTranslator(array(
'translate any entity',
'create content translations',
));
$nodes = array();
for ($i = 0; $i < 4; $i++) {
$nodes[$i] = $this
->createTranslatableNode('page');
}
$this
->drupalPostForm('admin/tmgmt/sources/content/node', array(
'items[' . $nodes[1]
->id() . ']' => TRUE,
'items[' . $nodes[2]
->id() . ']' => TRUE,
), t('Add to cart'));
$this
->drupalGet('admin/tmgmt/cart');
$this
->assertText($nodes[1]
->getTitle());
$this
->assertText($nodes[2]
->getTitle());
$this
->drupalGet('node/' . $nodes[3]
->id() . '/translations');
$this
->assertRaw(t('There are @count items in the <a href=":url">translation cart</a>.', array(
'@count' => 2,
':url' => Url::fromRoute('tmgmt.cart')
->toString(),
)));
$this
->drupalPostForm(NULL, array(), t('Add to cart'));
$this
->assertRaw(t('@count content source was added into the <a href=":url">cart</a>.', array(
'@count' => 1,
':url' => Url::fromRoute('tmgmt.cart')
->toString(),
)));
$this
->assertRaw(t('There are @count items in the <a href=":url">translation cart</a> including the current item.', array(
'@count' => 3,
':url' => Url::fromRoute('tmgmt.cart')
->toString(),
)));
$nodes = array();
for ($i = 0; $i < 50; $i++) {
$nodes[$i] = $this
->createTranslatableNode('page');
}
$this
->drupalGet('admin/tmgmt/sources/content/node');
$this
->assertRaw('<ul class="pager__items js-pager__items">');
$this
->assertEqual(count($this
->xpath('//nav[@class="pager"]/ul[@class="pager__items js-pager__items"]/li/a')), 5);
}
function testEmbeddedReferences() {
$field1 = FieldStorageConfig::create(array(
'field_name' => 'field1',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => array(
'target_type' => 'node',
),
));
$field1
->save();
$field2 = FieldStorageConfig::create(array(
'field_name' => 'field2',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => array(
'target_type' => 'node',
),
));
$field2
->save();
$field3 = FieldStorageConfig::create(array(
'field_name' => 'field3',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => array(
'target_type' => 'node',
),
));
$field3
->save();
$field4 = FieldStorageConfig::create(array(
'field_name' => 'field4',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => array(
'target_type' => 'user',
),
));
$field4
->save();
$this
->createNodeType('untranslatable', 'Untranslatable', FALSE);
FieldConfig::create(array(
'field_storage' => $field1,
'bundle' => 'article',
'label' => 'Field 1',
'translatable' => FALSE,
'settings' => array(),
))
->save();
FieldConfig::create(array(
'field_storage' => $field1,
'bundle' => 'untranslatable',
'label' => 'Field 1',
'translatable' => FALSE,
'settings' => array(),
))
->save();
FieldConfig::create(array(
'field_storage' => $field2,
'bundle' => 'untranslatable',
'label' => 'Field 2',
'translatable' => FALSE,
'settings' => array(),
))
->save();
FieldConfig::create(array(
'field_storage' => $field3,
'bundle' => 'article',
'label' => 'Field 3',
'translatable' => TRUE,
'settings' => array(),
))
->save();
FieldConfig::create(array(
'field_storage' => $field4,
'bundle' => 'article',
'label' => 'Field 4',
'translatable' => FALSE,
'settings' => array(),
))
->save();
EntityViewDisplay::load('node.article.default')
->setComponent('field1', [
'type' => 'entity_reference_entity_view',
'settings' => [
'view_mode' => 'teaser',
],
])
->save();
$this
->drupalGet('admin/tmgmt/settings');
$checked_reference_fields = array(
'embedded_fields[node][field1]' => TRUE,
'embedded_fields[node][field3]' => TRUE,
);
$this
->assertUniqueText('Note: This is a translatable field, embedding this will add a translation on the existing reference.');
$this
->assertNoField('embedded_fields[node][title]');
$this
->assertNoField('embedded_fields[node][uid]');
$this
->assertNoField('embedded_fields[node][field2]');
$this
->assertNoField('embedded_fields[node][field4]');
$this
->assertNoField('embedded_fields[node][type]');
$this
->drupalPostForm(NULL, $checked_reference_fields, t('Save configuration'));
$this
->assertText(t('The configuration options have been saved.'));
$this
->assertFieldChecked('edit-embedded-fields-node-field1');
$this
->assertFieldChecked('edit-embedded-fields-node-field3');
$edit = [
'title' => 'Child title',
'type' => 'article',
'langcode' => 'en',
];
$child_node = $this
->createNode($edit);
$edit = [
'title' => 'Parent title',
'type' => 'article',
'langcode' => 'en',
];
$edit['field1'][]['target_id'] = $child_node
->id();
$parent_node = $this
->createNode($edit);
$job = $this
->createJob('en', 'de');
$job->translator = $this->default_translator
->id();
$job
->save();
$job_item = tmgmt_job_item_create('content', $parent_node
->getEntityTypeId(), $parent_node
->id(), array(
'tjid' => $job
->id(),
));
$job_item
->save();
$job
->requestTranslation();
$this
->drupalGet(URL::fromRoute('entity.tmgmt_job_item.canonical', [
'tmgmt_job_item' => $job_item
->id(),
]));
$this
->clickLink(t('Preview'));
$this
->assertText('de(de-ch): ' . $parent_node
->getTitle());
$this
->assertText('de(de-ch): ' . $parent_node->body->value);
$this
->assertText('de(de-ch): ' . $child_node
->getTitle());
$this
->assertText('de(de-ch): ' . $child_node->body->value);
}
function testEntitySourcePreview() {
$bundle = BlockContentType::create([
'id' => 'basic',
'label' => 'basic',
]);
$bundle
->save();
$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.'));
$custom_block = BlockContent::create([
'type' => 'basic',
'info' => 'Custom Block',
'langcode' => 'en',
]);
$custom_block
->save();
$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'));
$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'));
$node
->delete();
$this
->clickLink(t('reviewed'));
$review_url = $this
->getSession()
->getCurrentUrl();
$this
->clickLink(t('Preview'));
$this
->assertResponse(404);
$this
->drupalPostForm($review_url, [
'title|0|value[translation]' => 'test_translation',
], t('Save'));
$this
->assertText(t('The translation has been saved successfully.'));
$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();
$this
->drupalGet(URL::fromRoute('entity.tmgmt_job_item.canonical', [
'tmgmt_job_item' => $job
->id(),
])
->setAbsolute()
->toString());
$this
->assertNoLink(t('Preview'));
$job
->requestTranslation();
$this
->drupalGet(URL::fromRoute('tmgmt_content.job_item_preview', [
'tmgmt_job_item' => $job
->id(),
])
->setAbsolute()
->toString());
$this
->assertResponse(403);
$this
->drupalGet(URL::fromRoute('entity.tmgmt_job_item.canonical', [
'tmgmt_job_item' => $job
->id(),
])
->setAbsolute()
->toString());
$this
->clickLink(t('Preview'));
$this
->assertResponse(200);
$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(),
]));
$this
->drupalLogout();
$this
->drupalGet(URL::fromRoute('tmgmt_content.job_item_preview', [
'tmgmt_job_item' => $job
->id(),
])
->setAbsolute()
->toString());
$this
->assertResponse(403);
$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',
]);
$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.');
$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();
$this
->drupalGet('admin/tmgmt/items/' . $node
->id(), array(
'query' => array(
'destination' => 'admin/tmgmt/items/' . $node
->id(),
),
));
$this
->assertNoLink(t('Preview'));
}
public function testEntitySourceAnonymousAccess() {
$node = $this
->createTranslatableNode('page', 'en');
$job = $this
->createJob('en', 'de');
$job->translator = $this->default_translator
->id();
$job
->save();
$job_item = tmgmt_job_item_create('content', $node
->getEntityTypeId(), $node
->id(), array(
'tjid' => $job
->id(),
));
$job_item
->save();
$node
->setUnpublished();
$node
->save();
$this
->drupalLogout();
$url = $job_item
->getSourceUrl();
$this
->drupalGet($url);
$this
->assertResponse(200);
\Drupal::configFactory()
->getEditable('tmgmt.settings')
->set('anonymous_access', FALSE)
->save();
$this
->drupalGet($url);
$this
->assertResponse(403);
\Drupal::configFactory()
->getEditable('tmgmt.settings')
->set('anonymous_access', TRUE)
->save();
$this
->drupalGet($url);
$this
->assertResponse(200);
$job
->aborted();
$this
->drupalGet($url);
$this
->assertResponse(403);
}
public function testSourceOverview() {
$node = $this
->createTranslatableNode('article', 'en');
$this
->drupalGet('admin/tmgmt/sources');
$this
->assertText($node
->getTitle());
$this
->assertSession()
->elementNotExists('css', '#edit-add-to-continuous-jobs');
$this
->assertSession()
->elementNotExists('css', '#edit-add-all-to-continuous-jobs');
$this
->createTranslatableNode('article', 'en');
$this
->createTranslatableNode('article', 'en');
$continuous_settings = [
'content' => [
'node' => [
'enabled' => 1,
'bundles' => [
'article' => 1,
'page' => 0,
],
],
],
];
$continuous_job = $this
->createJob('en', 'de', 0, [
'label' => 'Continuous job',
'job_type' => 'continuous',
'continuous_settings' => $continuous_settings,
'translator' => $this->default_translator
->id(),
]);
$this
->drupalGet('admin/tmgmt/sources');
$this
->assertSession()
->elementExists('css', '#edit-add-to-continuous-jobs');
$this
->assertSession()
->elementExists('css', '#edit-add-all-to-continuous-jobs');
$edit = [
'items[' . $node
->id() . ']' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Check for continuous jobs'));
$this
->assertUniqueText(t("1 continuous job item has been created."));
$items = $continuous_job
->getItems();
$item = reset($items);
$this
->assertLinkByHref('admin/tmgmt/items/' . $item
->id());
$continuous_job_items = $continuous_job
->getItems();
$continuous_job_item = reset($continuous_job_items);
$this
->assertEqual($node
->label(), $continuous_job_item
->label(), 'Continuous job item is created for selected node.');
$second_node = $this
->createTranslatableNode('page', 'en');
$this
->drupalGet('admin/tmgmt/sources');
$this
->assertText($second_node
->getTitle());
$second_edit = [
'items[' . $second_node
->id() . ']' => TRUE,
];
$this
->drupalPostForm(NULL, $second_edit, t('Check for continuous jobs'));
$this
->assertUniqueText(t("None of the selected sources can be added to continuous jobs."));
$this
->assertEqual(count($continuous_job
->getItems()), 1, 'There are no new job items for selected node.');
$this
->drupalGet('admin/tmgmt/sources');
$add_all_edit = [
'add_all_to_continuous_jobs' => TRUE,
];
$this
->drupalPostForm(NULL, $add_all_edit, t('Check for continuous jobs'));
$this
->assertUniqueText(t("2 continuous job items have been created."));
$this
->assertEqual(count($continuous_job
->getItems()), 3, 'There are two new job items for selected nodes.');
$this
->drupalGet('admin/tmgmt/sources');
$add_all_edit = [
'add_all_to_continuous_jobs' => TRUE,
];
$this
->drupalPostForm(NULL, $add_all_edit, t('Check for continuous jobs'));
$this
->assertUniqueText(t("None of the selected sources can be added to continuous jobs."));
$this
->assertEqual(count($continuous_job
->getItems()), 3, 'There are no new job items for selected nodes.');
}
public function testSourceUpdate() {
$node = $this
->createTranslatableNode('article', 'en');
$job = $this
->createJob('en', 'de');
$job
->save();
$job_item = tmgmt_job_item_create('content', $node
->getEntityTypeId(), $node
->id(), array(
'tjid' => $job
->id(),
));
$job_item
->save();
$updated_body = 'New body';
$edit = [
'body[0][value]' => $updated_body,
];
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, 'Save');
$this
->drupalGet('admin/tmgmt/items/' . $job_item
->id());
$this
->assertText($updated_body, 'Source updated correctly.');
}
public function testConsiderFieldSequences() {
$this
->createNodeType('article1', 'Article 1', TRUE, FALSE);
for ($i = 0; $i <= 5; $i++) {
$field_storage = FieldStorageConfig::create(array(
'field_name' => 'field_' . $i,
'entity_type' => 'node',
'type' => 'text',
'cardinality' => mt_rand(1, 5),
'translatable' => TRUE,
));
$field_storage
->save();
$field = FieldConfig::create(array(
'field_name' => 'field_' . $i,
'entity_type' => 'node',
'bundle' => 'article1',
'label' => 'Field' . $i,
'description' => $this
->randomString(30),
'widget' => array(
'type' => 'text',
'label' => $this
->randomString(10),
),
));
$field
->save();
$this->field_names['node']['article1'][] = 'field_' . $i;
}
$node = $this
->createTranslatableNode('article1', 'en');
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'article1', 'default')
->setComponent('body', array(
'type' => 'text_textarea_with_summary',
'weight' => 0,
))
->setComponent('title', array(
'type' => 'string_textfield',
'weight' => 1,
))
->setComponent('field_1', array(
'type' => 'string_textfield',
'weight' => 2,
))
->setComponent('field_2', array(
'type' => 'string_textfield',
'weight' => 5,
))
->setComponent('field_0', array(
'type' => 'string_textfield',
'weight' => 6,
))
->setComponent('field_4', array(
'type' => 'string_textfield',
'weight' => 7,
))
->save();
$job = $this
->createJob('en', 'de');
$job->translator = $this->default_translator
->id();
$job
->addItem('content', $node
->getEntityTypeId(), $node
->id());
$job
->save();
$job
->requestTranslation();
$this
->drupalGet(URL::fromRoute('entity.tmgmt_job_item.canonical', [
'tmgmt_job_item' => $node
->id(),
]));
$review_elements = $this
->xpath('//*[@id="edit-review"]/div');
$ids = [];
foreach ($review_elements as $review_element) {
$ids[] = $review_element
->getAttribute('id');
}
$this
->assertEqual($ids[0], 'tmgmt-ui-element-body-wrapper');
$this
->assertEqual($ids[1], 'tmgmt-ui-element-title-wrapper');
$this
->assertEqual($ids[2], 'tmgmt-ui-element-field-1-wrapper');
$this
->assertEqual($ids[3], 'tmgmt-ui-element-field-2-wrapper');
$this
->assertEqual($ids[4], 'tmgmt-ui-element-field-0-wrapper');
$this
->assertEqual($ids[5], 'tmgmt-ui-element-field-4-wrapper');
$this
->assertEqual($ids[6], 'tmgmt-ui-element-field-3-wrapper');
$this
->assertEqual($ids[7], 'tmgmt-ui-element-field-5-wrapper');
}
}