From 4e6f871bc4caf37ab0c310e819218d7939c412d1 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 12 Jun 2024 14:58:14 +0200 Subject: tests: Add a basic test for ActivityStreams This is just a basic test that parses a specific object, and tests that all the referenced objects are fetched from the originating servers. --- tests/unit/Lib/ActivityStreamsTest.php | 136 +++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 tests/unit/Lib/ActivityStreamsTest.php (limited to 'tests/unit/Lib') diff --git a/tests/unit/Lib/ActivityStreamsTest.php b/tests/unit/Lib/ActivityStreamsTest.php new file mode 100644 index 000000000..38be1792e --- /dev/null +++ b/tests/unit/Lib/ActivityStreamsTest.php @@ -0,0 +1,136 @@ + [ 'type' => 'Group' ], + 'https://lemmy.test/u/SomePerson' => [ 'type' => 'Person' ], + 'https://somesite.test/post/1197552' => [ 'type' => 'Note' ], + ]; + + $z_fetch_url_stub = $this->getFunctionMock('Zotlabs\Lib', 'z_fetch_url'); + $z_fetch_url_stub + ->expects($this->any()) + ->willReturnCallback(function ($url) use ($urlmap) { + if (isset($urlmap[$url])) { + $body = json_encode( + array_merge([ 'id' => $url ], $urlmap[$url]), + JSON_FORCE_OBJECT, + ); + + return [ + 'success' => true, + 'body' => $body, + ]; + } else { + // We should perhaps throw an error here to fail the test, + // as we're receiving an unexpected URL. + return [ + 'success' => false, + ]; + } + }); + + // Make sure we have a sys channel before we start + create_sys_channel(); + + $as = new ActivityStreams($payload); + + $this->assertTrue($as->valid); + + $this->assertEquals( + 'https://lemmy.test/activities/announce/like/9e583a54-e4e0-4436-9726-975a14f923ed', + $as->id + ); + + $this->assertEquals('Announce', $as->type); + + $this->assertIsArray($as->actor); + $this->assertArrayHasKey('id', $as->actor); + $this->assertEquals('https://lemmy.test/c/technology', $as->actor['id']); + $this->assertArrayHasKey('type', $as->actor); + $this->assertEquals('Group', $as->actor['type']); + + $this->assertIsArray($as->recips); + $this->assertContains('https://www.w3.org/ns/activitystreams#Public', $as->recips); + $this->assertContains('https://lemmy.test/c/technology/followers', $as->recips); + $this->assertContains('https://lemmy.test/c/technology', $as->recips); + + $this->assertIsArray($as->obj); + $this->assertArrayHasKey('id', $as->obj); + $this->assertEquals( + 'https://lemmy.test/activities/like/e6e38c8b-beee-406f-9523-9da7ec97a823', + $as->obj['id'] + ); + $this->assertArrayHasKey('type', $as->obj); + $this->assertEquals('Like', $as->obj['type']); + $this->assertArrayHasKey('object', $as->obj); + + $this->assertIsArray($as->obj['object']); + + $this->assertArrayHasKey('id', $as->obj['object']); + $this->assertEquals('https://somesite.test/post/1197552', $as->obj['object']['id']); + + $this->assertArrayHasKey('type', $as->obj['object']); + $this->assertEquals('Note', $as->obj['object']['type']); + + $this->assertIsArray($as->obj['actor']); + $this->assertArrayHasKey('id', $as->obj['actor']); + $this->assertEquals('https://lemmy.test/u/SomePerson', $as->obj['actor']['id']); + $this->assertArrayHasKey('type', $as->obj['actor']); + $this->assertEquals('Person', $as->obj['actor']['type']); + } +} -- cgit v1.2.3