[TESTS] Raise test coverage for App\Controller\Network to 100% and fixup related code

This commit is contained in:
Hugo Sales 2021-05-23 19:56:45 +00:00
parent c5d4b7ecac
commit 74f477489b
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
5 changed files with 108 additions and 8 deletions

View File

@ -41,6 +41,7 @@ use App\Core\VisibilityScope;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\NotFoundException;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class Network extends Controller class Network extends Controller
@ -65,8 +66,9 @@ class Network extends Controller
public function home(Request $request, string $nickname) public function home(Request $request, string $nickname)
{ {
$target = DB::findOneBy('gsactor', ['nickname' => $nickname]); try {
if ($target == null) { $target = DB::findOneBy('gsactor', ['nickname' => $nickname]);
} catch (NotFoundException) {
throw new ClientException(_m('User {nickname} doesn\'t exist', ['{nickname}' => $nickname])); throw new ClientException(_m('User {nickname} doesn\'t exist', ['{nickname}' => $nickname]));
} }
@ -106,7 +108,7 @@ END;
public function network(Request $request) public function network(Request $request)
{ {
$notes = Note::getAllNotes($this->instance_scope); $notes = Note::getAllNotes($this->public_scope);
Event::handle('FormatNoteList', [&$notes]); Event::handle('FormatNoteList', [&$notes]);

View File

@ -89,7 +89,7 @@ class GNUsocial implements EventSubscriberInterface
protected SanitizerInterface $sanitizer; protected SanitizerInterface $sanitizer;
protected ContainerBagInterface $config; protected ContainerBagInterface $config;
protected \Twig\Environment $twig; protected \Twig\Environment $twig;
protected Request $request; protected ?Request $request;
/** /**
* Symfony dependency injection gives us access to these services * Symfony dependency injection gives us access to these services
@ -140,7 +140,9 @@ class GNUsocial implements EventSubscriberInterface
{ {
if (!$this->initialized) { if (!$this->initialized) {
Common::setupConfig($this->config); Common::setupConfig($this->config);
Common::setRequest($this->request); if (!is_null($this->request)) {
Common::setRequest($this->request);
}
Log::setLogger($this->logger); Log::setLogger($this->logger);
Event::setDispatcher($this->event_dispatcher); Event::setDispatcher($this->event_dispatcher);
I18n::setTranslator($this->translator); I18n::setTranslator($this->translator);

View File

@ -2,6 +2,7 @@
namespace App\DataFixtures; namespace App\DataFixtures;
use App\Core\VisibilityScope;
use App\Entity\GroupInbox; use App\Entity\GroupInbox;
use App\Entity\GSActor; use App\Entity\GSActor;
use App\Entity\LocalGroup; use App\Entity\LocalGroup;
@ -27,8 +28,13 @@ class CoreFixtures extends Fixture
$actors[$nick] = $actor; $actors[$nick] = $actor;
} }
$note = Note::create(['gsactor_id' => $actors['taken_user']->getId(), 'content' => 'some content']); $n = Note::create(['gsactor_id' => $actors['taken_user']->getId(), 'content' => 'some content']);
$manager->persist($note); $manager->persist($n);
$notes[] = Note::create(['gsactor_id' => $actors['taken_user']->getId(), 'content' => 'some other content', 'reply_to' => $n->getId()]);
$notes[] = Note::create(['gsactor_id' => $actors['taken_user']->getId(), 'content' => 'private note', 'scope' => VisibilityScope::FOLLOWER]);
foreach ($notes as $note) {
$manager->persist($note);
}
$manager->persist(GroupInbox::create(['group_id' => $local_entities['taken_group']->getGroupId(), 'activity_id' => $note->getId()])); $manager->persist(GroupInbox::create(['group_id' => $local_entities['taken_group']->getGroupId(), 'activity_id' => $note->getId()]));
$manager->flush(); $manager->flush();

View File

@ -247,7 +247,7 @@ class Note extends Entity
* *
* @param mixed $a * @param mixed $a
*/ */
public function isVisibleTo(/* GSActor|LocalUser */ $a): bool public function isVisibleTo(GSActor | LocalUser $a): bool
{ {
$scope = VisibilityScope::create($this->scope); $scope = VisibilityScope::create($this->scope);
return $scope->public return $scope->public

View File

@ -0,0 +1,90 @@
<?php
// {{{ 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 App\Tests\Core;
use App\Controller\Network;
use App\Core\DB\DB;
use App\Core\Security;
use App\Core\VisibilityScope;
use App\Entity\Note;
use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\GNUsocialTestCase;
use Jchook\AssertThrows\AssertThrows;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security as SSecurity;
class NetworkTest extends GNUsocialTestCase
{
use AssertThrows;
public function testPublic()
{
$this->testRoute('public', fn ($vis) => $vis->public || $vis->site);
}
public function testHome()
{
$this->testRoute('home', fn ($vis) => !$vis->message, ['taken_user']);
}
public function testNetwork()
{
$this->testRoute('network', fn ($vis) => $vis->public);
}
public function testReplies()
{
$this->testRoute('replies', fn ($vis) => $vis->public, [], function () {
$user = DB::findOneBy('local_user', ['nickname' => 'taken_user']);
$sec = $this->getMockBuilder(SSecurity::class)->setConstructorArgs([self::$kernel->getContainer()])->getMock();
$sec->method('getUser')->willReturn($user);
Security::setHelper($sec, null);
});
}
private function testRoute(string $route, callable $visibility, array $extra_args = [], callable $setup_login = null)
{
parent::bootKernel();
if (!is_null($setup_login)) {
$setup_login();
}
$req = $this->createMock(Request::class);
$req_stack = $this->createMock(RequestStack::class);
$network = new Network($req_stack);
if ($route == 'home') {
static::assertThrows(ClientException::class, fn () => $network->home($req, 'username_not_taken'));
}
$result = $network->{$route}($req, ...$extra_args);
static::assertSame($result['_template'], 'network/public.html.twig');
foreach ($result['notes'] as $n) {
static::assertTrue(is_array($n['replies']));
}
$notes = Common::flattenNoteArray($result['notes']);
foreach ($notes as $n) {
static::assertTrue(get_class($n) == Note::class);
$vis = VisibilityScope::create($n->getScope());
static::assertTrue($visibility($vis));
}
}
}