@@ -56,24 +56,135 @@ public function testVerifyReturnsErrorOnMissingResponse()
56
56
$ rc = new ReCaptcha ('secret ' );
57
57
$ response = $ rc ->verify ('' );
58
58
$ this ->assertFalse ($ response ->isSuccess ());
59
- $ this ->assertEquals (array (' missing-input-response ' ), $ response ->getErrorCodes ());
59
+ $ this ->assertEquals (array (Recaptcha:: E_MISSING_INPUT_RESPONSE ), $ response ->getErrorCodes ());
60
60
}
61
61
62
- public function testVerifyReturnsResponse ( )
62
+ private function getMockRequestMethod ( $ responseJson )
63
63
{
64
64
$ method = $ this ->getMockBuilder (\ReCaptcha \RequestMethod::class)
65
65
->disableOriginalConstructor ()
66
66
->setMethods (array ('submit ' ))
67
67
->getMock ();
68
- $ method ->expects ($ this ->once ())
69
- ->method ('submit ' )
70
- ->with ($ this ->callback (function ($ params ) {
71
- return true ;
72
- }))
73
- ->will ($ this ->returnValue ('{"success": true} ' ));
74
- ;
68
+ $ method ->expects ($ this ->any ())
69
+ ->method ('submit ' )
70
+ ->with ($ this ->callback (function ($ params ) {
71
+ return true ;
72
+ }))
73
+ ->will ($ this ->returnValue ($ responseJson ));
74
+ return $ method ;
75
+ }
76
+
77
+ public function testVerifyReturnsResponse ()
78
+ {
79
+ $ method = $ this ->getMockRequestMethod ('{"success": true} ' );
75
80
$ rc = new ReCaptcha ('secret ' , $ method );
76
81
$ response = $ rc ->verify ('response ' );
77
82
$ this ->assertTrue ($ response ->isSuccess ());
78
83
}
84
+
85
+ public function testVerifyAndValidateReturnsInitialResponseWithoutAdditionalChecks ()
86
+ {
87
+ $ method = $ this ->getMockRequestMethod ('{"success": true} ' );
88
+ $ rc = new ReCaptcha ('secret ' , $ method );
89
+ $ initialResponse = $ rc ->verify ('response ' );
90
+ $ this ->assertEquals ($ initialResponse , $ rc ->verifyAndValidate ('response ' ));
91
+ }
92
+
93
+ public function testVerifyAndValidateHostnameMatch ()
94
+ {
95
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "hostname": "host.name"} ' );
96
+ $ rc = new ReCaptcha ('secret ' , $ method );
97
+ $ response = $ rc ->setExpectedHostname ('host.name ' )->verifyAndValidate ('response ' );
98
+ $ this ->assertTrue ($ response ->isSuccess ());
99
+ }
100
+
101
+ public function testVerifyAndValidateHostnameMisMatch ()
102
+ {
103
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "hostname": "host.NOTname"} ' );
104
+ $ rc = new ReCaptcha ('secret ' , $ method );
105
+ $ response = $ rc ->setExpectedHostname ('host.name ' )->verifyAndValidate ('response ' );
106
+ $ this ->assertFalse ($ response ->isSuccess ());
107
+ $ this ->assertEquals (array (ReCaptcha::E_HOSTNAME_MISMATCH ), $ response ->getErrorCodes ());
108
+ }
109
+
110
+ public function testVerifyAndValidateApkPackageNameMatch ()
111
+ {
112
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "apk_package_name": "apk.name"} ' );
113
+ $ rc = new ReCaptcha ('secret ' , $ method );
114
+ $ response = $ rc ->setExpectedApkPackageName ('apk.name ' )->verifyAndValidate ('response ' );
115
+ $ this ->assertTrue ($ response ->isSuccess ());
116
+ }
117
+
118
+ public function testVerifyAndValidateApkPackageNameMisMatch ()
119
+ {
120
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "apk_package_name": "apk.NOTname"} ' );
121
+ $ rc = new ReCaptcha ('secret ' , $ method );
122
+ $ response = $ rc ->setExpectedApkPackageName ('apk.name ' )->verifyAndValidate ('response ' );
123
+ $ this ->assertFalse ($ response ->isSuccess ());
124
+ $ this ->assertEquals (array (ReCaptcha::E_APK_PACKAGE_NAME_MISMATCH ), $ response ->getErrorCodes ());
125
+ }
126
+
127
+ public function testVerifyAndValidateActionMatch ()
128
+ {
129
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "action": "action/name"} ' );
130
+ $ rc = new ReCaptcha ('secret ' , $ method );
131
+ $ response = $ rc ->setExpectedAction ('action/name ' )->verifyAndValidate ('response ' );
132
+ $ this ->assertTrue ($ response ->isSuccess ());
133
+ }
134
+
135
+ public function testVerifyAndValidateActionMisMatch ()
136
+ {
137
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "action": "action/NOTname"} ' );
138
+ $ rc = new ReCaptcha ('secret ' , $ method );
139
+ $ response = $ rc ->setExpectedAction ('action/name ' )->verifyAndValidate ('response ' );
140
+ $ this ->assertFalse ($ response ->isSuccess ());
141
+ $ this ->assertEquals (array (ReCaptcha::E_ACTION_MISMATCH ), $ response ->getErrorCodes ());
142
+ }
143
+
144
+ public function testVerifyAndValidateAboveThreshold ()
145
+ {
146
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "score": "0.9"} ' );
147
+ $ rc = new ReCaptcha ('secret ' , $ method );
148
+ $ response = $ rc ->setScoreThreshold ('0.5 ' )->verifyAndValidate ('response ' );
149
+ $ this ->assertTrue ($ response ->isSuccess ());
150
+ }
151
+
152
+ public function testVerifyAndValidateBelowThreshold ()
153
+ {
154
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "score": "0.1"} ' );
155
+ $ rc = new ReCaptcha ('secret ' , $ method );
156
+ $ response = $ rc ->setScoreThreshold ('0.5 ' )->verifyAndValidate ('response ' );
157
+ $ this ->assertFalse ($ response ->isSuccess ());
158
+ $ this ->assertEquals (array (ReCaptcha::E_SCORE_THRESHOLD_NOT_MET ), $ response ->getErrorCodes ());
159
+ }
160
+
161
+ public function testVerifyAndValidateWithinTimeout ()
162
+ {
163
+ // Responses come back like 2018-07-31T13:48:41Z
164
+ $ challengeTs = date ('Y-M-d\TH:i:s\Z ' , time ());
165
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "challenge_ts": " ' .$ challengeTs .'"} ' );
166
+ $ rc = new ReCaptcha ('secret ' , $ method );
167
+ $ response = $ rc ->setChallengeTimeout ('1000 ' )->verifyAndValidate ('response ' );
168
+ $ this ->assertTrue ($ response ->isSuccess ());
169
+ }
170
+
171
+ public function testVerifyAndValidateOverTimeout ()
172
+ {
173
+ // Responses come back like 2018-07-31T13:48:41Z
174
+ $ challengeTs = date ('Y-M-d\TH:i:s\Z ' , time () - 600 );
175
+ $ method = $ this ->getMockRequestMethod ('{"success": true, "challenge_ts": " ' .$ challengeTs .'"} ' );
176
+ $ rc = new ReCaptcha ('secret ' , $ method );
177
+ $ response = $ rc ->setChallengeTimeout ('60 ' )->verifyAndValidate ('response ' );
178
+ $ this ->assertFalse ($ response ->isSuccess ());
179
+ $ this ->assertEquals (array (ReCaptcha::E_CHALLENGE_TIMEOUT ), $ response ->getErrorCodes ());
180
+ }
181
+
182
+ public function testVerifyAndValidateMergesErrors ()
183
+ {
184
+ $ method = $ this ->getMockRequestMethod ('{"success": false, "error-codes": ["initial-error"], "score": "0.1"} ' );
185
+ $ rc = new ReCaptcha ('secret ' , $ method );
186
+ $ response = $ rc ->setScoreThreshold ('0.5 ' )->verifyAndValidate ('response ' );
187
+ $ this ->assertFalse ($ response ->isSuccess ());
188
+ $ this ->assertEquals (array ('initial-error ' , ReCaptcha::E_SCORE_THRESHOLD_NOT_MET ), $ response ->getErrorCodes ());
189
+ }
79
190
}
0 commit comments