From 4e02319ffa1ddc486052507f1437236d141b1791 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sat, 2 Mar 2019 17:43:36 -0500 Subject: [PATCH] Test and register AddHandler --- src/ActivityPub.php | 2 + src/Config/ActivityPubModule.php | 5 + test/Activities/AddHandlerTest.php | 145 ++++++++++++++++++++++++++++- 3 files changed, 150 insertions(+), 2 deletions(-) diff --git a/src/ActivityPub.php b/src/ActivityPub.php index b4b108f..978db0c 100644 --- a/src/ActivityPub.php +++ b/src/ActivityPub.php @@ -5,6 +5,7 @@ namespace ActivityPub; use ActivityPub\Activities\AcceptHandler; +use ActivityPub\Activities\AddHandler; use ActivityPub\Activities\CreateHandler; use ActivityPub\Activities\DeleteHandler; use ActivityPub\Activities\FollowHandler; @@ -90,6 +91,7 @@ class ActivityPub $dispatcher->addSubscriber( $this->module->get( DeleteHandler::class ) ); $dispatcher->addSubscriber( $this->module->get( FollowHandler::class ) ); $dispatcher->addSubscriber( $this->module->get( AcceptHandler::class ) ); + $dispatcher->addSubscriber( $this->module->get( AddHandler::class ) ); } /** diff --git a/src/Config/ActivityPubModule.php b/src/Config/ActivityPubModule.php index 6debc2d..fdbefc6 100644 --- a/src/Config/ActivityPubModule.php +++ b/src/Config/ActivityPubModule.php @@ -5,6 +5,7 @@ namespace ActivityPub\Config; use ActivityPub\Activities\AcceptHandler; +use ActivityPub\Activities\AddHandler; use ActivityPub\Activities\CreateHandler; use ActivityPub\Activities\DeleteHandler; use ActivityPub\Activities\FollowHandler; @@ -144,6 +145,10 @@ class ActivityPubModule ->addArgument( new Reference( ObjectsService::class ) ) ->addArgument( new Reference( CollectionsService::class ) ) ->addArgument( new Reference( ContextProvider::class ) ); + + $this->injector->register( AddHandler::class, AddHandler::class ) + ->addArgument( new Reference( ObjectsService::class ) ) + ->addArgument( new Reference( CollectionsService::class ) ); } /** diff --git a/test/Activities/AddHandlerTest.php b/test/Activities/AddHandlerTest.php index 957d359..c57a95f 100644 --- a/test/Activities/AddHandlerTest.php +++ b/test/Activities/AddHandlerTest.php @@ -2,13 +2,154 @@ namespace ActivityPub\Test\Activities; +use ActivityPub\Activities\AddHandler; +use ActivityPub\Activities\InboxActivityEvent; +use ActivityPub\Activities\OutboxActivityEvent; +use ActivityPub\Objects\CollectionsService; +use ActivityPub\Objects\ObjectsService; use ActivityPub\Test\TestConfig\APTestCase; +use ActivityPub\Test\TestUtils\TestActivityPubObject; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; class AddHandlerTest extends APTestCase { + private static function getObjects() + { + return array( + 'https://elsewhere.com/collections/1' => array( + 'id' => 'https://elsewhere.com/collections/1', + ), + 'https://example.com/collections/1' => array( + 'id' => 'https://example.com/collections/1', + ), + ); + } + public function testHandleAdd() { - // TODO implement me - $this->assertTrue( false ); + $testCases = array( + array( + 'id' => 'basicTest', + 'eventName' => InboxActivityEvent::NAME, + 'event' => new InboxActivityEvent( + array( + 'id' => 'https://elsewhere.com/adds/1', + 'type' => 'Add', + 'object' => array( + 'id' => 'https://elsewhere.com/notes/1', + ), + 'target' => array( + 'id' => 'https://elsewhere.com/collections/1' + ) + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1', + ) ), + self::requestWithAttributes( + 'https://example.com/actors/1/inbox', + array( + 'actor' => TestActivityPubObject::fromArray( array( + 'id' => 'https://elsewhere.com/actors/1' + ) ) + ) + ) + ), + 'expectedNewItem' => array( + 'id' => 'https://elsewhere.com/notes/1', + ) + ), + array( + 'id' => 'outboxTest', + 'eventName' => OutboxActivityEvent::NAME, + 'event' => new OutboxActivityEvent( + array( + 'id' => 'https://example.com/adds/1', + 'type' => 'Add', + 'object' => array( + 'id' => 'https://example.com/notes/1', + ), + 'target' => array( + 'id' => 'https://example.com/collections/1' + ) + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1', + ) ), + self::requestWithAttributes( + 'https://example.com/actors/1/inbox', + array( + 'actor' => TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1' + ) ) + ) + ) + ), + 'expectedNewItem' => array( + 'id' => 'https://example.com/notes/1', + ) + ), + array( + 'id' => 'notAuthorizedTest', + 'eventName' => OutboxActivityEvent::NAME, + 'event' => new OutboxActivityEvent( + array( + 'id' => 'https://example.com/adds/1', + 'type' => 'Add', + 'object' => array( + 'id' => 'https://example.com/notes/1', + ), + 'target' => array( + 'id' => 'https://elsewhere.com/collections/1' + ) + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1', + ) ), + self::requestWithAttributes( + 'https://example.com/actors/1/inbox', + array( + 'actor' => TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actors/1' + ) ) + ) + ) + ), + 'expectedException' => AccessDeniedHttpException::class, + ), + ); + foreach ( $testCases as $testCase ) { + $objectsService = $this->getMock( ObjectsService::class ); + $objectsService->method( 'dereference')->willReturnCallback( function( $id ) { + $objects = self::getObjects(); + if ( array_key_exists( $id, $objects ) ) { + return TestActivityPubObject::fromArray( $objects[$id] ); + } else { + return null; + } + }); + $collectionsService = $this->getMockBuilder( CollectionsService::class ) + ->disableOriginalConstructor() + ->setMethods( array( 'addItem' ) ) + ->getMock(); + if ( array_key_exists( 'expectedNewItem', $testCase ) ) { + $collectionsService->expects( $this->once() ) + ->method( 'addItem' ) + ->with( + $this->anything(), + $this->equalTo( $testCase['expectedNewItem'] ) + ); + } else { + $collectionsService->expects( $this->never() ) + ->method( 'addItem' ); + } + $addHandler = new AddHandler( $objectsService, $collectionsService ); + $eventDispatcher = new EventDispatcher(); + $eventDispatcher->addSubscriber( $addHandler ); + if ( array_key_exists( 'expectedException', $testCase ) ) { + $this->setExpectedException( $testCase['expectedException'] ); + } + $eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] ); + } } } \ No newline at end of file