LocalUserTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Entity;
  20. use App\Entity\LocalUser;
  21. use App\Util\GNUsocialTestCase;
  22. use Exception;
  23. use Jchook\AssertThrows\AssertThrows;
  24. class LocalUserTest extends GNUsocialTestCase
  25. {
  26. use AssertThrows;
  27. public function testAlgoNameToConstant()
  28. {
  29. $if_exists = function ($name, $constant) {
  30. if (\defined($constant)) {
  31. static::assertSame(\constant($constant), LocalUser::algoNameToConstant($name));
  32. } else {
  33. static::assertThrows(Exception::class, fn () => LocalUser::algoNameToConstant($name));
  34. }
  35. };
  36. $if_exists('bcrypt', 'PASSWORD_BCRYPT');
  37. $if_exists('argon2', 'PASSWORD_ARGON2');
  38. $if_exists('argon2i', 'PASSWORD_ARGON2I');
  39. $if_exists('argon2d', 'PASSWORD_ARGON2D');
  40. $if_exists('argon2id', 'PASSWORD_ARGON2ID');
  41. static::assertSame(\PASSWORD_ARGON2ID, LocalUser::algoNameToConstant('argon2id'));
  42. }
  43. public function testChangePassword()
  44. {
  45. parent::bootKernel();
  46. $user = LocalUser::getByNickname('local_user_test_user');
  47. static::assertFalse($user->changePassword(old_password_plain_text: '', new_password_plain_text: 'password', override: false));
  48. static::assertTrue($user->changePassword(old_password_plain_text: '', new_password_plain_text: 'password', override: true));
  49. static::assertTrue($user->changePassword(old_password_plain_text: 'password', new_password_plain_text: 'new_password', override: false));
  50. static::assertFalse($user->changePassword(old_password_plain_text: 'wrong_password', new_password_plain_text: 'new_password', override: false));
  51. }
  52. public function testGetByNickname()
  53. {
  54. static::assertNotNull(LocalUser::getByNickname('taken_user'));
  55. }
  56. }