1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 protected function setUp(): void 35 { 36 parent::setUp(); 37 38 $cache_factory = $this->createMock(CacheFactoryInterface::class); 39 $cache_factory->method('array')->willReturn(new Cache(new NullAdapter())); 40 Registry::cache($cache_factory); 41 } 42 43 /** 44 * @covers \Fisharebest\Webtrees\User::__construct 45 * @covers \Fisharebest\Webtrees\User::id 46 * @covers \Fisharebest\Webtrees\User::email 47 * @covers \Fisharebest\Webtrees\User::realName 48 * @covers \Fisharebest\Webtrees\User::userName 49 * @return void 50 */ 51 public function testConstructor(): void 52 { 53 $user = new User(123, 'username', 'real name', 'email'); 54 55 self::assertInstanceOf(UserInterface::class, $user); 56 self::assertSame(123, $user->id()); 57 self::assertSame('email', $user->email()); 58 self::assertSame('real name', $user->realName()); 59 self::assertSame('username', $user->userName()); 60 } 61 62 /** 63 * @covers \Fisharebest\Webtrees\User::setUserName 64 * @covers \Fisharebest\Webtrees\User::userName 65 * @covers \Fisharebest\Webtrees\User::setRealName 66 * @covers \Fisharebest\Webtrees\User::realName 67 * @covers \Fisharebest\Webtrees\User::setEmail 68 * @covers \Fisharebest\Webtrees\User::email 69 * @covers \Fisharebest\Webtrees\User::setPassword 70 * @covers \Fisharebest\Webtrees\User::checkPassword 71 * @return void 72 */ 73 public function testGettersAndSetters(): void 74 { 75 $user_service = new UserService(); 76 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 77 78 self::assertSame(1, $user->id()); 79 80 self::assertSame('user', $user->userName()); 81 $user->setUserName('foo'); 82 self::assertSame('foo', $user->userName()); 83 84 self::assertSame('User', $user->realName()); 85 $user->setRealName('Foo'); 86 self::assertSame('Foo', $user->realName()); 87 88 self::assertSame('user@example.com', $user->email()); 89 $user->setEmail('foo@example.com'); 90 self::assertSame('foo@example.com', $user->email()); 91 92 self::assertTrue($user->checkPassword('secret')); 93 $user->setPassword('letmein'); 94 self::assertTrue($user->checkPassword('letmein')); 95 } 96 97 /** 98 * @covers \Fisharebest\Webtrees\User::setPreference 99 * @covers \Fisharebest\Webtrees\User::getPreference 100 * @return void 101 */ 102 public function testPreferences(): void 103 { 104 $user_service = new UserService(); 105 $user = $user_service->create('user', 'User', 'user@example.com', 'secret'); 106 107 self::assertSame('', $user->getPreference('foo')); 108 $user->setPreference('foo', 'bar'); 109 self::assertSame('bar', $user->getPreference('foo')); 110 } 111} 112