-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4108 from oat-sa/feat/ADF-1804/add-possibility-de…
…lete-translation feat: add possibility to delete translations
- Loading branch information
Showing
5 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
models/classes/Translation/Service/TranslationDeletionService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
test/unit/models/classes/Translation/Service/TranslationDeletionServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |