From 74f477489b6b6dade92e694d7029190768c96716 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 23 May 2021 19:56:45 +0000 Subject: [PATCH] [TESTS] Raise test coverage for App\Controller\Network to 100% and fixup related code --- src/Controller/Network.php | 8 +-- src/Core/GNUsocial.php | 6 ++- src/DataFixtures/CoreFixtures.php | 10 +++- src/Entity/Note.php | 2 +- tests/Controller/NetworkTest.php | 90 +++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 tests/Controller/NetworkTest.php diff --git a/src/Controller/Network.php b/src/Controller/Network.php index c906d1bb8b..e16c0fa34b 100644 --- a/src/Controller/Network.php +++ b/src/Controller/Network.php @@ -41,6 +41,7 @@ use App\Core\VisibilityScope; use App\Entity\Note; use App\Util\Common; use App\Util\Exception\ClientException; +use App\Util\Exception\NotFoundException; use Symfony\Component\HttpFoundation\Request; class Network extends Controller @@ -65,8 +66,9 @@ class Network extends Controller public function home(Request $request, string $nickname) { - $target = DB::findOneBy('gsactor', ['nickname' => $nickname]); - if ($target == null) { + try { + $target = DB::findOneBy('gsactor', ['nickname' => $nickname]); + } catch (NotFoundException) { throw new ClientException(_m('User {nickname} doesn\'t exist', ['{nickname}' => $nickname])); } @@ -106,7 +108,7 @@ END; public function network(Request $request) { - $notes = Note::getAllNotes($this->instance_scope); + $notes = Note::getAllNotes($this->public_scope); Event::handle('FormatNoteList', [&$notes]); diff --git a/src/Core/GNUsocial.php b/src/Core/GNUsocial.php index 95290e6e78..7cde3c1c25 100644 --- a/src/Core/GNUsocial.php +++ b/src/Core/GNUsocial.php @@ -89,7 +89,7 @@ class GNUsocial implements EventSubscriberInterface protected SanitizerInterface $sanitizer; protected ContainerBagInterface $config; protected \Twig\Environment $twig; - protected Request $request; + protected ?Request $request; /** * Symfony dependency injection gives us access to these services @@ -140,7 +140,9 @@ class GNUsocial implements EventSubscriberInterface { if (!$this->initialized) { Common::setupConfig($this->config); - Common::setRequest($this->request); + if (!is_null($this->request)) { + Common::setRequest($this->request); + } Log::setLogger($this->logger); Event::setDispatcher($this->event_dispatcher); I18n::setTranslator($this->translator); diff --git a/src/DataFixtures/CoreFixtures.php b/src/DataFixtures/CoreFixtures.php index 9e5f7e2ce7..9effa7e7ff 100644 --- a/src/DataFixtures/CoreFixtures.php +++ b/src/DataFixtures/CoreFixtures.php @@ -2,6 +2,7 @@ namespace App\DataFixtures; +use App\Core\VisibilityScope; use App\Entity\GroupInbox; use App\Entity\GSActor; use App\Entity\LocalGroup; @@ -27,8 +28,13 @@ class CoreFixtures extends Fixture $actors[$nick] = $actor; } - $note = Note::create(['gsactor_id' => $actors['taken_user']->getId(), 'content' => 'some content']); - $manager->persist($note); + $n = Note::create(['gsactor_id' => $actors['taken_user']->getId(), 'content' => 'some content']); + $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->flush(); diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 7a448358ce..b252a34fb2 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -247,7 +247,7 @@ class Note extends Entity * * @param mixed $a */ - public function isVisibleTo(/* GSActor|LocalUser */ $a): bool + public function isVisibleTo(GSActor | LocalUser $a): bool { $scope = VisibilityScope::create($this->scope); return $scope->public diff --git a/tests/Controller/NetworkTest.php b/tests/Controller/NetworkTest.php new file mode 100644 index 0000000000..c030f00817 --- /dev/null +++ b/tests/Controller/NetworkTest.php @@ -0,0 +1,90 @@ +. + +// }}} + +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)); + } + } +}