NetworkTest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace App\Tests\Core;
  19. use App\Controller\Network;
  20. use App\Core\DB\DB;
  21. use App\Core\Security;
  22. use App\Core\VisibilityScope;
  23. use App\Entity\Note;
  24. use App\Util\Common;
  25. use App\Util\Exception\ClientException;
  26. use App\Util\GNUsocialTestCase;
  27. use Jchook\AssertThrows\AssertThrows;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\RequestStack;
  30. use Symfony\Component\Security\Core\Security as SSecurity;
  31. class NetworkTest extends GNUsocialTestCase
  32. {
  33. use AssertThrows;
  34. public function testPublic()
  35. {
  36. $this->testRoute('public', fn ($vis) => $vis->public || $vis->site);
  37. }
  38. public function testHome()
  39. {
  40. $this->testRoute('home', fn ($vis) => !$vis->message, ['taken_user']);
  41. }
  42. public function testNetwork()
  43. {
  44. $this->testRoute('network', fn ($vis) => $vis->public);
  45. }
  46. public function testReplies()
  47. {
  48. $this->testRoute('replies', fn ($vis) => $vis->public, [], function () {
  49. $user = DB::findOneBy('local_user', ['nickname' => 'taken_user']);
  50. $sec = $this->getMockBuilder(SSecurity::class)->setConstructorArgs([self::$kernel->getContainer()])->getMock();
  51. $sec->method('getUser')->willReturn($user);
  52. Security::setHelper($sec, null);
  53. });
  54. }
  55. private function testRoute(string $route, callable $visibility, array $extra_args = [], callable $setup_login = null)
  56. {
  57. parent::bootKernel();
  58. if (!is_null($setup_login)) {
  59. $setup_login();
  60. }
  61. $req = $this->createMock(Request::class);
  62. $req_stack = $this->createMock(RequestStack::class);
  63. $network = new Network($req_stack);
  64. if ($route == 'home') {
  65. static::assertThrows(ClientException::class, fn () => $network->home($req, 'username_not_taken'));
  66. }
  67. $result = $network->{$route}($req, ...$extra_args);
  68. static::assertSame($result['_template'], 'network/public.html.twig');
  69. foreach ($result['notes'] as $n) {
  70. static::assertTrue(is_array($n['replies']));
  71. }
  72. $notes = Common::flattenNoteArray($result['notes']);
  73. foreach ($notes as $n) {
  74. static::assertTrue(get_class($n) == Note::class);
  75. $vis = VisibilityScope::create($n->getScope());
  76. static::assertTrue($visibility($vis));
  77. }
  78. }
  79. }