Skip to content

Commit

Permalink
Merge pull request #2645 from oat-sa/backport/AUT-3992-backport-to-20…
Browse files Browse the repository at this point in the history
…24-12

Backport/aut 3992 backport to 2024 12
  • Loading branch information
Karol-Stelmaczonek authored Dec 10, 2024
2 parents 0534c7b + e619436 commit 72b8918
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 8 deletions.
6 changes: 6 additions & 0 deletions model/qti/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ public function importQTIPACKFile(
$metaMetadataValues,
$itemClass
);
if (empty($mappedMetadataValues)) {
$mappedMetadataValues = $this->getMetaMetadataImportMapper()->mapMetadataToProperties(
$metadataValues,
$itemClass
);
}
}

$sharedFiles = [];
Expand Down
38 changes: 31 additions & 7 deletions model/qti/metadata/importer/MetaMetadataImportMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

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;
use oat\generis\model\GenerisRdf;
use oat\taoQtiItem\model\import\ChecksumGenerator;

Expand All @@ -39,22 +40,41 @@ 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) {
if ($match = $this->matchProperty($metaMetadataProperty, $itemClass->getProperties(true))) {
$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,
kernelClass $itemClass,
kernelClass $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 */
Expand All @@ -79,14 +99,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'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
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 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);
Expand Down Expand Up @@ -114,4 +117,62 @@ 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);
}
}

0 comments on commit 72b8918

Please sign in to comment.