if (isset($changeData['originalCBDs'])) { foreach ($changeData['originalCBDs'] as $change) { if (is_array($change) && isset($change[_ID_KEY])) { $from->add_tripod_array($change); } } } if (isset($changeData['newCBDs'])) { foreach ($changeData['newCBDs'] as $change) { if (is_array($change) && isset($change[_ID_KEY])) { $to->add_tripod_array($change); } } } try { $tripod->saveChanges($from, $to); $app->response()->setStatus(202); } catch (Exception $e) { error_log("POST failed: " . $e->getMessage()); $app->response()->setStatus(400); } } }); }); }); }); }); /** * @param string $format * @return string */
public function testSavingToAPreviouslyEmptyJoinUpdatesView() { // create a tripod with views sync $tripod = new \Tripod\Mongo\Driver("CBD_testing", "tripod_php_testing", array("defaultContext" => "http://talisaspire.com/", "async" => array(OP_VIEWS => false))); // should be no triples with "http://basedata.com/b/sequence123" as subject in existing view $view = $tripod->getViewForResource("http://basedata.com/b/docWithEmptySeq123", "v_doc_with_seqeunce"); $this->assertTrue($view->has_triples_about("http://basedata.com/b/docWithEmptySeq123")); $this->assertFalse($view->has_triples_about("http://schemas.talis.com/2005/user/schema#xyz")); $newGraph = new \Tripod\ExtendedGraph(); $newGraph->add_literal_triple("http://schemas.talis.com/2005/user/schema#xyz", "http://rdfs.org/sioc/spec/name", "Some name"); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $newGraph); // should be triples with "http://basedata.com/b/sequence123" as subject in new view $view = $tripod->getViewForResource("http://basedata.com/b/docWithEmptySeq123", "v_doc_with_seqeunce"); $this->assertTrue($view->has_triples_about("http://basedata.com/b/docWithEmptySeq123")); $this->assertTrue($view->has_triples_about("http://schemas.talis.com/2005/user/schema#xyz")); }
public function testWriteToUnconfiguredCollectionThrowsException() { // Exception: testing:SOME_COLLECTION is not referenced within config, so cannot be written to $this->setExpectedException('\\Tripod\\Exceptions\\ConfigException', 'Collection name \'SOME_COLLECTION\' not in configuration'); $tripod = new \Tripod\Mongo\Driver("SOME_COLLECTION", "tripod_php_testing"); $tripod->saveChanges(new \Tripod\ExtendedGraph(), new \Tripod\ExtendedGraph(), 'http://talisaspire.com/'); }
public function testDeleteResourceCreatesImpactedSubjects() { $uri = 'http://example.com/users/' . uniqid(); $labeller = new \Tripod\Mongo\Labeller(); $uriAlias = $labeller->uri_to_alias($uri); $graph = new \Tripod\ExtendedGraph(); $graph->add_resource_triple($uri, RDF_TYPE, $labeller->qname_to_uri('spec:User')); $graph->add_literal_triple($uri, $labeller->qname_to_uri('foaf:firstName'), 'Anne'); $graph->add_literal_triple($uri, $labeller->qname_to_uri('foaf:surname'), 'Onymous'); $uri2 = 'http://example.com/users/' . uniqid(); $uriAlias2 = $labeller->uri_to_alias($uri2); $graph2 = new \Tripod\ExtendedGraph(); $graph2->add_resource_triple($uri2, RDF_TYPE, $labeller->qname_to_uri('spec:User')); $graph2->add_literal_triple($uri2, $labeller->qname_to_uri('foaf:firstName'), 'Ann'); $graph2->add_literal_triple($uri2, $labeller->qname_to_uri('foaf:surname'), 'O\'ther'); // Save the graphs and ensure that table rows are generated $tripod = new \Tripod\Mongo\Driver($this->defaultPodName, $this->defaultStoreName, array('defaultContext' => $this->defaultContext, OP_ASYNC => array(OP_VIEWS => false, OP_TABLES => false, OP_SEARCH => false))); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $graph); $tableRows = $tripod->getTableRows('t_users', array(_ID_KEY . '.' . _ID_RESOURCE => $uriAlias, _ID_KEY . '.' . _ID_CONTEXT => $this->defaultContext)); $this->assertEquals(1, $tableRows['head']['count']); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $graph2); $tableRows = $tripod->getTableRows('t_users', array(_ID_KEY . '.' . _ID_RESOURCE => $uriAlias2, _ID_KEY . '.' . _ID_CONTEXT => $this->defaultContext)); $this->assertEquals(1, $tableRows['head']['count']); /** @var \Tripod\Mongo\Driver|PHPUnit_Framework_MockObject_MockObject $mockTripod */ $mockTripod = $this->getMockBuilder('\\Tripod\\Mongo\\Driver')->setMethods(array('getDataUpdater'))->setConstructorArgs(array($this->defaultPodName, $this->defaultStoreName, array('defaultContext' => $this->defaultContext, OP_ASYNC => array(OP_VIEWS => false, OP_TABLES => false, OP_SEARCH => false))))->getMock(); /** @var \Tripod\Mongo\Updates|PHPUnit_Framework_MockObject_MockObject $mockUpdates */ $mockTripodUpdates = $this->getMockBuilder('\\Tripod\\Mongo\\Updates')->setConstructorArgs(array($mockTripod, array('defaultContext' => $this->defaultContext, OP_ASYNC => array(OP_VIEWS => false, OP_TABLES => false, OP_SEARCH => false))))->setMethods(array('processSyncOperations'))->getMock(); $mockTripod->expects($this->once())->method('getDataUpdater')->will($this->returnValue($mockTripodUpdates)); $expectedSubjectsAndPredicatesOfChange = array($uriAlias => array('rdf:type', 'foaf:firstName', 'foaf:surname'), $uriAlias2 => array('rdf:type', 'foaf:firstName', 'foaf:surname')); $mockTripodUpdates->expects($this->once())->method('processSyncOperations')->with($expectedSubjectsAndPredicatesOfChange, $this->defaultContext); $graph->add_graph($graph2); // Delete both user resources $mockTripod->saveChanges($graph, new \Tripod\ExtendedGraph()); $deletedGraph = $mockTripod->describeResources(array($uri, $uri2)); $this->assertTrue($deletedGraph->is_empty()); // Manually walk through the tables operation /** @var \Tripod\Mongo\Composites\Tables $tables */ $tables = $mockTripod->getComposite(OP_TABLES); $expectedImpactedSubjects = array(new \Tripod\Mongo\ImpactedSubject(array(_ID_RESOURCE => $uriAlias, _ID_CONTEXT => $this->defaultContext), OP_TABLES, $this->defaultStoreName, $this->defaultPodName, array('t_users')), new \Tripod\Mongo\ImpactedSubject(array(_ID_RESOURCE => $uriAlias2, _ID_CONTEXT => $this->defaultContext), OP_TABLES, $this->defaultStoreName, $this->defaultPodName, array('t_users'))); $this->assertEquals($expectedImpactedSubjects, $tables->getImpactedSubjects($expectedSubjectsAndPredicatesOfChange, $this->defaultContext)); foreach ($expectedImpactedSubjects as $subject) { $tables->update($subject); } $tableRows = $tripod->getTableRows('t_users', array(_ID_KEY . '.' . _ID_RESOURCE => $uriAlias, _ID_KEY . '.' . _ID_CONTEXT => $this->defaultContext)); $this->assertEquals(0, $tableRows['head']['count']); $tableRows = $tripod->getTableRows('t_users', array(_ID_KEY . '.' . _ID_RESOURCE => $uriAlias2, _ID_KEY . '.' . _ID_CONTEXT => $this->defaultContext)); $this->assertEquals(0, $tableRows['head']['count']); }
public function testDeleteResourceCreatesImpactedSubjects() { $uri = 'http://example.com/resources/' . uniqid(); $labeller = new Tripod\Mongo\Labeller(); $uriAlias = $labeller->uri_to_alias($uri); $creatorUri = 'http://example.com/identities/oscar-wilde'; $creatorUriAlias = $labeller->uri_to_alias($creatorUri); $graph = new \Tripod\ExtendedGraph(); $graph->add_resource_triple($uri, RDF_TYPE, $labeller->qname_to_uri('acorn:Resource')); $graph->add_resource_triple($uri, RDF_TYPE, $labeller->qname_to_uri('bibo:Book')); $graph->add_literal_triple($uri, $labeller->qname_to_uri('dct:title'), 'The Importance of Being Earnest'); $graph->add_literal_triple($uri, $labeller->qname_to_uri('dct:subject'), 'Plays -- Satire'); $graph->add_resource_triple($uri, $labeller->qname_to_uri('dct:creator'), $creatorUri); $uri2 = 'http://example.com/resources/' . uniqid(); $uriAlias2 = $labeller->uri_to_alias($uri2); $graph2 = new \Tripod\ExtendedGraph(); $graph2->add_resource_triple($uri2, RDF_TYPE, $labeller->qname_to_uri('acorn:Resource')); $graph2->add_resource_triple($uri2, RDF_TYPE, $labeller->qname_to_uri('bibo:Book')); $graph2->add_literal_triple($uri2, $labeller->qname_to_uri('dct:title'), 'The Picture of Dorian Gray'); $graph2->add_literal_triple($uri2, $labeller->qname_to_uri('dct:subject'), 'Portraits -- Fiction'); $graph2->add_resource_triple($uri2, $labeller->qname_to_uri('dct:creator'), $creatorUri); $graph3 = new \Tripod\ExtendedGraph(); $graph3->add_resource_triple($creatorUri, RDF_TYPE, $labeller->qname_to_uri('foaf:Person')); $graph3->add_literal_triple($creatorUri, $labeller->qname_to_uri('foaf:name'), 'Oscar Wilde'); // Save the graphs and ensure that table rows are generated $tripod = new \Tripod\Mongo\Driver($this->defaultPodName, $this->defaultStoreName, array('defaultContext' => $this->defaultContext, OP_ASYNC => array(OP_VIEWS => false, OP_TABLES => false, OP_SEARCH => false))); // Save the author graph first so the joins work $tripod->saveChanges(new \Tripod\ExtendedGraph(), $graph3); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $graph); $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForSearchDocument($this->defaultStoreName, 'i_search_resource'); $query = array(_ID_KEY => array(_ID_RESOURCE => $uriAlias, _ID_CONTEXT => $this->defaultContext, _ID_TYPE => 'i_search_resource')); $this->assertEquals(1, $collection->count($query)); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $graph2); $query[_ID_KEY][_ID_RESOURCE] = $uriAlias2; $this->assertEquals(1, $collection->count($query)); $impactQuery = array(_ID_KEY . '.' . _ID_TYPE => 'i_search_resource', '_impactIndex' => array(_ID_RESOURCE => $creatorUriAlias, _ID_CONTEXT => $this->defaultContext), 'result.author' => 'Oscar Wilde'); $this->assertEquals(2, $collection->count($impactQuery)); /** @var \Tripod\Mongo\Driver|PHPUnit_Framework_MockObject_MockObject $mockTripod */ $mockTripod = $this->getMockBuilder('\\Tripod\\Mongo\\Driver')->setMethods(array('getDataUpdater'))->setConstructorArgs(array($this->defaultPodName, $this->defaultStoreName, array('defaultContext' => $this->defaultContext, OP_ASYNC => array(OP_VIEWS => false, OP_TABLES => false, OP_SEARCH => false))))->getMock(); $mockTripodUpdates = $this->getMockBuilder('\\Tripod\\Mongo\\Updates')->setConstructorArgs(array($mockTripod, array('defaultContext' => $this->defaultContext, OP_ASYNC => array(OP_VIEWS => false, OP_TABLES => false, OP_SEARCH => false))))->setMethods(array('processSyncOperations'))->getMock(); $mockTripod->expects($this->once())->method('getDataUpdater')->will($this->returnValue($mockTripodUpdates)); $expectedSubjectsAndPredicatesOfChange = array($creatorUriAlias => array('rdf:type', 'foaf:name')); $mockTripodUpdates->expects($this->once())->method('processSyncOperations')->with($expectedSubjectsAndPredicatesOfChange, $this->defaultContext); // Delete creator resource $mockTripod->saveChanges($graph3, new \Tripod\ExtendedGraph()); $deletedGraph = $mockTripod->describeResource($creatorUri); $this->assertTrue($deletedGraph->is_empty()); // Manually walk through the tables operation /** @var \Tripod\Mongo\Composites\SearchIndexer $search */ $search = $mockTripod->getComposite(OP_SEARCH); $expectedImpactedSubjects = array(new \Tripod\Mongo\ImpactedSubject(array(_ID_RESOURCE => $uriAlias, _ID_CONTEXT => $this->defaultContext), OP_SEARCH, $this->defaultStoreName, $this->defaultPodName, array('i_search_resource')), new \Tripod\Mongo\ImpactedSubject(array(_ID_RESOURCE => $uriAlias2, _ID_CONTEXT => $this->defaultContext), OP_SEARCH, $this->defaultStoreName, $this->defaultPodName, array('i_search_resource'))); $this->assertEquals($expectedImpactedSubjects, $search->getImpactedSubjects($expectedSubjectsAndPredicatesOfChange, $this->defaultContext)); foreach ($expectedImpactedSubjects as $subject) { $search->update($subject); } $query = array(_ID_KEY => array(_ID_RESOURCE => $uriAlias, _ID_CONTEXT => $this->defaultContext, _ID_TYPE => 'i_search_resource')); $this->assertEquals(1, $collection->count($query)); $query[_ID_KEY][_ID_RESOURCE] = $uriAlias2; $this->assertEquals(1, $collection->count($query)); // Deleted resource will still be impact indexes because join still exists $impactQuery = array(_ID_KEY . '.' . _ID_TYPE => 'i_search_resource', '_impactIndex' => array(_ID_RESOURCE => $creatorUriAlias, _ID_CONTEXT => $this->defaultContext)); $this->assertEquals(2, $collection->count($impactQuery)); // But the document should have been regenerated without the value $impactQuery['result.author'] = 'Oscar Wilde'; $this->assertEquals(0, $collection->count($impactQuery)); }