Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D8AGE-291: The translate actions. #309

Merged
merged 56 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
6a219ba
D8AGE-291: Create the request updater.
intelektron Apr 26, 2024
ae1a8f7
D8AGE-291: Add the controller for operations.
intelektron Apr 26, 2024
acb3bc5
D8AGE-291: Adjust the callback controller.
intelektron Apr 26, 2024
e39f464
D8AGE-291: Move the status conversion to the updater.
intelektron Apr 26, 2024
becf87b
D8AGE-291: Add all fields support to the updater.
intelektron Apr 29, 2024
0e8f524
D8AGE-291: Change the sample responses to be simplier.
intelektron Apr 29, 2024
be32c3a
D8AGE-291: Fix the Service Mock. for the Status API to support all fi…
intelektron Apr 29, 2024
4115051
D8AGE-291: Mock the translation process.
intelektron Apr 29, 2024
6189743
D8AGE-291: Support for fetching translated data.
intelektron Apr 29, 2024
7a6f628
D8AGE-291: Sending all possible job/request statuses.
intelektron Apr 30, 2024
42877ea
D8AGE-291: Cleanup obsolete exception.
intelektron May 13, 2024
e045d69
D8AGE-291: Update CDT branch.
intelektron May 13, 2024
2d8e916
D8AGE-291: Fix the FileApi bug that was causing problems with status …
intelektron May 13, 2024
a7c7287
D8AGE-291: Two additional language statuses.
intelektron May 13, 2024
c12c1ef
D8AGE-291: An additional method for the remote translation entity.
intelektron May 13, 2024
85a5f30
D8AGE-291: Status change hooks.
intelektron May 13, 2024
12760de
D8AGE-291: Adjust to new File API endpoint.
intelektron May 13, 2024
341143b
D8AGE-291: Fix actions bug in the mock module.
intelektron May 13, 2024
eb69eab
D8AGE-291: Downloading translations.
intelektron May 13, 2024
c0005eb
D8AGE-291: Additional status descriptions.
intelektron May 13, 2024
71986d3
D8AGE-291: Extended language status transitions.
intelektron May 13, 2024
78dcb93
D8AGE-291: Move statuses to constants.
intelektron May 14, 2024
bc935ab
D8AGE-291: Fix comments.
intelektron May 14, 2024
eba6725
D8AGE-291: Adjust tests.
intelektron May 14, 2024
6ba9800
D8AGE-291: Tighten permissions.
intelektron May 14, 2024
599fa01
D8AGE-291: Updated functional test.
intelektron May 14, 2024
173124c
D8AGE-291: Fixed status API mock.
intelektron May 14, 2024
4ed6696
D8AGE-291: Small updater improvements.
intelektron May 14, 2024
5ebe535
D8AGE-291: Use constants for statuses.
intelektron May 15, 2024
618755f
D8AGE-291: Kernel test for the Updater service.
intelektron May 15, 2024
bd182ad
D8AGE-291: Move the variable outside the loop, and rename buttons.
intelektron May 16, 2024
d618de7
D8AGE-291: Add comments to controller.
intelektron May 16, 2024
488067b
D8AGE-291: Add namespace to tests.
intelektron May 16, 2024
d56bb92
D8AGE-291: Update test buttons.
intelektron May 16, 2024
944d6d7
D8AGE-291: Phpcbf update and recheck.
intelektron May 16, 2024
45b0d9e
D8AGE-291: Changed example URLs to be similar to CDT.
intelektron May 16, 2024
1c9c26a
D8AGE-291: Changed API weights to avoid conflicts with the http mock …
intelektron May 16, 2024
9ab107f
D8AGE-291: Revert changes to the endpoints.
intelektron May 16, 2024
edf1b2f
D8AGE-291: Change composer.json.
intelektron May 22, 2024
76b2907
D8AGE-291: Fixed the interference with epoetry calls.
intelektron May 29, 2024
66ea2f6
D8AGE-291: Updated the functional test.
intelektron May 29, 2024
ee3d4cb
D8AGE-341: Access checkers for the dashboard.
intelektron May 21, 2024
0144c9f
D8AGE-341: Additional view fields.
intelektron May 21, 2024
53a72c5
D8AGE-341: The dashboard view.
intelektron May 21, 2024
ccb4ab1
D8AGE-341: Simplify the additional classes.
intelektron May 21, 2024
0d037d0
D8AGE-341: An addition to the functional test.
intelektron May 22, 2024
229e125
D8AGE-341: Rename the access check and apply it to the routes.
intelektron May 22, 2024
3264677
D8AGE-341: Change rendering method of field plugin.
intelektron May 22, 2024
2c3a954
D8AGE-341: Fix the view schema.
intelektron May 22, 2024
00d87bb
D8AGE-341: Kernel tests.
intelektron May 22, 2024
734d208
D8AGE-341: Fix comments and strict standards.
intelektron May 23, 2024
7318960
D8AGE-341: Add access tests.
intelektron May 24, 2024
90ac5ce
D8AGE-341: Fixed the renderer to allow attaching libraries.
intelektron May 30, 2024
5fa7dc3
D8AGE-341: Put the test inside a render context to avoid exceptions.
intelektron May 30, 2024
aeab27c
D8AGE-341: Fix the functional test after the rebase.
intelektron May 30, 2024
dc40226
Merge pull request #310 from openeuropa/D8AGE-341
drishu Jun 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"drupal/paragraphs": "^1.13",
"drupal/typed_link": "^2.0",
"drush/drush": "^12",
"openeuropa/cdt-client": "dev-D8AGE-289",
"openeuropa/cdt-client": "1.x-dev",
"openeuropa/code-review": "^2.0",
"openeuropa/epoetry-client": "1.x-dev || 2.x-dev",
"openeuropa/oe_content": "^3.0.0-beta2",
Expand Down Expand Up @@ -74,6 +74,7 @@
"autoload-dev": {
"psr-4": {
"Drupal\\Tests\\oe_translation\\": "./tests/src",
"Drupal\\Tests\\oe_translation_cdt\\": "./modules/oe_translation_cdt/tests/src",
"Drupal\\Tests\\oe_translation_epoetry\\": "./modules/oe_translation_epoetry/tests/src",
"Drupal\\Tests\\oe_translation_corporate_workflow\\": "./modules/oe_translation_corporate_workflow/tests/src",
"Drupal\\Tests\\oe_translation_active_revision\\": "./modules/oe_translation_corporate_workflow/modules/oe_translation_active_revision/tests/src",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* @file
* Primary module hooks for OpenEuropa Translation CDT mock module.
*/

declare(strict_types=1);

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\oe_translation_cdt\Api\CdtApiWrapperInterface;
use Drupal\oe_translation_cdt\TranslationRequestCdt;

/**
* Implements hook_entity_ENTITY_TYPE_view_alter().
*/
function oe_translation_cdt_mock_oe_translation_request_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($entity->bundle() !== 'cdt') {
return;
}

/** @var \Drupal\oe_translation_cdt\TranslationRequestCdtInterface $entity */
if (!isset($build['meta'])) {
return;
}
$row = &$build['meta']['#rows'][0];

$operations = [];
$all_statuses = [
CdtApiWrapperInterface::STATUS_REQUEST_COMPLETED => 'Completed',
CdtApiWrapperInterface::STATUS_REQUEST_IN_PROGRESS => 'In progress',
CdtApiWrapperInterface::STATUS_REQUEST_CANCELLED => 'Cancelled',
CdtApiWrapperInterface::STATUS_REQUEST_PENDING_APPROVAL => 'Pending approval',
CdtApiWrapperInterface::STATUS_REQUEST_UNDER_QUOTATION => 'Under quotation',
];
foreach ($all_statuses as $status => $label) {
$operations[$status] = [
'title' => sprintf('Update status to %s (mock)', $label),
'url' => Url::fromRoute('oe_translation_cdt_mock.request_status', [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why but when using the "In progress" update option, there is no information logged in the request. Afaik we do not have an ongoing status for our requests but we should still log the update in the request logs. Same thing happens for the job updates

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've got only these states of request in the CDT:

  • COMP - The request has been completed and delivered.
  • INPR - The request is in progress or request for quotation approved.
  • CANC - The request has been cancelled
  • PEND - The request quotation is ready to be approved by the client (Requests for Quotation only).
  • UNDE - The request quotation is under quotation.

So I have no way to distinguish "requested" from "in progress".

The log is only written when there is an actual change. When you start the translation, it is already in INPR state.

'translation_request' => $entity->id(),
], [
'query' => [
'destination' => Url::fromRoute('<current>')->toString(),
'status' => $status,
],
]),
];
}

if (!isset($row['cdt_operations'])) {
$row['cdt_operations'] = [
'data' => [
'#type' => 'operations',
'#links' => $operations,
],
];
}
else {
$row['cdt_operations']['data']['#links'] += $operations;
}
}

/**
* Implements template_preprocess_table__remote_language_list().
*/
function oe_translation_cdt_mock_preprocess_table__remote_language_list(array &$variables) {
$id = (string) $variables['attributes']['data-translation-request'];
$request = TranslationRequestCdt::load($id);
if (!$request || $request->bundle() !== 'cdt' || !$request->getCdtId()) {
return;
}

$all_statuses = [
CdtApiWrapperInterface::STATUS_JOB_COMPLETED => 'Completed',
CdtApiWrapperInterface::STATUS_JOB_FAILED => 'Failed',
CdtApiWrapperInterface::STATUS_JOB_IN_PROGRESS => 'In progress',
CdtApiWrapperInterface::STATUS_JOB_CANCELLED => 'Cancelled',
CdtApiWrapperInterface::STATUS_JOB_TO_BE_CANCELLED => 'To be cancelled',
];
foreach ($variables['rows'] as &$row) {
$langcode = (string) $row['attributes']['hreflang'];
foreach ($all_statuses as $status => $label) {
$row['cells']['operations']['content']['#links'][] = [
'title' => sprintf('Update status to %s (mock)', $label),
'url' => Url::fromRoute('oe_translation_cdt_mock.job_status', [
'translation_request' => $id,
'langcode' => $langcode,
], [
'query' => [
'destination' => Url::fromRoute('<current>')->toString(),
'status' => $status,
],
]),
];
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
oe_translation_cdt_mock.job_status:
path: '/translation-request/cdt/mock/job_status/{translation_request}/{langcode}'
defaults:
_title: 'Change job status'
_controller: '\Drupal\oe_translation_cdt_mock\Controller\MockController::changeJobStatus'
requirements:
_permission: 'translate any entity'
options:
parameters:
translation_request:
type: entity:oe_translation_request
langcode:
type: string

oe_translation_cdt_mock.request_status:
path: '/translation-request/cdt/mock/request_status/{translation_request}'
defaults:
_title: 'Change request status'
_controller: '\Drupal\oe_translation_cdt_mock\Controller\MockController::changeRequestStatus'
requirements:
_permission: 'translate any entity'
options:
parameters:
translation_request:
type: entity:oe_translation_request
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"departments": [
{
"code": "123",
"description": "External Translation Unit - Contracts Execution Department"
"description": "Department 1"
},
{
"code": "456",
"description": "Service Finance"
"description": "Department 2"
}
],
"priorities": [
Expand Down Expand Up @@ -309,13 +309,23 @@
],
"contacts": [
{
"email": "test@example.com",
"firstName": "DGTRAD - Ext. Transl. Unit ",
"lastName": "Contracts Execution Dept.",
"userName": "TESTUSER",
"email": "test1@example.com",
"firstName": "John",
"lastName": "Smith",
"userName": "TESTUSER1",
"countryCode": null,
"phoneCountryCode": null,
"phoneNumber": "+351234123123",
"phoneNumber": "+351231231231",
"countryName": ""
},
{
"email": "test2@example.com",
"firstName": "Jane",
"lastName": "Doe",
"userName": "TESTUSER2",
"countryCode": null,
"phoneCountryCode": null,
"phoneNumber": "+354564564564",
"countryName": ""
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"deliveryDate": "2024-03-30T14:41:34.7045082",
"title": "Test translation",
"service": "Translation",
"department": "External Translation Unit - Contracts Execution Department",
"department": "Department 1",
"contacts": [
"DGTRAD - Ext. Transl. Unit Contracts Execution Dept."
"John Smith"
],
"deliverToContacts": [
"DGTRAD - Ext. Transl. Unit Contracts Execution Dept."
"Jane Doe"
],
"sourceDocuments": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Drupal\oe_translation_cdt_mock\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\oe_translation_cdt\Mapper\LanguageCodeMapper;
use Drupal\oe_translation_cdt\TranslationRequestCdtInterface;
use Drupal\oe_translation_cdt\TranslationRequestUpdaterInterface;
use OpenEuropa\CdtClient\Model\Callback\JobStatus;
use OpenEuropa\CdtClient\Model\Callback\RequestStatus;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Returns responses for OpenEuropa Translation CDT mock routes.
*/
final class MockController extends ControllerBase {

/**
* Constructs the controller.
*
* @param \Drupal\oe_translation_cdt\TranslationRequestUpdaterInterface $updater
* The translation request updater.
*/
public function __construct(
private readonly TranslationRequestUpdaterInterface $updater,
) {}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new static(
$container->get('oe_translation_cdt.translation_request_updater'),
);
}

/**
* Changes the translation job status by mocking.
*
* @param \Drupal\oe_translation_cdt\TranslationRequestCdtInterface $translation_request
* The translation request.
* @param string $langcode
* The language code.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response.
*/
public function changeJobStatus(TranslationRequestCdtInterface $translation_request, string $langcode, Request $request): Response {
$job_status = new JobStatus();
$job_status->setTargetLanguageCode(LanguageCodeMapper::getCdtLanguageCode($langcode, $translation_request));
$job_status->setStatus((string) $request->query->get('status'));
$job_status->setRequestIdentifier((string) $translation_request->getCdtId());
$this->updater->updateFromJobStatus($translation_request, $job_status);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We talked about the event dispatcher here, but it turns out that the separate updater service is a clean solution.


$destination = $request->query->get('destination');
if (!$destination) {
throw new NotFoundHttpException();
}
return new RedirectResponse((string) $destination);
}

/**
* Changes the translation request status by mocking.
*
* @param \Drupal\oe_translation_cdt\TranslationRequestCdtInterface $translation_request
* The translation request.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response.
*/
public function changeRequestStatus(TranslationRequestCdtInterface $translation_request, Request $request): Response {
$request_status = new RequestStatus();
$request_status->setRequestIdentifier((string) $translation_request->getCdtId());
$request_status->setStatus((string) $request->query->get('status'));
$this->updater->updateFromRequestStatus($translation_request, $request_status);

$destination = $request->query->get('destination');
if (!$destination) {
throw new NotFoundHttpException();
}
return new RedirectResponse((string) $destination);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @ServiceMock(
* id = "oe_translation_cdt_file_api",
* label = @Translation("CDT mocked file responses for testing."),
* weight = -1,
* weight = -2,
* )
*/
class FileApi extends ServiceMockBase {
Expand Down Expand Up @@ -50,7 +50,7 @@ public function __construct(
protected ModuleExtensionList $moduleExtensionList,
protected EntityTypeManagerInterface $entityTypeManager,
protected LoggerChannelFactoryInterface $loggerFactory,
protected ContentFormatterInterface $xmlFormatter
protected ContentFormatterInterface $xmlFormatter,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $moduleExtensionList, $entityTypeManager, $loggerFactory);
}
Expand Down Expand Up @@ -92,16 +92,20 @@ public function getEndpointResponse(RequestInterface $request): ResponseInterfac
return new Response(400, [], $this->getResponseFromFile('file_response_400.json'));
}

// Create a duplicate of the current entity and change the text.
// This way, we avoid modifying the original entity, and saving it
// accidentally in the database.
$data = TranslationSourceHelper::filterTranslatable($entity->getData());
$translated_entity = $entity->createDuplicate();
foreach ($data as &$field) {
$field['#text'] = sprintf(
'%s translation of %s',
$parameters['language'],
$field['#text']
);
}
$entity->setData($data);
$xml = $this->xmlFormatter->export($entity);
$translated_entity->setData($data);
$xml = $this->xmlFormatter->export($translated_entity);

$this->log('200: Returning the mocked file.', $request);
return new Response(200, [], $xml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @ServiceMock(
* id = "oe_translation_cdt_identifier_api",
* label = @Translation("CDT mocked identifier responses for testing."),
* weight = -1,
* weight = -2,
* )
*/
class IdentifierApi extends ServiceMockBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @ServiceMock(
* id = "oe_translation_cdt_main_api",
* label = @Translation("CDT mocked checkConnection responses for testing."),
* weight = -1,
* weight = -2,
* )
*/
class MainApi extends ServiceMockBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @ServiceMock(
* id = "oe_translation_cdt_reference_data_api",
* label = @Translation("CDT mocked reference data responses for testing."),
* weight = -1,
* weight = -2,
* )
*/
class ReferenceDataApi extends ServiceMockBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @ServiceMock(
* id = "oe_translation_cdt_requests_api",
* label = @Translation("CDT mocked 'requests' responses for testing."),
* weight = -1,
* weight = -2,
* )
*/
class RequestsApi extends ServiceMockBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(
$plugin_definition,
protected ModuleExtensionList $moduleExtensionList,
protected EntityTypeManagerInterface $entityTypeManager,
protected LoggerChannelFactoryInterface $loggerFactory
protected LoggerChannelFactoryInterface $loggerFactory,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
Expand Down
Loading