<?php
namespace Drupal\Tests\tmgmt_content\Functional;
use Drupal\comment\Entity\Comment;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\tmgmt\Functional\TMGMTTestBase;
use Drupal\tmgmt\Entity\JobItem;
use Drupal\Tests\tmgmt\Functional\TmgmtEntityTestTrait;
use Drupal\Core\Language\LanguageInterface;
class ContentTmgmtEntitySourceListTest extends TMGMTTestBase {
use TmgmtEntityTestTrait;
protected static $modules = array(
'tmgmt_content',
'taxonomy',
'comment',
);
protected $nodes = array();
function setUp() : void {
parent::setUp();
$this
->loginAsAdmin();
$this
->addLanguage('de');
$this
->addLanguage('fr');
$this
->addLanguage('it');
$this
->createNodeType('article', 'Article', TRUE);
$this
->createNodeType('page', 'Page', TRUE);
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('node', 'article', TRUE);
$content_translation_manager
->setEnabled('node', 'page', FALSE);
$this->nodes['page']['en'][] = $this
->createTranslatableNode('page');
$this->nodes['article']['de'][0] = $this
->createTranslatableNode('article', 'de');
$this->nodes['article']['fr'][0] = $this
->createTranslatableNode('article', 'fr');
$this->nodes['article']['en'][3] = $this
->createTranslatableNode('article', 'en');
$this->nodes['article']['en'][2] = $this
->createTranslatableNode('article', 'en');
$this->nodes['article']['en'][1] = $this
->createTranslatableNode('article', 'en');
$this->nodes['article']['en'][0] = $this
->createTranslatableNode('article', 'en');
$this->nodes['article'][LanguageInterface::LANGCODE_NOT_SPECIFIED][0] = $this
->createTranslatableNode('article', LanguageInterface::LANGCODE_NOT_SPECIFIED);
$this->nodes['article'][LanguageInterface::LANGCODE_NOT_APPLICABLE][0] = $this
->createTranslatableNode('article', LanguageInterface::LANGCODE_NOT_APPLICABLE);
}
function testTermBundleFilter() {
$vocabulary1 = Vocabulary::create([
'vid' => 'vocab1',
'name' => $this
->randomMachineName(),
]);
$vocabulary1
->save();
$term1 = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary1
->id(),
]);
$term1
->save();
$vocabulary2 = Vocabulary::create([
'vid' => 'vocab2',
'name' => $this
->randomMachineName(),
]);
$vocabulary2
->save();
$term2 = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary2
->id(),
]);
$term2
->save();
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('taxonomy_term', $vocabulary1
->id(), TRUE);
$content_translation_manager
->setEnabled('taxonomy_term', $vocabulary2
->id(), TRUE);
$this
->drupalGet('admin/tmgmt/sources/content/taxonomy_term');
$this
->assertSession()
->pageTextContains($term1
->label());
$this
->assertSession()
->pageTextContains($term2
->label());
$this
->assertNotEmpty($this
->xpath('//td[text()=@vocabulary]', array(
'@vocabulary' => $vocabulary1
->label(),
)));
$this
->assertNotEmpty($this
->xpath('//td[text()=@vocabulary]', array(
'@vocabulary' => $vocabulary2
->label(),
)));
$edit = array();
$edit['search[vid]'] = $vocabulary1
->id();
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains($term1
->label());
$this
->assertSession()
->pageTextNotContains($term2
->label());
$this
->assertNotEmpty($this
->xpath('//td[text()=@vocabulary]', array(
'@vocabulary' => $vocabulary1
->label(),
)));
$this
->assertEmpty($this
->xpath('//td[text()=@vocabulary]', array(
'@vocabulary' => $vocabulary2
->label(),
)));
}
function testAvailabilityOfEntityLists() {
$this
->drupalGet('admin/tmgmt/sources/content/comment');
$this
->assertSession()
->pageTextContains('Comment overview (Content Entity)');
$this
->assertSession()
->pageTextContains('No source items matching given criteria have been found.');
$this
->drupalGet('admin/tmgmt/sources/content/node');
$this
->assertSession()
->pageTextContains('Content overview (Content Entity)');
$this
->assertSession()
->pageTextContains($this->nodes['article']['en'][0]
->label());
$this
->assertSession()
->pageTextNotContains($this->nodes['page']['en'][0]
->label());
$this
->assertSession()
->pageTextNotContains($this->nodes['article'][LanguageInterface::LANGCODE_NOT_SPECIFIED][0]
->label());
$this
->assertSession()
->pageTextNotContains($this->nodes['article'][LanguageInterface::LANGCODE_NOT_APPLICABLE][0]
->label());
}
function testTranslationStatuses() {
$this
->drupalGet('admin/tmgmt/sources/content/node');
$langstatus_en = $this
->xpath('//table[@id="edit-items"]/tbody/tr[1]/td[@class="langstatus-en"]/a/img');
$langstatus_de = $this
->xpath('//table[@id="edit-items"]/tbody/tr[1]/td[@class="langstatus-de"]/img');
$this
->assertEquals('Original language', $langstatus_en[0]
->getAttribute('title'));
$this
->assertEquals('Not translated', $langstatus_de[0]
->getAttribute('title'));
$job = $this
->createJob('en', 'de');
$job->translator = $this->default_translator
->id();
$job->settings = array();
$job
->save();
$job
->addItem('content', 'node', $this->nodes['article']['en'][0]
->id());
$job
->requestTranslation();
$this
->drupalGet('admin/tmgmt/sources/content/node');
$langstatus_de = $this
->xpath('//table[@id="edit-items"]/tbody/tr[1]/td[@class="langstatus-de"]/a/img');
$items = $job
->getItems();
$states = JobItem::getStates();
$label = t('Active job item: @state', array(
'@state' => $states[reset($items)
->getState()],
));
$this
->assertEquals($label, (string) $langstatus_de[0]
->getAttribute('title'));
foreach ($job
->getItems() as $job_item) {
$job_item
->acceptTranslation();
}
$this
->drupalGet('admin/tmgmt/sources/content/node');
$langstatus_de = $this
->xpath('//table[@id="edit-items"]/tbody/tr[1]/td[@class="langstatus-de"]/a/img');
$this
->assertEquals('Translation up to date', $langstatus_de[0]
->getAttribute('title'));
$job = $this
->createJob('en', 'de');
$job->translator = $this->default_translator
->id();
$job->settings = array();
$job
->save();
$job
->addItem('content', 'node', $this->nodes['article']['en'][0]
->id());
$this
->drupalGet('admin/tmgmt/sources/content/node');
$langstatus_de = $this
->xpath('//table[@id="edit-items"]/tbody/tr[1]/td[@class="langstatus-de"]/a/img');
$items = $job
->getItems();
$states = JobItem::getStates();
$label = t('Active job item: @state', array(
'@state' => $states[reset($items)
->getState()],
));
$this
->assertEquals($label, (string) $langstatus_de[1]
->getAttribute('title'));
}
function testTranslationSubmissions() {
$this
->drupalGet('admin/tmgmt/sources/content/node');
$nid = $this->nodes['article']['en'][0]
->id();
$edit = array();
$edit["items[{$nid}]"] = 1;
$this
->submitForm($edit, 'Request translation');
$this
->assertSession()
->pageTextContains('One job needs to be checked out.');
$this
->drupalGet('admin/tmgmt/sources/content/node');
$nid1 = $this->nodes['article']['en'][0]
->id();
$nid2 = $this->nodes['article']['en'][1]
->id();
$edit = array();
$edit["items[{$nid1}]"] = 1;
$edit["items[{$nid2}]"] = 1;
$this
->submitForm($edit, 'Request translation');
$this
->assertSession()
->pageTextContains('One job needs to be checked out.');
$this
->drupalGet('admin/tmgmt/sources/content/node');
$nid1 = $this->nodes['article']['en'][0]
->id();
$nid2 = $this->nodes['article']['en'][1]
->id();
$nid3 = $this->nodes['article']['en'][2]
->id();
$nid4 = $this->nodes['article']['en'][3]
->id();
$nid5 = $this->nodes['article']['de'][0]
->id();
$nid6 = $this->nodes['article']['fr'][0]
->id();
$edit = array();
$edit["items[{$nid1}]"] = 1;
$edit["items[{$nid2}]"] = 1;
$edit["items[{$nid3}]"] = 1;
$edit["items[{$nid4}]"] = 1;
$edit["items[{$nid5}]"] = 1;
$edit["items[{$nid6}]"] = 1;
$edit['target_language'] = 'it';
$this
->submitForm($edit, 'Request translation');
$this
->assertSession()
->pageTextContains(t('@count jobs need to be checked out.', array(
'@count' => '3',
)));
$this
->drupalGet('admin/tmgmt/sources/content/node');
$edit = array();
$edit["items[{$nid1}]"] = 1;
$edit["items[{$nid2}]"] = 1;
$edit["items[{$nid3}]"] = 1;
$edit["items[{$nid4}]"] = 1;
$edit["items[{$nid5}]"] = 1;
$edit["items[{$nid6}]"] = 1;
$edit['target_language'] = '_multiple';
$edit['target_languages[de]'] = TRUE;
$edit['target_languages[fr]'] = TRUE;
$this
->submitForm($edit, 'Request translation');
$this
->assertSession()
->pageTextContains(t('@count jobs need to be checked out.', array(
'@count' => 4,
)));
$this
->drupalGet('admin/tmgmt/sources/content/node');
$edit = array();
$edit["items[{$nid1}]"] = 1;
$edit["items[{$nid2}]"] = 1;
$edit["items[{$nid3}]"] = 1;
$edit["items[{$nid4}]"] = 1;
$edit["items[{$nid5}]"] = 1;
$edit["items[{$nid6}]"] = 1;
$edit['target_language'] = '_all';
$this
->submitForm($edit, 'Request translation');
$this
->assertSession()
->pageTextContains(t('@count jobs need to be checked out.', array(
'@count' => 9,
)));
$this
->drupalGet('admin/tmgmt/sources/content/node');
$edit = array();
$edit["items[{$nid1}]"] = 1;
$edit["items[{$nid2}]"] = 1;
$edit["items[{$nid3}]"] = 1;
$edit["items[{$nid4}]"] = 1;
$edit["items[{$nid5}]"] = 1;
$edit["items[{$nid6}]"] = 1;
$edit['source_language'] = 'fr';
$edit['target_language'] = '_all';
$this
->submitForm($edit, 'Request translation');
$this
->assertSession()
->pageTextContains(t('@count jobs need to be checked out.', array(
'@count' => 3,
)));
}
function testNodeEntityListings() {
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('node', 'article', FALSE);
$content_translation_manager
->setEnabled('node', 'page', FALSE);
$this
->drupalGet('admin/tmgmt/sources/content/node');
$this
->assertSession()
->pageTextContains('Entity translation is not enabled for any of existing content types. To use this functionality go to Content types administration and enable entity translation for desired content types.');
$content_translation_manager
->setEnabled('node', 'article', TRUE);
$content_translation_manager
->setEnabled('node', 'page', TRUE);
$page_node_translatable = $this
->createTranslatableNode('page');
$this
->drupalGet('admin/tmgmt/sources/content/node');
$this
->assertSession()
->pageTextContains($this->nodes['article']['en'][0]
->label());
$this
->assertSession()
->pageTextContains($page_node_translatable
->label());
$edit = array();
$edit['search[type]'] = 'article';
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains($this->nodes['article']['en'][0]
->label());
$this
->assertSession()
->pageTextNotContains($page_node_translatable
->label());
$this
->drupalGet('admin/tmgmt/sources/content/node');
$this
->submitForm($edit, 'Cancel');
$this
->assertSession()
->pageTextContains($this->nodes['article']['en'][0]
->label());
$this
->assertSession()
->pageTextContains($page_node_translatable
->label());
$this
->config('tmgmt.settings')
->set('source_list_limit', 8)
->save();
$translation = $this->nodes['article']['de'][0]
->addTranslation('en', $this->nodes['article']['de'][0]
->toArray());
$translation
->setNewRevision(TRUE);
$translation
->save();
$this
->drupalGet('admin/tmgmt/sources/content/node');
$this
->assertSession()
->linkNotExists('Next');
$this
->config('tmgmt.settings')
->set('source_list_limit', 4)
->save();
$this
->drupalGet('admin/tmgmt/sources/content/node');
$this
->assertSession()
->linkExists('Next');
$this
->assertSession()
->linkExists('Go to page 2');
$this
->assertSession()
->linkNotExists('Go to page 3');
}
function testEntitySourceListSearch() {
$title_part_1 = $this
->randomMachineName(4);
$title_part_2 = $this
->randomMachineName(4);
$title_part_3 = $this
->randomMachineName(4);
$this->nodes['article']['en'][0]->title = "{$title_part_1} {$title_part_2} {$title_part_3}";
$this->nodes['article']['en'][0]
->save();
$this
->drupalGet('admin/tmgmt/sources/content/node');
$edit = array();
$edit['search[title]'] = "{$title_part_1} {$title_part_3}";
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextNotContains("{$title_part_1} {$title_part_2} {$title_part_3}");
$edit['search[title]'] = "{$title_part_1} {$title_part_2}";
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains("{$title_part_1} {$title_part_2} {$title_part_3}");
$search_result_rows = $this
->xpath('//table[@id="edit-items"]/tbody/tr');
$this
->assertCount(1, $search_result_rows, 'The search result must return only one row.');
$this
->addDefaultCommentField('node', 'article');
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('comment', 'comment', TRUE);
$values = array(
'entity_type' => 'node',
'entity_id' => $this->nodes['article']['en'][0]
->id(),
'field_name' => 'comment',
'comment_type' => 'comment',
'comment_body' => $this
->randomMachineName(),
'subject' => $this
->randomMachineName(),
);
$comment = Comment::create($values);
$comment
->save();
$this
->drupalGet('admin/tmgmt/sources/content/comment');
$edit = array();
$edit['search[subject]'] = $comment
->getSubject();
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains($comment
->getSubject());
$this
->drupalGet('/admin/tmgmt/sources/content/node');
$this
->submitForm([
'search[title]' => $this->nodes['article']['en'][0]
->label(),
], 'Search');
$this
->assertSession()
->pageTextContains('Content overview');
$this
->assertSession()
->pageTextContains($this->nodes['article']['en'][0]
->label());
$this
->submitForm([
'search[title]' => 'wrong_value',
], 'Search');
$this
->assertSession()
->pageTextContains('Content overview');
$this
->assertSession()
->pageTextNotContains($this->nodes['article']['en'][0]
->label());
$options = [
'query' => [
'any_key' => 'any_value',
],
];
$this
->drupalGet('/admin/tmgmt/sources/content/node', $options);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($this->nodes['article']['en'][0]
->label());
$this
->drupalGet('/admin/tmgmt/sources/content/node');
$edit = [
'search[target_language]' => 'de',
'search[title]' => $this->nodes['article']['en'][0]
->label(),
];
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->linkExists($this->nodes['article']['en'][0]
->label());
}
}