xref: /webtrees/tests/app/UserTest.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
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
20/**
21 * Test the user functions
22 */
23class UserTest extends \Fisharebest\Webtrees\TestCase
24{
25    protected static $uses_database = true;
26
27    /**
28     * @covers \Fisharebest\Webtrees\User::__construct
29     * @covers \Fisharebest\Webtrees\User::create
30     * @return void
31     */
32    public function testCreate(): void
33    {
34        $user = User::create('user', 'User', 'user@example.com', 'secret');
35
36        $this->assertSame(1, $user->getUserId());
37        $this->assertSame('user', $user->getUserName());
38        $this->assertSame('User', $user->getRealName());
39        $this->assertSame('user@example.com', $user->getEmail());
40    }
41
42    /**
43     * @covers \Fisharebest\Webtrees\User::delete
44     * @return void
45     */
46    public function testDelete(): void
47    {
48        $user = User::create('user', 'User', 'user@example.com', 'secret');
49        $user_id = $user->getUserId();
50        $user->delete();
51
52        $this->assertNull(User::find($user_id));
53    }
54
55    /**
56     * @covers \Fisharebest\Webtrees\User::find
57     * @return void
58     */
59    public function testFindNonExistingUser(): void
60    {
61        $user = User::find(999);
62
63        $this->assertNull($user);
64    }
65
66    /**
67     * @covers \Fisharebest\Webtrees\User::find
68     * @return void
69     */
70    public function testFindExistingUser(): void
71    {
72        $user1 = User::create('user', 'User', 'user@example.com', 'secret');
73        $user2 = User::find($user1->getUserId());
74
75        $this->assertSame($user1, $user2);
76    }
77
78    /**
79     * @covers \Fisharebest\Webtrees\User::findByEmail
80     * @return void
81     */
82    public function testFindUserByEmail(): void
83    {
84        $user1 = User::create('user', 'User', 'user@example.com', 'secret');
85        $user2 = User::findByEmail($user1->getEmail());
86
87        $this->assertSame($user1, $user2);
88    }
89
90    /**
91     * @covers \Fisharebest\Webtrees\User::findByUserName
92     * @return void
93     */
94    public function testFindUserByUserName(): void
95    {
96        $user1 = User::create('user', 'User', 'user@example.com', 'secret');
97        $user2 = User::findByUserName($user1->getUserName());
98
99        $this->assertSame($user1, $user2);
100    }
101
102    /**
103     * @covers \Fisharebest\Webtrees\User::findByIdentifier
104     * @return void
105     */
106    public function testFindUserByIdentifier(): void
107    {
108        $user1 = User::create('user', 'User', 'user@example.com', 'secret');
109        $user2 = User::findByIdentifier($user1->getUsername());
110        $user3 = User::findByIdentifier($user1->getEmail());
111
112        $this->assertSame($user1, $user2);
113        $this->assertSame($user1, $user3);
114    }
115}
116