xref: /webtrees/tests/app/GuestUserTest.php (revision 6bd19c8ce39244f770147102c818d9c7d9e13667)
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\UserInterface;
23
24/**
25 * Test the GuestUser class
26 */
27class GuestUserTest extends TestCase
28{
29    /**
30     * @covers \Fisharebest\Webtrees\GuestUser::__construct
31     * @covers \Fisharebest\Webtrees\GuestUser::id
32     * @covers \Fisharebest\Webtrees\GuestUser::email
33     * @covers \Fisharebest\Webtrees\GuestUser::realName
34     * @covers \Fisharebest\Webtrees\GuestUser::userName
35     */
36    public function testAnonymous(): void
37    {
38        $user = new GuestUser();
39
40        self::assertInstanceOf(UserInterface::class, $user);
41        self::assertSame(0, $user->id());
42        self::assertSame('GUEST_USER', $user->email());
43        self::assertSame('GUEST_USER', $user->realName());
44        self::assertSame('', $user->userName());
45    }
46
47    /**
48     * @covers \Fisharebest\Webtrees\GuestUser::__construct
49     * @covers \Fisharebest\Webtrees\GuestUser::id
50     * @covers \Fisharebest\Webtrees\GuestUser::email
51     * @covers \Fisharebest\Webtrees\GuestUser::realName
52     * @covers \Fisharebest\Webtrees\GuestUser::userName
53     */
54    public function testVisitor(): void
55    {
56        $user = new GuestUser('guest@example.com', 'guest user');
57
58        self::assertInstanceOf(UserInterface::class, $user);
59        self::assertSame(0, $user->id());
60        self::assertSame('guest@example.com', $user->email());
61        self::assertSame('guest user', $user->realName());
62        self::assertSame('', $user->userName());
63    }
64
65    /**
66     * @covers \Fisharebest\Webtrees\GuestUser::getPreference
67     * @covers \Fisharebest\Webtrees\GuestUser::setPreference
68     */
69    public function testPreferences(): void
70    {
71        $user = new GuestUser();
72
73        self::assertSame('', $user->getPreference('foo'));
74        self::assertSame('', $user->getPreference('foo'));
75        self::assertSame('bar', $user->getPreference('foo', 'bar'));
76
77        // Guests users store preferences in the session
78        $user->setPreference('foo', 'bar');
79
80        self::assertSame('bar', $user->getPreference('foo'));
81    }
82}
83