. */ declare(strict_types=1); namespace Fisharebest\Webtrees; use Fisharebest\Webtrees\Contracts\CacheFactoryInterface; use Fisharebest\Webtrees\Contracts\UserInterface; use Fisharebest\Webtrees\Services\UserService; use Symfony\Component\Cache\Adapter\NullAdapter; /** * Test the user functions */ class UserTest extends TestCase { protected static bool $uses_database = true; /** * Things to run before every test. */ protected function setUp(): void { parent::setUp(); $cache_factory = $this->createMock(CacheFactoryInterface::class); $cache_factory->method('array')->willReturn(new Cache(new NullAdapter())); Registry::cache($cache_factory); } /** * @covers \Fisharebest\Webtrees\User::__construct * @covers \Fisharebest\Webtrees\User::id * @covers \Fisharebest\Webtrees\User::email * @covers \Fisharebest\Webtrees\User::realName * @covers \Fisharebest\Webtrees\User::userName */ public function testConstructor(): void { $user = new User(123, 'username', 'real name', 'email'); self::assertInstanceOf(UserInterface::class, $user); self::assertSame(123, $user->id()); self::assertSame('email', $user->email()); self::assertSame('real name', $user->realName()); self::assertSame('username', $user->userName()); } /** * @covers \Fisharebest\Webtrees\User::setUserName * @covers \Fisharebest\Webtrees\User::userName * @covers \Fisharebest\Webtrees\User::setRealName * @covers \Fisharebest\Webtrees\User::realName * @covers \Fisharebest\Webtrees\User::setEmail * @covers \Fisharebest\Webtrees\User::email * @covers \Fisharebest\Webtrees\User::setPassword * @covers \Fisharebest\Webtrees\User::checkPassword */ public function testGettersAndSetters(): void { $user_service = new UserService(); $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); self::assertSame(1, $user->id()); self::assertSame('user', $user->userName()); $user->setUserName('foo'); self::assertSame('foo', $user->userName()); self::assertSame('User', $user->realName()); $user->setRealName('Foo'); self::assertSame('Foo', $user->realName()); self::assertSame('user@example.com', $user->email()); $user->setEmail('foo@example.com'); self::assertSame('foo@example.com', $user->email()); self::assertTrue($user->checkPassword('secret')); $user->setPassword('letmein'); self::assertTrue($user->checkPassword('letmein')); } /** * @covers \Fisharebest\Webtrees\User::setPreference * @covers \Fisharebest\Webtrees\User::getPreference */ public function testPreferences(): void { $user_service = new UserService(); $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); self::assertSame('', $user->getPreference('foo')); $user->setPreference('foo', 'bar'); self::assertSame('bar', $user->getPreference('foo')); } }