xref: /webtrees/tests/app/UserTest.php (revision 3d0d3c4eac0d9885ecf85cce0ac6e76860c614d4)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20use Fisharebest\Webtrees\Contracts\UserInterface;
21use Fisharebest\Webtrees\Services\UserService;
22
23/**
24 * Test the user functions
25 */
26class UserTest extends TestCase
27{
28    protected static $uses_database = true;
29
30    /**
31     * @covers \Fisharebest\Webtrees\User::__construct
32     * @covers \Fisharebest\Webtrees\User::id
33     * @covers \Fisharebest\Webtrees\User::email
34     * @covers \Fisharebest\Webtrees\User::realName
35     * @covers \Fisharebest\Webtrees\User::userName
36     * @return void
37     */
38    public function testConstructor(): void
39    {
40        $user = new User(123, 'username', 'real name', 'email');
41
42        $this->assertInstanceOf(UserInterface::class, $user);
43        $this->assertSame(123, $user->id());
44        $this->assertSame('email', $user->email());
45        $this->assertSame('real name', $user->realName());
46        $this->assertSame('username', $user->userName());
47    }
48
49    /**
50     * @covers \Fisharebest\Webtrees\User::setUserName
51     * @covers \Fisharebest\Webtrees\User::userName
52     * @covers \Fisharebest\Webtrees\User::setRealName
53     * @covers \Fisharebest\Webtrees\User::realName
54     * @covers \Fisharebest\Webtrees\User::setEmail
55     * @covers \Fisharebest\Webtrees\User::email
56     * @covers \Fisharebest\Webtrees\User::setPassword
57     * @covers \Fisharebest\Webtrees\User::checkPassword
58     * @return void
59     */
60    public function testGettersAndSetters(): void
61    {
62        $user_service = new UserService();
63        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
64
65        $this->assertSame(1, $user->id());
66
67        $this->assertSame('user', $user->userName());
68        $user->setUserName('foo');
69        $this->assertSame('foo', $user->userName());
70
71        $this->assertSame('User', $user->realName());
72        $user->setRealName('Foo');
73        $this->assertSame('Foo', $user->realName());
74
75        $this->assertSame('user@example.com', $user->email());
76        $user->setEmail('foo@example.com');
77        $this->assertSame('foo@example.com', $user->email());
78
79        $this->assertTrue($user->checkPassword('secret'));
80        $user->setPassword('letmein');
81        $this->assertTrue($user->checkPassword('letmein'));
82    }
83
84    /**
85     * @covers \Fisharebest\Webtrees\User::setPreference
86     * @covers \Fisharebest\Webtrees\User::getPreference
87     * @return void
88     */
89    public function testPreferences(): void
90    {
91        $user_service = new UserService();
92        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
93
94        $this->assertSame('', $user->getPreference('foo'));
95        $user->setPreference('foo', 'bar');
96        $this->assertSame('bar', $user->getPreference('foo'));
97    }
98}
99