', 'CONTAINS') - ->sort('id') - ->execute(); - $this->assertResult('http://vegetable.example.com/002'); - - $this->results = $this->getQuery() - ->condition('text', '', 'STARTS WITH') - ->sort('id') - ->execute(); - $this->assertResult('http://vegetable.example.com/002'); - - $this->results = $this->getQuery() - ->condition('text', '', 'ENDS WITH') - ->sort('id') - ->execute(); - $this->assertResult('http://vegetable.example.com/002'); - - $this->results = $this->getQuery() - ->condition('text', '', 'ENDS WITH') - ->sort('id') - ->execute(); - $this->assertResult('http://vegetable.example.com/002'); - - $this->results = $this->getQuery() - ->condition('reference', $this->entities[6]->id()) - ->sort('id') - ->execute(); - $this->assertResult('http://vegetable.example.com/004', 'http://vegetable.example.com/010'); - - $this->results = $this->getQuery() - ->exists('text.format') - ->notExists('date') - ->sort('id') - ->execute(); - $this->assertResult('http://vegetable.example.com/002'); - - // OR conjunctions. - $this->results = $this->getQuery('OR') - ->exists('text.format') - ->condition('reference', $this->entities[6]->id()) - ->sort('id') - ->execute(); - $this->assertResult('http://vegetable.example.com/001', 'http://vegetable.example.com/002', 'http://vegetable.example.com/004', 'http://vegetable.example.com/010'); - } - - /** - * Tests more complex functionality. - */ - public function testNestedConditionGroups() { - $query = $this->getQuery('OR'); - - // Entity 001 should match. - $condition = $query->andConditionGroup(); - $condition->exists('text.format'); - $condition->condition('text.value', 'test', 'CONTAINS'); - $condition->condition('text.format', ['plain_text', 'filtered_html'], 'IN'); - $query->condition($condition); - - // Entity 002 should match. - $condition = $query->orConditionGroup(); - $condition->condition('text', 'condition('text.value', '/html>', 'ENDS WITH'); - - // Entity 006 should match. - $subcondition = $query->andConditionGroup(); - $subcondition->condition('text_multi', 'test text multi 1'); - $subcondition->condition('text_multi', 'sample string 2', 'CONTAINS'); - $condition->condition($subcondition); - - // Entity 004 should match. - $subcondition = $query->andConditionGroup(); - $subcondition->condition('reference', $this->entities[4]->id()); - $subcondition->notExists('date'); - $condition->condition($subcondition); - $query->condition($condition); - - $this->results = $query->sort('id')->execute(); - $this->assertResult('http://vegetable.example.com/001', 'http://vegetable.example.com/002', 'http://vegetable.example.com/004', 'http://vegetable.example.com/006'); - } - - /** - * Tests operators '<', '>', '<=', '>=' for Ids. - * - * @dataProvider idStringComparisonDataProvider - */ - public function testIdStringComparison($value, $operator, $expected_results) { - $query = $this->getQuery(); - $query->condition('id', $value, $operator); - $this->results = $query->sort('id')->execute(); - $this->assertResult($expected_results); - } - - /** - * Data provider for testIdStringComparison test. - */ - public function idStringComparisonDataProvider() { - return [ - [ - 'http://fruit.example.com/002', - '<', - ['http://fruit.example.com/001'], - ], - [ - 'http://fruit.example.com/002', - '<=', - ['http://fruit.example.com/001', 'http://fruit.example.com/002'], - ], - [ - 'http://fruit.example.com/009', - '>', - // The vegetable bundle entities have an id starting with 'http://m' - // which is sorted after the 'http://d' so all entities of the - // vegetable bundled are also returned for the '>' and '>=' operators. - [ - 'http://fruit.example.com/010', - 'http://vegetable.example.com/001', - 'http://vegetable.example.com/002', - 'http://vegetable.example.com/003', - 'http://vegetable.example.com/004', - 'http://vegetable.example.com/005', - 'http://vegetable.example.com/006', - 'http://vegetable.example.com/007', - 'http://vegetable.example.com/008', - 'http://vegetable.example.com/009', - 'http://vegetable.example.com/010', - ], - ], - [ - 'http://fruit.example.com/009', - '>=', - [ - 'http://fruit.example.com/009', - 'http://fruit.example.com/010', - 'http://vegetable.example.com/001', - 'http://vegetable.example.com/002', - 'http://vegetable.example.com/003', - 'http://vegetable.example.com/004', - 'http://vegetable.example.com/005', - 'http://vegetable.example.com/006', - 'http://vegetable.example.com/007', - 'http://vegetable.example.com/008', - 'http://vegetable.example.com/009', - 'http://vegetable.example.com/010', - ], - ], - ]; - } - - /** - * Asserts that arrays are identical. - */ - protected function assertResult() { - $assert = []; - $expected = func_get_args(); - if ($expected && is_array($expected[0])) { - $expected = $expected[0]; - } - foreach ($expected as $binary) { - $assert[$binary] = strval($binary); - } - $this->assertSame($assert, $this->results); - } - - /** - * Provides entity values for the test's setup. - * - * @return array - * An array of entity values. - */ - protected function getTestEntityValues() { - $time = \Drupal::time()->getRequestTime(); - $return = []; - // Entity 001. - $return[] = [ - 'text' => [ - 'value' => 'test text 1', - 'format' => 'plain_text', - ], - 'text_multi' => [ - 'test text multi 1', - ], - 'date' => $time, - 'reference' => [ - $this->entities[4]->id(), - ], - ]; - - // Entity 002. - $return[] = [ - 'text' => [ - 'value' => '
Hello world!
', - 'format' => 'full_html', - ], - 'text_multi' => [ - 'test text multi 1', - ], - ]; - - // Entity 003. - $return[] = [ - 'text' => 'test text 1', - 'text_multi' => [ - 'test text multi 2', - ], - 'date' => $time, - 'reference' => [ - $this->entities[2]->id(), - $this->entities[3]->id(), - $this->entities[4]->id(), - ], - ]; - - // Entity 004. - $return[] = [ - 'reference' => [ - $this->entities[0]->id(), - $this->entities[1]->id(), - $this->entities[2]->id(), - $this->entities[3]->id(), - $this->entities[4]->id(), - $this->entities[5]->id(), - $this->entities[6]->id(), - $this->entities[7]->id(), - $this->entities[8]->id(), - $this->entities[9]->id(), - ], - ]; - - // Entity 005. - $return[] = [ - 'text' => 'sample string 2', - 'text_multi' => [ - 'test text multi 1', - 'test text multi 3', - 'test text multi 2', - ], - ]; - - // Entity 006. - $return[] = [ - 'text' => 'sample string 1', - 'text_multi' => [ - 'test text multi 1', - 'sample string 2', - ], - 'reference' => [ - $this->entities[8]->id(), - $this->entities[9]->id(), - ], - ]; - - // Entity 007. - $return[] = [ - 'text' => 'test text 1', - 'text_multi' => [ - 'test text multi 1', - ], - 'date' => $time, - 'reference' => [ - $this->entities[5]->id(), - ], - ]; - - // Entity 008. - $return[] = [ - 'text' => 'test text 1', - 'text_multi' => [ - 'test text multi 1', - ], - 'date' => $time, - 'reference' => [ - $this->entities[8]->id(), - ], - ]; - - // Entity 009. - $return[] = [ - 'text' => 'test text 1', - 'text_multi' => [ - 'test text multi 1', - ], - 'date' => $time, - 'reference' => [ - $this->entities[1]->id(), - ], - ]; - - // Entity 010. - $return[] = [ - 'text' => 'test text 1', - 'text_multi' => [ - 'test text multi 1', - ], - 'date' => $time, - 'reference' => [ - $this->entities[6]->id(), - ], - ]; - - return $return; - } - - /** - * Returns the SPARQL entity query. - * - * @param string $operator - * (optional) The logic operator ('AND' or 'OR'). Defaults to 'AND'. - * - * @return \Drupal\sparql_entity_storage\Entity\Query\Sparql\SparqlQueryInterface - * The SPARQL entity query. - */ - protected function getQuery(string $operator = 'AND'): SparqlQueryInterface { - return $this->container->get('entity_type.manager') - ->getStorage('sparql_test') - ->getQuery($operator); - } - -} diff --git a/sparql_entity_storage/tests/src/Kernel/SparqlGraphTest.php b/sparql_entity_storage/tests/src/Kernel/SparqlGraphTest.php deleted file mode 100644 index b0fd26e0..00000000 --- a/sparql_entity_storage/tests/src/Kernel/SparqlGraphTest.php +++ /dev/null @@ -1,168 +0,0 @@ -container->get('entity_type.manager'); - /** @var \Drupal\sparql_entity_storage\SparqlEntityStorage $storage */ - $storage = $manager->getStorage('sparql_test'); - - // Create a 2nd graph. - $this->createGraph('foo', 10); - - $id = 'http://example.com/apple'; - /** @var \Drupal\sparql_test\Entity\SparqlTest $apple */ - $apple = $storage->create([ - 'id' => $id, - 'type' => 'fruit', - 'title' => 'Apple in foo graph', - 'graph' => 'foo', - ]); - $apple->save(); - - // Check that, by default, the entity exists only in the foo graph. - $apple = $storage->load($id); - $this->assertEquals('foo', $apple->graph->target_id); - $this->assertFalse($storage->hasGraph($apple, 'default')); - - // Check cascading over the graph candidate list. - $apple = $storage->load($id, ['default', 'foo']); - $this->assertEquals('foo', $apple->graph->target_id); - - // Set the 'default' graph. - $apple - ->set('graph', 'default') - ->set('title', 'Apple') - ->save(); - - // Check that, by default, the 'default' graph is loaded. - $apple = $storage->load($id); - $this->assertEquals('default', $apple->graph->target_id); - $this->assertTrue($storage->hasGraph($apple, 'default')); - $this->assertTrue($storage->hasGraph($apple, 'foo')); - - // Create a new 'arbitrary' graph and add it to the mapping. - $this->createGraph('arbitrary'); - - $apple - ->set('graph', 'arbitrary') - ->set('title', 'Apple in an arbitrary graph') - ->save(); - - $apple = $storage->load($id, ['arbitrary']); - $this->assertEquals('arbitrary', $apple->graph->target_id); - $this->assertEquals('Apple in an arbitrary graph', $apple->label()); - - // Delete the foo version. - $storage->deleteFromGraph([$apple], 'foo'); - $this->assertNull($storage->load($id, ['foo'])); - $this->assertNotNull($storage->load($id, ['default'])); - $this->assertNotNull($storage->load($id, ['arbitrary'])); - - // Create a graph that is excluded from the default graphs list. - // @see \Drupal\sparql_graph_test\DefaultGraphsSubscriber - $this->createGraph('non_default_graph'); - - $apple - ->set('graph', 'non_default_graph') - ->set('title', 'Apple in non_default_graph graph') - ->save(); - - // Delete the entity from 'default' and 'arbitrary'. - $storage->deleteFromGraph([$apple], 'default'); - $storage->deleteFromGraph([$apple], 'arbitrary'); - - // Check that the entity is loaded from an explicitly passed graph even it's - // a non-default graph. - $this->assertNotNull($storage->load($id, ['non_default_graph'])); - // Same for entity query. - $ids = $this->getQuery() - ->graphs(['non_default_graph']) - ->condition('id', $id) - ->execute(); - $this->assertCount(1, $ids); - $this->assertEquals($id, reset($ids)); - - // Even the entity exists in 'non_default_graph' is not returned because - // this graph is not a default graph. - $this->assertNull($storage->load($id)); - // Same for entity query. - $ids = $this->getQuery()->condition('id', $id)->execute(); - $this->assertEmpty($ids); - - // Delete the entity. - $apple->delete(); - // All versions are gone. - $this->assertNull($storage->load($id, ['default'])); - $this->assertNull($storage->load($id, ['foo'])); - $this->assertNull($storage->load($id, ['arbitrary'])); - $this->assertNull($storage->load($id, ['non_default_graph'])); - - // Check that the default graph method doesn't return a disabled or an - // invalid graph. - $this->createGraph('disabled_graph', 20, FALSE); - $default_graphs = $this->container->get('sparql.graph_handler') - ->getEntityTypeDefaultGraphIds('sparql_test'); - $this->assertNotContains('disabled_graph', $default_graphs); - $this->assertNotContains('non_existing_graph', $default_graphs); - - // Try to request the entity from a non-existing graph. - $this->setExpectedException(\InvalidArgumentException::class, "Graph 'invalid graph' doesn't exist for entity type 'sparql_test'."); - $storage->load($id, ['invalid graph', 'default', 'foo']); - } - - /** - * Creates a new graph entity and adds it to the 'fruit' mapping. - * - * @param string $id - * The graph ID. - * @param int $weight - * (optional) The graph weight. Defaults to 0. - * @param bool $status - * (optional) If the graph is enabled. Defaults to TRUE. - */ - protected function createGraph(string $id, int $weight = 0, bool $status = TRUE): void { - SparqlGraph::create(['id' => $id, 'label' => ucwords($id)]) - ->setStatus($status) - ->setWeight($weight) - ->save(); - SparqlMapping::loadByName('sparql_test', 'fruit') - ->addGraphs([$id => "http://example.com/fruit/graph/$id"]) - ->save(); - } - - /** - * Returns the entity query. - * - * @return \Drupal\sparql_entity_storage\Entity\Query\Sparql\SparqlQueryInterface - * The SPARQL entity query. - */ - protected function getQuery() { - return $this->container - ->get('entity_type.manager') - ->getStorage('sparql_test') - ->getQuery(); - } - -} diff --git a/sparql_entity_storage/tests/src/Kernel/SparqlKernelTestBase.php b/sparql_entity_storage/tests/src/Kernel/SparqlKernelTestBase.php deleted file mode 100644 index a06159fa..00000000 --- a/sparql_entity_storage/tests/src/Kernel/SparqlKernelTestBase.php +++ /dev/null @@ -1,48 +0,0 @@ -setUpSparql(); - $this->installConfig(['sparql_entity_storage', 'sparql_test']); - } - - /** - * {@inheritdoc} - */ - public function tearDown(): void { - // Delete 'sparql_test' entities that might have been created during tests. - $storage = $this->container->get('entity_type.manager')->getStorage('sparql_test'); - $ids = $storage->getQuery()->execute(); - $storage->delete($storage->loadMultiple($ids)); - parent::tearDown(); - } - -} diff --git a/sparql_entity_storage/tests/src/Kernel/SparqlSerializerTest.php b/sparql_entity_storage/tests/src/Kernel/SparqlSerializerTest.php deleted file mode 100644 index bce5d8ab..00000000 --- a/sparql_entity_storage/tests/src/Kernel/SparqlSerializerTest.php +++ /dev/null @@ -1,69 +0,0 @@ -setUpSparql(); - $this->installConfig(['sparql_entity_storage', 'sparql_entity_serializer_test']); - } - - /** - * Tests content negotiation. - */ - public function testContentNegotiation() { - $entity = Rdf::create([ - 'rid' => 'fruit', - 'id' => 'http://example.com/apple', - 'label' => 'Apple', - ]); - $entity->save(); - - $encoders = $this->container->getParameter('sparql_entity.encoders'); - $serializer = $this->container->get('sparql_entity.serializer'); - foreach ($encoders as $format => $content_type) { - $serialized = trim($serializer->serializeEntity($entity, $format)); - $expected = trim(file_get_contents(__DIR__ . "/../../fixtures/content-negotiation/rdf_entity/$format")); - $this->assertEquals($expected, $serialized); - } - } - - /** - * {@inheritdoc} - */ - public function tearDown() { - Rdf::load('http://example.com/apple')->delete(); - parent::tearDown(); - } - -} diff --git a/sparql_entity_storage/tests/src/Traits/SparqlConnectionTrait.php b/sparql_entity_storage/tests/src/Traits/SparqlConnectionTrait.php deleted file mode 100644 index 1c3b9676..00000000 --- a/sparql_entity_storage/tests/src/Traits/SparqlConnectionTrait.php +++ /dev/null @@ -1,88 +0,0 @@ -resetParameters(TRUE); - $client->setUri("http://{$this->sparqlConnectionInfo['host']}:{$this->sparqlConnectionInfo['port']}/"); - $client->setMethod('GET'); - $response = $client->request(); - $server_header = $response->getHeader('Server'); - if (strpos($server_header, "Virtuoso/06") !== FALSE) { - throw new \Exception('Not running on Virtuoso 6.'); - } - } - - /** - * Setup the db connection to the triple store. - * - * @throws \LogicException - * When SIMPLETEST_SPARQL_DB is not set. - */ - protected function setUpSparql() { - // If the test is run with argument db url then use it. - // export SIMPLETEST_SPARQL_DB='sparql://127.0.0.1:8890/'. - $db_url = getenv('SIMPLETEST_SPARQL_DB'); - if (empty($db_url)) { - throw new \LogicException('No Sparql connection was defined. Set the SIMPLETEST_SPARQL_DB environment variable.'); - } - - $this->sparqlConnectionInfo = Database::convertDbUrlToConnectionInfo($db_url, dirname(dirname(__FILE__))); - $this->sparqlConnectionInfo['namespace'] = 'Drupal\\Driver\\Database\\sparql'; - - // Do not allow Virtuoso 6. - $this->detectVirtuoso6(); - - Database::addConnectionInfo('sparql_default', 'default', $this->sparqlConnectionInfo); - - $this->sparql = Database::getConnection('default', 'sparql_default'); - } - - /** - * {@inheritdoc} - */ - protected function writeSettings(array $settings) { - // The BrowserTestBase is creating a new copy of the settings.php file to - // the test directory so the SPARQL entry needs to be inserted into the new - // configuration. - $key = 'sparql_default'; - $target = 'default'; - - $settings['databases'][$key][$target] = (object) [ - 'value' => Database::getConnectionInfo($key)[$target], - 'required' => TRUE, - ]; - - parent::writeSettings($settings); - } - -}