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

Set the default graph on entity creation #5

Merged
merged 2 commits into from
Oct 8, 2019
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
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);
}

}