From b5669612aed8dae1e046c624b1b0b02ea0daaebd Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Tue, 29 Jan 2019 11:20:42 -0500 Subject: [PATCH] Implement and test CreateHandler --- src/Activities/CreateHandler.php | 35 +++- test/Activities/CreateHandlerTest.php | 262 ++++++++++++++++++++++++++ 2 files changed, 291 insertions(+), 6 deletions(-) create mode 100644 test/Activities/CreateHandlerTest.php diff --git a/src/Activities/CreateHandler.php b/src/Activities/CreateHandler.php index 5ee4184..1349cb1 100644 --- a/src/Activities/CreateHandler.php +++ b/src/Activities/CreateHandler.php @@ -3,15 +3,28 @@ namespace ActivityPub\Activities; use ActivityPub\Activities\InboxActivityEvent; use ActivityPub\Activities\OutboxActivityEvent; +use ActivityPub\Objects\CollectionsService; use ActivityPub\Objects\IdProvider; use ActivityPub\Objects\ObjectsService; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class CreateHandler implements EventSubscriberInterface { + /** + * @var ObjextsService + */ private $objectsService; + + /** + * @var IdProvider + */ private $idProvider; + /** + * @var CollectionsService + */ + private $collectionsService; + public static function getSubscribedEvents() { return array( @@ -21,10 +34,12 @@ class CreateHandler implements EventSubscriberInterface } public function __construct( ObjectsService $objectsService, - IdProvider $idProvider ) + IdProvider $idProvider, + CollectionsService $collectionsService ) { $this->objectsService = $objectsService; $this->idProvider = $idProvider; + $this->collectionsService = $collectionsService; } public function handleInbox( InboxActivityEvent $event ) { @@ -32,8 +47,13 @@ class CreateHandler implements EventSubscriberInterface if ( $activity['type'] !== 'Create' ) { return; } - // normalize an incoming collection - // persist the object + $object = $activity['object']; + if ( in_array( $object['type'], array( 'Collection', 'OrderedCollection' ) ) ) { + $object = $this->collectionsService->normalizeCollection( $object ); + } + $this->objectsService->persist( $object ); + $activity['object'] = $object; + $event->setActivity( $activity ); } public function handleOutbox( OutboxActivityEvent $event ) @@ -56,9 +76,12 @@ class CreateHandler implements EventSubscriberInterface $activity = $this->copyFields( array( 'to', 'bto', 'cc', 'bcc', 'audience' ), $object, $activity ); + if ( in_array( $object['type'], array( 'Collection', 'OrderedCollection' ) ) ) { + $object = $this->collectionsService->normalizeCollection( $object ); + } $activity['object'] = $object; - // normalize an incoming collection - // persist the object + $this->objectsService->persist( $object ); + $event->setActivity( $activity ); } private function getActorId( array $activity ) @@ -83,7 +106,7 @@ class CreateHandler implements EventSubscriberInterface } else if ( ! array_key_exists( $field, $targetObj ) ) { $targetObj[$field] = $sourceObj[$field]; } else if ( is_array( $sourceObj[$field] ) && - is_array( $targetObject[$field] ) ) { + is_array( $targetObj[$field] ) ) { $targetObj[$field] = array_unique( array_merge( $sourceObj[$field], $targetObj[$field] ) ); diff --git a/test/Activities/CreateHandlerTest.php b/test/Activities/CreateHandlerTest.php new file mode 100644 index 0000000..8e35bf3 --- /dev/null +++ b/test/Activities/CreateHandlerTest.php @@ -0,0 +1,262 @@ +eventDispatcher = new EventDispatcher(); + $objectsService = $this->createMock( ObjectsService::class ); + $idProvider = $this->createMock( IdProvider::class ); + // TODO provision mocks + $collectionsService = new CollectionsService( + 4, + $this->createMock( AuthService::class ), + new ContextProvider(), + $this->createMock( Client::class ) + ); + $createHandler = new CreateHandler( + $objectsService, $idProvider, $collectionsService + ); + $this->eventDispatcher->addSubscriber( $createHandler ); + } + + public function testCreateHandler() + { + $testCases = array( + array( + 'id' => 'basicInboxTest', + 'eventName' => InboxActivityEvent::NAME, + 'event' => new InboxActivityEvent( + array( + 'id' => 'https://elsewhere.com/activities/1', + 'type' => 'Create', + 'actor' => 'https://elsewhere.com/actors/1', + 'object' => array( + 'id' => 'https://elsewhere.com/objects/1', + 'type' => 'Note', + ), + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://elsewhere.com/actors/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com/inbox', Request::METHOD_POST ) + ), + 'expectedEvent' => new InboxActivityEvent( + array( + 'id' => 'https://elsewhere.com/activities/1', + 'type' => 'Create', + 'actor' => 'https://elsewhere.com/actors/1', + 'object' => array( + 'id' => 'https://elsewhere.com/objects/1', + 'type' => 'Note', + ), + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://elsewhere.com/actors/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com/inbox', Request::METHOD_POST ) + ), + ), + array( + 'id' => 'basicOutboxTest', + 'eventName' => OutboxActivityEvent::NAME, + 'event' => new OutboxActivityEvent( + array( + 'id' => 'https://example.com/activities/1', + 'type' => 'Create', + 'actor' => array( + 'id' => 'https://example.com/actors/1' + ), + 'object' => array( + 'id' => 'https://example.com/objects/1', + 'type' => 'Note', + ), + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com/outbox', Request::METHOD_POST ) + ), + 'expectedEvent' => new OutboxActivityEvent( + array( + 'id' => 'https://example.com/activities/1', + 'type' => 'Create', + 'actor' => array( + 'id' => 'https://example.com/actors/1', + ), + 'object' => array( + 'id' => 'https://example.com/objects/1', + 'type' => 'Note', + 'attributedTo' => 'https://example.com/actors/1' + ), + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com/outbox', Request::METHOD_POST ) + ), + ), + array( + 'id' => 'copiesAudienceOutboxTest', + 'eventName' => OutboxActivityEvent::NAME, + 'event' => new OutboxActivityEvent( + array( + 'id' => 'https://example.com/activities/1', + 'type' => 'Create', + 'to' => 'https://www.w3.org/ns/activitystreams#Public', + 'cc' => array( + 'https://elsewhere.com/actors/2' + ), + 'audience' => 'https://elsewhere.com/actors/4', + 'actor' => array( + 'id' => 'https://example.com/actors/1' + ), + 'object' => array( + 'id' => 'https://example.com/objects/1', + 'type' => 'Note', + 'to' => 'https://elsewhere.com/actors/1', + 'cc' => 'https://elsewhere.com/actors/3', + 'audience' => array( 'https://elsewhere.com/actors/5' ) + ), + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com/outbox', Request::METHOD_POST ) + ), + 'expectedEvent' => new OutboxActivityEvent( + array( + 'id' => 'https://example.com/activities/1', + 'type' => 'Create', + 'to' => array( + 'https://elsewhere.com/actors/1', + 'https://www.w3.org/ns/activitystreams#Public', + ), + 'cc' => array( + 'https://elsewhere.com/actors/2', + 'https://elsewhere.com/actors/3', + ), + 'audience' => array( + 'https://elsewhere.com/actors/5', + 'https://elsewhere.com/actors/4', + ), + 'actor' => array( + 'id' => 'https://example.com/actors/1', + ), + 'object' => array( + 'id' => 'https://example.com/objects/1', + 'type' => 'Note', + 'attributedTo' => 'https://example.com/actors/1', + 'to' => array( + 'https://elsewhere.com/actors/1', + 'https://www.w3.org/ns/activitystreams#Public', + ), + 'cc' => array( + 'https://elsewhere.com/actors/2', + 'https://elsewhere.com/actors/3', + ), + 'audience' => array( + 'https://elsewhere.com/actors/5', + 'https://elsewhere.com/actors/4', + ), + ), + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com/outbox', Request::METHOD_POST ) + ), + ), + array( + 'id' => 'moreCopiesAudienceOutboxTest', + 'eventName' => OutboxActivityEvent::NAME, + 'event' => new OutboxActivityEvent( + array( + 'id' => 'https://example.com/activities/1', + 'type' => 'Create', + 'to' => 'https://www.w3.org/ns/activitystreams#Public', + 'cc' => array( + 'https://elsewhere.com/actors/2' + ), + 'actor' => array( + 'id' => 'https://example.com/actors/1' + ), + 'object' => array( + 'id' => 'https://example.com/objects/1', + 'type' => 'Note', + 'cc' => array( 'https://elsewhere.com/actors/3' ), + 'audience' => 'https://elsewhere.com/actors/5', + ), + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com/outbox', Request::METHOD_POST ) + ), + 'expectedEvent' => new OutboxActivityEvent( + array( + 'id' => 'https://example.com/activities/1', + 'type' => 'Create', + 'to' => 'https://www.w3.org/ns/activitystreams#Public', + 'cc' => array( + 'https://elsewhere.com/actors/2', + 'https://elsewhere.com/actors/3', + ), + 'audience' => 'https://elsewhere.com/actors/5', + 'actor' => array( + 'id' => 'https://example.com/actors/1', + ), + 'object' => array( + 'id' => 'https://example.com/objects/1', + 'type' => 'Note', + 'attributedTo' => 'https://example.com/actors/1', + 'to' => 'https://www.w3.org/ns/activitystreams#Public', + 'cc' => array( + 'https://elsewhere.com/actors/2', + 'https://elsewhere.com/actors/3', + ), + 'audience' => 'https://elsewhere.com/actors/5', + ), + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com/outbox', Request::METHOD_POST ) + ), + ), + ); + foreach ( $testCases as $testCase ) { + $event = $testCase['event']; + $this->eventDispatcher->dispatch( $testCase['eventName'], $event ); + $this->assertEquals( + $testCase['expectedEvent'], $event, "Error on test $testCase[id]" + ); + } + } +} +?>