<?php
namespace Drupal\Tests\tmgmt\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\tmgmt\Entity\Translator;
use Drupal\tmgmt\JobItemInterface;
trait TmgmtTestTrait {
protected $admin_permissions = array(
'administer languages',
'access administration pages',
'administer content types',
'administer tmgmt',
);
protected $admin_user = NULL;
protected $translator_permissions = array(
'create translation jobs',
'submit translation jobs',
'accept translation jobs',
);
protected $translator_user = NULL;
protected $languageWeight = 1;
function loginAsAdmin($additional_permissions = array(), $reset_permissions = FALSE) {
$permissions = $this->admin_permissions;
if ($reset_permissions) {
$permissions = $additional_permissions;
}
elseif (!empty($additional_permissions)) {
$permissions = array_merge($permissions, $additional_permissions);
}
$this->admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->admin_user);
return $this->admin_user;
}
function loginAsTranslator($additional_permissions = array(), $reset_permissions = FALSE) {
$permissions = $this->translator_permissions;
if ($reset_permissions) {
$permissions = $additional_permissions;
}
elseif (!empty($additional_permissions)) {
$permissions = array_merge($permissions, $additional_permissions);
}
$this->translator_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->translator_user);
return $this->translator_user;
}
function createTranslator(array $values = []) {
$translator = Translator::create($values + [
'name' => strtolower($this
->randomMachineName()),
'label' => $this
->randomMachineName(),
'plugin' => 'test_translator',
'remote_languages_mappings' => [],
'settings' => empty($values['plugin']) ? [
'key' => $this
->randomMachineName(),
'another_key' => $this
->randomMachineName(),
] : [],
]);
$this
->assertEquals(SAVED_NEW, $translator
->save());
return $translator;
}
function createJob($source = 'en', $target = 'de', $uid = 1, $values = array()) {
$job = tmgmt_job_create($source, $target, $uid, $values);
$this
->assertEquals(SAVED_NEW, $job
->save());
$this
->assertTrue($job
->id() > 0);
return $job;
}
function addLanguage($langcode) {
$language = ConfigurableLanguage::createFromLangcode($langcode);
$language
->save();
}
function assertJobItemLangCodes(JobItemInterface $job_item, $expected_source_lang, array $actual_lang_codes) {
$this
->assertEquals($expected_source_lang, $job_item
->getSourceLangCode());
$existing = $job_item
->getExistingLangCodes();
sort($existing);
sort($actual_lang_codes);
$this
->assertEquals($actual_lang_codes, $existing);
}
function clickLinkWithImageTitle($title) {
$urls = $this
->xpath('//a[img[@title=:title]]', [
':title' => 'Needs review',
]);
if (empty($urls)) {
$this
->fail('No image link with title' . $title . ' found');
return;
}
$url_target = $this
->getAbsoluteUrl($urls[0]
->getAttribute('href'));
$this
->drupalGet($url_target);
}
protected function assertTextByXpath($xpath, $text) {
$this
->assertEquals($text, (string) $this
->xpath($xpath)[0]
->getText());
}
}