8
8
use IMSGlobal \LTI \Tests \unit \helpers \DummyDatabase ;
9
9
use IMSGlobal \LTI \Tests \unit \helpers \InMemoryCache ;
10
10
11
- class LTI_OIDC_Login_Test extends TestBase {
11
+ class LTI_OIDC_Login_Test extends TestBase
12
+ {
12
13
private $ launchUrl = 'https://example.com/lti1p3/launch ' ;
13
14
14
15
public function testNewInstance ()
15
16
{
16
17
$ this ->assertInstanceOf (LTI_OIDC_Login::class, LTI_OIDC_Login::newInstance (
17
18
new DummyDatabase ()
18
19
));
19
- }
20
-
20
+ }
21
+
21
22
public function testDoOidcLoginRedirectEmptyLaunchUrl ()
22
23
{
23
24
$ this ->setExpectedException ('IMSGlobal\LTI\OIDC_Exception ' , 'No launch URL configured ' );
@@ -31,7 +32,7 @@ public function testValidateOidcLoginNoIssuer()
31
32
LTI_OIDC_Login::newInstance (new DummyDatabase ())->do_oidc_login_redirect (
32
33
$ this ->launchUrl ,
33
34
[]
34
- );
35
+ );
35
36
}
36
37
37
38
public function testValidateOidcLoginNoPasswordHint ()
@@ -40,9 +41,9 @@ public function testValidateOidcLoginNoPasswordHint()
40
41
LTI_OIDC_Login::newInstance (new DummyDatabase ())->do_oidc_login_redirect (
41
42
$ this ->launchUrl ,
42
43
['iss ' => 'aaa ' ]
43
- );
44
- }
45
-
44
+ );
45
+ }
46
+
46
47
public function testValidateOidcLoginRegistrationNotFound ()
47
48
{
48
49
$ this ->setExpectedException ('IMSGlobal\LTI\OIDC_Exception ' , 'Could not find registration details ' );
@@ -51,7 +52,7 @@ public function testValidateOidcLoginRegistrationNotFound()
51
52
$ registrationDatabase = $ this ->getMockBuilder (DummyDatabase::class)
52
53
->setMethods (['find_registration_by_issuer ' ])
53
54
->getMock ();
54
-
55
+
55
56
$ registrationDatabase ->expects ($ this ->atLeastOnce ())
56
57
->method ('find_registration_by_issuer ' )
57
58
->with ('xyz ' , null )
@@ -63,25 +64,25 @@ public function testValidateOidcLoginRegistrationNotFound()
63
64
'iss ' => 'xyz ' ,
64
65
'login_hint ' => 'password123 '
65
66
]
66
- );
67
- }
68
-
67
+ );
68
+ }
69
+
69
70
public function testDoOidcLoginRedirect ()
70
71
{
71
72
/** @var InMemoryCache|\PHPUnit_Framework_MockObject_MockObject */
72
73
$ cache = $ this ->getMockBuilder (InMemoryCache::class)
73
74
->setMethods (['cache_nonce ' ])
74
75
->getMock ();
75
-
76
+
76
77
$ cache ->expects ($ this ->once ())->method ('cache_nonce ' )->with (
77
78
$ this ->stringStartsWith ('nonce- ' )
78
79
);
79
-
80
+
80
81
/** @var Cookie|\PHPUnit_Framework_MockObject_MockObject $cookie */
81
82
$ cookie = $ this ->getMockBuilder (Cookie::class)
82
83
->setMethods (['set_cookie ' ])
83
84
->getMock ();
84
-
85
+
85
86
$ cookie ->expects ($ this ->once ())->method ('set_cookie ' )->with (
86
87
$ this ->stringStartsWith ('lti1p3_state- ' ),
87
88
$ this ->stringStartsWith ('state- ' )
@@ -98,14 +99,14 @@ public function testDoOidcLoginRedirect()
98
99
'login_hint ' => 'password123 ' ,
99
100
'lti_message_hint ' => 'my message hint '
100
101
]
101
- );
102
-
102
+ );
103
+
103
104
$ this ->assertInstanceOf (Redirect::class, $ redirect );
104
105
105
106
$ redirectUrl = parse_url ($ redirect ->get_redirect_url ());
106
107
107
108
$ this ->assertEquals ('https ' , $ redirectUrl ['scheme ' ]);
108
- $ this ->assertEquals ('example.com ' , $ redirectUrl ['host ' ]);
109
+ $ this ->assertEquals ('example.com ' , $ redirectUrl ['host ' ]);
109
110
$ this ->assertEquals ('/lti1p3/aaa/12345/auth_login_url ' , $ redirectUrl ['path ' ]);
110
111
parse_str ($ redirectUrl ['query ' ], $ query );
111
112
$ this ->assertEquals ('openid ' , $ query ['scope ' ]);
@@ -119,4 +120,79 @@ public function testDoOidcLoginRedirect()
119
120
$ this ->assertEquals ('password123 ' , $ query ['login_hint ' ]);
120
121
$ this ->assertEquals ('my message hint ' , $ query ['lti_message_hint ' ]);
121
122
}
123
+
124
+ /**
125
+ * @dataProvider clientIdProvider
126
+ * @param array $request The request array to use for the test
127
+ */
128
+ public function testClientIdIsSetOnOidcLogin (array $ request )
129
+ {
130
+ /** @var InMemoryCache|\PHPUnit_Framework_MockObject_MockObject */
131
+ $ cache = $ this ->getMockBuilder (InMemoryCache::class)
132
+ ->setMethods (['cache_nonce ' ])
133
+ ->getMock ();
134
+
135
+ $ cache ->expects ($ this ->once ())->method ('cache_nonce ' )->with (
136
+ $ this ->stringStartsWith ('nonce- ' )
137
+ );
138
+
139
+ /** @var Cookie|\PHPUnit_Framework_MockObject_MockObject $cookie */
140
+ $ cookie = $ this ->getMockBuilder (Cookie::class)
141
+ ->setMethods (['set_cookie ' ])
142
+ ->getMock ();
143
+
144
+ $ cookie ->expects ($ this ->once ())->method ('set_cookie ' )->with (
145
+ $ this ->stringStartsWith ('lti1p3_state- ' ),
146
+ $ this ->stringStartsWith ('state- ' )
147
+ )->willReturn ($ cookie );
148
+
149
+ $ redirect = LTI_OIDC_Login::newInstance (
150
+ new DummyDatabase (),
151
+ $ cache ,
152
+ $ cookie
153
+ )->do_oidc_login_redirect (
154
+ $ this ->launchUrl ,
155
+ $ request
156
+ );
157
+
158
+ $ expectedId = isset ($ request ['client_id ' ]) ? $ request ['client_id ' ] : $ request ['aud ' ];
159
+
160
+ $ this ->assertInstanceOf (Redirect::class, $ redirect );
161
+
162
+ $ redirectUrl = parse_url ($ redirect ->get_redirect_url ());
163
+
164
+ parse_str ($ redirectUrl ['query ' ], $ query );
165
+ $ this ->assertEquals ($ expectedId , $ query ['client_id ' ]);
166
+ }
167
+
168
+ public function clientIdProvider ()
169
+ {
170
+ return [
171
+ 'clientId as client_id ' => [
172
+ [
173
+ 'iss ' => 'aaa ' ,
174
+ 'login_hint ' => 'password123 ' ,
175
+ 'lti_message_hint ' => 'my message hint ' ,
176
+ 'client_id ' => '12345 '
177
+ ]
178
+ ],
179
+ 'clientId as aud ' => [
180
+ [
181
+ 'iss ' => 'bbb ' ,
182
+ 'login_hint ' => 'password123 ' ,
183
+ 'lti_message_hint ' => 'my message hint ' ,
184
+ 'aud ' => 'aud_12345 '
185
+ ]
186
+ ],
187
+ 'clientId and aud present ' => [
188
+ [
189
+ 'iss ' => 'ccc ' ,
190
+ 'login_hint ' => 'password123 ' ,
191
+ 'lti_message_hint ' => 'my message hint ' ,
192
+ 'aud ' => 'aud_12345 ' ,
193
+ 'client_id ' => '12345 '
194
+ ]
195
+ ]
196
+ ];
197
+ }
122
198
}
0 commit comments