FeedsTest.php 3.1 KB

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