diff --git a/src/Activities/VerificationHandler.php b/src/Activities/VerificationHandler.php new file mode 100644 index 0000000..6cea379 --- /dev/null +++ b/src/Activities/VerificationHandler.php @@ -0,0 +1,46 @@ + 'verifyInboxActivity', + OutboxActivityEvent::NAME => 'verifyOutboxActivity', + ); + } + + public function verifyInboxActivity( InboxActivityEvent $event ) + { + $activity = $event->getActivity(); + $this->requireFields( $activity, array( 'type', 'id', 'actor' ) ); + } + + public function verifyOutboxActivity( OutboxActivityEvent $event ) + { + $activity = $event->getActivity(); + $this->requireFields( $activity, array( 'type' ) ); + } + + private function requireFields( array $activity, array $fields ) + { + $missing = array(); + foreach ( $fields as $field ) { + if ( ! array_key_exists( $field, $activity ) ) { + $missing[] = $field; + } + } + if ( count( $missing ) > 0 ) { + throw new BadRequestHttpException( + "Missing activity fields: " . implode( ',', $missing ) + ); + } + } +} +?> diff --git a/test/Activities/VerificationHandlerTest.php b/test/Activities/VerificationHandlerTest.php new file mode 100644 index 0000000..ad625c2 --- /dev/null +++ b/test/Activities/VerificationHandlerTest.php @@ -0,0 +1,117 @@ +eventDispatcher = new EventDispatcher(); + $verificationHandler = new VerificationHandler(); + $this->eventDispatcher->addSubscriber( $verificationHandler ); + } + public function testVerificationHandler() + { + $testCases = array( + array( + 'id' => 'inboxRequiredFields', + 'activity' => array(), + 'eventName' => InboxActivityEvent::NAME, + 'event' => new InboxActivityEvent( + array(), + TestActivityPubObject::fromArray( array( + 'id' => 'https://notexample.com/actor/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com' ) + ), + 'expectedException' => BadRequestHttpException::class, + 'expectedExceptionMessage' => 'Missing activity fields: type,id,actor', + ), + array( + 'id' => 'outboxRequiredFields', + 'activity' => array(), + 'eventName' => OutboxActivityEvent::NAME, + 'event' => new OutboxActivityEvent( + array(), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actor/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com' ) + ), + 'expectedException' => BadRequestHttpException::class, + 'expectedExceptionMessage' => 'Missing activity fields: type', + ), + array( + 'id' => 'inboxPassesValidActivity', + 'activity' => array( + 'id' => 'https://notexample.com/activity/1', + 'type' => 'Create', + 'actor' => 'https://notexample.com/actor/1', + ), + 'eventName' => InboxActivityEvent::NAME, + 'event' => new InboxActivityEvent( + array( + 'id' => 'https://notexample.com/activity/1', + 'type' => 'Create', + 'actor' => 'https://notexample.com/actor/1', + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://notexample.com/actor/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com' ) + ), + ), + array( + 'id' => 'outboxPassesValidActivity', + 'activity' => array( + 'type' => 'Create', + ), + 'eventName' => OutboxActivityEvent::NAME, + 'event' => new OutboxActivityEvent( + array( + 'type' => 'Create', + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actor/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com' ) + ), + ), + ); + foreach ( $testCases as $testCase ) { + $activity = $testCase['activity']; + $event = $testCase['event']; + if ( array_key_exists( 'expectedException', $testCase ) ) { + $this->expectException( + $testCase['expectedException'], + "Error on test $testCase[id]" + ); + } + if ( array_key_exists( 'expectedExceptionMessage', $testCase ) ) { + $this->expectExceptionMessage( + $testCase['expectedExceptionMessage'], + "Error on test $testCase[id]" + ); + } + $this->eventDispatcher->dispatch( $testCase['eventName'], $event ); + } + } +} +?>