From 0649a5154c4d0b4629dce68694be4e7afb542f33 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Tue, 15 Mar 2022 17:49:42 +0000 Subject: [PATCH] [PLUGIN][ActivityPub][Test][Model][Note] fromJson --- .../Test/Fixtures/ActivityPubFixtures.php | 6 ++ .../gnusocial/objects/another_person.jsonld | 42 ++++++++ .../objects/note_with_mention.jsonld | 47 +++++++++ .../Test/Objects/GSObjectNoteTest.php | 99 +++++++++++++++++++ .../Test/Objects/GSObjectPageTest.php | 64 ++++++++++++ 5 files changed, 258 insertions(+) create mode 100755 plugins/ActivityPub/Test/Fixtures/gnusocial/objects/another_person.jsonld create mode 100755 plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note_with_mention.jsonld create mode 100644 plugins/ActivityPub/Test/Objects/GSObjectNoteTest.php create mode 100644 plugins/ActivityPub/Test/Objects/GSObjectPageTest.php diff --git a/plugins/ActivityPub/Test/Fixtures/ActivityPubFixtures.php b/plugins/ActivityPub/Test/Fixtures/ActivityPubFixtures.php index 70bf09c771..e4d1f04c8b 100644 --- a/plugins/ActivityPub/Test/Fixtures/ActivityPubFixtures.php +++ b/plugins/ActivityPub/Test/Fixtures/ActivityPubFixtures.php @@ -32,6 +32,9 @@ class ActivityPubFixtures extends Fixture $person_path = self::fixturesPath('objects/person.jsonld', $ontology); $person = Actor::fromJson(fread(fopen($person_path, 'r'), filesize($person_path))); DB::flush(); + $another_person_path = self::fixturesPath('objects/another_person.jsonld', $ontology); + $another_person = Actor::fromJson(fread(fopen($another_person_path, 'r'), filesize($another_person_path))); + DB::flush(); $group_path = self::fixturesPath('objects/group.jsonld', $ontology); $group = Actor::fromJson(fread(fopen($group_path, 'r'), filesize($group_path))); DB::flush(); @@ -46,6 +49,9 @@ class ActivityPubFixtures extends Fixture $reply_path = self::fixturesPath('objects/reply.jsonld', $ontology); $reply = Note::fromJson(fread(fopen($reply_path, 'r'), filesize($reply_path))); DB::flush(); + $note_with_mention_path = self::fixturesPath('objects/note_with_mention.jsonld', $ontology); + $note_with_mention = Note::fromJson(fread(fopen($note_with_mention_path, 'r'), filesize($note_with_mention_path))); + DB::flush(); // Load Activities $create_note_path = self::fixturesPath('activities/create_note.jsonld', $ontology); diff --git a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/another_person.jsonld b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/another_person.jsonld new file mode 100755 index 0000000000..44b6b61b83 --- /dev/null +++ b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/another_person.jsonld @@ -0,0 +1,42 @@ +{ + "type": "Person", + "streams": [], + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "gs": "https://www.gnu.org/software/social/ns#" + }, + { + "litepub": "http://litepub.social/ns#" + }, + { + "chatMessage": "litepub:chatMessage" + }, + { + "inConversation": { + "@id": "gs:inConversation", + "@type": "@id" + } + } + ], + "id": "https://another_instance.gnusocial.test/actor/43", + "inbox": "https://another_instance.gnusocial.test/actor/43/inbox.json", + "outbox": "https://another_instance.gnusocial.test/actor/43/outbox.json", + "following": "https://another_instance.gnusocial.test/actor/43/subscriptions", + "followers": "https://another_instance.gnusocial.test/actor/43/subscribers", + "liked": "https://another_instance.gnusocial.test/actor/43/favourites", + "preferredUsername": "alice", + "publicKey": { + "id": "https://another_instance.gnusocial.test/actor/43#public-key", + "owner": "https://another_instance.gnusocial.test/actor/43", + "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArRHP8lxqj1HqFL4q7CKh\noDyBvuhoaoWo/AGdjiWu5AEODL6utaQX+bCJApH9+XNICCkWvayKupgOvLPqBxkh\nl4TPUjlb1iCt+iZeMB8ftude4epaUNUDdqK1zG3g8z8AdF3nx9/cHI+8UY7+JAh6\naZ5EBi+wNYtl4UoDfizmLeRmmGIam5UQ6x2RseYCevIm1BBCZZHLdIaoPJphyjLW\n8sRJtHL4D3m28NkGG8GizctXHbMl7+RlVJ8HyQSr5taRMF+CmZ9ZDFqF2ewc9Pmw\nOMG4o/6m50Q2ELz23O8idjGxKgG7iGdEa3c5cQZTCQ0+2N77L+iS029AV9AKyZMi\nCwIDAQAB\n-----END PUBLIC KEY-----\n" + }, + "name": "Alice P. Hacker", + "published": "2022-02-23T17:20:30+00:00", + "updated": "2022-02-25T02:12:48+00:00", + "url": "https://another_instance.gnusocial.test/@alice", + "endpoints": { + "sharedInbox": "https://another_instance.gnusocial.test/inbox.json" + } +} \ No newline at end of file diff --git a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note_with_mention.jsonld b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note_with_mention.jsonld new file mode 100755 index 0000000000..0fca0cf93f --- /dev/null +++ b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note_with_mention.jsonld @@ -0,0 +1,47 @@ +{ + "type": "Note", + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "gs": "https://www.gnu.org/software/social/ns#" + }, + { + "litepub": "http://litepub.social/ns#" + }, + { + "chatMessage": "litepub:chatMessage" + }, + { + "inConversation": { + "@id": "gs:inConversation", + "@type": "@id" + } + } + ], + "id": "https://instance.gnusocial.test/object/note/1340", + "published": "2022-03-16T21:54:43+00:00", + "attributedTo": "https://instance.gnusocial.test/actor/42", + "content": "

This is a public root note with a mention to @alice@another_instance.gnusocial.test.

", + "mediaType": "text/html", + "source": { + "content": "This is a public root note with a mention to @alice@another_instance.gnusocial.test.", + "mediaType": "text/plain" + }, + "attachment": [], + "tag": [ + { + "type": "Mention", + "href": "https://another_instance.gnusocial.test/actor/43", + "name": "@alice@another_instance.gnusocial.test" + } + ], + "inConversation": "https://instance.gnusocial.test/conversation/1340", + "to": [ + "https://www.w3.org/ns/activitystreams#Public", + "https://testv3.gnusocial.rocks/actor/43" + ], + "cc": [ + "https://testv3.gnusocial.rocks/actor/43" + ] +} \ No newline at end of file diff --git a/plugins/ActivityPub/Test/Objects/GSObjectNoteTest.php b/plugins/ActivityPub/Test/Objects/GSObjectNoteTest.php new file mode 100644 index 0000000000..8cc9640afe --- /dev/null +++ b/plugins/ActivityPub/Test/Objects/GSObjectNoteTest.php @@ -0,0 +1,99 @@ +. + +// }}} + +namespace Plugin\ActivityPub\Test\Objects; + +use App\Core\VisibilityScope; +use App\Entity\Note; +use App\Util\GNUsocialTestCase; +use Plugin\ActivityPub\ActivityPub; +use Plugin\ActivityPub\Entity\ActivitypubObject; +use Plugin\ActivityPub\Util\Explorer; + +class GSObjectNoteTest extends GNUsocialTestCase +{ + public function testNoteFromJson() + { + self::bootKernel(); + + $actor_uri = 'https://instance.gnusocial.test/actor/42'; + $object_uri = 'https://instance.gnusocial.test/object/note/1337'; + $note = ActivityPub::getObjectByUri($object_uri, try_online: false); + static::assertInstanceOf(Note::class, $note); + + static::assertSame(Explorer::getOneFromUri($actor_uri)->getId(), $note->getActorId()); + static::assertSame('text/plain', $note->getContentType()); + static::assertSame('hello, world.', $note->getContent()); + static::assertSame('

hello, world.

', $note->getRendered()); + static::assertSame('ActivityPub', $note->getSource()); + static::assertNull($note->getReplyTo()); + static::assertFalse($note->getIsLocal()); + static::assertSame(VisibilityScope::EVERYWHERE, $note->getScope()); + static::assertSame($object_uri, $note->getUrl()); + static::assertNull($note->getLanguageId()); + static::assertSame('note', $note->getType()); + static::assertNull($note->getTitle()); + + $ap_object = ActivitypubObject::getByPK(['object_uri' => $object_uri]); + static::assertSame(Note::schemaName(), $ap_object->getObjectType()); + static::assertSame($object_uri, $ap_object->getObjectUri()); + static::assertSame($note->getId(), $ap_object->getObjectId()); + + static::assertSame([], $note->getAttentionTargets()); + static::assertSame([], $note->getMentionTargets()); + } + + public function testNoteWithMentionFromJson() + { + self::bootKernel(); + + $actor_uri = 'https://instance.gnusocial.test/actor/42'; + $another_actor_uri = 'https://another_instance.gnusocial.test/actor/43'; + $object_uri = 'https://instance.gnusocial.test/object/note/1340'; + $note = ActivityPub::getObjectByUri($object_uri, try_online: false); + static::assertInstanceOf(Note::class, $note); + + static::assertSame(Explorer::getOneFromUri($actor_uri, try_online: false)->getId(), $note->getActorId()); + static::assertSame('text/plain', $note->getContentType()); + static::assertSame('This is a public root note with a mention to @alice@another_instance.gnusocial.test.', $note->getContent()); + // TODO: implement proper sanitization rules + //static::assertSame('

This is a public root note with a mention to @alice@another_instance.gnusocial.test.

', $note->getRendered()); + static::assertSame('ActivityPub', $note->getSource()); + static::assertNull($note->getReplyTo()); + static::assertFalse($note->getIsLocal()); + static::assertSame(VisibilityScope::EVERYWHERE, $note->getScope()); + static::assertSame($object_uri, $note->getUrl()); + static::assertNull($note->getLanguageId()); + static::assertSame('note', $note->getType()); + static::assertNull($note->getTitle()); + + $ap_object = ActivitypubObject::getByPK(['object_uri' => $object_uri]); + static::assertSame(Note::schemaName(), $ap_object->getObjectType()); + static::assertSame($object_uri, $ap_object->getObjectUri()); + static::assertSame($note->getId(), $ap_object->getObjectId()); + + static::assertCount(1, $attT = $note->getAttentionTargets()); + static::assertObjectEquals(Explorer::getOneFromUri($another_actor_uri, try_online: false), $attT[0]); + static::assertSame([], $note->getMentionTargets()); + } +} diff --git a/plugins/ActivityPub/Test/Objects/GSObjectPageTest.php b/plugins/ActivityPub/Test/Objects/GSObjectPageTest.php new file mode 100644 index 0000000000..8a8f26d58d --- /dev/null +++ b/plugins/ActivityPub/Test/Objects/GSObjectPageTest.php @@ -0,0 +1,64 @@ +. + +// }}} + +namespace Plugin\ActivityPub\Test\Objects; + +use App\Core\VisibilityScope; +use App\Entity\Note; +use App\Util\GNUsocialTestCase; +use Plugin\ActivityPub\ActivityPub; +use Plugin\ActivityPub\Entity\ActivitypubObject; +use Plugin\ActivityPub\Util\Explorer; + +class GSObjectPageTest extends GNUsocialTestCase +{ + public function testNoteFromJson() + { + self::bootKernel(); + + $actor_uri = 'https://instance.gnusocial.test/actor/42'; + $object_uri = 'https://instance.gnusocial.test/object/note/1338'; + $page = ActivityPub::getObjectByUri($object_uri, try_online: false); + static::assertInstanceOf(Note::class, $page); + + static::assertSame(Explorer::getOneFromUri($actor_uri)->getId(), $page->getActorId()); + static::assertSame('text/plain', $page->getContentType()); + static::assertSame('This is an interesting page.', $page->getContent()); + static::assertSame('

This is an interesting page.

', $page->getRendered()); + static::assertSame('ActivityPub', $page->getSource()); + static::assertNull($page->getReplyTo()); + static::assertFalse($page->getIsLocal()); + static::assertSame(VisibilityScope::EVERYWHERE, $page->getScope()); + static::assertSame($object_uri, $page->getUrl()); + static::assertNull($page->getLanguageId()); + static::assertSame('page', $page->getType()); + static::assertSame('hello, world.', $page->getTitle()); + + $ap_object = ActivitypubObject::getByPK(['object_uri' => $object_uri]); + static::assertSame(Note::schemaName(), $ap_object->getObjectType()); + static::assertSame($object_uri, $ap_object->getObjectUri()); + static::assertSame($page->getId(), $ap_object->getObjectId()); + + //dd($page->getAttentionTargets(), $page->getMentionTargets()); + } +}