From 04b840ab24dd55ac5541bad2cdb3724b85fa2556 Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Wed, 4 Dec 2024 14:48:20 +0100 Subject: [PATCH 1/6] fix: handle import of props without metadata --- model/qti/ImportService.php | 6 +++++ .../importer/MetaMetadataImportMapper.php | 23 ++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/model/qti/ImportService.php b/model/qti/ImportService.php index f110bec89..3a8091faf 100755 --- a/model/qti/ImportService.php +++ b/model/qti/ImportService.php @@ -337,6 +337,12 @@ public function importQTIPACKFile( $metaMetadataValues, $itemClass ); + if (empty($mappedMetadataValues)) { + $mappedMetadataValues = $this->getMetaMetadataImportMapper()->mapMetadataToProperties( + $metadataValues, + $itemClass + ); + } } $sharedFiles = []; diff --git a/model/qti/metadata/importer/MetaMetadataImportMapper.php b/model/qti/metadata/importer/MetaMetadataImportMapper.php index f88fb8711..2a676e967 100644 --- a/model/qti/metadata/importer/MetaMetadataImportMapper.php +++ b/model/qti/metadata/importer/MetaMetadataImportMapper.php @@ -55,6 +55,19 @@ public function mapMetaMetadataToProperties( return $matchedProperties; } + public function mapMetadataToProperties(array $metadataProperties, core_kernel_classes_Class $itemClass, core_kernel_classes_Class $testClass = null): array + { + $parsedMetadataProperties = []; + foreach ($metadataProperties as $metadataProperty) { + foreach ($metadataProperty as $property) { + $parsedMetadataProperties[] = [ + 'uri' => $property->getPath()[1], + ]; + } + } + return $this->mapMetaMetadataToProperties($parsedMetadataProperties, $itemClass, $testClass); + } + private function matchProperty(array &$metaMetadataProperty, array $classProperties): ?Property { /** @var Property $itemClassProperty */ @@ -79,14 +92,18 @@ private function matchProperty(array &$metaMetadataProperty, array $classPropert private function isSynced(Property $classProperty, array &$metaMetadataProperty): bool { $multiple = $classProperty->getOnePropertyValue(new Property(GenerisRdf::PROPERTY_MULTIPLE)); - $checksum = $this->checksumGenerator->getRangeChecksum($classProperty); + try { + $checksum = $this->checksumGenerator->getRangeChecksum($classProperty); + } catch (\InvalidArgumentException $e) { + return false; + } $metaMetadataProperty['checksum_result'] = $checksum === $metaMetadataProperty['checksum']; $metaMetadataProperty['widget_result'] = - $classProperty->getWidget()->getUri() === $metaMetadataProperty['widget']; + $classProperty->getWidget() && $classProperty->getWidget()->getUri() === $metaMetadataProperty['widget']; return $multiple instanceof core_kernel_classes_Resource && $multiple->getUri() === $metaMetadataProperty['multiple'] && $checksum === $metaMetadataProperty['checksum'] - && $classProperty->getWidget()->getUri() === $metaMetadataProperty['widget']; + && $classProperty->getWidget() && $classProperty->getWidget()->getUri() === $metaMetadataProperty['widget']; } } From 669d118402cee4d56e2c031815bc51b6dab182ad Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Wed, 4 Dec 2024 15:26:44 +0100 Subject: [PATCH 2/6] fix: add import --- model/qti/metadata/importer/MetaMetadataImportMapper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/model/qti/metadata/importer/MetaMetadataImportMapper.php b/model/qti/metadata/importer/MetaMetadataImportMapper.php index 2a676e967..f7ea012af 100644 --- a/model/qti/metadata/importer/MetaMetadataImportMapper.php +++ b/model/qti/metadata/importer/MetaMetadataImportMapper.php @@ -25,6 +25,7 @@ use core_kernel_classes_Class; use core_kernel_classes_Property as Property; use core_kernel_classes_Resource; +use InvalidArgumentException; use oat\generis\model\GenerisRdf; use oat\taoQtiItem\model\import\ChecksumGenerator; @@ -94,7 +95,7 @@ private function isSynced(Property $classProperty, array &$metaMetadataProperty) $multiple = $classProperty->getOnePropertyValue(new Property(GenerisRdf::PROPERTY_MULTIPLE)); try { $checksum = $this->checksumGenerator->getRangeChecksum($classProperty); - } catch (\InvalidArgumentException $e) { + } catch (InvalidArgumentException $e) { return false; } $metaMetadataProperty['checksum_result'] = $checksum === $metaMetadataProperty['checksum']; From 1daa98bebd05040657a33549355ff1b78b50d465 Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Wed, 4 Dec 2024 16:54:01 +0100 Subject: [PATCH 3/6] feature: add unit tests --- .../importer/MetaMetadataImportMapperTest.php | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php b/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php index b3a05db11..3ac996eb7 100644 --- a/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php +++ b/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php @@ -27,11 +27,13 @@ use core_kernel_classes_Resource; use oat\taoQtiItem\model\import\ChecksumGenerator; use oat\taoQtiItem\model\qti\metadata\importer\MetaMetadataImportMapper; -use oat\taoQtiItem\model\qti\metadata\importer\PropertyDoesNotExistException; use PHPUnit\Framework\TestCase; class MetaMetadataImportMapperTest extends TestCase { + private ChecksumGenerator $checksumGeneratorMock; + private MetaMetadataImportMapper $subject; + public function setUp(): void { $this->checksumGeneratorMock = $this->createMock(ChecksumGenerator::class); @@ -114,4 +116,59 @@ public function testMapMetaMetadataToProperties(): void self::assertNotNull($result); self::assertEquals(1, count($result['itemProperties'])); } + + public function testMapMetadataToProperties(): void + { + $metadataProperty = $this->getMockBuilder(\stdClass::class) + ->addMethods(['getPath']) + ->getMock(); + $metadataProperty->method('getPath')->willReturn(['somePath', 'http://example.com/uri1']); + + $metadataProperties = [[$metadataProperty]]; + + $itemClass = $this->createMock(core_kernel_classes_Class::class); + $propertyMock = $this->createMock(core_kernel_classes_Property::class); + $resourceMock = $this->createMock(core_kernel_classes_Resource::class); + + $itemClass->method('getProperties')->willReturn([$propertyMock]); + $propertyMock->method('getUri')->willReturn('http://example.com/uri1'); + $propertyMock->method('getOnePropertyValue')->willReturn($resourceMock); + $propertyMock->method('getWidget')->willReturn($propertyMock); + + $resourceMock->method('getUri')->willReturn('http://resource.uri/false'); + + $this->checksumGeneratorMock->method('getRangeChecksum')->willReturn('qwerty1234'); + + $result = $this->subject->mapMetadataToProperties($metadataProperties, $itemClass); + + $this->assertArrayHasKey('itemProperties', $result); + $this->assertInstanceOf(core_kernel_classes_Property::class, $result['itemProperties']['http://example.com/uri1']); + } + + public function testHandlesInvalidArgumentExceptionInIsSynced(): void + { + $metaMetadataProperties = [ + [ + 'uri' => 'http://example.com/uri1', + 'label' => 'label1', + 'alias' => 'alias1', + 'checksum' => 'qwerty1234', + 'multiple' => 'http://resource.uri/false', + 'widget' => 'http://widget.uri' + ] + ]; + + $itemClass = $this->createMock(core_kernel_classes_Class::class); + $propertyMock = $this->createMock(core_kernel_classes_Property::class); + + $itemClass->method('getProperties')->willReturn([$propertyMock]); + $propertyMock->method('getLabel')->willReturn('label1'); + + $this->checksumGeneratorMock->method('getRangeChecksum') + ->willThrowException(new \InvalidArgumentException()); + + $result = $this->subject->mapMetaMetadataToProperties($metaMetadataProperties, $itemClass); + + $this->assertEmpty($result); + } } From 85384420b2cf3c7d157ee7f6a143a96267449f56 Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Thu, 5 Dec 2024 13:24:04 +0100 Subject: [PATCH 4/6] fix: use import --- .../qti/metadata/importer/MetaMetadataImportMapperTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php b/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php index 3ac996eb7..17acfd9ee 100644 --- a/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php +++ b/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php @@ -25,6 +25,7 @@ use core_kernel_classes_Class; use core_kernel_classes_Property; use core_kernel_classes_Resource; +use InvalidArgumentException; use oat\taoQtiItem\model\import\ChecksumGenerator; use oat\taoQtiItem\model\qti\metadata\importer\MetaMetadataImportMapper; use PHPUnit\Framework\TestCase; @@ -165,7 +166,7 @@ public function testHandlesInvalidArgumentExceptionInIsSynced(): void $propertyMock->method('getLabel')->willReturn('label1'); $this->checksumGeneratorMock->method('getRangeChecksum') - ->willThrowException(new \InvalidArgumentException()); + ->willThrowException(new InvalidArgumentException()); $result = $this->subject->mapMetaMetadataToProperties($metaMetadataProperties, $itemClass); From c1d48c42d16b10e03f33481509b39495c2ec9796 Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Thu, 5 Dec 2024 15:31:44 +0100 Subject: [PATCH 5/6] chore: linter --- .../importer/MetaMetadataImportMapper.php | 17 ++++++++++++----- .../importer/MetaMetadataImportMapperTest.php | 5 ++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/model/qti/metadata/importer/MetaMetadataImportMapper.php b/model/qti/metadata/importer/MetaMetadataImportMapper.php index f7ea012af..a1bc406e1 100644 --- a/model/qti/metadata/importer/MetaMetadataImportMapper.php +++ b/model/qti/metadata/importer/MetaMetadataImportMapper.php @@ -22,7 +22,7 @@ namespace oat\taoQtiItem\model\qti\metadata\importer; -use core_kernel_classes_Class; +use core_kernel_classes_Class as kernelClass; use core_kernel_classes_Property as Property; use core_kernel_classes_Resource; use InvalidArgumentException; @@ -40,8 +40,8 @@ public function __construct(ChecksumGenerator $checksumGenerator) public function mapMetaMetadataToProperties( array $metaMetadataProperties, - core_kernel_classes_Class $itemClass, - core_kernel_classes_Class $testClass = null + kernelClass $itemClass, + kernelClass $testClass = null ): array { $matchedProperties = []; foreach ($metaMetadataProperties as $metaMetadataProperty) { @@ -49,14 +49,21 @@ public function mapMetaMetadataToProperties( $matchedProperties['itemProperties'][$metaMetadataProperty['uri']] = $match; } - if ($testClass && $match = $this->matchProperty($metaMetadataProperty, $testClass->getProperties(true))) { + if ( + $testClass && + $match = $this->matchProperty($metaMetadataProperty, $testClass->getProperties(true)) + ) { $matchedProperties['testProperties'][$metaMetadataProperty['uri']] = $match; } } return $matchedProperties; } - public function mapMetadataToProperties(array $metadataProperties, core_kernel_classes_Class $itemClass, core_kernel_classes_Class $testClass = null): array + public function mapMetadataToProperties( + array $metadataProperties, + kernelClass $itemClass, + kernelClass $testClass = null + ): array { $parsedMetadataProperties = []; foreach ($metadataProperties as $metadataProperty) { diff --git a/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php b/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php index 17acfd9ee..fdd301bf2 100644 --- a/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php +++ b/test/unit/model/qti/metadata/importer/MetaMetadataImportMapperTest.php @@ -143,7 +143,10 @@ public function testMapMetadataToProperties(): void $result = $this->subject->mapMetadataToProperties($metadataProperties, $itemClass); $this->assertArrayHasKey('itemProperties', $result); - $this->assertInstanceOf(core_kernel_classes_Property::class, $result['itemProperties']['http://example.com/uri1']); + $this->assertInstanceOf( + core_kernel_classes_Property::class, + $result['itemProperties']['http://example.com/uri1'] + ); } public function testHandlesInvalidArgumentExceptionInIsSynced(): void From b69537e13ac722e99967f4f105323c8479934bcc Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Thu, 5 Dec 2024 16:32:22 +0100 Subject: [PATCH 6/6] chore: linter --- model/qti/metadata/importer/MetaMetadataImportMapper.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/model/qti/metadata/importer/MetaMetadataImportMapper.php b/model/qti/metadata/importer/MetaMetadataImportMapper.php index a1bc406e1..da35f4329 100644 --- a/model/qti/metadata/importer/MetaMetadataImportMapper.php +++ b/model/qti/metadata/importer/MetaMetadataImportMapper.php @@ -63,8 +63,7 @@ public function mapMetadataToProperties( array $metadataProperties, kernelClass $itemClass, kernelClass $testClass = null - ): array - { + ): array { $parsedMetadataProperties = []; foreach ($metadataProperties as $metadataProperty) { foreach ($metadataProperty as $property) {