xref: /webtrees/tests/app/DefaultUserTest.php (revision 774eb6bada09fdd46a086fd31e84fd157a6351c4)
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 PHPUnit\Framework\Attributes\CoversClass;
25use Symfony\Component\Cache\Adapter\NullAdapter;
26
27#[CoversClass(DefaultUser::class)]
28class DefaultUserTest extends TestCase
29{
30    protected static bool $uses_database = true;
31
32    /**
33     * Things to run before every test.
34     */
35    protected function setUp(): void
36    {
37        parent::setUp();
38
39        $cache_factory = $this->createMock(CacheFactoryInterface::class);
40        $cache_factory->method('array')->willReturn(new Cache(new NullAdapter()));
41        Registry::cache($cache_factory);
42    }
43
44    public function testDefaultUser(): void
45    {
46        $user = new DefaultUser();
47
48        self::assertInstanceOf(UserInterface::class, $user);
49        self::assertSame(-1, $user->id());
50        self::assertSame('DEFAULT_USER', $user->email());
51        self::assertSame('DEFAULT_USER', $user->realName());
52        self::assertSame('', $user->userName());
53    }
54
55    public function testPreferences(): void
56    {
57        $user = new DefaultUser();
58
59        self::assertSame('', $user->getPreference('foo'));
60        self::assertSame('', $user->getPreference('foo'));
61        self::assertSame('bar', $user->getPreference('foo', 'bar'));
62
63        // Default users store preferences in the database
64        $user->setPreference('foo', 'bar');
65
66        self::assertSame('bar', $user->getPreference('foo'));
67    }
68}
69