xref: /webtrees/tests/app/UserTest.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees;
20
21use Fisharebest\Webtrees\Contracts\UserInterface;
22use Fisharebest\Webtrees\Services\UserService;
23
24/**
25 * Test the user functions
26 */
27class UserTest extends TestCase
28{
29    protected static $uses_database = true;
30
31    /**
32     * @covers \Fisharebest\Webtrees\User::__construct
33     * @covers \Fisharebest\Webtrees\User::id
34     * @covers \Fisharebest\Webtrees\User::email
35     * @covers \Fisharebest\Webtrees\User::realName
36     * @covers \Fisharebest\Webtrees\User::userName
37     * @return void
38     */
39    public function testConstructor(): void
40    {
41        $user = new User(123, 'username', 'real name', 'email');
42
43        $this->assertInstanceOf(UserInterface::class, $user);
44        $this->assertSame(123, $user->id());
45        $this->assertSame('email', $user->email());
46        $this->assertSame('real name', $user->realName());
47        $this->assertSame('username', $user->userName());
48    }
49
50    /**
51     * @covers \Fisharebest\Webtrees\User::setUserName
52     * @covers \Fisharebest\Webtrees\User::userName
53     * @covers \Fisharebest\Webtrees\User::setRealName
54     * @covers \Fisharebest\Webtrees\User::realName
55     * @covers \Fisharebest\Webtrees\User::setEmail
56     * @covers \Fisharebest\Webtrees\User::email
57     * @covers \Fisharebest\Webtrees\User::setPassword
58     * @covers \Fisharebest\Webtrees\User::checkPassword
59     * @return void
60     */
61    public function testGettersAndSetters(): void
62    {
63        $user_service = new UserService();
64        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
65
66        $this->assertSame(1, $user->id());
67
68        $this->assertSame('user', $user->userName());
69        $user->setUserName('foo');
70        $this->assertSame('foo', $user->userName());
71
72        $this->assertSame('User', $user->realName());
73        $user->setRealName('Foo');
74        $this->assertSame('Foo', $user->realName());
75
76        $this->assertSame('user@example.com', $user->email());
77        $user->setEmail('foo@example.com');
78        $this->assertSame('foo@example.com', $user->email());
79
80        $this->assertTrue($user->checkPassword('secret'));
81        $user->setPassword('letmein');
82        $this->assertTrue($user->checkPassword('letmein'));
83    }
84
85    /**
86     * @covers \Fisharebest\Webtrees\User::setPreference
87     * @covers \Fisharebest\Webtrees\User::getPreference
88     * @return void
89     */
90    public function testPreferences(): void
91    {
92        $user_service = new UserService();
93        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
94
95        $this->assertSame('', $user->getPreference('foo'));
96        $user->setPreference('foo', 'bar');
97        $this->assertSame('bar', $user->getPreference('foo'));
98    }
99}
100