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