NicknameTest.php 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Util\Common;
  20. use App\Util\Exception\NicknameEmptyException;
  21. use App\Util\Exception\NicknameInvalidException;
  22. use App\Util\Exception\NicknameReservedException;
  23. use App\Util\Exception\NicknameTakenException;
  24. use App\Util\Exception\NicknameTooLongException;
  25. use App\Util\Exception\NicknameTooShortException;
  26. use App\Util\GNUsocialTestCase;
  27. use App\Util\Nickname;
  28. use Jchook\AssertThrows\AssertThrows;
  29. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  30. class NicknameTest extends GNUsocialTestCase
  31. {
  32. use AssertThrows;
  33. public function testNormalize()
  34. {
  35. $conf = ['nickname' => ['min_length' => 4, 'reserved' => ['this_nickname_is_reserved']]];
  36. $cb = $this->createMock(ContainerBagInterface::class);
  37. static::assertTrue($cb instanceof ContainerBagInterface);
  38. $cb->method('get')
  39. ->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
  40. Common::setupConfig($cb);
  41. static::assertThrows(NicknameTooLongException::class, fn () => Nickname::normalize(str_repeat('longstring-', 128), check_already_used: false));
  42. static::assertThrows(NicknameInvalidException::class, fn () => Nickname::normalize('null\0', 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. }