2019-01-31 20:49:36 +00:00
|
|
|
<?php
|
2019-02-16 17:51:24 +00:00
|
|
|
|
2019-03-20 23:16:56 +00:00
|
|
|
namespace ActivityPub\Test\ActivityEventHandlers;
|
2019-01-31 20:49:36 +00:00
|
|
|
|
2019-03-20 23:14:10 +00:00
|
|
|
use ActivityPub\ActivityEventHandlers\DeleteHandler;
|
|
|
|
use ActivityPub\ActivityEventHandlers\InboxActivityEvent;
|
|
|
|
use ActivityPub\ActivityEventHandlers\OutboxActivityEvent;
|
2019-01-31 20:49:36 +00:00
|
|
|
use ActivityPub\Objects\ObjectsService;
|
2019-02-16 17:51:24 +00:00
|
|
|
use ActivityPub\Test\TestConfig\APTestCase;
|
2019-01-31 20:49:36 +00:00
|
|
|
use ActivityPub\Test\TestUtils\TestActivityPubObject;
|
|
|
|
use ActivityPub\Test\TestUtils\TestDateTimeProvider;
|
|
|
|
use DateTime;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
|
|
|
|
2019-02-07 03:48:00 +00:00
|
|
|
class DeleteHandlerTest extends APTestCase
|
2019-01-31 20:49:36 +00:00
|
|
|
{
|
2019-04-12 03:41:25 +00:00
|
|
|
public function provideTestDeleteHandler()
|
2019-01-31 20:49:36 +00:00
|
|
|
{
|
2019-04-12 03:41:25 +00:00
|
|
|
return array(
|
|
|
|
array( array(
|
2019-01-31 20:49:36 +00:00
|
|
|
'id' => 'basicInboxTest',
|
|
|
|
'eventName' => InboxActivityEvent::NAME,
|
|
|
|
'event' => new InboxActivityEvent(
|
|
|
|
array(
|
|
|
|
'id' => 'https://elsewhere.com/activities/1',
|
|
|
|
'type' => 'Delete',
|
|
|
|
'object' => 'https://elsewhere.com/objects/1'
|
|
|
|
),
|
|
|
|
TestActivityPubObject::fromArray( array(
|
|
|
|
'id' => 'https://example.com/actor/1',
|
|
|
|
) ),
|
|
|
|
self::requestWithAttributes(
|
|
|
|
'https://example.com/inbox',
|
|
|
|
array( 'actor' => TestActivityPubObject::fromArray( array(
|
|
|
|
'id' => 'https://elsewhere.com/actors/1',
|
|
|
|
) ) )
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'expectedTombstone' => array(
|
|
|
|
'@context' => 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id' => 'https://elsewhere.com/objects/1',
|
|
|
|
'formerType' => 'Note',
|
|
|
|
'type' => 'Tombstone',
|
|
|
|
'deleted' => '2014-01-05T21:31:40+0000',
|
|
|
|
),
|
2019-04-12 03:41:25 +00:00
|
|
|
) ),
|
|
|
|
array( array(
|
2019-01-31 20:49:36 +00:00
|
|
|
'id' => 'basicOutboxTest',
|
|
|
|
'eventName' => OutboxActivityEvent::NAME,
|
|
|
|
'event' => new OutboxActivityEvent(
|
|
|
|
array(
|
|
|
|
'id' => 'https://example.com/activities/1',
|
|
|
|
'type' => 'Delete',
|
|
|
|
'object' => 'https://example.com/objects/1'
|
|
|
|
),
|
|
|
|
TestActivityPubObject::fromArray( array(
|
|
|
|
'id' => 'https://example.com/actor/1',
|
|
|
|
) ),
|
|
|
|
self::requestWithAttributes(
|
|
|
|
'https://example.com/outbox',
|
|
|
|
array( 'actor' => TestActivityPubObject::fromArray( array(
|
|
|
|
'id' => 'https://example.com/actors/1',
|
|
|
|
) ) )
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'expectedTombstone' => array(
|
|
|
|
'@context' => 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id' => 'https://example.com/objects/1',
|
|
|
|
'formerType' => 'Note',
|
|
|
|
'type' => 'Tombstone',
|
|
|
|
'deleted' => '2014-01-05T21:31:40+0000',
|
|
|
|
),
|
2019-04-12 03:41:25 +00:00
|
|
|
) ),
|
|
|
|
array( array(
|
2019-01-31 20:49:36 +00:00
|
|
|
'id' => 'outboxAuthTest',
|
|
|
|
'eventName' => OutboxActivityEvent::NAME,
|
|
|
|
'event' => new OutboxActivityEvent(
|
|
|
|
array(
|
|
|
|
'id' => 'https://example.com/activities/1',
|
|
|
|
'type' => 'Delete',
|
|
|
|
'object' => 'https://example.com/objects/1'
|
|
|
|
),
|
|
|
|
TestActivityPubObject::fromArray( array(
|
|
|
|
'id' => 'https://example.com/actor/1',
|
|
|
|
) ),
|
|
|
|
self::requestWithAttributes(
|
|
|
|
'https://example.com/outbox',
|
|
|
|
array( 'actor' => TestActivityPubObject::fromArray( array(
|
|
|
|
'id' => 'https://example.com/actors/2',
|
|
|
|
) ) )
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'expectedException' => UnauthorizedHttpException::class,
|
2019-04-12 03:41:25 +00:00
|
|
|
) ),
|
|
|
|
array( array(
|
2019-01-31 20:49:36 +00:00
|
|
|
'id' => 'inboxAuthTest',
|
|
|
|
'eventName' => InboxActivityEvent::NAME,
|
|
|
|
'event' => new InboxActivityEvent(
|
|
|
|
array(
|
|
|
|
'id' => 'https://elsewhere.com/activities/1',
|
|
|
|
'type' => 'Delete',
|
|
|
|
'object' => 'https://elsewhere.com/objects/1'
|
|
|
|
),
|
|
|
|
TestActivityPubObject::fromArray( array(
|
|
|
|
'id' => 'https://example.com/actor/1',
|
|
|
|
) ),
|
|
|
|
self::requestWithAttributes(
|
|
|
|
'https://example.com/inbox',
|
|
|
|
array( 'actor' => TestActivityPubObject::fromArray( array(
|
|
|
|
'id' => 'https://elsewhere.com/actors/2',
|
|
|
|
) ) )
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'expectedException' => UnauthorizedHttpException::class,
|
2019-04-12 03:41:25 +00:00
|
|
|
) ),
|
2019-01-31 20:49:36 +00:00
|
|
|
);
|
2019-04-12 03:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestDeleteHandler
|
|
|
|
*/
|
|
|
|
public function testDeleteHandler( $testCase )
|
|
|
|
{
|
|
|
|
$eventDispatcher = new EventDispatcher();
|
|
|
|
$dateTimeProvider = new TestDateTimeProvider( array(
|
|
|
|
'activities.delete' => DateTime::createFromFormat(
|
|
|
|
DateTime::RFC2822, 'Sun, 05 Jan 2014 21:31:40 GMT'
|
|
|
|
),
|
|
|
|
) );
|
|
|
|
$objectsService = $this->getMockBuilder( ObjectsService::class )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods( array( 'dereference', 'replace' ) )
|
|
|
|
->getMock();
|
|
|
|
$objectsService->method( 'dereference' )->will( $this->returnCallback(
|
|
|
|
function ( $id ) {
|
|
|
|
if ( array_key_exists( $id, self::getObjects() ) ) {
|
|
|
|
$objects = self::getObjects();
|
|
|
|
return TestActivityPubObject::fromArray( $objects[$id] );
|
2019-01-31 20:49:36 +00:00
|
|
|
}
|
2019-04-12 03:41:25 +00:00
|
|
|
return null;
|
2019-01-31 20:49:36 +00:00
|
|
|
}
|
2019-04-12 03:41:25 +00:00
|
|
|
) );
|
|
|
|
if ( array_key_exists( 'expectedException', $testCase ) ) {
|
|
|
|
$this->setExpectedException( $testCase['expectedException'] );
|
|
|
|
} else {
|
|
|
|
$objectsService->expects( $this->once() )
|
|
|
|
->method( 'replace' )
|
|
|
|
->with(
|
|
|
|
$this->anything(),
|
|
|
|
$this->equalTo( $testCase['expectedTombstone'] )
|
|
|
|
);
|
2019-01-31 20:49:36 +00:00
|
|
|
}
|
2019-04-12 03:41:25 +00:00
|
|
|
$deleteHandler = new DeleteHandler( $dateTimeProvider, $objectsService );
|
|
|
|
$eventDispatcher->addSubscriber( $deleteHandler );
|
|
|
|
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
|
2019-01-31 20:49:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 17:51:24 +00:00
|
|
|
private static function getObjects()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'https://elsewhere.com/objects/1' => array(
|
|
|
|
'id' => 'https://elsewhere.com/objects/1',
|
|
|
|
'type' => 'Note',
|
|
|
|
'attributedTo' => 'https://elsewhere.com/actors/1',
|
|
|
|
),
|
|
|
|
'https://example.com/objects/1' => array(
|
|
|
|
'id' => 'https://example.com/objects/1',
|
|
|
|
'type' => 'Note',
|
|
|
|
'attributedTo' => 'https://example.com/actors/1',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2019-01-31 20:49:36 +00:00
|
|
|
}
|
2019-02-07 03:48:00 +00:00
|
|
|
|