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