Completed
Push — EZP-31044-site-access-provider ( 6f7695...16abcc )
by
unknown
14:57
created

UserWrappedTest::testRegularUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 40
rs 9.28
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the UserWrappedTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\MVC\Symfony\Security\Tests;
10
11
use eZ\Publish\API\Repository\Values\User\User as APIUser;
12
use eZ\Publish\Core\MVC\Symfony\Security\UserInterface;
13
use eZ\Publish\Core\MVC\Symfony\Security\UserWrapped;
14
use PHPUnit\Framework\TestCase;
15
use Symfony\Component\Security\Core\User\EquatableInterface;
16
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
17
18
class UserWrappedTest extends TestCase
19
{
20
    /** @var \PHPUnit\Framework\MockObject\MockObject */
21
    private $apiUser;
22
23
    protected function setUp(): void
24
    {
25
        parent::setUp();
26
        $this->apiUser = $this->createMock(APIUser::class);
27
    }
28
29 View Code Duplication
    public function testGetSetAPIUser()
30
    {
31
        $originalUser = $this->createMock(SymfonyUserInterface::class);
32
        $userWrapped = new UserWrapped($originalUser, $this->apiUser);
33
        $this->assertSame($this->apiUser, $userWrapped->getAPIUser());
34
35
        $newApiUser = $this->createMock(APIUser::class);
36
        $userWrapped->setAPIUser($newApiUser);
37
        $this->assertSame($newApiUser, $userWrapped->getAPIUser());
38
    }
39
40 View Code Duplication
    public function testGetSetWrappedUser()
41
    {
42
        $originalUser = $this->createMock(SymfonyUserInterface::class);
43
        $userWrapped = new UserWrapped($originalUser, $this->apiUser);
44
        $this->assertSame($originalUser, $userWrapped->getWrappedUser());
45
46
        $newWrappedUser = $this->createMock(UserInterface::class);
47
        $userWrapped->setWrappedUser($newWrappedUser);
48
        $this->assertSame($newWrappedUser, $userWrapped->getWrappedUser());
49
    }
50
51
    public function testRegularUser()
52
    {
53
        $originalUser = $this->createMock(SymfonyUserInterface::class);
54
        $user = new UserWrapped($originalUser, $this->apiUser);
55
56
        $this->assertTrue($user->isEqualTo($this->createMock(SymfonyUserInterface::class)));
57
58
        $originalUser
59
            ->expects($this->once())
60
            ->method('eraseCredentials');
61
        $user->eraseCredentials();
62
63
        $username = 'lolautruche';
64
        $password = 'NoThisIsNotMyRealPassword';
65
        $roles = ['ROLE_USER', 'ROLE_TEST'];
66
        $salt = md5(microtime(true));
67
        $originalUser
68
            ->expects($this->exactly(2))
69
            ->method('getUsername')
70
            ->will($this->returnValue($username));
71
        $originalUser
72
            ->expects($this->once())
73
            ->method('getPassword')
74
            ->will($this->returnValue($password));
75
        $originalUser
76
            ->expects($this->once())
77
            ->method('getRoles')
78
            ->will($this->returnValue($roles));
79
        $originalUser
80
            ->expects($this->once())
81
            ->method('getSalt')
82
            ->will($this->returnValue($salt));
83
84
        $this->assertSame($username, $user->getUsername());
85
        $this->assertSame($username, (string)$user);
86
        $this->assertSame($password, $user->getPassword());
87
        $this->assertSame($roles, $user->getRoles());
88
        $this->assertSame($salt, $user->getSalt());
89
        $this->assertSame($originalUser, $user->getWrappedUser());
90
    }
91
92
    public function testIsEqualTo()
93
    {
94
        $originalUser = $this->createMock(UserEquatableInterface::class);
95
        $user = new UserWrapped($originalUser, $this->apiUser);
96
        $otherUser = $this->createMock(SymfonyUserInterface::class);
97
        $originalUser
98
            ->expects($this->once())
99
            ->method('isEqualTo')
100
            ->with($otherUser)
101
            ->will($this->returnValue(false));
102
        $this->assertFalse($user->isEqualTo($otherUser));
103
    }
104
}
105
106
/**
107
 * @internal For use with tests only
108
 */
109
interface UserEquatableInterface extends UserInterface, EquatableInterface
110
{
111
}
112