ResponseTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * This is a PHP library that handles calling reCAPTCHA.
  4. *
  5. * BSD 3-Clause License
  6. * @copyright (c) 2019, Google Inc.
  7. * @link https://www.google.com/recaptcha
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. Neither the name of the copyright holder nor the names of its
  20. * contributors may be used to endorse or promote products derived from
  21. * this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. namespace ReCaptcha;
  35. use PHPUnit\Framework\TestCase;
  36. class ResponseTest extends TestCase
  37. {
  38. /**
  39. * @dataProvider provideJson
  40. */
  41. public function testFromJson($json, $success, $errorCodes, $hostname, $challengeTs, $apkPackageName, $score, $action)
  42. {
  43. $response = Response::fromJson($json);
  44. $this->assertEquals($success, $response->isSuccess());
  45. $this->assertEquals($errorCodes, $response->getErrorCodes());
  46. $this->assertEquals($hostname, $response->getHostname());
  47. $this->assertEquals($challengeTs, $response->getChallengeTs());
  48. $this->assertEquals($apkPackageName, $response->getApkPackageName());
  49. $this->assertEquals($score, $response->getScore());
  50. $this->assertEquals($action, $response->getAction());
  51. }
  52. public function provideJson()
  53. {
  54. return array(
  55. array(
  56. '{"success": true}',
  57. true, array(), null, null, null, null, null,
  58. ),
  59. array(
  60. '{"success": true, "hostname": "google.com"}',
  61. true, array(), 'google.com', null, null, null, null,
  62. ),
  63. array(
  64. '{"success": false, "error-codes": ["test"]}',
  65. false, array('test'), null, null, null, null, null,
  66. ),
  67. array(
  68. '{"success": false, "error-codes": ["test"], "hostname": "google.com"}',
  69. false, array('test'), 'google.com', null, null, null, null,
  70. ),
  71. array(
  72. '{"success": false, "error-codes": ["test"], "hostname": "google.com", "challenge_ts": "timestamp", "apk_package_name": "apk", "score": "0.5", "action": "action"}',
  73. false, array('test'), 'google.com', 'timestamp', 'apk', 0.5, 'action',
  74. ),
  75. array(
  76. '{"success": true, "error-codes": ["test"]}',
  77. true, array(), null, null, null, null, null,
  78. ),
  79. array(
  80. '{"success": true, "error-codes": ["test"], "hostname": "google.com"}',
  81. true, array(), 'google.com', null, null, null, null,
  82. ),
  83. array(
  84. '{"success": false}',
  85. false, array(ReCaptcha::E_UNKNOWN_ERROR), null, null, null, null, null,
  86. ),
  87. array(
  88. '{"success": false, "hostname": "google.com"}',
  89. false, array(ReCaptcha::E_UNKNOWN_ERROR), 'google.com', null, null, null, null,
  90. ),
  91. array(
  92. 'BAD JSON',
  93. false, array(ReCaptcha::E_INVALID_JSON), null, null, null, null, null,
  94. ),
  95. );
  96. }
  97. public function testIsSuccess()
  98. {
  99. $response = new Response(true);
  100. $this->assertTrue($response->isSuccess());
  101. $response = new Response(false);
  102. $this->assertFalse($response->isSuccess());
  103. $response = new Response(true, array(), 'example.com');
  104. $this->assertEquals('example.com', $response->getHostName());
  105. }
  106. public function testGetErrorCodes()
  107. {
  108. $errorCodes = array('test');
  109. $response = new Response(true, $errorCodes);
  110. $this->assertEquals($errorCodes, $response->getErrorCodes());
  111. }
  112. public function testGetHostname()
  113. {
  114. $hostname = 'google.com';
  115. $errorCodes = array();
  116. $response = new Response(true, $errorCodes, $hostname);
  117. $this->assertEquals($hostname, $response->getHostname());
  118. }
  119. public function testGetChallengeTs()
  120. {
  121. $timestamp = 'timestamp';
  122. $errorCodes = array();
  123. $response = new Response(true, array(), 'hostname', $timestamp);
  124. $this->assertEquals($timestamp, $response->getChallengeTs());
  125. }
  126. public function TestGetApkPackageName()
  127. {
  128. $apk = 'apk';
  129. $response = new Response(true, array(), 'hostname', 'timestamp', 'apk');
  130. $this->assertEquals($apk, $response->getApkPackageName());
  131. }
  132. public function testGetScore()
  133. {
  134. $score = 0.5;
  135. $response = new Response(true, array(), 'hostname', 'timestamp', 'apk', $score);
  136. $this->assertEquals($score, $response->getScore());
  137. }
  138. public function testGetAction()
  139. {
  140. $action = 'homepage';
  141. $response = new Response(true, array(), 'hostname', 'timestamp', 'apk', '0.5', 'homepage');
  142. $this->assertEquals($action, $response->getAction());
  143. }
  144. public function testToArray()
  145. {
  146. $response = new Response(true, array(), 'hostname', 'timestamp', 'apk', '0.5', 'homepage');
  147. $expected = array(
  148. 'success' => true,
  149. 'error-codes' => array(),
  150. 'hostname' => 'hostname',
  151. 'challenge_ts' => 'timestamp',
  152. 'apk_package_name' => 'apk',
  153. 'score' => 0.5,
  154. 'action' => 'homepage',
  155. );
  156. $this->assertEquals($expected, $response->toArray());
  157. }
  158. }