Commit 0aabb613 authored by Loparev's avatar Loparev
Browse files

Request 100 translated only for needed locales + handle locales mapping changes

parent 27d3ffbf
......@@ -11,6 +11,7 @@ use DateTime;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\tmgmt\Entity\Translator;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt_smartling\Smartling\Submission\SearchTranslationRequestParamsV3;
use Exception;
use Psr\Log\LoggerInterface;
use Smartling\AuditLog\Params\CreateRecordParameters;
......@@ -427,11 +428,11 @@ class SmartlingApiWrapper {
* Returns translation request (without submission) by search params.
*
* @param $bucketName
* @param \Smartling\TranslationRequests\Params\SearchTranslationRequestParams $params
*
* @param SearchTranslationRequestParamsV3 $params
* @return mixed
*/
public function searchOnlyTranslationRequest($bucketName, SearchTranslationRequestParams $params) {
public function searchOnlyTranslationRequest($bucketName, SearchTranslationRequestParamsV3 $params) {
$result = [];
try {
......
<?php
namespace Drupal\tmgmt_smartling\Smartling\Submission;
use Smartling\TranslationRequests\Params\SearchTranslationRequestParams;
/**
* Class SearchTranslationRequestParamsV3
* @package Smartling\TranslationRequests
*/
class SearchTranslationRequestParamsV3 extends SearchTranslationRequestParams
{
/**
* @param array $targetLocaleIds
* @return $this
*/
public function setTargetLocaleIds(array $targetLocaleIds)
{
$this->set('targetLocaleId', $targetLocaleIds);
return $this;
}
}
......@@ -88,7 +88,8 @@ class TranslationRequestManager {
$result = FALSE;
}
else {
$result = in_array($translation_request['translationSubmissions'][0]['state'], [
$submission = $this->getSubmissionFromRequestByTargetLocaleId($translation_request, $job->getRemoteTargetLanguage());
$result = in_array($submission['state'], [
TranslationSubmissionStates::STATE_TRANSLATED,
TranslationSubmissionStates::STATE_COMPLETED
]);
......@@ -188,6 +189,8 @@ class TranslationRequestManager {
* @return bool
*/
protected function updateExistingTranslationRequest(JobInterface $job, array $translation_request) {
$submission = $this->getSubmissionFromRequestByTargetLocaleId($translation_request, $job->getRemoteTargetLanguage());
$update_submission_params = new UpdateTranslationSubmissionParams();
$update_submission_params
->setSubmitterName($this->getSubmitterName($job))
......@@ -196,7 +199,7 @@ class TranslationRequestManager {
'batch_execute_on_job' => $job->getSetting('batch_execute_on_job')
])
->setState(TranslationSubmissionStates::STATE_NEW)
->setTranslationSubmissionUid($translation_request['translationSubmissions'][0]['translationSubmissionUid']);
->setTranslationSubmissionUid($submission['translationSubmissionUid']);
$update_request_params = new UpdateTranslationRequestParams();
$update_request_params
......@@ -219,10 +222,12 @@ class TranslationRequestManager {
return FALSE;
}
$submission = $this->getSubmissionFromRequestByTargetLocaleId($translation_request, $job->getRemoteTargetLanguage());
$update_submission_params = new UpdateTranslationSubmissionParams();
$update_submission_params
->setSubmittedDate(new \DateTime('now', new \DateTimeZone('UTC')))
->setTranslationSubmissionUid($translation_request['translationSubmissions'][0]['translationSubmissionUid']);
->setTranslationSubmissionUid($submission['translationSubmissionUid']);
$update_request_params = new UpdateTranslationRequestParams();
$update_request_params
......@@ -249,11 +254,13 @@ class TranslationRequestManager {
return FALSE;
}
$submission = $this->getSubmissionFromRequestByTargetLocaleId($translation_request, $job->getRemoteTargetLanguage());
$update_submission_params = new UpdateTranslationSubmissionParams();
$update_submission_params
->setState(TranslationSubmissionStates::STATE_FAILED)
->setLastErrorMessage(mb_substr($e->getMessage() . ': ' . $e->getTraceAsString(), 0, 1024))
->setTranslationSubmissionUid($translation_request['translationSubmissions'][0]['translationSubmissionUid']);
->setTranslationSubmissionUid($submission['translationSubmissionUid']);
$update_request_params = new UpdateTranslationRequestParams();
$update_request_params
......@@ -278,12 +285,14 @@ class TranslationRequestManager {
return FALSE;
}
$submission = $this->getSubmissionFromRequestByTargetLocaleId($translation_request, $job->getRemoteTargetLanguage());
$update_submission_params = new UpdateTranslationSubmissionParams();
$update_submission_params
->setLastExportedDate(new \DateTime('now', new \DateTimeZone('UTC')))
->setTranslationSubmissionUid($translation_request['translationSubmissions'][0]['translationSubmissionUid']);
->setTranslationSubmissionUid($submission['translationSubmissionUid']);
if ($translation_request['translationSubmissions'][0]['state'] === TranslationSubmissionStates::STATE_TRANSLATED) {
if ($submission['state'] === TranslationSubmissionStates::STATE_TRANSLATED) {
$update_submission_params
->setState(TranslationSubmissionStates::STATE_COMPLETED);
}
......@@ -316,4 +325,40 @@ class TranslationRequestManager {
return $is_translation_submission_is_missed;
}
/**
* @param $translation_request
* @param $target_locale_id
*
* @return array
*/
protected function getSubmissionFromRequestByTargetLocaleId($translation_request, $target_locale_id) {
if (empty($translation_request['translationSubmissions'])) {
return [
'translationSubmissionUid' => null,
'state' => null,
'targetLocaleId' => null
];
}
foreach ($translation_request['translationSubmissions'] as $translation_submission) {
if (empty($translation_submission['targetLocaleId'])) {
return [
'translationSubmissionUid' => null,
'state' => null,
'targetLocaleId' => null
];
}
if ($translation_submission['targetLocaleId'] === $target_locale_id) {
return $translation_submission;
}
}
return [
'translationSubmissionUid' => null,
'state' => null,
'targetLocaleId' => null
];
}
}
......@@ -124,7 +124,7 @@ abstract class SmartlingTestBase extends TMGMTKernelTestBase {
* @return \Drupal\tmgmt\JobInterface
*/
protected function createJobWithItems(array $settings, $translator = 'smartling') {
$job = parent::createJob();
$job = parent::createJob('en', 'de');
for ($i = 1; $i < 3; $i++) {
$job->addItem('test_source', 'test', $i);
......
......@@ -161,7 +161,8 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test',
'translationSubmissions' => [
[
'translationSubmissionUid' => 'test'
'translationSubmissionUid' => 'test',
'targetLocaleId' => 'de'
]
]
]);
......@@ -310,7 +311,8 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test_translation_request_uid',
'translationSubmissions' => [
[
'translationSubmissionUid' => 'test_translation_submission_uid'
'translationSubmissionUid' => 'test_translation_submission_uid',
'targetLocaleId' => 'de'
]
]
];
......@@ -358,7 +360,8 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test_translation_request_uid',
'translationSubmissions' => [
[
'translationSubmissionUid' => 'test_translation_submission_uid'
'translationSubmissionUid' => 'test_translation_submission_uid',
'targetLocaleId' => 'de'
]
]
];
......@@ -427,7 +430,8 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test_translation_request_uid',
'translationSubmissions' => [
[
'translationSubmissionUid' => 'test_translation_submission_uid'
'translationSubmissionUid' => 'test_translation_submission_uid',
'targetLocaleId' => 'de'
]
]
];
......@@ -499,6 +503,7 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
[
'translationSubmissionUid' => 'test_translation_submission_uid',
'state' => TranslationSubmissionStates::STATE_TRANSLATED,
'targetLocaleId' => 'de'
]
]
];
......@@ -542,6 +547,7 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
[
'translationSubmissionUid' => 'test_translation_submission_uid',
'state' => TranslationSubmissionStates::STATE_IN_PROGRESS,
'targetLocaleId' => 'de'
]
]
];
......@@ -625,7 +631,8 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test',
'translationSubmissions' => [
[
'state' => TranslationSubmissionStates::STATE_TRANSLATED
'state' => TranslationSubmissionStates::STATE_TRANSLATED,
'targetLocaleId' => 'de'
]
]
]);
......@@ -682,7 +689,8 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test',
'translationSubmissions' => [
[
'state' => TranslationSubmissionStates::STATE_NEW
'state' => TranslationSubmissionStates::STATE_NEW,
'targetLocaleId' => 'de'
]
]
]);
......@@ -714,7 +722,8 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test',
'translationSubmissions' => [
[
'state' => TranslationSubmissionStates::STATE_IN_PROGRESS
'state' => TranslationSubmissionStates::STATE_IN_PROGRESS,
'targetLocaleId' => 'de'
]
]
]);
......@@ -746,7 +755,8 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test',
'translationSubmissions' => [
[
'state' => TranslationSubmissionStates::STATE_COMPLETED
'state' => TranslationSubmissionStates::STATE_COMPLETED,
'targetLocaleId' => 'de'
]
]
]);
......@@ -778,7 +788,8 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test',
'translationSubmissions' => [
[
'state' => TranslationSubmissionStates::STATE_DELETED
'state' => TranslationSubmissionStates::STATE_DELETED,
'targetLocaleId' => 'de'
]
]
]);
......@@ -810,13 +821,89 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
'translationRequestUid' => 'test',
'translationSubmissions' => [
[
'state' => TranslationSubmissionStates::STATE_FAILED
'state' => TranslationSubmissionStates::STATE_FAILED,
'targetLocaleId' => 'de'
]
]
]);
$this->assertEquals($translation_request_manager_mock->isTranslationRequestReadyForDownload($this->job), FALSE);
}
/**
* Get submission from request by target locale: no submissions in request.
*/
public function testGetSubmissionFromRequestByTargetLocaleIdNoSubmissions() {
$translation_request_manager_mock = $this
->getMockBuilder(TranslationRequestManagerTested::class)
->setConstructorArgs([$this->apiWrapperMock, $this->stateMock, $this->loggerMock])
->setMethods(['initApiWrapper'])
->getMock();
$this->assertEquals($translation_request_manager_mock->getSubmissionFromRequestByTargetLocaleId([], 'de'), [
'translationSubmissionUid' => null,
'state' => null,
'targetLocaleId' => null
]);
}
/**
* Get submission from request by target locale: invalid submission (no target locale id).
*/
public function testGetSubmissionFromRequestByTargetLocaleIdInvalidSubmission() {
$translation_request_manager_mock = $this
->getMockBuilder(TranslationRequestManagerTested::class)
->setConstructorArgs([$this->apiWrapperMock, $this->stateMock, $this->loggerMock])
->setMethods(['initApiWrapper'])
->getMock();
$this->assertEquals($translation_request_manager_mock->getSubmissionFromRequestByTargetLocaleId(['translationSubmissions' => [[
'translationSubmissionUid' => 'test'
]]], 'de'), [
'translationSubmissionUid' => null,
'state' => null,
'targetLocaleId' => null
]);
}
/**
* Get submission from request by target locale: submission not found.
*/
public function testGetSubmissionFromRequestByTargetLocaleIdNotFound() {
$translation_request_manager_mock = $this
->getMockBuilder(TranslationRequestManagerTested::class)
->setConstructorArgs([$this->apiWrapperMock, $this->stateMock, $this->loggerMock])
->setMethods(['initApiWrapper'])
->getMock();
$this->assertEquals($translation_request_manager_mock->getSubmissionFromRequestByTargetLocaleId(['translationSubmissions' => [[
'translationSubmissionUid' => 'test',
'targetLocaleId' => 'fr-FR'
]]], 'de'), [
'translationSubmissionUid' => null,
'state' => null,
'targetLocaleId' => null
]);
}
/**
* Get submission from request by target locale: success.
*/
public function testGetSubmissionFromRequestByTargetLocaleIdSuccess() {
$translation_request_manager_mock = $this
->getMockBuilder(TranslationRequestManagerTested::class)
->setConstructorArgs([$this->apiWrapperMock, $this->stateMock, $this->loggerMock])
->setMethods(['initApiWrapper'])
->getMock();
$this->assertEquals($translation_request_manager_mock->getSubmissionFromRequestByTargetLocaleId(['translationSubmissions' => [[
'translationSubmissionUid' => 'test',
'targetLocaleId' => 'de'
]]], 'de'), [
'translationSubmissionUid' => 'test',
'targetLocaleId' => 'de'
]);
}
}
/**
......@@ -833,4 +920,8 @@ class TranslationRequestManagerTested extends TranslationRequestManager {
public function updateExistingTranslationRequest(JobInterface $job, array $translation_request) {
return parent::updateExistingTranslationRequest($job, $translation_request);
}
public function getSubmissionFromRequestByTargetLocaleId($translation_request, $target_locale_id) {
return parent::getSubmissionFromRequestByTargetLocaleId($translation_request, $target_locale_id);
}
}
......@@ -18,10 +18,10 @@ use Drupal\tmgmt\Entity\Translator;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\JobItemInterface;
use Drupal\tmgmt_extension_suit\ExtendedTranslatorPluginInterface;
use Drupal\tmgmt_smartling\Smartling\Submission\SearchTranslationRequestParamsV3;
use Drupal\views\ViewExecutable;
use Smartling\AuditLog\Params\CreateRecordParameters;
use Smartling\File\Params\DownloadFileParameters;
use Smartling\TranslationRequests\Params\SearchTranslationRequestParams;
use Smartling\TranslationRequests\Params\TranslationSubmissionStates;
use Smartling\TranslationRequests\Params\UpdateTranslationRequestParams;
use Smartling\TranslationRequests\Params\UpdateTranslationSubmissionParams;
......@@ -50,9 +50,16 @@ function tmgmt_smartling_cron() {
$settings = $smartling_provider_config->get("settings");
$api_wrapper->setSettings($settings);
$search_params = new SearchTranslationRequestParams();
$search_params = new SearchTranslationRequestParamsV3();
$search_params->setState(TranslationSubmissionStates::STATE_TRANSLATED);
$search_params->setLimit(100);
$search_params->setTargetLocaleIds(
array_values(
array_filter(
array_values($smartling_provider_config->get('remote_languages_mappings'))
)
)
);
$bucket_name = Drupal::state()->get('tmgmt_smartling.bucket_name', 'tmgmt_smartling_default_bucket_name');
$result = $api_wrapper->searchOnlyTranslationRequest($bucket_name, $search_params);
......@@ -78,15 +85,18 @@ function tmgmt_smartling_cron() {
continue;
}
$update_submission_params = new UpdateTranslationSubmissionParams();
$update_submission_params
->setState(TranslationSubmissionStates::STATE_FAILED)
->setLastErrorMessage("TMGMT Job $tmgmt_job_id doesn't exist in Drupal and will not be scheduled for download")
->setTranslationSubmissionUid($translation_request_with_submission['translationSubmissions'][0]['translationSubmissionUid']);
$update_request_params = new UpdateTranslationRequestParams();
$update_request_params
->addTranslationSubmission($update_submission_params);
foreach ($translation_request_with_submission['translationSubmissions'] as $submission) {
$update_submission_params = new UpdateTranslationSubmissionParams();
$update_submission_params
->setState(TranslationSubmissionStates::STATE_FAILED)
->setLastErrorMessage("TMGMT Job $tmgmt_job_id doesn't exist in Drupal and will not be scheduled for download")
->setTranslationSubmissionUid($submission['translationSubmissionUid']);
$update_request_params
->addTranslationSubmission($update_submission_params);
}
$api_wrapper->updateTranslationRequest(
$bucket_name,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment