Code Duplication    Length = 161-161 lines in 2 locations

eZ/Publish/Core/MVC/Symfony/Security/Tests/User/EmailProviderTest.php 1 location

@@ 26-186 (lines=161) @@
23
use PHPUnit\Framework\TestCase;
24
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
25
26
class EmailProviderTest extends TestCase
27
{
28
    /** @var \eZ\Publish\API\Repository\UserService|\PHPUnit\Framework\MockObject\MockObject */
29
    private $userService;
30
31
    /** @var \eZ\Publish\API\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */
32
    private $permissionResolver;
33
34
    /** @var \eZ\Publish\Core\MVC\Symfony\Security\User\EmailProvider */
35
    private $userProvider;
36
37
    protected function setUp(): void
38
    {
39
        parent::setUp();
40
        $this->userService = $this->createMock(UserService::class);
41
        $this->permissionResolver = $this->createMock(PermissionResolver::class);
42
        $this->userProvider = new EmailProvider($this->userService, $this->permissionResolver);
43
    }
44
45
    public function testLoadUserByUsernameAlreadyUserObject()
46
    {
47
        $user = $this->createMock(UserInterface::class);
48
        $this->assertSame($user, $this->userProvider->loadUserByUsername($user));
49
    }
50
51
    public function testLoadUserByUsernameUserNotFound()
52
    {
53
        $this->expectException(\Symfony\Component\Security\Core\Exception\UsernameNotFoundException::class);
54
55
        $username = '[email protected]';
56
        $this->userService
57
            ->expects($this->once())
58
            ->method('loadUserByEmail')
59
            ->with($username)
60
            ->will($this->throwException(new NotFoundException('user', $username)));
61
        $this->userProvider->loadUserByUsername($username);
62
    }
63
64
    public function testLoadUserByUsername()
65
    {
66
        $username = '[email protected]';
67
        $apiUser = $this->createMock(APIUser::class);
68
69
        $this->userService
70
            ->expects($this->once())
71
            ->method('loadUserByEmail')
72
            ->with($username)
73
            ->will($this->returnValue($apiUser));
74
75
        $user = $this->userProvider->loadUserByUsername($username);
76
        $this->assertInstanceOf(UserInterface::class, $user);
77
        $this->assertSame($apiUser, $user->getAPIUser());
78
        $this->assertSame(['ROLE_USER'], $user->getRoles());
79
    }
80
81
    public function testRefreshUserNotSupported()
82
    {
83
        $this->expectException(\Symfony\Component\Security\Core\Exception\UnsupportedUserException::class);
84
85
        $user = $this->createMock(SymfonyUserInterface::class);
86
        $this->userProvider->refreshUser($user);
87
    }
88
89
    public function testRefreshUser()
90
    {
91
        $userId = 123;
92
        $apiUser = new User(
93
            [
94
                'content' => new Content(
95
                    [
96
                        'versionInfo' => new VersionInfo(
97
                            ['contentInfo' => new ContentInfo(['id' => $userId])]
98
                        ),
99
                    ]
100
                ),
101
            ]
102
        );
103
        $refreshedAPIUser = clone $apiUser;
104
        $user = $this->createMock(UserInterface::class);
105
        $user
106
            ->expects($this->once())
107
            ->method('getAPIUser')
108
            ->will($this->returnValue($apiUser));
109
        $user
110
            ->expects($this->once())
111
            ->method('setAPIUser')
112
            ->with($refreshedAPIUser);
113
114
        $this->userService
115
            ->expects($this->once())
116
            ->method('loadUser')
117
            ->with($userId)
118
            ->will($this->returnValue($refreshedAPIUser));
119
120
        $this->permissionResolver
121
            ->expects($this->once())
122
            ->method('setCurrentUserReference')
123
            ->with(new UserReference($apiUser->getUserId()));
124
125
        $this->assertSame($user, $this->userProvider->refreshUser($user));
126
    }
127
128
    public function testRefreshUserNotFound()
129
    {
130
        $this->expectException(\Symfony\Component\Security\Core\Exception\UsernameNotFoundException::class);
131
132
        $userId = 123;
133
        $apiUser = new User(
134
            [
135
                'content' => new Content(
136
                    [
137
                        'versionInfo' => new VersionInfo(
138
                            ['contentInfo' => new ContentInfo(['id' => $userId])]
139
                        ),
140
                    ]
141
                ),
142
            ]
143
        );
144
        $user = $this->createMock(UserInterface::class);
145
        $user
146
            ->expects($this->once())
147
            ->method('getAPIUser')
148
            ->will($this->returnValue($apiUser));
149
150
        $this->userService
151
            ->expects($this->once())
152
            ->method('loadUser')
153
            ->with($userId)
154
            ->will($this->throwException(new NotFoundException('user', 'foo')));
155
156
        $this->userProvider->refreshUser($user);
157
    }
158
159
    /**
160
     * @dataProvider supportsClassProvider
161
     */
162
    public function testSupportsClass($class, $supports)
163
    {
164
        $this->assertSame($supports, $this->userProvider->supportsClass($class));
165
    }
166
167
    public function supportsClassProvider()
168
    {
169
        return [
170
            [SymfonyUserInterface::class, false],
171
            [MVCUser::class, true],
172
            [get_class($this->createMock(MVCUser::class)), true],
173
        ];
174
    }
175
176
    public function testLoadUserByAPIUser()
177
    {
178
        $apiUser = $this->createMock(APIUser::class);
179
180
        $user = $this->userProvider->loadUserByAPIUser($apiUser);
181
182
        $this->assertInstanceOf(MVCUser::class, $user);
183
        $this->assertSame($apiUser, $user->getAPIUser());
184
        $this->assertSame(['ROLE_USER'], $user->getRoles());
185
    }
186
}
187

eZ/Publish/Core/MVC/Symfony/Security/Tests/User/UsernameProviderTest.php 1 location

@@ 26-186 (lines=161) @@
23
use PHPUnit\Framework\TestCase;
24
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
25
26
class UsernameProviderTest extends TestCase
27
{
28
    /** @var \eZ\Publish\API\Repository\UserService|\PHPUnit\Framework\MockObject\MockObject */
29
    private $userService;
30
31
    /** @var \eZ\Publish\API\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */
32
    private $permissionResolver;
33
34
    /** @var \eZ\Publish\Core\MVC\Symfony\Security\User\UsernameProvider */
35
    private $userProvider;
36
37
    protected function setUp(): void
38
    {
39
        parent::setUp();
40
        $this->userService = $this->createMock(UserService::class);
41
        $this->permissionResolver = $this->createMock(PermissionResolver::class);
42
        $this->userProvider = new UsernameProvider($this->userService, $this->permissionResolver);
43
    }
44
45
    public function testLoadUserByUsernameAlreadyUserObject()
46
    {
47
        $user = $this->createMock(UserInterface::class);
48
        $this->assertSame($user, $this->userProvider->loadUserByUsername($user));
49
    }
50
51
    public function testLoadUserByUsernameUserNotFound()
52
    {
53
        $this->expectException(\Symfony\Component\Security\Core\Exception\UsernameNotFoundException::class);
54
55
        $username = 'foobar';
56
        $this->userService
57
            ->expects($this->once())
58
            ->method('loadUserByLogin')
59
            ->with($username)
60
            ->will($this->throwException(new NotFoundException('user', $username)));
61
        $this->userProvider->loadUserByUsername($username);
62
    }
63
64
    public function testLoadUserByUsername()
65
    {
66
        $username = 'foobar';
67
        $apiUser = $this->createMock(APIUser::class);
68
69
        $this->userService
70
            ->expects($this->once())
71
            ->method('loadUserByLogin')
72
            ->with($username)
73
            ->will($this->returnValue($apiUser));
74
75
        $user = $this->userProvider->loadUserByUsername($username);
76
        $this->assertInstanceOf(UserInterface::class, $user);
77
        $this->assertSame($apiUser, $user->getAPIUser());
78
        $this->assertSame(['ROLE_USER'], $user->getRoles());
79
    }
80
81
    public function testRefreshUserNotSupported()
82
    {
83
        $this->expectException(\Symfony\Component\Security\Core\Exception\UnsupportedUserException::class);
84
85
        $user = $this->createMock(SymfonyUserInterface::class);
86
        $this->userProvider->refreshUser($user);
87
    }
88
89
    public function testRefreshUser()
90
    {
91
        $userId = 123;
92
        $apiUser = new User(
93
            [
94
                'content' => new Content(
95
                    [
96
                        'versionInfo' => new VersionInfo(
97
                            ['contentInfo' => new ContentInfo(['id' => $userId])]
98
                        ),
99
                    ]
100
                ),
101
            ]
102
        );
103
        $refreshedAPIUser = clone $apiUser;
104
        $user = $this->createMock(UserInterface::class);
105
        $user
106
            ->expects($this->once())
107
            ->method('getAPIUser')
108
            ->will($this->returnValue($apiUser));
109
        $user
110
            ->expects($this->once())
111
            ->method('setAPIUser')
112
            ->with($refreshedAPIUser);
113
114
        $this->userService
115
            ->expects($this->once())
116
            ->method('loadUser')
117
            ->with($userId)
118
            ->will($this->returnValue($refreshedAPIUser));
119
120
        $this->permissionResolver
121
            ->expects($this->once())
122
            ->method('setCurrentUserReference')
123
            ->with(new UserReference($apiUser->getUserId()));
124
125
        $this->assertSame($user, $this->userProvider->refreshUser($user));
126
    }
127
128
    public function testRefreshUserNotFound()
129
    {
130
        $this->expectException(\Symfony\Component\Security\Core\Exception\UsernameNotFoundException::class);
131
132
        $userId = 123;
133
        $apiUser = new User(
134
            [
135
                'content' => new Content(
136
                    [
137
                        'versionInfo' => new VersionInfo(
138
                            ['contentInfo' => new ContentInfo(['id' => $userId])]
139
                        ),
140
                    ]
141
                ),
142
            ]
143
        );
144
        $user = $this->createMock(UserInterface::class);
145
        $user
146
            ->expects($this->once())
147
            ->method('getAPIUser')
148
            ->will($this->returnValue($apiUser));
149
150
        $this->userService
151
            ->expects($this->once())
152
            ->method('loadUser')
153
            ->with($userId)
154
            ->will($this->throwException(new NotFoundException('user', 'foo')));
155
156
        $this->userProvider->refreshUser($user);
157
    }
158
159
    /**
160
     * @dataProvider supportsClassProvider
161
     */
162
    public function testSupportsClass($class, $supports)
163
    {
164
        $this->assertSame($supports, $this->userProvider->supportsClass($class));
165
    }
166
167
    public function supportsClassProvider()
168
    {
169
        return [
170
            [SymfonyUserInterface::class, false],
171
            [MVCUser::class, true],
172
            [get_class($this->createMock(MVCUser::class)), true],
173
        ];
174
    }
175
176
    public function testLoadUserByAPIUser()
177
    {
178
        $apiUser = $this->createMock(APIUser::class);
179
180
        $user = $this->userProvider->loadUserByAPIUser($apiUser);
181
182
        $this->assertInstanceOf(MVCUser::class, $user);
183
        $this->assertSame($apiUser, $user->getAPIUser());
184
        $this->assertSame(['ROLE_USER'], $user->getRoles());
185
    }
186
}
187