activitypub-php/test/ActivityEventHandlers/FollowHandlerTest.php

97 lines
3.7 KiB
PHP
Raw Normal View History

2019-02-01 16:07:26 +00:00
<?php
2019-02-16 17:51:24 +00:00
2019-03-20 23:16:56 +00:00
namespace ActivityPub\Test\ActivityEventHandlers;
2019-02-01 16:07:26 +00:00
use ActivityPub\ActivityEventHandlers\FollowHandler;
use ActivityPub\ActivityEventHandlers\InboxActivityEvent;
use ActivityPub\ActivityEventHandlers\OutboxActivityEvent;
2019-02-01 16:07:26 +00:00
use ActivityPub\Objects\ContextProvider;
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;
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(
'@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 )
);
$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 );
}
}