Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport/aut 3992 backport to 2024 12 #2645

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}