Skip to content

Commit

Permalink
Merge pull request #4108 from oat-sa/feat/ADF-1804/add-possibility-de…
Browse files Browse the repository at this point in the history
…lete-translation

feat: add possibility to delete translations
  • Loading branch information
gabrielfs7 authored Oct 3, 2024
2 parents 95c41b3 + be8d554 commit cf5b9c0
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 0 deletions.
24 changes: 24 additions & 0 deletions actions/class.Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use oat\tao\model\Translation\Service\ResourceTranslationRetriever;
use oat\tao\model\Translation\Service\ResourceTranslatableRetriever;
use oat\tao\model\Translation\Service\TranslationCreationService;
use oat\tao\model\Translation\Service\TranslationDeletionService;
use oat\tao\model\Translation\Service\TranslationSyncService;
use oat\tao\model\Translation\Service\TranslationUpdateService;

Expand Down Expand Up @@ -55,6 +56,24 @@ public function update(): void
}
}

/**
* @requiresRight id WRITE
*/
public function delete(): void
{
try {
$resource = $this->getTranslationDeletionService()->deleteByRequest($this->getPsrRequest());

$this->setSuccessJsonResponse(
[
'resourceUri' => $resource->getUri()
]
);
} catch (Throwable $exception) {
$this->setErrorJsonResponse($exception->getMessage());
}
}

/**
* @requiresRight id WRITE
*/
Expand Down Expand Up @@ -141,4 +160,9 @@ private function getTranslationSyncService(): TranslationSyncService
{
return $this->getPsrContainer()->get(TranslationSyncService::class);
}

private function getTranslationDeletionService(): TranslationDeletionService
{
return $this->getPsrContainer()->get(TranslationDeletionService::class);
}
}
26 changes: 26 additions & 0 deletions doc/taoApi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ paths:
id:
type: string
description: The resource URI
languageUri:
type: string
description: The language resource URI
responses:
200:
$ref: '#/components/responses/TranslateResponse'
400:
$ref: '#/components/responses/BadRequestResponse'
500:
$ref: '#/components/responses/InternalServerErrorResponse'
/tao/Translation/delete:
post:
summary: delete translation
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
id:
type: string
description: The resource URI
languageUri:
type: string
description: The language resource URI
responses:
200:
$ref: '#/components/responses/TranslateResponse'
Expand Down
105 changes: 105 additions & 0 deletions models/classes/Translation/Service/TranslationDeletionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2024 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\tao\model\Translation\Service;

use core_kernel_classes_Resource;
use oat\generis\model\data\Ontology;
use oat\generis\model\resource\Contract\ResourceDeleterInterface;
use oat\tao\model\Translation\Entity\AbstractResource;
use oat\tao\model\Translation\Exception\ResourceTranslationException;
use oat\tao\model\Translation\Query\ResourceTranslationQuery;
use oat\tao\model\Translation\Repository\ResourceTranslationRepository;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Throwable;

class TranslationDeletionService
{
private Ontology $ontology;
private ResourceDeleterInterface $resourceDeleter;
private ResourceTranslationRepository $resourceTranslationRepository;
private LoggerInterface $logger;

public function __construct(
Ontology $ontology,
ResourceDeleterInterface $resourceDeleter,
ResourceTranslationRepository $resourceTranslationRepository,
LoggerInterface $logger
) {
$this->ontology = $ontology;
$this->resourceDeleter = $resourceDeleter;
$this->resourceTranslationRepository = $resourceTranslationRepository;
$this->logger = $logger;
}

public function deleteByRequest(ServerRequestInterface $request): core_kernel_classes_Resource
{
$requestParams = $request->getParsedBody();
$resourceUri = $requestParams['id'] ?? null;
$languageUri = $requestParams['languageUri'] ?? null;

if (empty($resourceUri)) {
throw new ResourceTranslationException('Resource id is required');
}

if (empty($languageUri)) {
throw new ResourceTranslationException('Parameter languageUri is mandatory');
}

try {
$translations = $this->resourceTranslationRepository
->find(new ResourceTranslationQuery([$resourceUri], $languageUri));

if ($translations->count() === 0) {
throw new ResourceTranslationException(
sprintf(
'Translation does not exist for [id=%s, locale=%s]',
$resourceUri,
$languageUri
)
);
}

/** @var AbstractResource $translation */
foreach ($translations as $translation) {
$resource = $this->ontology->getResource($translation->getResourceUri());

$this->resourceDeleter->delete($resource);
}

return $resource;
} catch (Throwable $exception) {
$this->logger->error(
sprintf(
'Could not delete translation [id=%s, language=%s] (%s): %s',
$resourceUri,
$languageUri,
get_class($exception),
$exception->getMessage()
)
);

throw $exception;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use oat\generis\model\data\Ontology;
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService;
use oat\generis\model\resource\Service\ResourceDeleter;
use oat\oatbox\log\LoggerService;
use oat\oatbox\user\UserLanguageServiceInterface;
use oat\tao\model\featureFlag\FeatureFlagChecker;
Expand All @@ -39,6 +40,7 @@
use oat\tao\model\Translation\Service\ResourceTranslatableRetriever;
use oat\tao\model\Translation\Service\ResourceTranslationRetriever;
use oat\tao\model\Translation\Service\TranslationCreationService;
use oat\tao\model\Translation\Service\TranslationDeletionService;
use oat\tao\model\Translation\Service\TranslationSyncService;
use oat\tao\model\Translation\Service\TranslationUpdateService;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand Down Expand Up @@ -128,6 +130,18 @@ public function __invoke(ContainerConfigurator $configurator): void
)
->public();

$services
->set(TranslationDeletionService::class, TranslationDeletionService::class)
->args(
[
service(Ontology::SERVICE_ID),
service(ResourceDeleter::class),
service(ResourceTranslationRepository::class),
service(LoggerService::SERVICE_ID),
]
)
->public();

$services
->set(TranslationUpdateService::class, TranslationUpdateService::class)
->args(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2024 (original work) Open Assessment Technologies SA;
*
* @author Gabriel Felipe Soares <gabriel.felipe.soares@taotesting.com>
*/

declare(strict_types=1);

namespace oat\tao\test\unit\models\classes\Translation\Service;

use core_kernel_classes_Resource;
use oat\generis\model\resource\Contract\ResourceDeleterInterface;
use oat\tao\model\Translation\Entity\ResourceTranslation;
use oat\tao\model\Translation\Exception\ResourceTranslationException;
use oat\tao\model\Translation\Repository\ResourceTranslationRepository;
use oat\tao\model\Translation\Entity\ResourceCollection;
use oat\generis\model\data\Ontology;
use oat\tao\model\Translation\Service\TranslationDeletionService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;

class TranslationDeletionServiceTest extends TestCase
{
private TranslationDeletionService $service;

/** @var Ontology|MockObject */
private $ontology;

/** @var ResourceDeleterInterface|MockObject */
private $resourceDeleter;

/** @var ResourceTranslationRepository|MockObject */
private $resourceTranslationRepository;

/** @var MockObject|LoggerInterface */
private $logger;

protected function setUp(): void
{
$this->ontology = $this->createMock(Ontology::class);
$this->resourceDeleter = $this->createMock(ResourceDeleterInterface::class);
$this->resourceTranslationRepository = $this->createMock(ResourceTranslationRepository::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->service = new TranslationDeletionService(
$this->ontology,
$this->resourceDeleter,
$this->resourceTranslationRepository,
$this->logger,
);
}

public function testDelete(): void
{
$resourceUri = 'id1';
$languageUri = 'http://www.tao.lu/Ontologies/TAO.rdf#Langpt-BR';
$translationResourceId = 'translationId1';

$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())
->method('getParsedBody')
->willReturn(
[
'id' => $resourceUri,
'languageUri' => $languageUri,
]
);

$translation = new ResourceTranslation($translationResourceId, 'something');
$resourceCollection = new ResourceCollection($translation);

$resource = $this->createMock(core_kernel_classes_Resource::class);

$this->resourceTranslationRepository
->expects($this->once())
->method('find')
->willReturn($resourceCollection);

$this->ontology
->expects($this->once())
->method('getResource')
->with($translationResourceId)
->willReturn($resource);

$this->assertSame($resource, $this->service->deleteByRequest($request));
}


public function testDeleteWithMissingIdParamWillThrowException(): void
{
$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())
->method('getParsedBody')
->willReturn(
[
'languageUri' => 'http://www.tao.lu/Ontologies/TAO.rdf#Langpt-BR',
]
);

$this->expectException(ResourceTranslationException::class);
$this->expectExceptionMessage('Resource id is required');

$this->service->deleteByRequest($request);
}

public function testDeleteWithMissingLanguageParamWillThrowException(): void
{
$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())
->method('getParsedBody')
->willReturn(
[
'id' => 'id1',
]
);

$this->expectException(ResourceTranslationException::class);
$this->expectExceptionMessage('Parameter languageUri is mandatory');

$this->service->deleteByRequest($request);
}
}

0 comments on commit cf5b9c0

Please sign in to comment.