Test FollowHandler

This commit is contained in:
Jeremy Dormitzer 2019-02-01 11:07:26 -05:00
parent efc355b7f9
commit eba10d28e9
2 changed files with 101 additions and 7 deletions

View File

@ -16,11 +16,6 @@ class FollowHandler implements EventSubscriberInterface
*/ */
private $autoAccepts; private $autoAccepts;
/**
* @var Client
*/
private $httpClient;
/** /**
* @var ContextProvider * @var ContextProvider
*/ */
@ -34,11 +29,9 @@ class FollowHandler implements EventSubscriberInterface
} }
public function __construct( bool $autoAccepts, public function __construct( bool $autoAccepts,
Client $httpClient,
ContextProvider $contextProvider ) ContextProvider $contextProvider )
{ {
$this->autoAccepts = $autoAccepts; $this->autoAccepts = $autoAccepts;
$this->httpsClient = $httpClient;
$this->contextProvider = $contextProvider; $this->contextProvider = $contextProvider;
} }
@ -52,6 +45,13 @@ class FollowHandler implements EventSubscriberInterface
} }
if ( $this->autoAccepts ) { if ( $this->autoAccepts ) {
$localActor = $event->getActor(); $localActor = $event->getActor();
$objectId = $activity['object'];
if ( is_array( $objectId ) && array_key_exists( 'id', $objectId ) ) {
$objectId = $objectId['id'];
}
if ( $localActor['id'] !== $objectId ) {
return;
}
$accept = array( $accept = array(
'@context' => $this->contextProvider->getContext(), '@context' => $this->contextProvider->getContext(),
'type' => 'Accept', 'type' => 'Accept',

View File

@ -0,0 +1,94 @@
<?php
namespace ActivityPub\Test\Activities;
use ActivityPub\Activities\FollowHandler;
use ActivityPub\Activities\InboxActivityEvent;
use ActivityPub\Activities\OutboxActivityEvent;
use ActivityPub\Objects\ContextProvider;
use ActivityPub\Test\TestUtils\TestActivityPubObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
class FollowHandlerTest extends TestCase
{
public function testFollowHandler()
{
$eventDispatcher = new EventDispatcher();
$contextProvider = new ContextProvider();
$followHandler = new FollowHandler( true, $contextProvider );
$eventDispatcher->addSubscriber( $followHandler );
$outboxDispatched = false;
$actor = TestActivityPubObject::fromArray( array(
'id' => 'https://example.com/actor/1',
'outbox' => 'https://example.com/actor/1/outbox',
) );
$follow = array(
'id' => 'https://elsewhere.com/activities/1',
'type' => 'Follow',
'object' => 'https://example.com/actor/1',
);
$eventDispatcher->addListener( OutboxActivityEvent::NAME, function( $event, $name )
use ( &$outboxDispatched, $actor )
{
$this->assertEquals( OutboxActivityEvent::NAME, $name );
$outboxDispatched = true;
$accept = array(
'@context' => ContextProvider::DEFAULT_CONTEXT,
'type' => 'Accept',
'actor' => 'https://example.com/actor/1',
'object' => 'https://elsewhere.com/activities/1',
);
$expectedRequest = Request::create(
'https://example.com/actor/1/outbox',
Request::METHOD_POST,
array(), array(), array(),
array(
'HTTP_ACCEPT' => 'application/ld+json',
'CONTENT_TYPE' => 'application/json',
),
json_encode( $accept )
);
$expectedRequest->attributes->set( 'actor', $actor );
$this->assertEquals(
new OutboxActivityEvent( $accept, $actor, $expectedRequest ), $event
);
} );
$eventDispatcher->dispatch( InboxActivityEvent::NAME, new InboxActivityEvent(
$follow,
$actor,
Request::create( 'https://example.com/actor/1/inbox' )
) );
$this->assertTrue( $outboxDispatched );
}
public function testItChecksForFollowObject()
{
$eventDispatcher = new EventDispatcher();
$contextProvider = new ContextProvider();
$followHandler = new FollowHandler( true, $contextProvider );
$eventDispatcher->addSubscriber( $followHandler );
$outboxDispatched = false;
$actor = TestActivityPubObject::fromArray( array(
'id' => 'https://example.com/actor/1',
'outbox' => 'https://example.com/actor/1/outbox',
) );
$follow = array(
'id' => 'https://elsewhere.com/activities/1',
'type' => 'Follow',
'object' => 'https://example.com/actor/2',
);
$eventDispatcher->addListener( OutboxActivityEvent::NAME, function( $event )
use ( &$outboxDispatched )
{
$outboxDispatched = true;
} );
$eventDispatcher->dispatch( InboxActivityEvent::NAME, new InboxActivityEvent(
$follow,
$actor,
Request::create( 'https://example.com/actor/1/inbox' )
) );
$this->assertFalse( $outboxDispatched );
}
}
?>