1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU 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 * This program 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 General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Fisharebest\Webtrees\Contracts\CacheFactoryInterface; 23use Fisharebest\Webtrees\Contracts\UserInterface; 24use Fisharebest\Webtrees\Services\UserService; 25use Symfony\Component\Cache\Adapter\NullAdapter; 26 27/** 28 * Test the user functions 29 */ 30class UserTest extends TestCase 31{ 32 protected static bool $uses_database = true; 33 34 /** 35 * Things to run before every test. 36 */ 37 protected function setUp(): void 38 { 39 parent::setUp(); 40 41 $cache_factory = $this->createMock(CacheFactoryInterface::class); 42 $cache_factory->method('array')->willReturn(new Cache(new NullAdapter())); 43 Registry::cache($cache_factory); 44 } 45 46 /** 47 * @covers \Fisharebest\Webtrees\User::__construct 48 * @covers \Fisharebest\Webtrees\User::id 49 * @covers \Fisharebest\Webtrees\User::email 50 * @covers \Fisharebest\Webtrees\User::realName 51 * @covers \Fisharebest\Webtrees\User::userName 52 * @return void 53 */ 54 public function testConstructor(): void 55 { 56 $user = new User(123, 'username', 'real name', 'email'); 57 58 self::assertInstanceOf(UserInterface::class, $user); 59 self::assertSame(123, $user->id()); 60 self::assertSame('email', $user->email()); 61 self::assertSame('real name', $user->realName()); 62 self::assertSame('username', $user->userName()); 63 } 64 65 /** 66 * @covers \Fisharebest\Webtrees\User::setUserName 67 * @covers \Fisharebest\Webtrees\User::userName 68 * @covers \Fisharebest\Webtrees\User::setRealName 69 * @covers \Fisharebest\Webtrees\User::realName 70 * @covers \Fisharebest\Webtrees\User::setEmail 71 * @covers \Fisharebest\Webtrees\User::email 72 * @covers \Fisharebest\Webtrees\User::setPassword 73 * @covers \Fisharebest\Webtrees\User::checkPassword 74 * @return void 75 */ 76 public function testGettersAndSetters(): void 77 { 78 $user_service = new UserService(); 79 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 80 81 self::assertSame(1, $user->id()); 82 83 self::assertSame('user', $user->userName()); 84 $user->setUserName('foo'); 85 self::assertSame('foo', $user->userName()); 86 87 self::assertSame('User', $user->realName()); 88 $user->setRealName('Foo'); 89 self::assertSame('Foo', $user->realName()); 90 91 self::assertSame('user@example.com', $user->email()); 92 $user->setEmail('foo@example.com'); 93 self::assertSame('foo@example.com', $user->email()); 94 95 self::assertTrue($user->checkPassword('secret')); 96 $user->setPassword('letmein'); 97 self::assertTrue($user->checkPassword('letmein')); 98 } 99 100 /** 101 * @covers \Fisharebest\Webtrees\User::setPreference 102 * @covers \Fisharebest\Webtrees\User::getPreference 103 * @return void 104 */ 105 public function testPreferences(): void 106 { 107 $user_service = new UserService(); 108 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 109 110 self::assertSame('', $user->getPreference('foo')); 111 $user->setPreference('foo', 'bar'); 112 self::assertSame('bar', $user->getPreference('foo')); 113 } 114} 115