From 05221cf91f37dda7a0204171beba1cbfc3253e01 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 31 Jan 2019 10:00:00 -0500 Subject: [PATCH] Test UpdateHandler and ObjectsService::replace --- test/Activities/UpdateHandlerTest.php | 215 +++++++++++++++++++++++++- test/Objects/ObjectsServiceTest.php | 56 +++---- 2 files changed, 238 insertions(+), 33 deletions(-) diff --git a/test/Activities/UpdateHandlerTest.php b/test/Activities/UpdateHandlerTest.php index 559ecf7..b075e0c 100644 --- a/test/Activities/UpdateHandlerTest.php +++ b/test/Activities/UpdateHandlerTest.php @@ -1,14 +1,225 @@ 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; } } ?> diff --git a/test/Objects/ObjectsServiceTest.php b/test/Objects/ObjectsServiceTest.php index c64258b..f086bb0 100644 --- a/test/Objects/ObjectsServiceTest.php +++ b/test/Objects/ObjectsServiceTest.php @@ -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'] ) + ); + } } } } } ?> + + + + + + + +