diff --git a/src/classes/ExtendedGraph.class.php b/src/classes/ExtendedGraph.class.php index 22779539..46a967da 100644 --- a/src/classes/ExtendedGraph.class.php +++ b/src/classes/ExtendedGraph.class.php @@ -141,7 +141,7 @@ public function make_resource_array($resource) { * @return boolean true if the triple was new, false if it already existed in the graph */ public function add_resource_triple($s, $p, $o) { - if($this->isValidResourceValue($o)) { + if($this->isValidTripleValue($s) && $this->isValidTripleValue($p) && $this->isValidTripleValue($o)) { return $this->_add_triple($s, $p, array('type' => strpos($o, '_:') === 0 ? 'bnode' : 'uri', 'value' => $o)); } return false; @@ -157,7 +157,7 @@ public function add_resource_triple($s, $p, $o) { * @return boolean true if the triple was new, false if it already existed in the graph */ public function add_literal_triple($s, $p, $o, $lang = null, $dt = null) { - if($this->isValidLiteralValue($o)) { + if($this->isValidTripleValue($s) && $this->isValidTripleValue($p) && $this->isValidLiteralValue($o)) { $o_info = array('type' => 'literal', 'value' => $o); if ($lang != null) { $o_info['lang'] = $lang; @@ -177,17 +177,22 @@ public function add_literal_triple($s, $p, $o, $lang = null, $dt = null) { * @return bool */ private function _add_triple($s, $p, Array $o_info) { - if (!isset($this->_index[$s])) { - $this->_index[$s] = array(); - $this->_index[$s][$p] = array($o_info); - return true; - } elseif (!isset($this->_index[$s][$p])) { - $this->_index[$s][$p] = array($o_info); - return true; - } else { - if (!in_array($o_info, $this->_index[$s][$p])) { - $this->_index[$s][$p][] = $o_info; + // The value $o should already have been validated by this point + // It's validation differs depending on whether it is a literal or resource + // So just check the subject and predicate here... + if($this->isValidTripleValue($s) && $this->isValidTripleValue($p)) { + if (!isset($this->_index[$s])) { + $this->_index[$s] = array(); + $this->_index[$s][$p] = array($o_info); + return true; + } elseif (!isset($this->_index[$s][$p])) { + $this->_index[$s][$p] = array($o_info); return true; + } else { + if (!in_array($o_info, $this->_index[$s][$p])) { + $this->_index[$s][$p][] = $o_info; + return true; + } } } return false; @@ -215,7 +220,7 @@ protected function isValidLiteralValue($value){ * * @return bool */ - protected function isValidResourceValue($value){ + protected function isValidTripleValue($value){ if(!is_string($value)){ return false; } diff --git a/src/mongo/MongoGraph.class.php b/src/mongo/MongoGraph.class.php index 1a5c319e..ead47088 100644 --- a/src/mongo/MongoGraph.class.php +++ b/src/mongo/MongoGraph.class.php @@ -128,11 +128,20 @@ private function add_tarray_to_index($tarray) { if($key[0] != '_') { - $predicate = $this->qname_to_uri($key); - $graphValueObject = $this->toGraphValueObject($value); - // Only add if valid values have been found - if($graphValueObject !== false) { - $predObjects[$predicate] = $graphValueObject; + // Make sure the predicate is valid + if($this->isValidTripleValue($key)){ + $predicate = $this->qname_to_uri($key); + $graphValueObject = $this->toGraphValueObject($value); + // Only add if valid values have been found + if ($graphValueObject !== false) { + $predObjects[$predicate] = $graphValueObject; + } + } + } + else if($key == "_id"){ + // If the subject is not valid then return + if(!isset($value['r']) || !$this->isValidTripleValue($value['r'])){ + return; } } } @@ -163,7 +172,7 @@ private function toGraphValueObject($mongoValueObject) else if (array_key_exists(VALUE_URI,$mongoValueObject)) { // only allow valid values - if($this->isValidResourceValue($mongoValueObject[VALUE_URI])) { + if($this->isValidTripleValue($mongoValueObject[VALUE_URI])) { // single value uri $simpleGraphValueObject[] = array( 'type' => 'uri', @@ -185,7 +194,7 @@ private function toGraphValueObject($mongoValueObject) $valueTypeLabel = 'literal'; } else{ - if(!$this->isValidResourceValue($value)){ + if(!$this->isValidTripleValue($value)){ continue; } $valueTypeLabel = 'uri'; diff --git a/test/unit/ExtendedGraphTest.php b/test/unit/ExtendedGraphTest.php index 500d9e83..41134376 100644 --- a/test/unit/ExtendedGraphTest.php +++ b/test/unit/ExtendedGraphTest.php @@ -64,6 +64,56 @@ public function addInvalidValueToLiteralResultsInNoTriple_Provider(){ ); } + /** + * @dataProvider addInvalidSubjectToLiteralResultsInNoTriple_Provider + */ + public function testAddInvalidSubjectToLiteralResultsInNoTriple($value) + { + $graph = new ExtendedGraph(); + + $addResult = $graph->add_resource_triple($value, 'http://some/predicate', 'http://someplace.com'); + $this->assertFalse($addResult, 'The triple should not have been added for this value'); + + $graph->get_triple_count(); + $this->assertEquals(0, $graph->get_triple_count(), 'The triple should not have been added for this value'); + } + public function addInvalidSubjectToLiteralResultsInNoTriple_Provider(){ + return array( + array(1), + array(1.2), + array(true), + array(array()), + array(null), + array(new stdClass()), + array(function(){}) + ); + } + + /** + * @dataProvider addInvalidSubjectToLiteralResultsInNoTriple_Provider + */ + public function testAddInvalidPredicateToLiteralResultsInNoTriple($value) + { + $graph = new ExtendedGraph(); + + $addResult = $graph->add_resource_triple('http://some/subject/1', $value, 'http://someplace.com'); + $this->assertFalse($addResult, 'The triple should not have been added for this value'); + + $graph->get_triple_count(); + $this->assertEquals(0, $graph->get_triple_count(), 'The triple should not have been added for this value'); + } + public function addInvalidPredicateToLiteralResultsInNoTriple_Provider(){ + return array( + array(1), + array(1.2), + array(true), + array(array()), + array(null), + array(new stdClass()), + array(function(){}) + ); + } + public function testAddValidValueToResourceResultsInTriple() { $value = 'A String'; @@ -101,6 +151,56 @@ public function addInvalidValueToResourceResultsInNoTriple_Provider(){ ); } + /** + * @dataProvider addInvalidSubjectToResourceResultsInNoTriple_Provider + */ + public function testAddInvalidSubjectToResourceResultsInNoTriple($value) + { + $graph = new ExtendedGraph(); + + $addResult = $graph->add_resource_triple($value, 'http://some/predicate', 'http://someplace.com'); + $this->assertFalse($addResult, 'The triple should not have been added for this value'); + + $graph->get_triple_count(); + $this->assertEquals(0, $graph->get_triple_count(), 'The triple should not have been added for this value'); + } + public function addInvalidSubjectToResourceResultsInNoTriple_Provider(){ + return array( + array(1), + array(1.2), + array(true), + array(array()), + array(null), + array(new stdClass()), + array(function(){}) + ); + } + + /** + * @dataProvider addInvalidSubjectToLiteralResultsInNoTriple_Provider + */ + public function testAddInvalidPredicateToResourceResultsInNoTriple($value) + { + $graph = new ExtendedGraph(); + + $addResult = $graph->add_resource_triple('http://some/subject/1', $value, 'http://someplace.com'); + $this->assertFalse($addResult, 'The triple should not have been added for this value'); + + $graph->get_triple_count(); + $this->assertEquals(0, $graph->get_triple_count(), 'The triple should not have been added for this value'); + } + public function addInvalidPredicateToResourceResultsInNoTriple_Provider(){ + return array( + array(1), + array(1.2), + array(true), + array(array()), + array(null), + array(new stdClass()), + array(function(){}) + ); + } + public function testRemoveProperties() { $graph = new ExtendedGraph(); diff --git a/test/unit/mongo/MongoGraphTest.php b/test/unit/mongo/MongoGraphTest.php index 7534baa0..304027b7 100644 --- a/test/unit/mongo/MongoGraphTest.php +++ b/test/unit/mongo/MongoGraphTest.php @@ -171,6 +171,64 @@ public function addTripodArrayContainingInvalidLiteralValues_Provider(){ ); } + /** + * @dataProvider addTripodArrayContainingInvalidPredicates_Provider + */ + public function testAddTripodArrayContainingInvalidPredicates($value) + { + $doc = array( + "_id"=>array("r"=>"http://talisaspire.com/works/4d101f63c10a6-2", "c"=>"http://talisaspire.com/works/4d101f63c10a6-2"), + "_version"=>0, + "rdf:type"=>array( + array("l"=>"a Value"), + ), + "bibo:isbn13"=>array("l"=>"9211234567890"), + $value=>array("l"=>"9211234567890") + ); + + $expected = new \Tripod\Mongo\MongoGraph(); + $expected->add_literal_triple("http://talisaspire.com/works/4d101f63c10a6-2", $expected->qname_to_uri("bibo:isbn13"),"9211234567890"); + $expected->add_literal_triple("http://talisaspire.com/works/4d101f63c10a6-2", $expected->qname_to_uri("rdf:type"),"a Value"); + + $g = new \Tripod\Mongo\MongoGraph(); + $g->add_tripod_array($doc); + + $this->assertEquals($expected, $g); + } + public function addTripodArrayContainingInvalidPredicates_Provider(){ + return array( + array(1), + array(1.2), + array(true), + ); + } + + /** + * @dataProvider addTripodArrayContainingInvalidSubject_Provider + */ + public function testAddTripodArrayContainingInvalidSubject($value) + { + $doc = array( + "_id"=>array("r"=>$value, "c"=>"http://talisaspire.com/works/4d101f63c10a6-2"), + "_version"=>0, + "rdf:type"=>array( + array("l"=>"a Value"), + ), + "bibo:isbn13"=>array("l"=>"9211234567890"), + ); + + $g = new \Tripod\Mongo\MongoGraph(); + $g->add_tripod_array($doc); + $this->assertEquals(0, $g->get_triple_count()); + } + public function addTripodArrayContainingInvalidSubject_Provider(){ + return array( + array(1), + array(1.2), + array(true), + ); + } + public function testAddTripodArrayContainingValidResourceValues() {