activitypub-php/test/Controllers/PostControllerTest.php

270 lines
11 KiB
PHP
Raw Normal View History

<?php
2019-02-16 17:51:24 +00:00
namespace ActivityPub\Test\Controllers;
use ActivityPub\ActivityEventHandlers\InboxActivityEvent;
use ActivityPub\ActivityEventHandlers\OutboxActivityEvent;
use ActivityPub\Controllers\PostController;
use ActivityPub\Objects\ObjectsService;
use ActivityPub\Test\TestConfig\APTestCase;
2019-02-16 17:51:24 +00:00
use ActivityPub\Test\TestUtils\TestActivityPubObject;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
2019-01-23 19:27:27 +00:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
class PostControllerTest extends APTestCase
{
/**
* @var array
*/
private $objects;
/**
* @var array
*/
private $refs;
public function testPostController()
{
$this->objects = self::getObjects();
$this->refs = self::getRefs();
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'query' )->will(
2019-02-16 17:51:24 +00:00
$this->returnCallback( function ( $query ) {
if ( array_key_exists( 'id', $query ) &&
2019-02-16 17:51:24 +00:00
array_key_exists( $query['id'], $this->objects ) ) {
2019-01-23 20:43:04 +00:00
$object = TestActivityPubObject::fromArray(
$this->objects[$query['id']]
2019-01-23 20:43:04 +00:00
);
if ( array_key_exists( $query['id'], $this->refs ) ) {
$ref = $this->refs[$query['id']];
2019-01-23 19:16:40 +00:00
$referencingObject = TestActivityPubObject::fromArray(
$this->objects[$ref['referencingObject']]
2019-01-23 19:16:40 +00:00
);
$referencingField = $referencingObject->getField( $ref['field'] );
$object->addReferencingField( $referencingField );
}
return array( $object );
} else {
return array();
}
} )
);
$objectsService->method( 'dereference' )->will(
2019-02-16 17:51:24 +00:00
$this->returnCallback( function ( $id ) {
if ( array_key_exists( $id, $this->objects ) ) {
return TestActivityPubObject::fromArray( $this->objects[$id] );
} else {
return null;
}
} )
);
$testCases = array(
array(
'id' => 'basicInboxTest',
2019-01-23 19:16:40 +00:00
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
2019-01-23 19:16:40 +00:00
array(
'signed' => true,
2019-01-23 20:43:04 +00:00
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:16:40 +00:00
)
),
'expectedEventName' => InboxActivityEvent::NAME,
'expectedEvent' => new InboxActivityEvent(
array(
'type' => 'Create',
'actor' => 'https://elsewhere.com/actor/1'
),
2019-01-23 20:43:04 +00:00
TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:16:40 +00:00
$this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
2019-01-23 19:16:40 +00:00
array(
'signed' => true,
2019-01-23 20:43:04 +00:00
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:16:40 +00:00
)
)
),
),
2019-01-23 19:27:27 +00:00
array(
'id' => 'basicOutboxTest',
'request' => $this->makeRequest(
'https://example.com/actor/1/outbox',
Request::METHOD_POST,
'{"type": "Create"}',
array(
2019-01-23 20:43:04 +00:00
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:27:27 +00:00
)
),
'expectedEventName' => OutboxActivityEvent::NAME,
'expectedEvent' => new OutboxActivityEvent(
array( 'type' => 'Create' ),
2019-01-23 20:43:04 +00:00
TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:27:27 +00:00
$this->makeRequest(
'https://example.com/actor/1/outbox',
Request::METHOD_POST,
'{"type": "Create"}',
array(
2019-01-23 20:43:04 +00:00
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:27:27 +00:00
)
)
),
),
array(
'id' => 'inboxRequestMustBeSigned',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
2019-01-23 19:27:27 +00:00
array(
2019-01-23 20:43:04 +00:00
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:27:27 +00:00
)
),
'expectedException' => UnauthorizedHttpException::class,
),
array(
'id' => 'outboxRequestsMustBeAuthed',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
2019-01-23 19:27:27 +00:00
array()
),
'expectedException' => UnauthorizedHttpException::class,
),
array(
'id' => '404sIfNotFound',
'request' => $this->makeRequest(
'https://example.com/actor/notreal/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
2019-01-23 19:27:27 +00:00
array(
'signed' => true,
2019-01-23 20:43:04 +00:00
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:27:27 +00:00
)
),
'expectedException' => NotFoundHttpException::class,
),
array(
'id' => 'BadRequestIfNoBody',
'request' => $this->makeRequest(
'https://example.com/actor/notreal/inbox',
Request::METHOD_POST,
'',
array(
'signed' => true,
2019-01-23 20:43:04 +00:00
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:27:27 +00:00
)
),
'expectedException' => BadRequestHttpException::class,
),
array(
'id' => 'BadRequestIfMalformedBody',
'request' => $this->makeRequest(
'https://example.com/actor/notreal/inbox',
Request::METHOD_POST,
'this is not JSON',
array(
'signed' => 'true',
2019-01-23 20:43:04 +00:00
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
2019-01-23 20:43:04 +00:00
),
2019-01-23 19:27:27 +00:00
)
),
'expectedException' => BadRequestHttpException::class,
),
);
foreach ( $testCases as $testCase ) {
$eventDispatcher = $this->getMockBuilder( EventDispatcher::class )
2019-02-16 17:51:24 +00:00
->setMethods( array( 'dispatch' ) )
->getMock();
if ( array_key_exists( 'expectedEvent', $testCase ) ) {
$eventDispatcher->expects( $this->once() )
->method( 'dispatch' )
->with(
2019-02-16 17:51:24 +00:00
$this->equalTo( $testCase['expectedEventName'] ),
$this->equalTo( $testCase['expectedEvent'] )
);
}
$postController = new PostController( $eventDispatcher, $objectsService );
$request = $testCase['request'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$postController->handle( $request );
}
}
2019-01-23 19:16:40 +00:00
2019-02-16 17:51:24 +00:00
private static function getObjects()
{
return array(
'https://example.com/actor/1/inbox' => array(
'id' => 'https://example.com/actor/1/inbox',
),
'https://example.com/actor/1/outbox' => array(
'id' => 'https://example.com/actor/1/outbox',
),
'https://example.com/actor/1' => array(
'id' => 'https://example.com/actor/1',
'inbox' => array(
'id' => 'https://example.com/actor/1/inbox',
),
'outbox' => array(
'id' => 'https://example.com/actor/1/outbox',
),
),
'https://elsewhere.com/actor/1' => array(
'id' => 'https://elsewhere.com/actor/1',
),
);
}
private static function getRefs()
{
return array(
'https://example.com/actor/1/inbox' => array(
'field' => 'inbox',
'referencingObject' => 'https://example.com/actor/1',
),
'https://example.com/actor/1/outbox' => array(
'field' => 'outbox',
'referencingObject' => 'https://example.com/actor/1',
),
);
}
2019-01-23 19:16:40 +00:00
private function makeRequest( $uri, $method, $body, $attributes )
{
$request = Request::create(
$uri, $method, array(), array(), array(), array(), $body
);
$request->attributes->add( $attributes );
// This populates the pathInfo, requestUri, and baseUrl fields on the request:
$request->getUri();
return $request;
}
}