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