View source
<?php
namespace Drupal\Tests\tmgmt_extension_suit\Functional;
use Drupal;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\SchemaObjectExistsException;
use Drupal\Core\Queue\DatabaseQueue;
use Drupal\Tests\tmgmt\Functional\TMGMTTestBase;
use Drupal\tmgmt\Entity\JobItem;
putenv('SYMFONY_DEPRECATIONS_HELPER=disabled');
abstract class TmgmtExtensionSuitTestBase extends TMGMTTestBase {
private $userForTranslations;
public static $modules = [
'tmgmt',
'tmgmt_demo',
'tmgmt_extension_suit_test',
'tmgmt_extension_suit',
];
public function setUp() {
parent::setUp();
$this->userForTranslations = $this
->loginAsAdmin([
'edit any translatable_node content',
]);
$uploadQueue = new DatabaseQueue('tmgmt_extension_suit_upload', Database::getConnection());
$database_schema = Drupal::database()
->schema();
try {
if (!$database_schema
->tableExists('queue')) {
$schema_definition = $uploadQueue
->schemaDefinition();
$database_schema
->createTable('queue', $schema_definition);
}
} catch (SchemaObjectExistsException $e) {
}
}
protected function isFieldChecked($id) {
$elements = $this
->xpath('//input[@id=:id]', [
':id' => $id,
]);
return isset($elements[0]) && !empty($elements[0]['checked']);
}
protected function requestTranslation(array $nids, $targetLanguage, $jobId, $translator = 'tes_local_test') {
$data = [];
foreach ($nids as $nid) {
$data["items[{$nid}]"] = "item[{$nid}]";
}
$this
->drupalPostForm('admin/tmgmt/sources', $data, t('Request translation'));
$data = [
'label[0][value]' => 'Test job',
'target_language' => $targetLanguage,
'translator' => $translator,
];
$this
->drupalPostForm("admin/tmgmt/jobs/{$jobId}", $data, t('Submit to provider'));
}
protected function requestBulkTranslation(array $nids, array $targetLanguages, $provider = 'tes_local_test', array $settings = []) {
$data = [];
foreach ($nids as $nid) {
$data["items[{$nid}]"] = "item[{$nid}]";
}
$this
->drupalPostForm('admin/tmgmt/sources', $data, t('Request translation in a batch'));
if (!empty($targetLanguages)) {
$new_data = [];
foreach ($targetLanguages as $targetLanguage) {
$new_data["target_language[{$targetLanguage}]"] = $targetLanguage;
}
$new_data['translator'] = $provider;
foreach ($settings as $setting => $value) {
$new_data[$setting] = $value;
}
$this
->drupalPostForm(NULL, $new_data, t('Request translation'));
}
}
protected function getUserForTranslations() {
return $this->userForTranslations;
}
protected function translateJobItems(array $jobItemIds) {
foreach ($jobItemIds as $jobItemId) {
$jobItem = JobItem::load($jobItemId);
$jobItem
->setState(JobItem::STATE_REVIEW);
$jobItem
->save();
}
}
protected function getNodeHash($nid, $jobId) {
$hashQuery = Drupal::database()
->select('tmgmt_job_item', 'tji');
$hashQuery
->join('tmgmt_job', 'tj', 'tj.tjid = tji.tjid');
$hash = $hashQuery
->condition('tj.tjid', $jobId)
->condition('tji.item_id', $nid)
->fields('tji', [
'tjid',
'tes_source_content_hash',
])
->execute()
->fetchAllKeyed();
return $hash ? reset($hash) : FALSE;
}
protected function getCountOfItemsInQueue($queueName) {
return Drupal::database()
->select('queue', 'q')
->condition('q.name', $queueName)
->countQuery()
->execute()
->fetchField();
}
protected function isItemAddedToQueue($queue, $jobId) {
return Drupal::database()
->select('queue', 'q')
->condition('q.name', $queue)
->condition('q.data', "a:1:{s:2:\"id\";i:{$jobId};}")
->countQuery()
->execute()
->fetchField();
}
}