[PLUGIN][ActivityPub][Test][Model][Note] fromJson

This commit is contained in:
Diogo Peralta Cordeiro 2022-03-15 17:49:42 +00:00
parent 91fecd77ba
commit 0649a5154c
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
5 changed files with 258 additions and 0 deletions

View File

@ -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);

View File

@ -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"
}
}

View File

@ -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": "<p>This is a public root note with a mention to @<span class=\"h-card\"><a href=\"https://another_instance.gnusocial.test/actor/43\" class=\"h-card u-url p-nickname mention\">alice@another_instance.gnusocial.test</a></span>.</p>",
"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"
]
}

View File

@ -0,0 +1,99 @@
<?php
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
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('<p>hello, world.</p>', $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('<p>This is a public root note with a mention to @<span class=\"h-card\"><a href=\"https://another_instance.gnusocial.test/actor/43\" class=\"h-card u-url p-nickname mention\">alice@another_instance.gnusocial.test</a></span>.</p>', $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());
}
}

View File

@ -0,0 +1,64 @@
<?php
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
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('<p>This is an interesting page.</p>', $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());
}
}