xref: /webtrees/tests/app/UserTest.php (revision b58089917417db210abced6ec6c9fa30e02f3238)
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\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 $uses_database = true;
33
34    public function setUp(): void
35    {
36        parent::setUp();
37
38        $cache_factory = $this->createMock(CacheFactoryInterface::class);
39        $cache_factory->method('array')->willReturn(new Cache(new NullAdapter()));
40        Factory::cache($cache_factory);
41    }
42
43    /**
44     * @covers \Fisharebest\Webtrees\User::__construct
45     * @covers \Fisharebest\Webtrees\User::id
46     * @covers \Fisharebest\Webtrees\User::email
47     * @covers \Fisharebest\Webtrees\User::realName
48     * @covers \Fisharebest\Webtrees\User::userName
49     * @return void
50     */
51    public function testConstructor(): void
52    {
53        $user = new User(123, 'username', 'real name', 'email');
54
55        $this->assertInstanceOf(UserInterface::class, $user);
56        $this->assertSame(123, $user->id());
57        $this->assertSame('email', $user->email());
58        $this->assertSame('real name', $user->realName());
59        $this->assertSame('username', $user->userName());
60    }
61
62    /**
63     * @covers \Fisharebest\Webtrees\User::setUserName
64     * @covers \Fisharebest\Webtrees\User::userName
65     * @covers \Fisharebest\Webtrees\User::setRealName
66     * @covers \Fisharebest\Webtrees\User::realName
67     * @covers \Fisharebest\Webtrees\User::setEmail
68     * @covers \Fisharebest\Webtrees\User::email
69     * @covers \Fisharebest\Webtrees\User::setPassword
70     * @covers \Fisharebest\Webtrees\User::checkPassword
71     * @return void
72     */
73    public function testGettersAndSetters(): void
74    {
75        $user_service = new UserService();
76        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
77
78        $this->assertSame(1, $user->id());
79
80        $this->assertSame('user', $user->userName());
81        $user->setUserName('foo');
82        $this->assertSame('foo', $user->userName());
83
84        $this->assertSame('User', $user->realName());
85        $user->setRealName('Foo');
86        $this->assertSame('Foo', $user->realName());
87
88        $this->assertSame('user@example.com', $user->email());
89        $user->setEmail('foo@example.com');
90        $this->assertSame('foo@example.com', $user->email());
91
92        $this->assertTrue($user->checkPassword('secret'));
93        $user->setPassword('letmein');
94        $this->assertTrue($user->checkPassword('letmein'));
95    }
96
97    /**
98     * @covers \Fisharebest\Webtrees\User::setPreference
99     * @covers \Fisharebest\Webtrees\User::getPreference
100     * @return void
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        $this->assertSame('', $user->getPreference('foo'));
108        $user->setPreference('foo', 'bar');
109        $this->assertSame('bar', $user->getPreference('foo'));
110    }
111}
112