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