[ENTITY][TESTS] Fix Note->isVisibleTo with and associated test

This commit is contained in:
Hugo Sales 2022-03-13 15:03:03 +00:00
parent eb3c848fc8
commit cf05d3dbb0
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 16 additions and 20 deletions

View File

@ -98,8 +98,8 @@ class CoreFixtures extends Fixture
}
$group_notes = [];
$group_notes[] = $public_group_note = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'group note', 'scope' => VisibilityScope::GROUP, 'content_type' => 'text/plain', 'is_local' => true]);
$group_notes[] = $private_group_note = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'group note', 'scope' => VisibilityScope::GROUP, 'content_type' => 'text/plain', 'is_local' => true]);
$group_notes[] = $public_group_note = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'group note public', 'scope' => VisibilityScope::EVERYWHERE, 'content_type' => 'text/plain', 'is_local' => true]);
$group_notes[] = $private_group_note = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'group note private', 'scope' => VisibilityScope::GROUP, 'content_type' => 'text/plain', 'is_local' => true]);
foreach ($group_notes as $note) {
$manager->persist($note);
$activity = Activity::create(['actor_id' => $actors['taken_user']->getId(), 'verb' => 'create', 'object_type' => 'note', 'object_id' => $note->getId(), 'source' => 'auto-test']);

View File

@ -32,6 +32,7 @@ use App\Core\Log;
use App\Core\Router\Router;
use App\Core\VisibilityScope;
use App\Util\Common;
use App\Util\Exception\BugFoundException;
use App\Util\Exception\ClientException;
use App\Util\Exception\NoSuchNoteException;
use App\Util\Formatting;
@ -468,19 +469,18 @@ class Note extends Entity
// Either is a Public Group OR
!($in->getRoles() & ActorLocalRoles::PRIVATE_GROUP)
// Both the actor and the note are elements that concern the group
|| DB::dql( // TODO: Fix this query, @see NoteTest.php
|| DB::dql(
<<<'EOF'
SELECT m FROM \Component\Group\Entity\GroupMember m
JOIN \Component\Notification\Entity\Notification att WITH m.group_id = att.target_id
JOIN \App\Entity\Activity a WITH att.activity_id = a.id
WHERE a.object_type = 'note' AND a.object_id = :note_id AND m.actor_id = :actor_id
WHERE a.object_type = 'note' AND a.object_id = :note_id AND m.group_id = :group_id AND m.actor_id = :actor_id
EOF,
['note_id' => $this->id, 'actor_id' => $in->getId()]
['note_id' => $this->getId(), 'group_id' => $in->getId(), 'actor_id' => $actor->getId()]
) !== []
);
default:
Log::error("Unknown scope found: {$this->getScope()->value}.");
throw new BugFoundException("Unknown scope found: {$this->getScope()->value}");
}
return false;
}

View File

@ -50,29 +50,25 @@ class NoteTest extends GNUsocialTestCase
public function testIsVisibleTo()
{
$actor1 = DB::findOneBy(Actor::class, ['nickname' => 'taken_user']);
static::bootKernel();
$actor = DB::findOneBy(Actor::class, ['nickname' => 'taken_user']);
$private_group = DB::findOneBy(Actor::class, ['nickname' => 'taken_private_group']);
$private_group_member = DB::findOneBy(Actor::class, ['nickname' => 'some_user']);
$public_group = DB::findOneBy(Actor::class, ['nickname' => 'taken_public_group']);
$actor2 = DB::findOneBy(Actor::class, ['nickname' => 'some_user']);
$note_visible_to_1 = DB::findBy(Note::class, ['actor_id' => $actor1->getId(), 'content' => 'private note', 'scope' => VisibilityScope::MESSAGE->value], limit: 1)[0];
static::assertTrue($note_visible_to_1->isVisibleTo($actor1));
static::assertFalse($note_visible_to_1->isVisibleTo($actor2));
$note_visible_to_1 = DB::findBy(Note::class, ['actor_id' => $actor->getId(), 'content' => 'private note', 'scope' => VisibilityScope::MESSAGE->value], limit: 1)[0];
static::assertTrue($note_visible_to_1->isVisibleTo($actor));
static::assertFalse($note_visible_to_1->isVisibleTo($private_group));
static::assertFalse($note_visible_to_1->isVisibleTo($private_group_member));
$note_public = DB::findBy(Note::class, ['actor_id' => $actor1->getId(), 'content' => 'some other content'], limit: 1)[0];
static::assertTrue($note_public->isVisibleTo($actor1));
static::assertTrue($note_public->isVisibleTo($actor2));
$note_public = DB::findBy(Note::class, ['actor_id' => $actor->getId(), 'content' => 'some other content'], limit: 1)[0];
static::assertTrue($note_public->isVisibleTo($actor));
static::assertTrue($note_public->isVisibleTo($private_group));
static::assertTrue($note_public->isVisibleTo($private_group_member));
$group_note = DB::findBy(Note::class, ['actor_id' => $actor1->getId(), 'content' => 'group note', 'scope' => VisibilityScope::GROUP->value], limit: 1)[0];
// TODO: Fix group query, @see Note->isVisibleTo
//static::assertTrue($group_note->isVisibleTo($private_group_member, in: $private_group));
static::assertFalse($group_note->isVisibleTo($actor1, in: $private_group));
$group_note = DB::findBy(Note::class, ['actor_id' => $actor->getId(), 'content' => 'group note private', 'scope' => VisibilityScope::GROUP->value], limit: 1)[0];
static::assertTrue($group_note->isVisibleTo($private_group_member, in: $private_group));
static::assertFalse($group_note->isVisibleTo($actor, in: $private_group));
static::assertFalse($group_note->isVisibleTo($private_group, in: $private_group));
static::assertFalse($group_note->isVisibleTo($actor2, in: $private_group));
}
}