-
Notifications
You must be signed in to change notification settings - Fork 13
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 #2634 from oat-sa/feat/HKD-6/integration
Feat/hkd 6/integration
- Loading branch information
Showing
5 changed files
with
240 additions
and
1 deletion.
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
65 changes: 65 additions & 0 deletions
65
model/Translation/Service/ResourceTranslatableStatusHandler.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,65 @@ | ||
<?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\taoQtiItem\model\Translation\Service; | ||
|
||
use oat\generis\model\data\Ontology; | ||
use oat\tao\model\Translation\Entity\ResourceTranslatableStatus; | ||
use oat\taoQtiItem\model\qti\Item; | ||
use oat\taoQtiItem\model\qti\Service; | ||
use Psr\Log\LoggerInterface; | ||
use Throwable; | ||
|
||
class ResourceTranslatableStatusHandler | ||
{ | ||
private Service $qtiItemService; | ||
private LoggerInterface $logger; | ||
private Ontology $ontology; | ||
|
||
public function __construct(Service $qtiItemService, LoggerInterface $logger, Ontology $ontology) | ||
{ | ||
$this->qtiItemService = $qtiItemService; | ||
$this->logger = $logger; | ||
$this->ontology = $ontology; | ||
} | ||
|
||
public function __invoke(ResourceTranslatableStatus $status): void | ||
{ | ||
try { | ||
$item = $this->ontology->getResource($status->getUri()); | ||
|
||
$itemData = $this->qtiItemService->getDataItemByRdfItem($item); | ||
|
||
$status->setEmpty(!$itemData || $itemData->isEmpty()); | ||
} catch (Throwable $exception) { | ||
$this->logger->error( | ||
sprintf( | ||
'An error occurred while retrieving item data: %s. Trace: %s', | ||
$exception->getMessage(), | ||
$exception->getTraceAsString() | ||
) | ||
); | ||
|
||
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
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
149 changes: 149 additions & 0 deletions
149
test/unit/model/Translation/Service/ResourceTranslatableStatusHandlerTest.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,149 @@ | ||
<?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\taoQtiItem\test\unit\model\Translation\Service; | ||
|
||
use core_kernel_classes_Resource; | ||
use oat\generis\model\data\Ontology; | ||
use oat\tao\model\TaoOntology; | ||
use oat\tao\model\Translation\Entity\ResourceTranslatableStatus; | ||
use oat\taoQtiItem\model\qti\Item; | ||
use oat\taoQtiItem\model\qti\Service; | ||
use oat\taoQtiItem\model\Translation\Service\ResourceTranslatableStatusHandler; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class ResourceTranslatableStatusHandlerTest extends TestCase | ||
{ | ||
/** @var core_kernel_classes_Resource|MockObject */ | ||
private core_kernel_classes_Resource $item; | ||
|
||
/** @var Service|MockObject */ | ||
private Service $qtiItemService; | ||
|
||
/** @var LoggerInterface|MockObject */ | ||
private LoggerInterface $logger; | ||
|
||
/** @var Ontology|MockObject */ | ||
private Ontology $ontology; | ||
|
||
private ResourceTranslatableStatus $status; | ||
private ResourceTranslatableStatusHandler $sut; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->item = $this->createMock(core_kernel_classes_Resource::class); | ||
$this->item | ||
->method('getUri') | ||
->willReturn('itemUri'); | ||
|
||
$this->qtiItemService = $this->createMock(Service::class); | ||
$this->logger = $this->createMock(LoggerInterface::class); | ||
$this->ontology = $this->createMock(Ontology::class); | ||
|
||
$this->status = new ResourceTranslatableStatus( | ||
'itemUri', | ||
TaoOntology::CLASS_URI_ITEM, | ||
'languageUri', | ||
true, | ||
true | ||
); | ||
|
||
$this->sut = new ResourceTranslatableStatusHandler( | ||
$this->qtiItemService, | ||
$this->logger, | ||
$this->ontology | ||
); | ||
} | ||
|
||
public function testWillSetEmptyToFalseIfItemHasContent(): void | ||
{ | ||
$itemData = $this->createMock(Item::class); | ||
$itemData | ||
->expects($this->once()) | ||
->method('isEmpty') | ||
->willReturn(false); | ||
|
||
$this->ontology | ||
->expects($this->once()) | ||
->method('getResource') | ||
->with('itemUri') | ||
->willReturn($this->item); | ||
|
||
$this->qtiItemService | ||
->expects($this->once()) | ||
->method('getDataItemByRdfItem') | ||
->with($this->item) | ||
->willReturn($itemData); | ||
|
||
$this->assertTrue($this->status->isEmpty()); | ||
|
||
$this->sut->__invoke($this->status); | ||
|
||
$this->assertFalse($this->status->isEmpty()); | ||
} | ||
|
||
public function testWillSetEmptyToTrueIfItemHasEmptyContentSaved(): void | ||
{ | ||
$itemData = $this->createMock(Item::class); | ||
$itemData | ||
->expects($this->once()) | ||
->method('isEmpty') | ||
->willReturn(true); | ||
|
||
$this->ontology | ||
->expects($this->once()) | ||
->method('getResource') | ||
->with('itemUri') | ||
->willReturn($this->item); | ||
|
||
$this->qtiItemService | ||
->expects($this->once()) | ||
->method('getDataItemByRdfItem') | ||
->with($this->item) | ||
->willReturn($itemData); | ||
|
||
$this->sut->__invoke($this->status); | ||
|
||
$this->assertTrue($this->status->isEmpty()); | ||
} | ||
|
||
public function testWillSetEmptyToTrueIfItemHasNoContent(): void | ||
{ | ||
$this->ontology | ||
->expects($this->once()) | ||
->method('getResource') | ||
->with('itemUri') | ||
->willReturn($this->item); | ||
|
||
$this->qtiItemService | ||
->expects($this->once()) | ||
->method('getDataItemByRdfItem') | ||
->with($this->item) | ||
->willReturn(null); | ||
|
||
$this->sut->__invoke($this->status); | ||
|
||
$this->assertTrue($this->status->isEmpty()); | ||
} | ||
} |