Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from ec-europa/set-default-graph
Browse files Browse the repository at this point in the history
Set the default graph on entity creation
  • Loading branch information
pfrenssen authored Oct 8, 2019
2 parents 8b66978 + b06bfe3 commit 65b3a8f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/SparqlEntityStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\sparql_entity_storage\Database\Driver\sparql\ConnectionInterface;
use Drupal\sparql_entity_storage\Entity\Query\Sparql\SparqlArg;
use Drupal\sparql_entity_storage\Entity\SparqlGraph;
use Drupal\sparql_entity_storage\Exception\DuplicatedIdException;
use EasyRdf\Graph;
use EasyRdf\Literal;
Expand Down Expand Up @@ -186,6 +187,19 @@ protected static function getGraph($graph_uri) {
return $graph;
}

/**
* {@inheritdoc}
*/
public function create(array $values = []) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = parent::create($values);
// Ensure the default graph if no explicit graph has been set.
if ($entity->get('graph')->isEmpty()) {
$entity->set('graph', SparqlGraph::DEFAULT);
}
return $entity;
}

/**
* {@inheritdoc}
*/
Expand Down
29 changes: 29 additions & 0 deletions tests/src/Kernel/EntityCreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Drupal\Tests\sparql_entity_storage\Kernel;

use Drupal\rdf_entity\RdfInterface;
use Drupal\sparql_entity_storage\Entity\SparqlGraph;
use Drupal\sparql_entity_storage\Exception\DuplicatedIdException;
use Drupal\sparql_test\Entity\SparqlTest;

Expand Down Expand Up @@ -49,4 +51,31 @@ public function testOverlappingIds(): void {
])->save();
}

/**
* Tests that the default graph is set on entity creation.
*
* @covers ::create
*/
public function testDefaultGraphSetOnCreate(): void {
// Check that the default graph is set when no graph is specified.
$entity = SparqlTest::create([
'type' => 'waffle',
'id' => 'http://example.com/kempense-galet',
'title' => 'Kempense galet',
]);

$this->assertEquals(SparqlGraph::DEFAULT, $entity->get('graph')->target_id);

// Check that it is possible to specify a custom graph.
/** @var \Drupal\rdf_entity\RdfInterface $entity */
$entity = SparqlTest::create([
'type' => 'waffle',
'id' => 'http://example.com/liege-waffle',
'title' => 'Liège waffle',
'graph' => 'custom_graph',
]);

$this->assertEquals('custom_graph', $entity->get('graph')->target_id);
}

}

0 comments on commit 65b3a8f

Please sign in to comment.