Implement and test CreateHandler

This commit is contained in:
Jeremy Dormitzer 2019-01-29 11:20:42 -05:00
parent c5813c4441
commit b5669612ae
2 changed files with 291 additions and 6 deletions

View File

@ -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] )
);

View File

@ -0,0 +1,262 @@
<?php
namespace ActivityPub\Test\Activities;
use ActivityPub\Auth\AuthService;
use ActivityPub\Activities\CreateHandler;
use ActivityPub\Activities\InboxActivityEvent;
use ActivityPub\Activities\OutboxActivityEvent;
use ActivityPub\Objects\CollectionsService;
use ActivityPub\Objects\ContextProvider;
use ActivityPub\Objects\IdProvider;
use ActivityPub\Objects\ObjectsService;
use ActivityPub\Test\TestUtils\TestActivityPubObject;
use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
class CreateHandlerTest extends TestCase
{
private $eventDispatcher;
public function setUp()
{
$this->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]"
);
}
}
}
?>