Test UpdateHandler and ObjectsService::replace

This commit is contained in:
Jeremy Dormitzer 2019-01-31 10:00:00 -05:00
parent 9d988d25fb
commit 05221cf91f
2 changed files with 238 additions and 33 deletions

View File

@ -1,14 +1,225 @@
<?php
namespace ActivityPub\Test\Activities;
use ActivityPub\Activities\InboxActivityEvent;
use ActivityPub\Activities\OutboxActivityEvent;
use ActivityPub\Activities\UpdateHandler;
use ActivityPub\Objects\ObjectsService;
use ActivityPub\Test\TestUtils\TestActivityPubObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
class UpdateHandlerTest extends TestCase
{
const OBJECTS = array(
'https://elsewhere.com/objects/1' => array(
'id' => 'https://elsewhere.com/objects/1',
'attributedTo' => 'https://elsewhere.com/actors/1',
),
'https://example.com/objects/1' => array(
'id' => 'https://example.com/objects/1',
'attributedTo' => 'https://example.com/actors/1',
'type' => 'Note',
'content' => 'This is a note',
),
);
/**
* @var EventDispatcher
*/
private $eventDispatcher;
public function setUp()
{
$objectsService = $this->createMock( ObjectsService::class );
$objectsService->method( 'dereference' )->will( $this->returnCallback(
function( $id ) {
if ( array_key_exists( $id, self::OBJECTS ) ) {
return TestActivityPubObject::fromArray( self::OBJECTS[$id] );
}
}
) );
$objectsService->method( 'update' )->will( $this->returnCallback(
function( $id, $updateFields ) {
if ( array_key_exists( $id, self::OBJECTS ) ) {
$existing = self::OBJECTS[$id];
foreach ( $updateFields as $field => $newValue ) {
if ( $newValue === null && array_key_exists( $field, $existing ) ) {
unset( $existing[$field] );
} else {
$existing[$field] = $newValue;
}
}
return TestActivityPubObject::fromArray( $existing );
}
}
) );
$updateHandler = new UpdateHandler( $objectsService );
$this->eventDispatcher = new EventDispatcher();
$this->eventDispatcher->addSubscriber( $updateHandler );
}
public function testUpdateHandler()
{
// TODO implement me
$this->assertTrue( false );
$testCases = array(
array(
'id' => 'basicInboxTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
array(
'id' => 'https://elsewhere.com/activities/1',
'type' => 'Update',
'object' => array(
'id' => 'https://elsewhere.com/objects/1',
),
),
TestActivityPubObject::fromArray( array(
'id' => 'https://elsewhere.com/actor/1',
) ),
self::requestWithAttributes(
'https://example.com/inbox',
array( 'actor' => TestActivityPubObject::fromArray( array(
'id' => 'https://elsewhere.com/actors/1',
) ) )
)
),
'expectedEvent' => new InboxActivityEvent(
array(
'id' => 'https://elsewhere.com/activities/1',
'type' => 'Update',
'object' => array(
'id' => 'https://elsewhere.com/objects/1',
),
),
TestActivityPubObject::fromArray( array(
'id' => 'https://elsewhere.com/actor/1',
) ),
self::requestWithAttributes(
'https://example.com/inbox',
array( 'actor' => TestActivityPubObject::fromArray( array(
'id' => 'https://elsewhere.com/actors/1',
) ) )
)
),
),
array(
'id' => 'basicOutboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
array(
'id' => 'https://example.com/activities/1',
'type' => 'Update',
'object' => array(
'id' => 'https://example.com/objects/1',
'content' => 'This is an updated note',
),
),
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',
) ) )
)
),
'expectedEvent' => new OutboxActivityEvent(
array(
'id' => 'https://example.com/activities/1',
'type' => 'Update',
'object' => array(
'id' => 'https://example.com/objects/1',
'type' => 'Note',
'attributedTo' => 'https://example.com/actors/1',
'content' => 'This is an updated note',
),
),
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',
) ) )
)
),
),
array(
'id' => 'checksInboxAuth',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
array(
'id' => 'https://elsewhere.com/activities/1',
'type' => 'Update',
'object' => array(
'id' => 'https://elsewhere.com/objects/1',
),
),
TestActivityPubObject::fromArray( array(
'id' => 'https://elsewhere.com/actors/2',
) ),
self::requestWithAttributes(
'https://example.com/inbox',
array(
'actor' => TestActivityPubObject::fromArray( array(
'id' => 'https://elsewhere.com/actors/1',
) )
)
)
),
'expectedException' => UnauthorizedHttpException::class,
),
array(
'id' => 'checksOutboxAuth',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
array(
'id' => 'https://example.com/activities/1',
'type' => 'Update',
'object' => array(
'id' => 'https://example.com/objects/1',
),
),
TestActivityPubObject::fromArray( array(
'id' => 'https://example.com/actors/2',
) ),
self::requestWithAttributes(
'https://example.com/outbox',
array(
'actor' => TestActivityPubObject::fromArray( array(
'id' => 'https://example.com/actors/2',
) )
)
)
),
'expectedException' => UnauthorizedHttpException::class,
),
);
foreach ( $testCases as $testCase ) {
$event = $testCase['event'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->expectException( $testCase['expectedException'] );
}
$this->eventDispatcher->dispatch( $testCase['eventName'], $event );
if ( array_key_exists( 'expectedEvent', $testCase ) ) {
$this->assertEquals(
$testCase['expectedEvent'], $event, "Error on test $testCase[id]"
);
}
}
}
public static function requestWithAttributes( $uri, $attributes )
{
$request = Request::create( $uri );
$request->attributes->add( $attributes );
return $request;
}
}
?>

View File

@ -1221,7 +1221,7 @@ class ObjectsServiceTest extends SQLiteTestCase
),
),
array(
'id' => 'itUpdatesTargetObject',
'id' => 'itDeletesOrphanedNodes',
'object' => array(
'id' => 'https://example.com/objects/2',
'type' => 'Article',
@ -1243,31 +1243,22 @@ class ObjectsServiceTest extends SQLiteTestCase
'type' => 'Article',
'attributedTo' => 'https://example.com/actors/1',
),
'expectedDatabaseState' => array(
'objects' => array(
array(
'id' => 1,
'created' => $this->getTime( 'objects-service.create' ),
'updated' => $this->getTime( 'objects-service.update' ),
),
array(
'id' => 2,
'created' => $this->getTime( 'objects-service.create' ),
'updated' => $this->getTime( 'objects-service.update' ),
),
array(
'id' => 3,
'created' => $this->getTime( 'objects-service.create' ),
'updated' => $this->getTime( 'objects-service.update' ),
'expectedQueryResults' => array(
array(
'query' => array( 'id' => 'https://example.com/actors/1' ),
'expectedResult' => array(
array( 'id' => 'https://example.com/actors/1' )
),
),
'fields' => array(
array(
'query' => array( 'bar' => 'baz' ),
'expectedResult' => array(),
),
),
),
);
foreach ( $testCases as $testCase ) {
$this->setUp();
$this->objectsService->persist( $testCase['object'] );
$replacement = $this->objectsService->replace(
$testCase['replacementId'],
@ -1278,20 +1269,23 @@ class ObjectsServiceTest extends SQLiteTestCase
$replacement->asArray(),
"Error on test $testCase[id]"
);
if ( array_key_exists( 'expectedDatabaseState', $testCase ) ) {
$expectedDb = new ArrayDataSet( $testCase['expectedDatabaseState'] );
$expectedObjectsTable = $expectedDb->getTable('objects');
$expectedFieldsTable = $expectedDb->getTable('fields');
$objectsQueryTable = $this->getConnection()->createQueryTable(
'objects', 'SELECT * FROM objects'
);
$fieldsQueryTable = $this->getConnection()->createQueryTable(
'fields', 'SELECT * FROM fields'
);
$this->assertTablesEqual( $expectedObjectsTable, $objectsQueryTable );
$this->assertTablesEqual( $expectedFieldsTable, $fieldsQueryTable );
if ( array_key_exists( 'expectedQueryResults', $testCase ) ) {
foreach ( $testCase['expectedQueryResults'] as $expectedQueryResult ) {
$result = array_map(
function( $obj ) { return $obj->asArray(); },
$this->objectsService->query( $expectedQueryResult['query'] )
);
}
}
}
}
}
?>