xref: /webtrees/tests/app/UserTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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 PHPUnit\Framework\Attributes\CoversClass;
26use Symfony\Component\Cache\Adapter\NullAdapter;
27
28
29#[CoversClass(User::class)]
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    public function testConstructor(): void
47    {
48        $user = new User(123, 'username', 'real name', 'email');
49
50        self::assertInstanceOf(UserInterface::class, $user);
51        self::assertSame(123, $user->id());
52        self::assertSame('email', $user->email());
53        self::assertSame('real name', $user->realName());
54        self::assertSame('username', $user->userName());
55    }
56
57    public function testGettersAndSetters(): void
58    {
59        $user_service = new UserService();
60        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
61
62        self::assertSame(1, $user->id());
63
64        self::assertSame('user', $user->userName());
65        $user->setUserName('foo');
66        self::assertSame('foo', $user->userName());
67
68        self::assertSame('User', $user->realName());
69        $user->setRealName('Foo');
70        self::assertSame('Foo', $user->realName());
71
72        self::assertSame('user@example.com', $user->email());
73        $user->setEmail('foo@example.com');
74        self::assertSame('foo@example.com', $user->email());
75
76        self::assertTrue($user->checkPassword('secret'));
77        $user->setPassword('letmein');
78        self::assertTrue($user->checkPassword('letmein'));
79    }
80
81    public function testPreferences(): void
82    {
83        $user_service = new UserService();
84        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
85
86        self::assertSame('', $user->getPreference('foo'));
87        $user->setPreference('foo', 'bar');
88        self::assertSame('bar', $user->getPreference('foo'));
89    }
90}
91