NicknameTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Util;
  19. use App\Entity\GSActor;
  20. use App\Util\Common;
  21. use App\Util\Exception\NicknameEmptyException;
  22. use App\Util\Exception\NicknameInvalidException;
  23. use App\Util\Exception\NicknameReservedException;
  24. use App\Util\Exception\NicknameTakenException;
  25. use App\Util\Exception\NicknameTooLongException;
  26. use App\Util\Exception\NicknameTooShortException;
  27. use App\Util\GNUsocialTestCase;
  28. use App\Util\Nickname;
  29. use Jchook\AssertThrows\AssertThrows;
  30. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  31. class NicknameTest extends GNUsocialTestCase
  32. {
  33. use AssertThrows;
  34. public function testNormalize()
  35. {
  36. $conf = ['nickname' => ['min_length' => 4, 'reserved' => ['this_nickname_is_reserved']]];
  37. $cb = $this->createMock(ContainerBagInterface::class);
  38. static::assertTrue($cb instanceof ContainerBagInterface);
  39. $cb->method('get')
  40. ->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
  41. Common::setupConfig($cb);
  42. static::assertThrows(NicknameTooLongException::class, fn () => Nickname::normalize(serialize(random_bytes(128)), check_already_used: false));
  43. static::assertSame('foobar', Nickname::normalize('foobar', check_already_used: false));
  44. static::assertSame('foobar', Nickname::normalize(' foobar ', check_already_used: false));
  45. static::assertSame('foobar', Nickname::normalize('foo_bar', check_already_used: false));
  46. static::assertSame('foobar', Nickname::normalize('FooBar', check_already_used: false));
  47. static::assertThrows(NicknameTooShortException::class, fn () => Nickname::normalize('foo', check_already_used: false));
  48. static::assertThrows(NicknameEmptyException::class, fn () => Nickname::normalize('', check_already_used: false));
  49. static::assertThrows(NicknameInvalidException::class, fn () => Nickname::normalize('FóóBár', check_already_used: false));
  50. static::assertThrows(NicknameReservedException::class, fn () => Nickname::normalize('this_nickname_is_reserved', check_already_used: false));
  51. static::bootKernel();
  52. static::assertSame('foobar', Nickname::normalize('foobar', check_already_used: true));
  53. static::assertThrows(NicknameTakenException::class, fn () => Nickname::normalize('taken_user', check_already_used: true));
  54. }
  55. public function testIsValid()
  56. {
  57. static::assertTrue(Nickname::isValid('nick', check_already_used: false));
  58. static::assertFalse(Nickname::isValid('', check_already_used: false));
  59. }
  60. public function testIsCanonical()
  61. {
  62. static::assertTrue(Nickname::isCanonical('foo'));
  63. static::assertFalse(Nickname::isCanonical('fóó'));
  64. }
  65. public function testIsReserved()
  66. {
  67. $conf = ['nickname' => ['min_length' => 4, 'reserved' => ['this_nickname_is_reserved']]];
  68. $cb = $this->createMock(ContainerBagInterface::class);
  69. static::assertTrue($cb instanceof ContainerBagInterface);
  70. $cb->method('get')->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
  71. Common::setupConfig($cb);
  72. static::assertTrue(Nickname::isReserved('this_nickname_is_reserved'));
  73. static::assertFalse(Nickname::isReserved('this_nickname_is_not_reserved'));
  74. $conf = ['nickname' => ['min_length' => 4, 'reserved' => []]];
  75. $cb = $this->createMock(ContainerBagInterface::class);
  76. $cb->method('get')->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
  77. Common::setupConfig($cb);
  78. static::assertFalse(Nickname::isReserved('this_nickname_is_reserved'));
  79. }
  80. public function testCheckTaken()
  81. {
  82. static::bootKernel();
  83. static::assertNull(Nickname::checkTaken('not_taken_user'));
  84. static::assertTrue(Nickname::checkTaken('taken_user') instanceof GSActor);
  85. static::assertNull(Nickname::checkTaken('not_taken_group'));
  86. static::assertTrue(Nickname::checkTaken('taken_group') instanceof GSActor);
  87. }
  88. }