Commit 204aaa0e authored by Loparev's avatar Loparev
Browse files

Added tests\

parent 651a9eb9
......@@ -695,9 +695,7 @@ class SmartlingTranslator extends TranslatorPluginBase implements
}
/**
* @param JobInterface $job
* @param JobItemInterface|NULL $jobItem
* @return bool
* @inheritdoc
*/
public function downloadTranslation(JobInterface $job, JobItemInterface $jobItem = NULL) {
return tmgmt_smartling_download_file($job, $jobItem);
......
......@@ -88,7 +88,10 @@ class TranslationRequestManager {
$result = FALSE;
}
else {
$result = $translation_request['translationSubmissions'][0]['state'] == TranslationSubmissionStates::STATE_TRANSLATED;
$result = in_array($translation_request['translationSubmissions'][0]['state'], [
TranslationSubmissionStates::STATE_TRANSLATED,
TranslationSubmissionStates::STATE_COMPLETED
]);
}
if (empty($result)) {
......
......@@ -2,6 +2,10 @@
namespace Drupal\Tests\tmgmt_smartling\Kernel;
use Drupal\Core\Field\FieldItemList;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Entity\JobItem;
use Drupal\tmgmt\JobItemInterface;
use Smartling\AuditLog\Params\CreateRecordParameters;
/**
......@@ -423,4 +427,394 @@ class DownloadFlowTest extends SmartlingTestBase {
tmgmt_smartling_download_file($translate_job);
}
/**
* Download success full flow: download by TMGMT Job (all TMGMT Job Items).
*/
public function testDownloadByTmgmtJob() {
$real_job = $this->createJobWithItems([
'batch_uid' => 'uid',
'batch_execute_on_job' => 1,
]);
$field_item_list = $this->getMockBuilder(FieldItemList::class)
->disableOriginalConstructor()
->setMethods([
'getValue'
])
->getMock();
$field_item_list->expects($this->any())
->method('getValue')
->willReturn([
0 => [
'value' => NULL
]
]);
$job_item_mock_1 = $this->getMockBuilder(JobItem::class)
->disableOriginalConstructor()
->setMethods([
'save',
'id',
'setState',
'addTranslatedData',
'getItemId',
'getItemType'
])
->getMock();
$job_item_mock_1->expects($this->any())
->method('id')
->willReturn(1);
$job_item_mock_1->expects($this->any())
->method('getItemId')
->willReturn(1);
$job_item_mock_1->expects($this->any())
->method('getItemType')
->willReturn('node');
$job_item_mock_1->expects($this->once())
->method('setState')
->with(JobItemInterface::STATE_ACTIVE);
$job_item_mock_1->expects($this->once())
->method('addTranslatedData');
$job_item_mock_2 = $this->getMockBuilder(JobItem::class)
->disableOriginalConstructor()
->setMethods([
'id',
'setState',
'addTranslatedData',
'getItemId',
'getItemType'
])
->getMock();
$job_item_mock_2->expects($this->any())
->method('id')
->willReturn(2);
$job_item_mock_1->expects($this->any())
->method('getItemId')
->willReturn(1);
$job_item_mock_1->expects($this->any())
->method('getItemType')
->willReturn('node');
$job_item_mock_2->expects($this->once())
->method('setState')
->with(JobItemInterface::STATE_ACTIVE);
$job_item_mock_2->expects($this->once())
->method('addTranslatedData');
$translate_job = $this->getMockBuilder(Job::class)
->disableOriginalConstructor()
->setMethods([
'id',
'getItems',
'getTranslator',
'getTranslatorPlugin',
'getSetting',
'getSourceLangcode',
'getTargetLangcode',
'save',
'set',
'get',
'addMessage',
'getFieldDefinitions'
])
->getMock();
$translate_job->expects($this->any())
->method('id')
->willReturn(1);
$translate_job->expects($this->any())
->method('getSetting')
->willReturn('public');
$translate_job->expects($this->any())
->method('getTranslator')
->willReturn($real_job->getTranslator());
$translate_job->expects($this->any())
->method('getTranslatorPlugin')
->willReturn($real_job->getTranslatorPlugin());
$translate_job->expects($this->any())
->method('getSourceLangcode')
->willReturn('en');
$translate_job->expects($this->any())
->method('getTargetLangcode')
->willReturn('de');
$translate_job->expects($this->any())
->method('get')
->willReturn($field_item_list);
$translate_job->expects($this->any())
->method('getFieldDefinitions')
->willReturn([]);
$translate_job->expects($this->once())
->method('getItems')
->willReturn([
1 => $job_item_mock_1,
2 => $job_item_mock_2
]);
$this->translationRequestManagerMock->expects($this->never())
->method('commitError');
$this->translationRequestManagerMock->expects($this->once())
->method('getTranslationRequest')
->with($translate_job)
->willReturn(['translationRequestUid' => 'test']);
$this->apiWrapperMock->expects($this->once())
->method('getApi')
->with('file')
->willReturn($this->fileApiMock);
$this->fileApiMock->expects($this->once())
->method('downloadFile')
->with(
'JobID1_en_de.public',
'de',
$this->callback(function($subject) {
$params = $subject->exportToArray();
return $params['retrievalType'] == 'published';
})
)
->willReturn('xml');
$this->pluginMock->expects($this->once())
->method('validateImport')
->with(
'public://tmgmt_smartling_translations/JobID1_en_de.public',
$translate_job
)
->willReturn(TRUE);
$this->pluginMock->expects($this->once())
->method('import')
->with(
'public://tmgmt_smartling_translations/JobID1_en_de.public',
$translate_job
)
->willReturn([
1 => [],
2 => []
]);
$this->translationRequestManagerMock->expects($this->once())
->method('commitSuccessfulDownload')
->with($translate_job)
->willReturn(TRUE);
tmgmt_smartling_download_file($translate_job);
}
/**
* Download success full flow: download by TMGMT Job and TMGMT Job Item.
*/
public function testDownloadByTmgmtJobAndTmgmtJobItem() {
$real_job = $this->createJobWithItems([
'batch_uid' => 'uid',
'batch_execute_on_job' => 1,
]);
$field_item_list = $this->getMockBuilder(FieldItemList::class)
->disableOriginalConstructor()
->setMethods([
'getValue'
])
->getMock();
$field_item_list->expects($this->any())
->method('getValue')
->willReturn([
0 => [
'value' => NULL
]
]);
$job_item_mock_1 = $this->getMockBuilder(JobItem::class)
->disableOriginalConstructor()
->setMethods([
'save',
'id',
'setState',
'addTranslatedData',
'getItemId',
'getItemType'
])
->getMock();
$job_item_mock_1->expects($this->any())
->method('id')
->willReturn(1);
$job_item_mock_1->expects($this->any())
->method('getItemId')
->willReturn(1);
$job_item_mock_1->expects($this->any())
->method('getItemType')
->willReturn('node');
$job_item_mock_1->expects($this->never())
->method('setState')
->with(JobItemInterface::STATE_ACTIVE);
$job_item_mock_1->expects($this->never())
->method('addTranslatedData');
$job_item_mock_2 = $this->getMockBuilder(JobItem::class)
->disableOriginalConstructor()
->setMethods([
'id',
'setState',
'addTranslatedData',
'getItemId',
'getItemType'
])
->getMock();
$job_item_mock_2->expects($this->any())
->method('id')
->willReturn(2);
$job_item_mock_1->expects($this->any())
->method('getItemId')
->willReturn(1);
$job_item_mock_1->expects($this->any())
->method('getItemType')
->willReturn('node');
$job_item_mock_2->expects($this->once())
->method('setState')
->with(JobItemInterface::STATE_ACTIVE);
$job_item_mock_2->expects($this->once())
->method('addTranslatedData');
$translate_job = $this->getMockBuilder(Job::class)
->disableOriginalConstructor()
->setMethods([
'id',
'getItems',
'getTranslator',
'getTranslatorPlugin',
'getSetting',
'getSourceLangcode',
'getTargetLangcode',
'save',
'set',
'get',
'addMessage',
'getFieldDefinitions'
])
->getMock();
$translate_job->expects($this->any())
->method('id')
->willReturn(1);
$translate_job->expects($this->any())
->method('getSetting')
->willReturn('public');
$translate_job->expects($this->any())
->method('getTranslator')
->willReturn($real_job->getTranslator());
$translate_job->expects($this->any())
->method('getTranslatorPlugin')
->willReturn($real_job->getTranslatorPlugin());
$translate_job->expects($this->any())
->method('getSourceLangcode')
->willReturn('en');
$translate_job->expects($this->any())
->method('getTargetLangcode')
->willReturn('de');
$translate_job->expects($this->any())
->method('get')
->willReturn($field_item_list);
$translate_job->expects($this->any())
->method('getFieldDefinitions')
->willReturn([]);
$translate_job->expects($this->once())
->method('getItems')
->willReturn([
1 => $job_item_mock_1,
2 => $job_item_mock_2
]);
$this->translationRequestManagerMock->expects($this->never())
->method('commitError');
$this->translationRequestManagerMock->expects($this->once())
->method('getTranslationRequest')
->with($translate_job)
->willReturn(['translationRequestUid' => 'test']);
$this->apiWrapperMock->expects($this->once())
->method('getApi')
->with('file')
->willReturn($this->fileApiMock);
$this->fileApiMock->expects($this->once())
->method('downloadFile')
->with(
'JobID1_en_de.public',
'de',
$this->callback(function($subject) {
$params = $subject->exportToArray();
return $params['retrievalType'] == 'published';
})
)
->willReturn('xml');
$this->pluginMock->expects($this->once())
->method('validateImport')
->with(
'public://tmgmt_smartling_translations/JobID1_en_de.public',
$translate_job
)
->willReturn(TRUE);
$this->pluginMock->expects($this->once())
->method('import')
->with(
'public://tmgmt_smartling_translations/JobID1_en_de.public',
$translate_job
)
->willReturn([
1 => [],
2 => []
]);
$this->translationRequestManagerMock->expects($this->once())
->method('commitSuccessfulDownload')
->with($translate_job)
->willReturn(TRUE);
tmgmt_smartling_download_file($translate_job, $job_item_mock_2);
}
}
......@@ -602,7 +602,7 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
}
/**
* Is translation submission missed: true, state translated.
* Is ready for download: true, state translated.
*/
public function testIsTranslationRequestReadyForDownloadStateTranslatedTrue() {
$translation_request_manager_mock = $this
......@@ -634,7 +634,7 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
}
/**
* Is translation submission missed: false, missing submission.
* Is ready for download: false, missing submission.
*/
public function testIsTranslationRequestReadyForDownloadMissingSubmissionFalse() {
$translation_request_manager_mock = $this
......@@ -659,7 +659,7 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
}
/**
* Is translation submission missed: false, state new.
* Is ready for download: false, state new.
*/
public function testIsTranslationRequestReadyForDownloadStateNewFalse() {
$translation_request_manager_mock = $this
......@@ -691,7 +691,7 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
}
/**
* Is translation submission missed: false, state in progress.
* Is ready for download: false, state in progress.
*/
public function testIsTranslationRequestReadyForDownloadStateInProgressFalse() {
$translation_request_manager_mock = $this
......@@ -723,9 +723,9 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
}
/**
* Is translation submission missed: false, state completed.
* Is ready for download: true, state completed.
*/
public function testIsTranslationRequestReadyForDownloadStateCompletedFalse() {
public function testIsTranslationRequestReadyForDownloadStateCompletedTrue() {
$translation_request_manager_mock = $this
->getMockBuilder(TranslationRequestManager::class)
->setConstructorArgs([$this->apiWrapperMock, $this->stateMock, $this->loggerMock])
......@@ -751,11 +751,11 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
]
]);
$this->assertEquals($translation_request_manager_mock->isTranslationRequestReadyForDownload($this->job), FALSE);
$this->assertEquals($translation_request_manager_mock->isTranslationRequestReadyForDownload($this->job), TRUE);
}
/**
* Is translation submission missed: false, state deleted.
* Is ready for download: false, state deleted.
*/
public function testIsTranslationRequestReadyForDownloadStateDeletedFalse() {
$translation_request_manager_mock = $this
......@@ -787,7 +787,7 @@ class TranslationRequestManagerTest extends SmartlingTestBase {
}
/**
* Is translation submission missed: false, state failed.
* Is ready for download: false, state failed.
*/
public function testIsTranslationRequestReadyForDownloadStateFailedFalse() {
$translation_request_manager_mock = $this
......
......@@ -107,6 +107,10 @@ function tmgmt_smartling_cron() {
}
}
function tmgmt_smartling_skip_passed_job_item_processing(JobItemInterface $current_job_item, JobItemInterface $passed_job_item = NULL) {
return !empty($passed_job_item) && $current_job_item->id() != $passed_job_item->id();
}
function tmgmt_smartling_download_file(JobInterface $job, JobItemInterface $passed_job_item = NULL) {
$api_wrapper = $job->getTranslatorPlugin()->getApiWrapper($job->getTranslator()->getSettings());
$translation_request_manager = Drupal::service('tmgmt_smartling.translation_request_manager');
......@@ -207,7 +211,7 @@ function tmgmt_smartling_download_file(JobInterface $job, JobItemInterface $pass
$entity_type_manager = \Drupal::entityTypeManager();
foreach ($job_items as $job_item) {
if (!empty($passed_job_item) && $job_item->id() != $passed_job_item->id()) {
if (tmgmt_smartling_skip_passed_job_item_processing($job_item, $passed_job_item)) {
continue;
}
......@@ -242,7 +246,7 @@ function tmgmt_smartling_download_file(JobInterface $job, JobItemInterface $pass
// Validation successful, start import.
foreach ($plugin->import($file->getFileUri(), $job) as $key => $value) {
if (isset($job_items[$key])) {
if (!empty($passed_job_item) && $job_items[$key]->id() != $passed_job_item->id()) {
if (tmgmt_smartling_skip_passed_job_item_processing($job_items[$key], $passed_job_item)) {
continue;
}
......
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