'Object1', ), $this->asContext, 'name', 'Object1', ), array( (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams', ), 'name' => 'Object2', ), $this->asContext, 'name', 'Object2', ), array( (object) array( 'https://www.w3.org/ns/activitystreams#name' => 'Object1', ), $this->asContext, 'https://www.w3.org/ns/activitystreams#name', 'Object1', ), array( (object) array( 'https://www.w3.org/ns/activitystreams#name' => 'Object1', ), $this->asContext, 'foo', null, PropertyNotDefinedException::class, ), array( (object) array( 'https://www.w3.org/ns/activitystreams#name' => 'Object1', ), (object) array( 'as' => 'https://www.w3.org/ns/activitystreams#' ), 'as:name', 'Object1', ), array( (object) array( 'https://www.w3.org/ns/activitystreams#subject' => array( 'https://example.org/item/1', 'https://example.org/item/2', ), ), $this->asContext, 'subject', 'https://example.org/item/1', ), ); } /** * @dataProvider provideForBasicGetProperty */ public function testBasicGetProperty( $inputObj, $context, $propertyName, $expectedValue, $expectedException = null ) { $node = $this->makeJsonLdNode( $inputObj, $context ); if ( $expectedException ) { $this->setExpectedException( $expectedException ); } $propertyValue = $node->get( $propertyName ); $this->assertEquals( $expectedValue, $propertyValue ); } /** * @dataProvider provideForBasicGetProperty */ public function testBasicMagicGetProperty( $inputObj, $context, $propertyName, $expectedValue, $expectedException = null ) { $node = $this->makeJsonLdNode( $inputObj, $context ); if ( $expectedException ) { $this->setExpectedException( $expectedException ); } $propertyValue = $node->$propertyName; $this->assertEquals( $expectedValue, $propertyValue ); } /** * @dataProvider provideForBasicGetProperty */ public function testBasicArrayAccessProperty( $inputObj, $context, $propertyName, $expectedValue, $expectedException = null ) { $node = $this->makeJsonLdNode( $inputObj, $context ); if ( $expectedException ) { $this->setExpectedException( $expectedException ); } $propertyValue = $node[$propertyName]; $this->assertEquals( $expectedValue, $propertyValue ); } public function provideForBasicGetMany() { return array( array( (object) array( 'https://www.w3.org/ns/activitystreams#name' => 'Object1', ), $this->asContext, 'name', array( 'Object1' ), ), array( (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams', ), 'name' => 'Object2', ), $this->asContext, 'name', array( 'Object2' ), ), array( (object) array( 'https://www.w3.org/ns/activitystreams#name' => 'Object1', ), $this->asContext, 'https://www.w3.org/ns/activitystreams#name', array( 'Object1' ), ), array( (object) array( 'https://www.w3.org/ns/activitystreams#name' => 'Object1', ), $this->asContext, 'foo', null, PropertyNotDefinedException::class, ), array( (object) array( 'https://www.w3.org/ns/activitystreams#name' => 'Object1', ), (object) array( 'as' => 'https://www.w3.org/ns/activitystreams#' ), 'as:name', array( 'Object1' ), ), array( (object) array( 'https://www.w3.org/ns/activitystreams#subject' => array( 'https://example.org/item/1', 'https://example.org/item/2', ), ), $this->asContext, 'subject', array( 'https://example.org/item/1', 'https://example.org/item/2' ), ), ); } /** * @dataProvider provideForBasicGetMany */ public function testBasicGetMany( $inputObj, $context, $propertyName, $expectedValue, $expectedException = null ) { $node = $this->makeJsonLdNode( $inputObj, $context ); if ( $expectedException ) { $this->setExpectedException( $expectedException ); } $propertyValue = $node->getMany( $propertyName ); $this->assertEquals( $expectedValue, $propertyValue ); } public function provideForBasicSetProperty() { return array( array( new stdClass(), $this->asContext, 'name', 'NewName', 'https://www.w3.org/ns/activitystreams#name', 'NewName' ), array( (object) array( 'https://www.w3.org/ns/activitystreams#name' => 'OldName', ), $this->asContext, 'name', 'NewName', 'https://www.w3.org/ns/activitystreams#name', 'NewName' ), ); } /** * @dataProvider provideForBasicSetProperty */ public function testBasicSetProperty( $inputObj, $context, $propertyName, $newValue, $getPropertyName, $expectedValue, $expectedException = null ) { $node = $this->makeJsonLdNode( $inputObj, $context ); if ( $expectedException ) { $this->setExpectedException( $expectedException ); } $node->set( $propertyName, $newValue ); $this->assertEquals( $expectedValue, $node->$getPropertyName ); } /** * @dataProvider provideForBasicSetProperty */ public function testBasicMagicSetProperty( $inputObj, $context, $propertyName, $newValue, $getPropertyName, $expectedValue, $expectedException = null ) { $node = $this->makeJsonLdNode( $inputObj, $context ); if ( $expectedException ) { $this->setExpectedException( $expectedException ); } $node->$propertyName = $newValue; $this->assertEquals( $expectedValue, $node->$getPropertyName ); } /** * @dataProvider provideForBasicSetProperty */ public function testBasicArraySetProperty( $inputObj, $context, $propertyName, $newValue, $getPropertyName, $expectedValue, $expectedException = null ) { $node = $this->makeJsonLdNode( $inputObj, $context ); if ( $expectedException ) { $this->setExpectedException( $expectedException ); } $node[$propertyName] = $newValue; $this->assertEquals( $expectedValue, $node[$getPropertyName] ); } public function provideForBasicAddPropertyValue() { return array( array( new stdClass(), $this->asContext, 'name', 'NewName', 'name', array( 'NewName' ), ), array( (object) array( 'https://www.w3.org/ns/activitystreams#name' => 'OldName', ), $this->asContext, 'name', 'NewName', 'name', array( 'OldName', 'NewName' ), ), ); } /** * @dataProvider provideForBasicAddPropertyValue */ public function testBasicAddPropertyValue( $inputObj, $context, $propertyName, $newValue, $getPropertyName, $expectedValue, $expectedException = null ) { $node = $this->makeJsonLdNode( $inputObj, $context ); if ( $expectedException ) { $this->setExpectedException( $expectedException ); } $node->add( $propertyName, $newValue ); $this->assertEquals( $expectedValue, $node->getMany( $getPropertyName ) ); } public function provideForGetLinkedNode() { return array( array( (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Announce', 'object' => 'https://example.org/objects/1', ), $this->asContext, array( 'https://example.org/objects/1' => (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/objects/1', 'type' => 'Note', ), ), 'object', (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/objects/1', 'type' => 'Note', ), ), array( (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Announce', 'object' => 'https://example.org/objects/1', ), $this->asContext, array(), 'object', null, NodeNotFoundException::class, ), array( (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Announce', 'object' => 'https://example.org/objects/1', ), $this->asContext, array( 'https://example.org/objects/1' => (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/objects/1', 'type' => 'Note', 'inReplyTo' => (object) array( 'id' => 'https://example.org/articles/1', 'type' => 'Article', ), ), ), 'object', (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/objects/1', 'type' => 'Note', 'inReplyTo' => (object) array( 'id' => 'https://example.org/articles/1', 'type' => 'Article', ), ), ), array( (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Announce', 'object' => 'https://example.org/objects/1', ), $this->asContext, array( 'https://example.org/objects/1' => (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/objects/1', 'type' => 'Note', 'inReplyTo' => 'https://example.org/articles/1', ), ), 'object', (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/objects/1', 'type' => 'Note', 'inReplyTo' => 'https://example.org/articles/1', ), ), ); } /** * @dataProvider provideForGetLinkedNode */ public function testGetLinkedNode( $inputObj, $context, $nodeGraph, $propertyName, $expectedValue, $expectedException = null ) { $node = $this->makeJsonLdNode( $inputObj, $context, $nodeGraph ); if ( $expectedException ) { $this->setExpectedException( $expectedException ); } $actualValue = $node->get( $propertyName ); if ( $actualValue instanceof JsonLdNode ) { $actualValue = $actualValue->asObject(); } $this->assertEquals( $expectedValue, $actualValue ); } public function provideForBackreferencesOnGet() { return array( array( (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/objects/1', 'type' => 'Note', 'inReplyTo' => (object) array( 'id' => 'https://example.org/articles/1', 'type' => 'Article', ), ), $this->asContext, 'inReplyTo', ), ); } /** * @dataProvider provideForBackreferencesOnGet */ public function testBackreferencesOnGet( $inputObj, $context, $childNodeField, $nodeGraph = array() ) { $parentNode = $this->makeJsonLdNode( $inputObj, $context, $nodeGraph ); $childNode = $parentNode->$childNodeField; $this->assertInstanceOf( JsonLdNode::class, $childNode ); $this->assertEquals( $childNode->getBackReferences( $childNodeField ), array( $parentNode ) ); } public function provideForBackreferencesOnSet() { return array( array( new stdClass(), $this->asContext, 'inReplyTo', (object) array( 'id' => 'https://example.org/articles/1', 'type' => 'Article', ) ), array( new stdClass(), $this->asContext, 'to', array( (object) array( 'id' => 'https://example.org/sally', 'type' => 'Person', ), (object) array( 'id' => 'https://example.org/bob', 'type' => 'Person', ) ), ) ); } /** * @dataProvider provideForBackreferencesOnSet */ public function testBackreferencesOnSet( $inputObj, $context, $newPropertyName, $newNodeValue ) { $parentNode = $this->makeJsonLdNode( $inputObj, $context ); $parentNode->set( $newPropertyName, $newNodeValue ); $childNodes = $parentNode->getMany( $newPropertyName ); foreach ( $childNodes as $childNode ) { $this->assertInstanceOf( JsonLdNode::class, $childNode ); $this->assertEquals( $childNode->getBackReferences( $newPropertyName ), array( $parentNode ) ); } } public function provideToRdfTriple() { return array( array( (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/collections/1', 'type' => 'Collection', 'name' => 'MyCollection', 'published' => '2019-05-01', 'items' => array( 'https://example.org/collections/1/items/1', 'https://example.org/collections/1/items/2', ) ), $this->asContext, array( TypedRdfTriple::create( 'https://example.org/collections/1', '@id', 'https://example.org/collections/1' ), TypedRdfTriple::create( 'https://example.org/collections/1', 'https://www.w3.org/ns/activitystreams#items', 'https://example.org/collections/1/items/1', '@id' ), TypedRdfTriple::create( 'https://example.org/collections/1', 'https://www.w3.org/ns/activitystreams#items', 'https://example.org/collections/1/items/2', '@id' ), TypedRdfTriple::create( 'https://example.org/collections/1/items/1', '@id', 'https://example.org/collections/1/items/1' ), TypedRdfTriple::create( 'https://example.org/collections/1/items/1', '@type', 'https://www.w3.org/ns/activitystreams#Note', ), TypedRdfTriple::create( 'https://example.org/collections/1/items/2', '@id', 'https://example.org/collections/1/items/2' ), TypedRdfTriple::create( 'https://example.org/collections/1/items/2', '@type', 'https://www.w3.org/ns/activitystreams#Note', ), TypedRdfTriple::create( 'https://example.org/collections/1', 'https://www.w3.org/ns/activitystreams#name', 'MyCollection', 'http://www.w3.org/2001/XMLSchema#string' ), TypedRdfTriple::create( 'https://example.org/collections/1', 'https://www.w3.org/ns/activitystreams#published', '2019-05-01', 'http://www.w3.org/2001/XMLSchema#dateTime' ), TypedRdfTriple::create( 'https://example.org/collections/1', '@type', 'https://www.w3.org/ns/activitystreams#Collection', ), ), array( 'https://example.org/collections/1/items/1' => (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/collections/1/items/1', 'type' => 'Note', ), 'https://example.org/collections/1/items/2' => (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'id' => 'https://example.org/collections/1/items/2', 'type' => 'Note', ), ), ), array( (object) array( '@context' => array( 'https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1', ), 'id' => 'https://example.org/sally', 'type' => 'Actor', 'publicKey' => (object) array( 'publicKeyPem' => 'the_public_key', ) ), array( 'https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1' ), array( TypedRdfTriple::create( 'https://example.org/sally', '@id', 'https://example.org/sally' ), TypedRdfTriple::create( $this->uuids[0], 'https://w3id.org/security#publicKeyPem', 'the_public_key', 'http://www.w3.org/2001/XMLSchema#string' ), TypedRdfTriple::create( 'https://example.org/sally', '@type', 'https://www.w3.org/ns/activitystreams#Actor' ), TypedRdfTriple::create( 'https://example.org/sally', 'https://w3id.org/security/v1#publicKey', $this->uuids[0] ), ), ), ); } /** * @dataProvider provideToRdfTriple */ public function testToRdfTriple( $inputObj, $context, $expectedTriples, $nodeGraph = array() ) { $r = Request::create( 'https://example.org' ); $r->overrideGlobals(); $node = $this->makeJsonLdNode( $inputObj, $context, $nodeGraph ); $triples = $node->toRdfTriples(); $this->assertEquals( $expectedTriples, $triples ); } private function makeJsonLdNode( $inputObj, $context, $nodeGraph = array() ) { $factory = new JsonLdNodeFactory( $context, new TestDereferencer( $nodeGraph ), new Logger(), new TestUuidProvider( $this->uuids ) ); return $factory->newNode( $inputObj ); } }