NicknameTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\Util;
  20. use App\Util\Common;
  21. use App\Util\Exception\NicknameEmptyException;
  22. use App\Util\Exception\NicknameInvalidException;
  23. use App\Util\Exception\NicknameNotAllowedException;
  24. use App\Util\Exception\NicknameTakenException;
  25. use App\Util\Exception\NicknameTooLongException;
  26. use App\Util\GNUsocialTestCase;
  27. use App\Util\Nickname;
  28. use Jchook\AssertThrows\AssertThrows;
  29. class NicknameTest extends GNUsocialTestCase
  30. {
  31. use AssertThrows;
  32. public function testNormalize()
  33. {
  34. static::bootKernel();
  35. Common::setConfig('nickname', 'blacklist', ['this_nickname_is_reserved'], transient: true);
  36. static::assertThrows(NicknameTooLongException::class, fn () => Nickname::normalize(str_repeat('longstring-', 128), check_already_used: false));
  37. static::assertThrows(NicknameInvalidException::class, fn () => Nickname::normalize('null\0', check_already_used: false));
  38. static::assertSame('foobar', Nickname::normalize('foobar', check_already_used: false));
  39. static::assertSame('foobar', Nickname::normalize(' foobar ', check_already_used: false));
  40. // static::assertSame('foobar', Nickname::normalize('foo_bar', check_already_used: false));
  41. // static::assertSame('foobar', Nickname::normalize('FooBar', check_already_used: false));
  42. static::assertThrows(NicknameEmptyException::class, fn () => Nickname::normalize('', check_already_used: false));
  43. // static::assertThrows(NicknameInvalidException::class, fn () => Nickname::normalize('FóóBár', check_already_used: false));
  44. static::assertThrows(NicknameNotAllowedException::class, fn () => Nickname::normalize('this_nickname_is_reserved', check_already_used: false));
  45. static::assertSame('foobar', Nickname::normalize('foobar', check_already_used: true));
  46. static::assertThrows(NicknameTakenException::class, fn () => Nickname::normalize('taken_user', check_already_used: true));
  47. }
  48. public function testIsValid()
  49. {
  50. static::assertTrue(Nickname::isValid('nick', check_already_used: false));
  51. static::assertFalse(Nickname::isValid('', check_already_used: false));
  52. }
  53. public function testIsCanonical()
  54. {
  55. static::assertTrue(Nickname::isCanonical('foo'));
  56. static::assertFalse(Nickname::isCanonical('fóó'));
  57. }
  58. public function testIsReserved()
  59. {
  60. static::bootKernel();
  61. Common::setConfig('nickname', 'blacklist', ['this_nickname_is_reserved'], transient: true);
  62. static::assertTrue(Nickname::isBlacklisted('this_nickname_is_reserved'));
  63. static::assertFalse(Nickname::isBlacklisted('this_nickname_is_not_reserved'));
  64. Common::setConfig('nickname', 'blacklist', [], transient: true);
  65. static::assertFalse(Nickname::isBlacklisted('this_nickname_is_reserved'));
  66. }
  67. }