Skip to content

Commit

Permalink
Enforce subjects and predicates are also strings
Browse files Browse the repository at this point in the history
  • Loading branch information
lordtatty committed Jul 22, 2015
1 parent 6a96dcb commit 460f258
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 20 deletions.
31 changes: 18 additions & 13 deletions src/classes/ExtendedGraph.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -215,7 +220,7 @@ protected function isValidLiteralValue($value){
*
* @return bool
*/
protected function isValidResourceValue($value){
protected function isValidTripleValue($value){
if(!is_string($value)){
return false;
}
Expand Down
23 changes: 16 additions & 7 deletions src/mongo/MongoGraph.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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',
Expand All @@ -185,7 +194,7 @@ private function toGraphValueObject($mongoValueObject)
$valueTypeLabel = 'literal';
}
else{
if(!$this->isValidResourceValue($value)){
if(!$this->isValidTripleValue($value)){
continue;
}
$valueTypeLabel = 'uri';
Expand Down
100 changes: 100 additions & 0 deletions test/unit/ExtendedGraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
58 changes: 58 additions & 0 deletions test/unit/mongo/MongoGraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 460f258

Please sign in to comment.