Skip to content

Commit

Permalink
Merge pull request #2685 from oat-sa/fix/hkd-295/duplicated-propertie…
Browse files Browse the repository at this point in the history
…s-during-import

[Fix] Remove old properties before setting new props during QTI import
  • Loading branch information
shpran authored Feb 17, 2025
2 parents f68f51e + 176d271 commit fa4a924
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
10 changes: 10 additions & 0 deletions model/qti/metadata/ontology/MappedMetadataInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function __construct(ListService $listService)

public function inject(array $mappedProperties, array $metadataValues, Resource $resource): void
{
$removedProperties = [];

/** @var SimpleMetadataValue $metadataValue */
foreach ($metadataValues as $metadataValue) {
foreach ($metadataValue->getPath() as $mappedPath) {
Expand Down Expand Up @@ -76,6 +78,13 @@ public function inject(array $mappedProperties, array $metadataValues, Resource
}

if ($mappedProperties[$mappedPath]->getRange() !== null) {
$propertyUri = $mappedProperties[$mappedPath]->getUri();

if (!in_array($propertyUri, $removedProperties, true)) {
$resource->removePropertyValues($mappedProperties[$mappedPath]);
$removedProperties[] = $propertyUri;
}

$this->setListValue($mappedProperties[$mappedPath], $resource, $metadataValue);
break;
}
Expand All @@ -99,6 +108,7 @@ private function isInjectableProperty(array $mappedProperties, string $mappedPat
private function setListValue(Property $property, Resource $resource, SimpleMetadataValue $metadataValue): void
{
$list = $this->listService->getListElements($property->getRange());

foreach ($list as $listElement) {
if (
$listElement->getLabel() === $metadataValue->getValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,29 @@
use oat\taoBackOffice\model\lists\ListService;
use oat\taoQtiItem\model\qti\metadata\ontology\MappedMetadataInjector;
use oat\taoQtiItem\model\qti\metadata\simple\SimpleMetadataValue;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class MappedMetadataInjectorTest extends TestCase
{
/** @var ListService|MockObject */
private ListService $listServiceMock;

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

private MappedMetadataInjector $subject;

public function setUp(): void
{
$this->listServiceMock = $this->createMock(ListService::class);
$this->ontologyMock = $this->createMock(Ontology::class);

$this->subject = new MappedMetadataInjector($this->listServiceMock);
$this->subject->setModel($this->ontologyMock);
}

public function testInject()
public function testInject(): void
{
$propertyMock = $this->createMock(core_kernel_classes_Property::class);
$simpleMetadataValue = $this->createMock(SimpleMetadataValue::class);
Expand All @@ -65,19 +75,24 @@ public function testInject()
->method('getPath')
->willReturn(['mappedPath1', 'mappedPath2']);

$this->listServiceMock->method('getListElements')
$this->listServiceMock
->method('getListElements')
->willReturn([$valueMock, $valueMock, $valueMock]);

$propertyMock->method('getRange')
$propertyMock
->method('getRange')
->willReturn($classMock);

$propertyMock->method('getWidget')
$propertyMock
->method('getWidget')
->willReturn($widgetPropertyMock);

$widgetPropertyMock->method('getUri')
$widgetPropertyMock
->method('getUri')
->willReturn('someUri');

$valueMock->method('getLabel')
$valueMock
->method('getLabel')
->willReturn(
'label',
'valueLabel',
Expand All @@ -87,22 +102,28 @@ public function testInject()
'otherLabel'
);



$this->ontologyMock->method('getResource')
$this->ontologyMock
->method('getResource')
->willReturn($resourceMock);

$resourceMock->method('getLabel')
$resourceMock
->expects($this->once())
->method('removePropertyValues');

$resourceMock
->method('getLabel')
->willReturn('label', 'mismatchLabel', 'otherMatchedLabel');

$simpleMetadataValue->method('getValue')
$simpleMetadataValue
->method('getValue')
->willReturn('label', 'otherMismatchedLabel', 'matchedUri', 'otherMatchedLabel');

$valueMock->method('getOriginalUri')
$valueMock
->method('getOriginalUri')
->willReturn('matchedUri', 'otherUri', 'otherUri');

$resourceMock
->expects(self::exactly(2))
->expects($this->exactly(2))
->method('setPropertyValue')
->with($propertyMock, $resourceMock);

Expand Down

0 comments on commit fa4a924

Please sign in to comment.