diff options
Diffstat (limited to 'tests/unit/Lib')
-rw-r--r-- | tests/unit/Lib/ActivityStreamsTest.php | 136 | ||||
-rw-r--r-- | tests/unit/Lib/ActivityTest.php | 2 | ||||
-rw-r--r-- | tests/unit/Lib/MailerTest.php | 62 | ||||
-rw-r--r-- | tests/unit/Lib/PermissionDescriptionTest.php | 20 | ||||
-rw-r--r-- | tests/unit/Lib/ZotlibTest.php | 38 |
5 files changed, 237 insertions, 21 deletions
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 @@ +<?php +/** + * Unit tests for Zotlabs\Lib\ActivityStreams. + * + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\Lib; + +use phpmock\phpunit\PHPMock; +use Zotlabs\Lib\ActivityStreams; +use Zotlabs\Tests\Unit\UnitTestCase; + +class ActivityStreamsTest extends UnitTestCase { + + // Import PHPMock methods into this class + use PHPMock; + + /** + * Test parsing an announce activity of a like from a remote server of + * a note from a third server. + * + * Also test that we fetch the referenced objects when the received + * activity is parsed, + */ + public function test_parse_announce_of_like(): void { + $payload = <<<JSON + { + "actor": "https://lemmy.test/c/technology", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "object": { + "id": "https://lemmy.test/activities/like/e6e38c8b-beee-406f-9523-9da7ec97a823", + "actor": "https://lemmy.test/u/SomePerson", + "object": "https://somesite.test/post/1197552", + "type": "Like", + "audience": "https://lemmy.test/c/technology" + }, + "cc": [ + "https://lemmy.test/c/technology/followers" + ], + "type": "Announce", + "id": "https://lemmy.test/activities/announce/like/9e583a54-e4e0-4436-9726-975a14f923ed" + } + JSON; + + // + // Mock z_fetch_url to prevent us from spamming real servers during test runs + // + // We just create some sample ActivityStreams objects to return for the various + // URL's to make it a somewhat realistic test. Each object will have it's URL as + // it's id and only specify the object type as laid out in the $urlmap below. + + $urlmap = [ + 'https://lemmy.test/c/technology' => [ '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']); + } +} diff --git a/tests/unit/Lib/ActivityTest.php b/tests/unit/Lib/ActivityTest.php index c9ce79d8c..0e2703f2b 100644 --- a/tests/unit/Lib/ActivityTest.php +++ b/tests/unit/Lib/ActivityTest.php @@ -19,7 +19,7 @@ class ActivityTest extends UnitTestCase { /** * Dataprovider for test_get_textfield. */ - private function get_textfield_provider(): array { + public static function get_textfield_provider(): array { return [ 'get content field' => [ ['content' => 'Some content'], diff --git a/tests/unit/Lib/MailerTest.php b/tests/unit/Lib/MailerTest.php new file mode 100644 index 000000000..038c7ef4c --- /dev/null +++ b/tests/unit/Lib/MailerTest.php @@ -0,0 +1,62 @@ +<?php +/* + * Tests for the Zotlabs\LibMÌ€ailer class. + * + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\Lib; + +use App; +use phpmock\phpunit\PHPMock; +use Zotlabs\Lib\Mailer; +use Zotlabs\Tests\Unit\UnitTestCase; + +class MailerTest extends UnitTestCase { + + use PHPMock; + + public function test_optional_params_replaced_by_defaults(): void { + $hostname = App::get_hostname(); + $recipient = 'recipient@somesite.test'; + $subject = 'A test email'; + $body = <<<EOF + Dear recipient, + + This is an test email message for you. + + Sincerely, + Hubzilla + EOF; + + // + // Catch calls to the php mail function, and verify + // that it is called with the args we're expecting + // + $this->getFunctionMock('Zotlabs\Lib', 'mail') + ->expects($this->once()) + ->with( + $this->identicalTo($recipient), + $this->identicalTo($subject), + $this->identicalTo($body), + $this->identicalTo(<<<EOF + From: <Administrator@{$hostname}> + Reply-To: <noreply@{$hostname}> + Content-Type: text/plain; charset=UTF-8 + EOF + ) + ) + ->willReturn(true); + + $mailer = new Mailer([ + 'toEmail' => $recipient, + 'messageSubject' => $subject, + 'textVersion' => $body, + ]); + + $mailer->deliver(); + } +} diff --git a/tests/unit/Lib/PermissionDescriptionTest.php b/tests/unit/Lib/PermissionDescriptionTest.php index fdd676f61..1e4b5292c 100644 --- a/tests/unit/Lib/PermissionDescriptionTest.php +++ b/tests/unit/Lib/PermissionDescriptionTest.php @@ -46,16 +46,6 @@ class PermissionDescriptionTest extends UnitTestCase { } public function testFromStandalonePermission() { - // Create a stub for global function t() - $t = $this->getFunctionMock('Zotlabs\Lib', 't'); - $t->expects($this->atLeastOnce())->willReturnCallback( - function ($string) { - return $string; - } - ); - // Create a mock for global function logger() - $this->getFunctionMock('Zotlabs\Lib', 'logger'); - $permDescUnknown = PermissionDescription::fromStandalonePermission(-1); $permDescSelf = PermissionDescription::fromStandalonePermission(0); @@ -113,16 +103,6 @@ class PermissionDescriptionTest extends UnitTestCase { } public function testGetPermissionDescription() { - // Create a stub for global function t() - $t = $this->getFunctionMock('Zotlabs\Lib', 't'); - $t->expects($this->atLeastOnce())->willReturnCallback( - function ($string) { - return $string; - } - ); - // Create a mock for global function logger() - $this->getFunctionMock('Zotlabs\Lib', 'logger'); - // Create a stub for the PermissionDescription class $stub = $this->createMock(PermissionDescription::class); $stub->method('get_permission_description') diff --git a/tests/unit/Lib/ZotlibTest.php b/tests/unit/Lib/ZotlibTest.php new file mode 100644 index 000000000..0ab89dc2f --- /dev/null +++ b/tests/unit/Lib/ZotlibTest.php @@ -0,0 +1,38 @@ +<?php +namespace Zotlabs\Tests\Unit\Lib; + +use Zotlabs\Tests\Unit\UnitTestCase; + +class ZotlibTest extends UnitTestCase { + /** + * Test the `get_rpost_path` function. + * + * @dataProvider get_rpost_path_provider + */ + public function test_get_rpost_path(string $expected, string $xchan_url) : void { + $observer = [ 'xchan_url' => $xchan_url ]; + + $this->assertEquals($expected, \Zotlabs\Lib\Libzot::get_rpost_path($observer)); + } + + public static function get_rpost_path_provider() : array { + return [ + 'xchan_url without port' => [ + 'https://example.com/rpost?f=', + 'https://example.com' + ], + 'xchan_url with port' => [ + 'https://example.com:666/rpost?f=', + 'https://example.com:666' + ], + 'xchan_url ignores path and args' => [ + 'https://example.com/rpost?f=', + 'https://example.com/path?arg1=balle' + ], + 'xchan_url with no scheme should default to https' => [ + 'https://example.com/rpost?f=', + 'example.com', + ], + ]; + } +} |