Implement and test CreateHandler
This commit is contained in:
parent
c5813c4441
commit
b5669612ae
@ -3,15 +3,28 @@ namespace ActivityPub\Activities;
|
|||||||
|
|
||||||
use ActivityPub\Activities\InboxActivityEvent;
|
use ActivityPub\Activities\InboxActivityEvent;
|
||||||
use ActivityPub\Activities\OutboxActivityEvent;
|
use ActivityPub\Activities\OutboxActivityEvent;
|
||||||
|
use ActivityPub\Objects\CollectionsService;
|
||||||
use ActivityPub\Objects\IdProvider;
|
use ActivityPub\Objects\IdProvider;
|
||||||
use ActivityPub\Objects\ObjectsService;
|
use ActivityPub\Objects\ObjectsService;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
|
||||||
class CreateHandler implements EventSubscriberInterface
|
class CreateHandler implements EventSubscriberInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var ObjextsService
|
||||||
|
*/
|
||||||
private $objectsService;
|
private $objectsService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IdProvider
|
||||||
|
*/
|
||||||
private $idProvider;
|
private $idProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var CollectionsService
|
||||||
|
*/
|
||||||
|
private $collectionsService;
|
||||||
|
|
||||||
public static function getSubscribedEvents()
|
public static function getSubscribedEvents()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
@ -21,10 +34,12 @@ class CreateHandler implements EventSubscriberInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function __construct( ObjectsService $objectsService,
|
public function __construct( ObjectsService $objectsService,
|
||||||
IdProvider $idProvider )
|
IdProvider $idProvider,
|
||||||
|
CollectionsService $collectionsService )
|
||||||
{
|
{
|
||||||
$this->objectsService = $objectsService;
|
$this->objectsService = $objectsService;
|
||||||
$this->idProvider = $idProvider;
|
$this->idProvider = $idProvider;
|
||||||
|
$this->collectionsService = $collectionsService;
|
||||||
}
|
}
|
||||||
public function handleInbox( InboxActivityEvent $event )
|
public function handleInbox( InboxActivityEvent $event )
|
||||||
{
|
{
|
||||||
@ -32,8 +47,13 @@ class CreateHandler implements EventSubscriberInterface
|
|||||||
if ( $activity['type'] !== 'Create' ) {
|
if ( $activity['type'] !== 'Create' ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// normalize an incoming collection
|
$object = $activity['object'];
|
||||||
// persist the 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 )
|
public function handleOutbox( OutboxActivityEvent $event )
|
||||||
@ -56,9 +76,12 @@ class CreateHandler implements EventSubscriberInterface
|
|||||||
$activity = $this->copyFields(
|
$activity = $this->copyFields(
|
||||||
array( 'to', 'bto', 'cc', 'bcc', 'audience' ), $object, $activity
|
array( 'to', 'bto', 'cc', 'bcc', 'audience' ), $object, $activity
|
||||||
);
|
);
|
||||||
|
if ( in_array( $object['type'], array( 'Collection', 'OrderedCollection' ) ) ) {
|
||||||
|
$object = $this->collectionsService->normalizeCollection( $object );
|
||||||
|
}
|
||||||
$activity['object'] = $object;
|
$activity['object'] = $object;
|
||||||
// normalize an incoming collection
|
$this->objectsService->persist( $object );
|
||||||
// persist the object
|
$event->setActivity( $activity );
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getActorId( array $activity )
|
private function getActorId( array $activity )
|
||||||
@ -83,7 +106,7 @@ class CreateHandler implements EventSubscriberInterface
|
|||||||
} else if ( ! array_key_exists( $field, $targetObj ) ) {
|
} else if ( ! array_key_exists( $field, $targetObj ) ) {
|
||||||
$targetObj[$field] = $sourceObj[$field];
|
$targetObj[$field] = $sourceObj[$field];
|
||||||
} else if ( is_array( $sourceObj[$field] ) &&
|
} else if ( is_array( $sourceObj[$field] ) &&
|
||||||
is_array( $targetObject[$field] ) ) {
|
is_array( $targetObj[$field] ) ) {
|
||||||
$targetObj[$field] = array_unique(
|
$targetObj[$field] = array_unique(
|
||||||
array_merge( $sourceObj[$field], $targetObj[$field] )
|
array_merge( $sourceObj[$field], $targetObj[$field] )
|
||||||
);
|
);
|
||||||
|
262
test/Activities/CreateHandlerTest.php
Normal file
262
test/Activities/CreateHandlerTest.php
Normal 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]"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
Loading…
Reference in New Issue
Block a user