Skip to content

Commit

Permalink
Check property domains for all resources of classes mentioned in the …
Browse files Browse the repository at this point in the history
…ontology
  • Loading branch information
zozlak committed Apr 15, 2021
1 parent 9088c54 commit 4caf1d4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/acdhOeaw/arche/Doorkeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,14 @@ static private function checkPropertyTypes(Resource $meta): void {
* @throws DoorkeeperException
*/
static private function checkCardinalities(Resource $meta): void {
$inDomain = [RDF::RDF_TYPE];
foreach ($meta->allResources(RDF::RDF_TYPE) as $class) {
$classDef = self::$ontology->getClass((string) $class);
if ($classDef === null) {
continue;
}
foreach ($classDef->properties as $p) {
$inDomain = array_merge($inDomain, $p->property);
if (($p->min > 0 || $p->max !== null) && $p->automatedFill === false) {
$co = $cd = 0;
$cdl = ['' => 0];
Expand All @@ -454,6 +456,15 @@ static private function checkCardinalities(Resource $meta): void {
}
}
}
// check only resources of a class(es) defined in the ontology
if (count($inDomain) > 1) {
$outDomain = array_diff($meta->propertyUris(), $inDomain); // properties allowed on resource classes
$owlThing = self::$ontology->getClass(RDF::OWL_THING);
$outDomain = array_diff($outDomain, array_keys($owlThing->properties)); // properties allowed on all resources
if (count($outDomain) > 0) {
throw new DoorkeeperException("Properties with a wrong domain: " . implode(', ', $outDomain));
}
}
}

static private function checkBiblatex(Resource $meta): void {
Expand Down
35 changes: 18 additions & 17 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function testPropertyType(): void {
public function testCardinalitiesMax(): void {
$idProp = self::$config->schema->id;
$prop = 'https://vocabs.acdh.oeaw.ac.at/schema#hasTransferDate';
$im = self::createMetadata([], 'https://vocabs.acdh.oeaw.ac.at/schema#BinaryContent');
$im = self::createMetadata([], 'https://vocabs.acdh.oeaw.ac.at/schema#Resource');
$im->addLiteral($prop, '2020-07-01');
self::$repo->begin();
$r = self::$repo->createResource($im);
Expand All @@ -229,41 +229,42 @@ public function testCardinalitiesMax(): void {
}

public function testDefaultProperties(): void {
$mimeProp = self::$config->schema->mime;
$accessRestProp = self::$config->schema->accessRestriction;
$creationDateProp = self::$config->schema->creationDate;
$im = self::createMetadata([
RDF::RDF_TYPE => 'https://vocabs.acdh.oeaw.ac.at/schema#RepoObject',
RDF::RDF_TYPE => 'https://vocabs.acdh.oeaw.ac.at/schema#Collection',
]);
$skip = [
self::$config->schema->hosting, $accessRestProp, $creationDateProp
];
$class = self::$ontology->getClass('https://vocabs.acdh.oeaw.ac.at/schema#RepoObject');
$class = self::$ontology->getClass('https://vocabs.acdh.oeaw.ac.at/schema#Collection');
foreach ($class->getProperties() as $i) {
if ($i->min > 0 && $im->get($i->uri) === null && !in_array($i->uri, $skip)) {
$im->add($i->property[0], self::createSampleProperty($i));
}
}
self::$repo->begin();
$r = self::$repo->createResource($im, new BinaryPayload('foo bar', null, 'text/plain'));
$r = self::$repo->createResource($im);
$rm = $r->getGraph();
$this->assertEquals(date('Y-m-d'), substr($rm->getLiteral($creationDateProp), 0, 10));
// accessRestriction is only on BinaryContent (Resource/Metadata) and not on RepoObject
$this->assertNull($rm->get($accessRestProp));
$this->assertNull($rm->get($mimeProp));

$rh = new RepoResource((string) $rm->get(self::$config->schema->hosting), self::$repo);
$this->assertContains(self::getPropertyDefault(self::$config->schema->hosting), $rh->getIds());

$this->assertEquals(date('Y-m-d'), substr($rm->getLiteral($creationDateProp), 0, 10));
$this->assertEquals('text/plain', (string) $rm->getLiteral(self::$config->schema->mime));

// accessRestriction is only on BinaryContent and not on RepoObject
$this->assertNull($rm->get($accessRestProp));

$im = self::createMetadata([], 'https://vocabs.acdh.oeaw.ac.at/schema#BinaryContent');
$im = self::createMetadata([], 'https://vocabs.acdh.oeaw.ac.at/schema#Resource');
$r = self::$repo->createResource($im, new BinaryPayload('foo bar', null, 'text/plain'));
$rm = $r->getGraph();
$this->assertEquals('text/plain', (string) $rm->getLiteral($mimeProp));
$rar = new RepoResource((string) $r->getGraph()->get($accessRestProp), self::$repo);
$this->assertContains(self::getPropertyDefault($accessRestProp), $rar->getIds());
}

public function testAccessRightsAuto(): void {
$im = self::createMetadata([], 'https://vocabs.acdh.oeaw.ac.at/schema#BinaryContent');
$im = self::createMetadata([], 'https://vocabs.acdh.oeaw.ac.at/schema#Resource');
self::$repo->begin();
$r = self::$repo->createResource($im);
$om = $r->getGraph();
Expand All @@ -277,7 +278,7 @@ public function testAccessRightsAcademic(): void {
$accessRestProp = self::$config->schema->accessRestriction;
$im = self::createMetadata([
$accessRestProp => 'https://vocabs.acdh.oeaw.ac.at/archeaccessrestrictions/academic',
], 'https://vocabs.acdh.oeaw.ac.at/schema#RepoObject');
], 'https://vocabs.acdh.oeaw.ac.at/schema#Resource');
$bp = new BinaryPayload('dummy content');
self::$repo->begin();
$r = self::$repo->createResource($im, $bp);
Expand All @@ -299,7 +300,7 @@ public function testAccessRightsRestricted(): void {
$im = self::createMetadata([
$accessRestProp => 'https://vocabs.acdh.oeaw.ac.at/archeaccessrestrictions/restricted',
self::$config->schema->accessRole => 'foo',
], 'https://vocabs.acdh.oeaw.ac.at/schema#RepoObject');
], 'https://vocabs.acdh.oeaw.ac.at/schema#Resource');
$bp = new BinaryPayload('dummy content');
self::$repo->begin();
$r = self::$repo->createResource($im, $bp);
Expand Down Expand Up @@ -331,7 +332,7 @@ public function testAccessRightsRise(): void {

$im = self::createMetadata([
$accessRestProp => 'https://vocabs.acdh.oeaw.ac.at/archeaccessrestrictions/restricted',
], 'https://vocabs.acdh.oeaw.ac.at/schema#RepoObject');
], 'https://vocabs.acdh.oeaw.ac.at/schema#Resource');
$bp = new BinaryPayload('dummy content');
self::$repo->begin();
$r = self::$repo->createResource($im, $bp);
Expand Down Expand Up @@ -365,7 +366,7 @@ public function testAccessRightsLower(): void {

$im = self::createMetadata([
$accessRestrProp => 'https://vocabs.acdh.oeaw.ac.at/archeaccessrestrictions/public',
], 'https://vocabs.acdh.oeaw.ac.at/schema#RepoObject');
], 'https://vocabs.acdh.oeaw.ac.at/schema#Resource');
$bp = new BinaryPayload('dummy content');
self::$repo->begin();
$r = self::$repo->createResource($im, $bp);
Expand Down Expand Up @@ -572,7 +573,7 @@ public function testPidPreserving(): void {
public function testUnknownProperty(): void {
$cfgFile = __DIR__ . '/../config/yaml/config-repo.yaml';
$cfg = yaml_parse_file($cfgFile);
$im = self::createMetadata(['https://vocabs.acdh.oeaw.ac.at/schema#foo' => 'bar'], 'https://vocabs.acdh.oeaw.ac.at/schema#RepoObject');
$im = self::createMetadata(['https://vocabs.acdh.oeaw.ac.at/schema#foo' => 'bar']);

// turn off the check
$cfg['doorkeeper']['checkUnknownProperties'] = false;
Expand Down
3 changes: 2 additions & 1 deletion tests/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,10 @@ public function testTopCollectionAggregates(): void {

public function testAutoGenResource(): void {
$collClass = self::$config->schema->classes->collection;
$parentProp = self::$config->schema->parent;
$randRes = 'https://bar/' . rand();
self::$repo->begin();
$r = self::$repo->createResource(self::createMetadata(['https://foo' => $randRes], $collClass));
$r = self::$repo->createResource(self::createMetadata([$parentProp => $randRes], $collClass));
$this->toDelete[] = $r;
try {
self::$repo->commit();
Expand Down

0 comments on commit 4caf1d4

Please sign in to comment.