CurlPostTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\RequestMethod;
  35. use \ReCaptcha\ReCaptcha;
  36. use \ReCaptcha\RequestParameters;
  37. use PHPUnit\Framework\TestCase;
  38. class CurlPostTest extends TestCase
  39. {
  40. protected function setUp()
  41. {
  42. if (!extension_loaded('curl')) {
  43. $this->markTestSkipped(
  44. 'The cURL extension is not available.'
  45. );
  46. }
  47. }
  48. public function testSubmit()
  49. {
  50. $curl = $this->getMockBuilder(\ReCaptcha\RequestMethod\Curl::class)
  51. ->disableOriginalConstructor()
  52. ->setMethods(array('init', 'setoptArray', 'exec', 'close'))
  53. ->getMock();
  54. $curl->expects($this->once())
  55. ->method('init')
  56. ->willReturn(new \stdClass);
  57. $curl->expects($this->once())
  58. ->method('setoptArray')
  59. ->willReturn(true);
  60. $curl->expects($this->once())
  61. ->method('exec')
  62. ->willReturn('RESPONSEBODY');
  63. $curl->expects($this->once())
  64. ->method('close');
  65. $pc = new CurlPost($curl);
  66. $response = $pc->submit(new RequestParameters("secret", "response"));
  67. $this->assertEquals('RESPONSEBODY', $response);
  68. }
  69. public function testOverrideSiteVerifyUrl()
  70. {
  71. $url = 'OVERRIDE';
  72. $curl = $this->getMockBuilder(\ReCaptcha\RequestMethod\Curl::class)
  73. ->disableOriginalConstructor()
  74. ->setMethods(array('init', 'setoptArray', 'exec', 'close'))
  75. ->getMock();
  76. $curl->expects($this->once())
  77. ->method('init')
  78. ->with($url)
  79. ->willReturn(new \stdClass);
  80. $curl->expects($this->once())
  81. ->method('setoptArray')
  82. ->willReturn(true);
  83. $curl->expects($this->once())
  84. ->method('exec')
  85. ->willReturn('RESPONSEBODY');
  86. $curl->expects($this->once())
  87. ->method('close');
  88. $pc = new CurlPost($curl, $url);
  89. $response = $pc->submit(new RequestParameters("secret", "response"));
  90. $this->assertEquals('RESPONSEBODY', $response);
  91. }
  92. public function testConnectionFailureReturnsError()
  93. {
  94. $curl = $this->getMockBuilder(\ReCaptcha\RequestMethod\Curl::class)
  95. ->disableOriginalConstructor()
  96. ->setMethods(array('init', 'setoptArray', 'exec', 'close'))
  97. ->getMock();
  98. $curl->expects($this->once())
  99. ->method('init')
  100. ->willReturn(new \stdClass);
  101. $curl->expects($this->once())
  102. ->method('setoptArray')
  103. ->willReturn(true);
  104. $curl->expects($this->once())
  105. ->method('exec')
  106. ->willReturn(false);
  107. $curl->expects($this->once())
  108. ->method('close');
  109. $pc = new CurlPost($curl);
  110. $response = $pc->submit(new RequestParameters("secret", "response"));
  111. $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}', $response);
  112. }
  113. }