|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\OpenIDBundle\Tests\EventListener; |
|
12
|
|
|
|
|
13
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
14
|
|
|
use JMS\Serializer\GenericSerializationVisitor; |
|
15
|
|
|
use LoginCidadao\APIBundle\Service\VersionService; |
|
16
|
|
|
use LoginCidadao\OAuthBundle\Entity\Client; |
|
17
|
|
|
use LoginCidadao\OAuthBundle\Model\AccessTokenManager; |
|
18
|
|
|
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata; |
|
19
|
|
|
use LoginCidadao\OpenIDBundle\EventListener\PersonSerializeEventListener; |
|
20
|
|
|
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService; |
|
21
|
|
|
|
|
22
|
|
|
class PersonSerializeEventListenerTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
public function testGetSubscribedEvents() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->assertEquals([ |
|
27
|
|
|
[ |
|
28
|
|
|
'event' => 'serializer.post_serialize', |
|
29
|
|
|
'method' => 'onPostSerialize', |
|
30
|
|
|
'class' => 'LoginCidadao\CoreBundle\Model\PersonInterface', |
|
31
|
|
|
], |
|
32
|
|
|
], PersonSerializeEventListener::getSubscribedEvents()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testOnPostSerializeNonPerson() |
|
36
|
|
|
{ |
|
37
|
|
|
$object = new \stdClass(); |
|
38
|
|
|
|
|
39
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ObjectEvent $event */ |
|
40
|
|
|
$event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\ObjectEvent') |
|
41
|
|
|
->disableOriginalConstructor()->getMock(); |
|
42
|
|
|
$event->expects($this->once())->method('getObject')->willReturn($object); |
|
43
|
|
|
$event->expects($this->never())->method('getVisitor'); |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$listener = new PersonSerializeEventListener( |
|
47
|
|
|
$this->getAccessTokenManager(), |
|
48
|
|
|
$this->getSubjectIdentifierService(), |
|
49
|
|
|
$this->getVersionService() |
|
50
|
|
|
); |
|
51
|
|
|
$listener->onPostSerialize($event); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testOnPostSerializePersonV2() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->runOnPostSerializeTest(['major' => 2, 0, 0], true, 3); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testOnPostSerializePersonV1() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->runOnPostSerializeTest(['major' => 1, 0, 0], false, 2); |
|
62
|
|
|
$this->runOnPostSerializeTest(['major' => 1, 1, 0], true, 4); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|AccessTokenManager |
|
67
|
|
|
*/ |
|
68
|
|
|
private function getAccessTokenManager() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->getMockBuilder('LoginCidadao\OAuthBundle\Model\AccessTokenManager') |
|
71
|
|
|
->disableOriginalConstructor()->getMock(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|SubjectIdentifierService |
|
76
|
|
|
*/ |
|
77
|
|
|
private function getSubjectIdentifierService() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->getMockBuilder('LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService') |
|
80
|
|
|
->disableOriginalConstructor()->getMock(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|VersionService |
|
85
|
|
|
*/ |
|
86
|
|
|
private function getVersionService() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->getMockBuilder('LoginCidadao\APIBundle\Service\VersionService') |
|
89
|
|
|
->disableOriginalConstructor()->getMock(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|GenericSerializationVisitor |
|
94
|
|
|
*/ |
|
95
|
|
|
private function getVisitor() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->getMockBuilder('JMS\Serializer\GenericSerializationVisitor') |
|
98
|
|
|
->disableOriginalConstructor()->getMock(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function getAddDataCallback($sub, $pictureUrl) |
|
102
|
|
|
{ |
|
103
|
|
|
return function ($key, $value) use ($sub, $pictureUrl) { |
|
104
|
|
|
switch ($key) { |
|
105
|
|
|
case 'id': |
|
106
|
|
|
case 'sub': |
|
107
|
|
|
$this->assertEquals($sub, $value); |
|
108
|
|
|
break; |
|
109
|
|
|
case 'picture': |
|
110
|
|
|
$this->assertEquals($pictureUrl, $value); |
|
111
|
|
|
break; |
|
112
|
|
|
case 'email_verified': |
|
113
|
|
|
$this->assertTrue($value); |
|
114
|
|
|
break; |
|
115
|
|
|
default: |
|
116
|
|
|
$this->fail("Unexpected addData call: {$key}"); |
|
117
|
|
|
} |
|
118
|
|
|
}; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function runOnPostSerializeTest($version, $expectOIDCFields, $addDataCount) |
|
122
|
|
|
{ |
|
123
|
|
|
$pictureUrl = 'https://picture.url/pic.jpg'; |
|
124
|
|
|
$person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
125
|
|
|
if ($expectOIDCFields) { |
|
126
|
|
|
$person->expects($this->once())->method('getProfilePictureUrl')->willReturn($pictureUrl); |
|
127
|
|
|
$person->expects($this->once())->method('getEmailConfirmedAt')->willReturn(new \DateTime()); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$visitor = $this->getVisitor(); |
|
131
|
|
|
$visitor->expects($this->exactly($addDataCount)) |
|
132
|
|
|
->method('addData') |
|
133
|
|
|
->willReturnCallback($this->getAddDataCallback('sub', $pictureUrl)); |
|
134
|
|
|
|
|
135
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ObjectEvent $event */ |
|
136
|
|
|
$event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\ObjectEvent') |
|
137
|
|
|
->disableOriginalConstructor()->getMock(); |
|
138
|
|
|
$event->expects($this->exactly(3))->method('getObject')->willReturn($person); |
|
139
|
|
|
$event->expects($this->exactly(2))->method('getVisitor')->willReturn($visitor); |
|
140
|
|
|
|
|
141
|
|
|
$client = new Client(); |
|
142
|
|
|
$metadata = new ClientMetadata(); |
|
143
|
|
|
$client->setMetadata($metadata); |
|
144
|
|
|
$metadata->setClient($client); |
|
145
|
|
|
|
|
146
|
|
|
$accessTokenManager = $this->getAccessTokenManager(); |
|
147
|
|
|
$accessTokenManager->expects($this->once()) |
|
148
|
|
|
->method('getTokenClient') |
|
149
|
|
|
->willReturn($client); |
|
150
|
|
|
|
|
151
|
|
|
$subjectIdentifierService = $this->getSubjectIdentifierService(); |
|
152
|
|
|
$subjectIdentifierService->expects($this->once()) |
|
153
|
|
|
->method('getSubjectIdentifier') |
|
154
|
|
|
->with($person, $metadata) |
|
155
|
|
|
->willReturn('sub'); |
|
156
|
|
|
|
|
157
|
|
|
$versionService = $this->getVersionService(); |
|
158
|
|
|
$versionService->expects($this->exactly(2))->method('getVersionFromRequest')->willReturn($version); |
|
159
|
|
|
$versionService->expects($this->once())->method('getString')->willReturn(implode('.', $version)); |
|
160
|
|
|
|
|
161
|
|
|
$listener = new PersonSerializeEventListener( |
|
162
|
|
|
$accessTokenManager, |
|
163
|
|
|
$subjectIdentifierService, |
|
164
|
|
|
$versionService |
|
165
|
|
|
); |
|
166
|
|
|
$listener->onPostSerialize($event); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|