diff --git a/model/qti/metadata/importer/MetaMetadataImportMapper.php b/model/qti/metadata/importer/MetaMetadataImportMapper.php index aadc6f15f5..f88fb8711b 100644 --- a/model/qti/metadata/importer/MetaMetadataImportMapper.php +++ b/model/qti/metadata/importer/MetaMetadataImportMapper.php @@ -46,15 +46,10 @@ public function mapMetaMetadataToProperties( foreach ($metaMetadataProperties as $metaMetadataProperty) { if ($match = $this->matchProperty($metaMetadataProperty, $itemClass->getProperties(true))) { $matchedProperties['itemProperties'][$metaMetadataProperty['uri']] = $match; - continue; } if ($testClass && $match = $this->matchProperty($metaMetadataProperty, $testClass->getProperties(true))) { $matchedProperties['testProperties'][$metaMetadataProperty['uri']] = $match; - continue; - } - if ($match === null) { - throw new PropertyDoesNotExistException($metaMetadataProperty); } } return $matchedProperties; diff --git a/model/qti/metadata/ontology/MappedMetadataInjector.php b/model/qti/metadata/ontology/MappedMetadataInjector.php index 929498290c..79d400c5be 100644 --- a/model/qti/metadata/ontology/MappedMetadataInjector.php +++ b/model/qti/metadata/ontology/MappedMetadataInjector.php @@ -27,6 +27,7 @@ use oat\generis\model\OntologyAwareTrait; use oat\taoBackOffice\model\lists\ListService; use oat\taoQtiItem\model\qti\metadata\simple\SimpleMetadataValue; +use tao_helpers_form_elements_Readonly as ReadOnlyWidget; class MappedMetadataInjector { @@ -54,27 +55,37 @@ public function inject(array $mappedProperties, array $metadataValues, Resource if ($currentValue && $currentValue === $metadataValue->getValue()) { continue; } - if ($mappedProperties[$mappedPath]->getRange()->getUri() === RDFS_LITERAL) { + if ( + $mappedProperties[$mappedPath]->getWidget() + && $mappedProperties[$mappedPath]->getWidget()->getUri() === ReadOnlyWidget::WIDGET_ID + ) { + continue; + } + if ( + $mappedProperties[$mappedPath]->getRange() + && $mappedProperties[$mappedPath]->getRange()->getUri() === RDFS_LITERAL + ) { + // If resource already has property value, remove it. + if ($resource->getPropertyValuesCollection($mappedProperties[$mappedPath])->count() > 0) { + $propertyValue = $resource->getUniquePropertyValue($mappedProperties[$mappedPath]); + $resource->removePropertyValue($mappedProperties[$mappedPath], $propertyValue); + } + $resource->setPropertyValue($mappedProperties[$mappedPath], $metadataValue->getValue()); break; } - $list = $this->listService->getListElements($mappedProperties[$mappedPath]->getRange()); - foreach ($list as $listElement) { - if ( - $listElement->getLabel() === $metadataValue->getValue() - || $listElement->getOriginalUri() === $metadataValue->getValue() - ) { - $resource->setPropertyValue( - $mappedProperties[$mappedPath], - $this->getResource($listElement->getUri()) - ); - /** @var Property $property */ - $property = $mappedProperties[$mappedPath]; - if ($property->isMultiple() === false) { - break; - } - } + if ($mappedProperties[$mappedPath]->getRange() !== null) { + $this->setListValue($mappedProperties[$mappedPath], $resource, $metadataValue); + break; + } + + if ($mappedProperties[$mappedPath]->getRange() === null) { + $resource->setPropertyValue( + $mappedProperties[$mappedPath], + $this->getResource($metadataValue->getValue()) + ); + break; } } } @@ -84,4 +95,24 @@ private function isInjectableProperty(array $mappedProperties, string $mappedPat { return isset($mappedProperties[$mappedPath]) && $mappedProperties[$mappedPath] instanceof Property; } + + 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() + || $listElement->getOriginalUri() === $metadataValue->getValue() + ) { + $resource->setPropertyValue( + $property, + $this->getResource($listElement->getUri()) + ); + + if ($property->isMultiple() === false) { + break; + } + } + } + } } diff --git a/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php b/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php index d257a59198..b3a05db114 100644 --- a/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php +++ b/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php @@ -72,7 +72,6 @@ public function testMapMetaMetadataToProperties(): void $propertyMock = $this->createMock(core_kernel_classes_Property::class); $resourceMock = $this->createMock(core_kernel_classes_Resource::class); - $itemClass->method('getProperties') ->willReturn([$propertyMock, $propertyMock, $propertyMock]); $testClass->method('getProperties') @@ -86,7 +85,15 @@ public function testMapMetaMetadataToProperties(): void 'http://some-other-uri', 'http://widget.uri', 'http://some-other-uri', - 'http://widget.uri' + 'http://widget.uri', + 'http://example.com/uri1', + 'http://widget.uri', + 'http://some-other-uri', + 'http://widget.uri', + 'http://some-other-uri', + 'http://widget.uri', + 'http://some-other-uri', + 'http://widget.uri', ); $propertyMock->method('getLabel') ->willReturn('label2', 'some-other-label', 'some-other-other-label'); @@ -105,31 +112,6 @@ public function testMapMetaMetadataToProperties(): void $result = $this->subject->mapMetaMetadataToProperties($metaMetadataProperties, $itemClass, $testClass); self::assertNotNull($result); - self::assertEquals(3, count($result['itemProperties'])); - } - - public function testMapMetaMetadataToPropertiesThrowErrorWhenCannotMapProperty(): void - { - $this->expectException(PropertyDoesNotExistException::class); - $metaMetadataProperties = [ - [ - 'uri' => 'http://example.com/uri1', - 'label' => 'label1', - 'alias' => 'alias1', - 'checksum' => 'qwerty1234', - 'multiple' => 'http://resource.uri/false' - ] - ]; - - $itemClass = $this->createMock(core_kernel_classes_Class::class); - $testClass = $this->createMock(core_kernel_classes_Class::class); - $propertyMock = $this->createMock(core_kernel_classes_Property::class); - - $itemClass->method('getProperties') - ->willReturn([$propertyMock, $propertyMock, $propertyMock]); - $testClass->method('getProperties') - ->willReturn([$propertyMock, $propertyMock, $propertyMock]); - $result = $this->subject->mapMetaMetadataToProperties($metaMetadataProperties, $itemClass, $testClass); - self::assertNotNull($result); + self::assertEquals(1, count($result['itemProperties'])); } } diff --git a/test/unit/model/qti/metadata/ontology/MappedMetadataInjectorTest.php b/test/unit/model/qti/metadata/ontology/MappedMetadataInjectorTest.php index b2f9914e48..94d8fe1715 100644 --- a/test/unit/model/qti/metadata/ontology/MappedMetadataInjectorTest.php +++ b/test/unit/model/qti/metadata/ontology/MappedMetadataInjectorTest.php @@ -49,6 +49,7 @@ public function testInject() $classMock = $this->createMock(core_kernel_classes_Class::class); $resourceMock = $this->createMock(core_kernel_classes_Resource::class); $valueMock = $this->createMock(Value::class); + $widgetPropertyMock = $this->createMock(core_kernel_classes_Property::class); $mappedProperties = [ 'mappedPath1' => $propertyMock, @@ -70,6 +71,12 @@ public function testInject() $propertyMock->method('getRange') ->willReturn($classMock); + $propertyMock->method('getWidget') + ->willReturn($widgetPropertyMock); + + $widgetPropertyMock->method('getUri') + ->willReturn('someUri'); + $valueMock->method('getLabel') ->willReturn( 'label', @@ -81,6 +88,7 @@ public function testInject() ); + $this->ontologyMock->method('getResource') ->willReturn($resourceMock); @@ -94,7 +102,7 @@ public function testInject() ->willReturn('matchedUri', 'otherUri', 'otherUri'); $resourceMock - ->expects(self::exactly(4)) + ->expects(self::exactly(2)) ->method('setPropertyValue') ->with($propertyMock, $resourceMock);