NicknameTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\NicknameTooShortException;
  24. use App\Util\Nickname;
  25. use Jchook\AssertThrows\AssertThrows;
  26. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  27. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  28. class NicknameTest extends WebTestCase
  29. {
  30. use AssertThrows;
  31. public function testNormalize()
  32. {
  33. $conf = ['nickname' => ['min_length' => 4, 'reserved' => ['this_nickname_is_reserved']]];
  34. $cb = $this->createMock(ContainerBagInterface::class);
  35. static::assertTrue($cb instanceof ContainerBagInterface);
  36. $cb->method('get')
  37. ->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
  38. Common::setupConfig($cb);
  39. static::assertSame('foobar', Nickname::normalize('foobar', check_already_used: false));
  40. static::assertSame('foobar', Nickname::normalize(' foobar ', check_already_used: false));
  41. static::assertSame('foobar', Nickname::normalize('foo_bar', check_already_used: false));
  42. static::assertSame('foobar', Nickname::normalize('FooBar', check_already_used: false));
  43. static::assertThrows(NicknameTooShortException::class, function () { return Nickname::normalize('foo', check_already_used: false); });
  44. static::assertThrows(NicknameEmptyException::class, function () { return Nickname::normalize('', check_already_used: false); });
  45. static::assertThrows(NicknameInvalidException::class, function () { return Nickname::normalize('FóóBár', check_already_used: false); });
  46. static::assertThrows(NicknameReservedException::class, function () { return Nickname::normalize('this_nickname_is_reserved', check_already_used: false); });
  47. }
  48. public function testIsCanonical()
  49. {
  50. static::assertTrue(Nickname::isCanonical('foo'));
  51. static::assertFalse(Nickname::isCanonical('fóó'));
  52. }
  53. public function testIsReserved()
  54. {
  55. $conf = ['nickname' => ['min_length' => 4, 'reserved' => ['this_nickname_is_reserved']]];
  56. $cb = $this->createMock(ContainerBagInterface::class);
  57. static::assertTrue($cb instanceof ContainerBagInterface);
  58. $cb->method('get')
  59. ->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
  60. Common::setupConfig($cb);
  61. static::assertTrue(Nickname::isReserved('this_nickname_is_reserved'));
  62. static::assertFalse(Nickname::isReserved('this_nickname_is_not_reserved'));
  63. }
  64. }