From eba10d28e93154c349adb97fe80ba5989e15cfd2 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Fri, 1 Feb 2019 11:07:26 -0500 Subject: [PATCH] Test FollowHandler --- src/Activities/FollowHandler.php | 14 ++-- test/Activities/FollowHandlerTest.php | 94 +++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 test/Activities/FollowHandlerTest.php diff --git a/src/Activities/FollowHandler.php b/src/Activities/FollowHandler.php index c0a62e5..f5e9cff 100644 --- a/src/Activities/FollowHandler.php +++ b/src/Activities/FollowHandler.php @@ -16,11 +16,6 @@ class FollowHandler implements EventSubscriberInterface */ private $autoAccepts; - /** - * @var Client - */ - private $httpClient; - /** * @var ContextProvider */ @@ -34,11 +29,9 @@ class FollowHandler implements EventSubscriberInterface } public function __construct( bool $autoAccepts, - Client $httpClient, ContextProvider $contextProvider ) { $this->autoAccepts = $autoAccepts; - $this->httpsClient = $httpClient; $this->contextProvider = $contextProvider; } @@ -52,6 +45,13 @@ class FollowHandler implements EventSubscriberInterface } if ( $this->autoAccepts ) { $localActor = $event->getActor(); + $objectId = $activity['object']; + if ( is_array( $objectId ) && array_key_exists( 'id', $objectId ) ) { + $objectId = $objectId['id']; + } + if ( $localActor['id'] !== $objectId ) { + return; + } $accept = array( '@context' => $this->contextProvider->getContext(), 'type' => 'Accept', diff --git a/test/Activities/FollowHandlerTest.php b/test/Activities/FollowHandlerTest.php new file mode 100644 index 0000000..ecfdb81 --- /dev/null +++ b/test/Activities/FollowHandlerTest.php @@ -0,0 +1,94 @@ +addSubscriber( $followHandler ); + $outboxDispatched = false; + $actor = TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actor/1', + 'outbox' => 'https://example.com/actor/1/outbox', + ) ); + $follow = array( + 'id' => 'https://elsewhere.com/activities/1', + 'type' => 'Follow', + 'object' => 'https://example.com/actor/1', + ); + $eventDispatcher->addListener( OutboxActivityEvent::NAME, function( $event, $name ) + use ( &$outboxDispatched, $actor ) + { + $this->assertEquals( OutboxActivityEvent::NAME, $name ); + $outboxDispatched = true; + $accept = array( + '@context' => ContextProvider::DEFAULT_CONTEXT, + 'type' => 'Accept', + 'actor' => 'https://example.com/actor/1', + 'object' => 'https://elsewhere.com/activities/1', + ); + $expectedRequest = Request::create( + 'https://example.com/actor/1/outbox', + Request::METHOD_POST, + array(), array(), array(), + array( + 'HTTP_ACCEPT' => 'application/ld+json', + 'CONTENT_TYPE' => 'application/json', + ), + json_encode( $accept ) + ); + $expectedRequest->attributes->set( 'actor', $actor ); + $this->assertEquals( + new OutboxActivityEvent( $accept, $actor, $expectedRequest ), $event + ); + } ); + $eventDispatcher->dispatch( InboxActivityEvent::NAME, new InboxActivityEvent( + $follow, + $actor, + Request::create( 'https://example.com/actor/1/inbox' ) + ) ); + $this->assertTrue( $outboxDispatched ); + } + + public function testItChecksForFollowObject() + { + $eventDispatcher = new EventDispatcher(); + $contextProvider = new ContextProvider(); + $followHandler = new FollowHandler( true, $contextProvider ); + $eventDispatcher->addSubscriber( $followHandler ); + $outboxDispatched = false; + $actor = TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actor/1', + 'outbox' => 'https://example.com/actor/1/outbox', + ) ); + $follow = array( + 'id' => 'https://elsewhere.com/activities/1', + 'type' => 'Follow', + 'object' => 'https://example.com/actor/2', + ); + $eventDispatcher->addListener( OutboxActivityEvent::NAME, function( $event ) + use ( &$outboxDispatched ) + { + $outboxDispatched = true; + } ); + $eventDispatcher->dispatch( InboxActivityEvent::NAME, new InboxActivityEvent( + $follow, + $actor, + Request::create( 'https://example.com/actor/1/inbox' ) + ) ); + $this->assertFalse( $outboxDispatched ); + } +} +?>