xref: /webtrees/tests/app/UserTest.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
137b0f34fSGreg Roach<?php
23976b470SGreg Roach
337b0f34fSGreg Roach/**
437b0f34fSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
637b0f34fSGreg Roach * This program is free software: you can redistribute it and/or modify
737b0f34fSGreg Roach * it under the terms of the GNU General Public License as published by
837b0f34fSGreg Roach * the Free Software Foundation, either version 3 of the License, or
937b0f34fSGreg Roach * (at your option) any later version.
1037b0f34fSGreg Roach * This program is distributed in the hope that it will be useful,
1137b0f34fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1237b0f34fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1337b0f34fSGreg Roach * GNU General Public License for more details.
1437b0f34fSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1637b0f34fSGreg Roach */
17fcfa147eSGreg Roach
1837b0f34fSGreg Roachdeclare(strict_types=1);
1937b0f34fSGreg Roach
2037b0f34fSGreg Roachnamespace Fisharebest\Webtrees;
2137b0f34fSGreg Roach
2269675509SGreg Roachuse Fisharebest\Webtrees\Contracts\CacheFactoryInterface;
23184daea2SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
25*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
2669675509SGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter;
27e5a6b4d4SGreg Roach
28*202c018bSGreg Roach#[CoversClass(User::class)]
29e5a6b4d4SGreg Roachclass UserTest extends TestCase
3037b0f34fSGreg Roach{
31cd94ca66SGreg Roach    protected static bool $uses_database = true;
3237b0f34fSGreg Roach
3392a78a2fSGreg Roach    /**
3492a78a2fSGreg Roach     * Things to run before every test.
3592a78a2fSGreg Roach     */
3657b69958SGreg Roach    protected function setUp(): void
3769675509SGreg Roach    {
3869675509SGreg Roach        parent::setUp();
3969675509SGreg Roach
40cd94ca66SGreg Roach        $cache_factory = $this->createMock(CacheFactoryInterface::class);
4169675509SGreg Roach        $cache_factory->method('array')->willReturn(new Cache(new NullAdapter()));
426b9cb339SGreg Roach        Registry::cache($cache_factory);
4369675509SGreg Roach    }
4469675509SGreg Roach
45184daea2SGreg Roach    public function testConstructor(): void
4637b0f34fSGreg Roach    {
47184daea2SGreg Roach        $user = new User(123, 'username', 'real name', 'email');
4837b0f34fSGreg Roach
495e933c21SGreg Roach        self::assertInstanceOf(UserInterface::class, $user);
505e933c21SGreg Roach        self::assertSame(123, $user->id());
515e933c21SGreg Roach        self::assertSame('email', $user->email());
525e933c21SGreg Roach        self::assertSame('real name', $user->realName());
535e933c21SGreg Roach        self::assertSame('username', $user->userName());
54639e2984SGreg Roach    }
55639e2984SGreg Roach
56639e2984SGreg Roach    public function testGettersAndSetters(): void
57639e2984SGreg Roach    {
58e5a6b4d4SGreg Roach        $user_service = new UserService();
59e5a6b4d4SGreg Roach        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
60639e2984SGreg Roach
615e933c21SGreg Roach        self::assertSame(1, $user->id());
62639e2984SGreg Roach
635e933c21SGreg Roach        self::assertSame('user', $user->userName());
64639e2984SGreg Roach        $user->setUserName('foo');
655e933c21SGreg Roach        self::assertSame('foo', $user->userName());
66639e2984SGreg Roach
675e933c21SGreg Roach        self::assertSame('User', $user->realName());
68639e2984SGreg Roach        $user->setRealName('Foo');
695e933c21SGreg Roach        self::assertSame('Foo', $user->realName());
70639e2984SGreg Roach
715e933c21SGreg Roach        self::assertSame('user@example.com', $user->email());
72639e2984SGreg Roach        $user->setEmail('foo@example.com');
735e933c21SGreg Roach        self::assertSame('foo@example.com', $user->email());
74639e2984SGreg Roach
755e933c21SGreg Roach        self::assertTrue($user->checkPassword('secret'));
76639e2984SGreg Roach        $user->setPassword('letmein');
775e933c21SGreg Roach        self::assertTrue($user->checkPassword('letmein'));
78639e2984SGreg Roach    }
79639e2984SGreg Roach
80639e2984SGreg Roach    public function testPreferences(): void
81639e2984SGreg Roach    {
82e5a6b4d4SGreg Roach        $user_service = new UserService();
83e5a6b4d4SGreg Roach        $user         = $user_service->create('user', 'User', 'user@example.com', 'secret');
84639e2984SGreg Roach
855e933c21SGreg Roach        self::assertSame('', $user->getPreference('foo'));
86639e2984SGreg Roach        $user->setPreference('foo', 'bar');
875e933c21SGreg Roach        self::assertSame('bar', $user->getPreference('foo'));
88639e2984SGreg Roach    }
8937b0f34fSGreg Roach}
90