FeedsTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Controller\Feeds;
  21. use App\Core\DB\DB;
  22. use App\Core\Security;
  23. use App\Core\VisibilityScope;
  24. use App\Entity\Note;
  25. use App\Util\Common;
  26. use App\Util\Exception\ClientException;
  27. use App\Util\GNUsocialTestCase;
  28. use Jchook\AssertThrows\AssertThrows;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\RequestStack;
  31. use Symfony\Component\Security\Core\Security as SSecurity;
  32. class FeedsTest extends GNUsocialTestCase
  33. {
  34. use AssertThrows;
  35. public function testPublic()
  36. {
  37. $this->testRoute('public', fn ($vis) => $vis->public || $vis->site);
  38. }
  39. public function testHome()
  40. {
  41. $this->testRoute('home', fn ($vis) => !$vis->message, ['taken_user']);
  42. }
  43. public function testFeeds()
  44. {
  45. $this->testRoute('network', fn ($vis) => $vis->public);
  46. }
  47. // TODO replies, re-enable
  48. // public function testReplies()
  49. // {
  50. // $this->testRoute('replies', fn ($vis) => $vis->public, [], function () {
  51. // $user = DB::findOneBy('local_user', ['nickname' => 'taken_user']);
  52. // $sec = $this->getMockBuilder(SSecurity::class)->setConstructorArgs([self::$kernel->getContainer()])->getMock();
  53. // $sec->method('getUser')->willReturn($user);
  54. // Security::setHelper($sec, null);
  55. // });
  56. // }
  57. private function testRoute(string $route, callable $visibility, array $extra_args = [], ?callable $setup_login = null)
  58. {
  59. parent::bootKernel();
  60. if (!\is_null($setup_login)) {
  61. $setup_login();
  62. }
  63. $req = $this->createMock(Request::class);
  64. $req_stack = $this->createMock(RequestStack::class);
  65. $feeds = new Feeds($req_stack);
  66. if ($route == 'home') {
  67. static::assertThrows(ClientException::class, fn () => $feeds->home($req, 'username_not_taken'));
  68. }
  69. $result = $feeds->{$route}($req, ...$extra_args);
  70. static::assertSame($result['_template'], 'feeds/feed.html.twig');
  71. foreach ($result['notes'] as $n) {
  72. static::assertIsArray($n['replies']);
  73. }
  74. $notes = Common::flattenNoteArray($result['notes']);
  75. foreach ($notes as $n) {
  76. static::assertTrue(\get_class($n) == Note::class);
  77. $vis = VisibilityScope::create($n->getScope());
  78. static::assertTrue($visibility($vis));
  79. }
  80. }
  81. }