xref: /webtrees/tests/app/UserTest.php (revision e5a6b4d4f6f6e7ff2fba7ae2cf27546ae68a79cc)
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\Services\UserService;
21
22/**
23 * Test the user functions
24 */
25class UserTest extends TestCase
26{
27    protected static $uses_database = true;
28
29    /**
30     * @covers \Fisharebest\Webtrees\User::__construct
31     * @covers \Fisharebest\Webtrees\User::create
32     * @covers \Fisharebest\Webtrees\User::id
33     * @return void
34     */
35    public function testCreate(): void
36    {
37        $user_service = new UserService();
38        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
39
40        $this->assertSame(1, $user->id());
41    }
42
43    /**
44     * @covers \Fisharebest\Webtrees\User::setUserName
45     * @covers \Fisharebest\Webtrees\User::userName
46     * @covers \Fisharebest\Webtrees\User::setRealName
47     * @covers \Fisharebest\Webtrees\User::realName
48     * @covers \Fisharebest\Webtrees\User::setEmail
49     * @covers \Fisharebest\Webtrees\User::email
50     * @covers \Fisharebest\Webtrees\User::setPassword
51     * @covers \Fisharebest\Webtrees\User::checkPassword
52     * @return void
53     */
54    public function testGettersAndSetters(): void
55    {
56        $user_service = new UserService();
57        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
58
59        $this->assertSame(1, $user->id());
60
61        $this->assertSame('user', $user->userName());
62        $user->setUserName('foo');
63        $this->assertSame('foo', $user->userName());
64
65        $this->assertSame('User', $user->realName());
66        $user->setRealName('Foo');
67        $this->assertSame('Foo', $user->realName());
68
69        $this->assertSame('user@example.com', $user->email());
70        $user->setEmail('foo@example.com');
71        $this->assertSame('foo@example.com', $user->email());
72
73        $this->assertTrue($user->checkPassword('secret'));
74        $user->setPassword('letmein');
75        $this->assertTrue($user->checkPassword('letmein'));
76    }
77
78    /**
79     * @covers \Fisharebest\Webtrees\User::checkPassword
80     * @return void
81     */
82    public function testCheckPasswordCaseSensitive(): void
83    {
84        $user_service = new UserService();
85        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
86
87        $this->assertTrue($user->checkPassword('secret'));
88        $this->assertFalse($user->checkPassword('SECRET'));
89    }
90
91    /**
92     * @covers \Fisharebest\Webtrees\User::delete
93     * @return void
94     */
95    public function testDelete(): void
96    {
97        $user_service = new UserService();
98        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
99        $user_id      = $user->id();
100        $user_service->delete($user);
101
102        $this->assertNull($user_service->find($user_id));
103    }
104
105    /**
106     * @covers \Fisharebest\Webtrees\User::setPreference
107     * @covers \Fisharebest\Webtrees\User::getPreference
108     * @return void
109     */
110    public function testPreferences(): void
111    {
112        $user_service = new UserService();
113        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
114
115        $this->assertSame('', $user->getPreference('foo'));
116        $user->setPreference('foo', 'bar');
117        $this->assertSame('bar', $user->getPreference('foo'));
118    }
119}
120