Implement and test removeHandler

This commit is contained in:
Jeremy Dormitzer 2019-03-03 12:49:31 -05:00
parent 94c9ee417b
commit b6cf22464a
2 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<?php
namespace ActivityPub\Activities;
use ActivityPub\Objects\CollectionsService;
use ActivityPub\Objects\ObjectsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class RemoveHandler implements EventSubscriberInterface
{
/**
* @var ObjectsService
*/
private $objectsService;
/**
* @var CollectionsService
*/
private $collectionsService;
public static function getSubscribedEvents()
{
return array(
InboxActivityEvent::NAME => 'handleRemove',
OutboxActivityEvent::NAME => 'handleRemove',
);
}
public function __construct( ObjectsService $objectsService,
CollectionsService $collectionsService )
{
$this->objectsService = $objectsService;
$this->collectionsService = $collectionsService;
}
public function handleRemove( ActivityEvent $event )
{
$activity = $event->getActivity();
if ( $activity['type'] !== 'Remove' ) {
return;
}
$collectionId = $activity['target'];
if ( is_array( $collectionId ) && array_key_exists( 'id', $collectionId ) ) {
$collectionId = $collectionId['id'];
}
$collection = $this->objectsService->dereference( $collectionId );
$requestActor = $event->getRequest()->attributes->get( 'actor' );
$requestActorHost = parse_url( $requestActor['id'], PHP_URL_HOST );
$collectionHost = parse_url( $collection['id'], PHP_URL_HOST );
if ( $requestActorHost !== $collectionHost ) {
throw new AccessDeniedHttpException();
}
$objectId = $activity['object'];
if ( is_array( $objectId ) && array_key_exists( 'id', $objectId ) ) {
$objectId = $objectId['id'];
}
if ( ! is_string( $objectId ) ) {
return;
}
$this->collectionsService->removeItem( $collection, $objectId );
}
}

View File

@ -0,0 +1,151 @@
<?php
namespace ActivityPub\Test\Activities;
use ActivityPub\Activities\InboxActivityEvent;
use ActivityPub\Activities\OutboxActivityEvent;
use ActivityPub\Activities\RemoveHandler;
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 RemoveHandlerTest 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 testHandleRemove()
{
$testCases = array(
array(
'id' => 'basicTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
array(
'id' => 'https://elsewhere.com/removes/1',
'type' => 'Remove',
'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'
) )
)
)
),
'expectedRemovedItemId' => 'https://elsewhere.com/notes/1',
),
array(
'id' => 'outboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
array(
'id' => 'https://example.com/removes/1',
'type' => 'Remove',
'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'
) )
)
)
),
'expectedRemovedItemId' => 'https://example.com/notes/1',
),
array(
'id' => 'notAuthorizedTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
array(
'id' => 'https://example.com/removes/1',
'type' => 'Remove',
'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( 'removeItem' ) )
->getMock();
if ( array_key_exists( 'expectedRemovedItemId', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'removeItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedRemovedItemId'] )
);
} else {
$collectionsService->expects( $this->never() )
->method( 'removeItem' );
}
$removeHandler = new RemoveHandler( $objectsService, $collectionsService );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $removeHandler );
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
}
}