|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Nette\Security\User authentication. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Nette\Security\IAuthenticator; |
| 10 | +use Nette\Security\SimpleIdentity; |
| 11 | +use Tester\Assert; |
| 12 | + |
| 13 | + |
| 14 | +require __DIR__ . '/../bootstrap.php'; |
| 15 | +require __DIR__ . '/MockUserStorage.php'; |
| 16 | + |
| 17 | +// Setup environment |
| 18 | +$_COOKIE = []; |
| 19 | +ob_start(); |
| 20 | + |
| 21 | + |
| 22 | +class Authenticator implements IAuthenticator |
| 23 | +{ |
| 24 | + public function authenticate(array $credentials): Nette\Security\IIdentity |
| 25 | + { |
| 26 | + [$username, $password] = $credentials; |
| 27 | + if ($username !== 'john') { |
| 28 | + throw new Nette\Security\AuthenticationException('Unknown user', self::IDENTITY_NOT_FOUND); |
| 29 | + |
| 30 | + } elseif ($password !== 'xxx') { |
| 31 | + throw new Nette\Security\AuthenticationException('Password not match', self::INVALID_CREDENTIAL); |
| 32 | + |
| 33 | + } else { |
| 34 | + return new SimpleIdentity('John Doe', 'admin'); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +$user = new Nette\Security\User(new MockUserStorage); |
| 41 | + |
| 42 | +$counter = (object) [ |
| 43 | + 'login' => 0, |
| 44 | + 'logout' => 0, |
| 45 | +]; |
| 46 | + |
| 47 | +$user->onLoggedIn[] = function () use ($counter) { |
| 48 | + $counter->login++; |
| 49 | +}; |
| 50 | + |
| 51 | +$user->onLoggedOut[] = function () use ($counter) { |
| 52 | + $counter->logout++; |
| 53 | +}; |
| 54 | + |
| 55 | + |
| 56 | +Assert::false($user->isLoggedIn()); |
| 57 | +Assert::null($user->getIdentity()); |
| 58 | +Assert::null($user->getId()); |
| 59 | + |
| 60 | + |
| 61 | +// authenticate |
| 62 | +Assert::exception(function () use ($user) { |
| 63 | + // login without handler |
| 64 | + $user->login('jane', ''); |
| 65 | +}, Nette\InvalidStateException::class, 'Authenticator has not been set.'); |
| 66 | + |
| 67 | +$handler = new Authenticator; |
| 68 | +$user->setAuthenticator($handler); |
| 69 | + |
| 70 | +Assert::exception(function () use ($user) { |
| 71 | + // login as jane |
| 72 | + $user->login('jane', ''); |
| 73 | +}, Nette\Security\AuthenticationException::class, 'Unknown user'); |
| 74 | + |
| 75 | +Assert::exception(function () use ($user) { |
| 76 | + // login as john |
| 77 | + $user->login('john', ''); |
| 78 | +}, Nette\Security\AuthenticationException::class, 'Password not match'); |
| 79 | + |
| 80 | +// login as john#2 |
| 81 | +$user->login('john', 'xxx'); |
| 82 | +Assert::same(1, $counter->login); |
| 83 | +Assert::true($user->isLoggedIn()); |
| 84 | +Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity()); |
| 85 | +Assert::same('John Doe', $user->getId()); |
| 86 | + |
| 87 | +// login as john#3 |
| 88 | +$user->logout(true); |
| 89 | +Assert::same(1, $counter->logout); |
| 90 | +$user->login(new SimpleIdentity('John Doe', 'admin')); |
| 91 | +Assert::same(2, $counter->login); |
| 92 | +Assert::true($user->isLoggedIn()); |
| 93 | +Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity()); |
| 94 | + |
| 95 | + |
| 96 | +// log out |
| 97 | +// logging out... |
| 98 | +$user->logout(false); |
| 99 | +Assert::same(2, $counter->logout); |
| 100 | + |
| 101 | +Assert::false($user->isLoggedIn()); |
| 102 | +Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity()); |
| 103 | + |
| 104 | + |
| 105 | +// logging out and clearing identity... |
| 106 | +$user->logout(true); |
| 107 | +Assert::same(2, $counter->logout); // not logged in -> logout event not triggered |
| 108 | + |
| 109 | +Assert::false($user->isLoggedIn()); |
| 110 | +Assert::null($user->getIdentity()); |
| 111 | + |
| 112 | + |
| 113 | +// namespace |
| 114 | +// login as john#2? |
| 115 | +$user->login('john', 'xxx'); |
| 116 | +Assert::same(3, $counter->login); |
| 117 | +Assert::true($user->isLoggedIn()); |
0 commit comments