2019-02-01 16:07:26 +00:00
|
|
|
<?php
|
2019-02-16 17:51:24 +00:00
|
|
|
|
2019-02-01 16:07:26 +00:00
|
|
|
namespace ActivityPub\Test\Activities;
|
|
|
|
|
|
|
|
use ActivityPub\Activities\FollowHandler;
|
|
|
|
use ActivityPub\Activities\InboxActivityEvent;
|
|
|
|
use ActivityPub\Activities\OutboxActivityEvent;
|
|
|
|
use ActivityPub\Objects\ContextProvider;
|
2019-02-07 03:48:00 +00:00
|
|
|
use ActivityPub\Test\TestConfig\APTestCase;
|
2019-02-16 17:51:24 +00:00
|
|
|
use ActivityPub\Test\TestUtils\TestActivityPubObject;
|
2019-02-01 16:07:26 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
2019-02-07 03:48:00 +00:00
|
|
|
class FollowHandlerTest extends APTestCase
|
2019-02-01 16:07:26 +00:00
|
|
|
{
|
|
|
|
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',
|
|
|
|
);
|
2019-02-16 17:51:24 +00:00
|
|
|
$eventDispatcher->addListener( OutboxActivityEvent::NAME, function ( $event, $name )
|
|
|
|
use ( &$outboxDispatched, $actor, $follow ) {
|
2019-02-01 16:07:26 +00:00
|
|
|
$this->assertEquals( OutboxActivityEvent::NAME, $name );
|
|
|
|
$outboxDispatched = true;
|
|
|
|
$accept = array(
|
2019-02-07 03:48:00 +00:00
|
|
|
'@context' => ContextProvider::getDefaultContext(),
|
2019-02-01 16:07:26 +00:00
|
|
|
'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 )
|
|
|
|
);
|
2019-02-07 03:48:00 +00:00
|
|
|
$expectedRequest->attributes->add( array(
|
|
|
|
'actor' => $actor,
|
|
|
|
'follow' => $follow,
|
|
|
|
) );
|
2019-02-01 16:07:26 +00:00
|
|
|
$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',
|
|
|
|
);
|
2019-02-16 17:51:24 +00:00
|
|
|
$eventDispatcher->addListener( OutboxActivityEvent::NAME, function ()
|
|
|
|
use ( &$outboxDispatched ) {
|
|
|
|
$outboxDispatched = true;
|
|
|
|
} );
|
2019-02-01 16:07:26 +00:00
|
|
|
$eventDispatcher->dispatch( InboxActivityEvent::NAME, new InboxActivityEvent(
|
|
|
|
$follow,
|
|
|
|
$actor,
|
|
|
|
Request::create( 'https://example.com/actor/1/inbox' )
|
|
|
|
) );
|
|
|
|
$this->assertFalse( $outboxDispatched );
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 03:48:00 +00:00
|
|
|
|