/trunk/includes/recaptcha/tests/ReCaptcha/ReCaptchaTest.php |
---|
0,0 → 1,75 |
<?php |
/** |
* This is a PHP library that handles calling reCAPTCHA. |
* |
* @copyright Copyright (c) 2015, Google Inc. |
* @link http://www.google.com/recaptcha |
* |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
* of this software and associated documentation files (the "Software"), to deal |
* in the Software without restriction, including without limitation the rights |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
* copies of the Software, and to permit persons to whom the Software is |
* furnished to do so, subject to the following conditions: |
* |
* The above copyright notice and this permission notice shall be included in |
* all copies or substantial portions of the Software. |
* |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
* THE SOFTWARE. |
*/ |
namespace ReCaptcha; |
class ReCaptchaTest extends \PHPUnit_Framework_TestCase |
{ |
/** |
* @expectedException \RuntimeException |
* @dataProvider invalidSecretProvider |
*/ |
public function testExceptionThrownOnInvalidSecret($invalid) |
{ |
$rc = new ReCaptcha($invalid); |
} |
public function invalidSecretProvider() |
{ |
return array( |
array(''), |
array(null), |
array(0), |
array(new \stdClass()), |
array(array()), |
); |
} |
public function testVerifyReturnsErrorOnMissingResponse() |
{ |
$rc = new ReCaptcha('secret'); |
$response = $rc->verify(''); |
$this->assertFalse($response->isSuccess()); |
$this->assertEquals(array('missing-input-response'), $response->getErrorCodes()); |
} |
public function testVerifyReturnsResponse() |
{ |
$method = $this->getMock('\\ReCaptcha\\RequestMethod', array('submit')); |
$method->expects($this->once()) |
->method('submit') |
->with($this->callback(function ($params) { |
return true; |
})) |
->will($this->returnValue('{"success": true}')); |
; |
$rc = new ReCaptcha('secret', $method); |
$response = $rc->verify('response'); |
$this->assertTrue($response->isSuccess()); |
} |
} |
/trunk/includes/recaptcha/tests/ReCaptcha/RequestMethod/CurlPostTest.php |
---|
0,0 → 1,63 |
<?php |
/** |
* This is a PHP library that handles calling reCAPTCHA. |
* |
* @copyright Copyright (c) 2015, Google Inc. |
* @link http://www.google.com/recaptcha |
* |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
* of this software and associated documentation files (the "Software"), to deal |
* in the Software without restriction, including without limitation the rights |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
* copies of the Software, and to permit persons to whom the Software is |
* furnished to do so, subject to the following conditions: |
* |
* The above copyright notice and this permission notice shall be included in |
* all copies or substantial portions of the Software. |
* |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
* THE SOFTWARE. |
*/ |
namespace ReCaptcha\RequestMethod; |
use \ReCaptcha\RequestParameters; |
class CurlPostTest extends \PHPUnit_Framework_TestCase |
{ |
protected function setUp() |
{ |
if (!extension_loaded('curl')) { |
$this->markTestSkipped( |
'The cURL extension is not available.' |
); |
} |
} |
public function testSubmit() |
{ |
$curl = $this->getMock('\\ReCaptcha\\RequestMethod\\Curl', |
array('init', 'setoptArray', 'exec', 'close')); |
$curl->expects($this->once()) |
->method('init') |
->willReturn(new \stdClass); |
$curl->expects($this->once()) |
->method('setoptArray') |
->willReturn(true); |
$curl->expects($this->once()) |
->method('exec') |
->willReturn('RESPONSEBODY'); |
$curl->expects($this->once()) |
->method('close'); |
$pc = new CurlPost($curl); |
$response = $pc->submit(new RequestParameters("secret", "response")); |
$this->assertEquals('RESPONSEBODY', $response); |
} |
} |
/trunk/includes/recaptcha/tests/ReCaptcha/RequestMethod/PostTest.php |
---|
0,0 → 1,118 |
<?php |
/** |
* This is a PHP library that handles calling reCAPTCHA. |
* |
* @copyright Copyright (c) 2015, Google Inc. |
* @link http://www.google.com/recaptcha |
* |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
* of this software and associated documentation files (the "Software"), to deal |
* in the Software without restriction, including without limitation the rights |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
* copies of the Software, and to permit persons to whom the Software is |
* furnished to do so, subject to the following conditions: |
* |
* The above copyright notice and this permission notice shall be included in |
* all copies or substantial portions of the Software. |
* |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
* THE SOFTWARE. |
*/ |
namespace ReCaptcha\RequestMethod; |
use ReCaptcha\RequestParameters; |
class PostTest extends \PHPUnit_Framework_TestCase |
{ |
public static $assert = null; |
protected $parameters = null; |
protected $runcount = 0; |
public function setUp() |
{ |
$this->parameters = new RequestParameters("secret", "response", "remoteip", "version"); |
} |
public function tearDown() |
{ |
self::$assert = null; |
} |
public function testHTTPContextOptions() |
{ |
$req = new Post(); |
self::$assert = array($this, "httpContextOptionsCallback"); |
$req->submit($this->parameters); |
$this->assertEquals(1, $this->runcount, "The assertion was ran"); |
} |
public function testSSLContextOptions() |
{ |
$req = new Post(); |
self::$assert = array($this, "sslContextOptionsCallback"); |
$req->submit($this->parameters); |
$this->assertEquals(1, $this->runcount, "The assertion was ran"); |
} |
public function httpContextOptionsCallback(array $args) |
{ |
$this->runcount++; |
$this->assertCommonOptions($args); |
$options = stream_context_get_options($args[2]); |
$this->assertArrayHasKey('http', $options); |
$this->assertArrayHasKey('method', $options['http']); |
$this->assertEquals("POST", $options['http']['method']); |
$this->assertArrayHasKey('content', $options['http']); |
$this->assertEquals($this->parameters->toQueryString(), $options['http']['content']); |
$this->assertArrayHasKey('header', $options['http']); |
$headers = array( |
"Content-type: application/x-www-form-urlencoded", |
); |
foreach ($headers as $header) { |
$this->assertContains($header, $options['http']['header']); |
} |
} |
public function sslContextOptionsCallback(array $args) |
{ |
$this->runcount++; |
$this->assertCommonOptions($args); |
$options = stream_context_get_options($args[2]); |
$this->assertArrayHasKey('http', $options); |
$this->assertArrayHasKey('verify_peer', $options['http']); |
$this->assertTrue($options['http']['verify_peer']); |
$key = version_compare(PHP_VERSION, "5.6.0", "<") ? "CN_name" : "peer_name"; |
$this->assertArrayHasKey($key, $options['http']); |
$this->assertEquals("www.google.com", $options['http'][$key]); |
} |
protected function assertCommonOptions(array $args) |
{ |
$this->assertCount(3, $args); |
$this->assertStringStartsWith("https://www.google.com/", $args[0]); |
$this->assertFalse($args[1]); |
$this->assertTrue(is_resource($args[2]), "The context options should be a resource"); |
} |
} |
function file_get_contents() |
{ |
if (PostTest::$assert) { |
return call_user_func(PostTest::$assert, func_get_args()); |
} |
// Since we can't represent maxlen in userland... |
return call_user_func_array('file_get_contents', func_get_args()); |
} |
/trunk/includes/recaptcha/tests/ReCaptcha/RequestMethod/SocketPostTest.php |
---|
0,0 → 1,90 |
<?php |
/** |
* This is a PHP library that handles calling reCAPTCHA. |
* |
* @copyright Copyright (c) 2015, Google Inc. |
* @link http://www.google.com/recaptcha |
* |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
* of this software and associated documentation files (the "Software"), to deal |
* in the Software without restriction, including without limitation the rights |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
* copies of the Software, and to permit persons to whom the Software is |
* furnished to do so, subject to the following conditions: |
* |
* The above copyright notice and this permission notice shall be included in |
* all copies or substantial portions of the Software. |
* |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
* THE SOFTWARE. |
*/ |
namespace ReCaptcha\RequestMethod; |
use ReCaptcha\RequestParameters; |
class SocketPostTest extends \PHPUnit_Framework_TestCase |
{ |
public function testSubmitSuccess() |
{ |
$socket = $this->getMock('\\ReCaptcha\\RequestMethod\\Socket', array('fsockopen', 'fwrite', 'fgets', 'feof', 'fclose')); |
$socket->expects($this->once()) |
->method('fsockopen') |
->willReturn(true); |
$socket->expects($this->once()) |
->method('fwrite'); |
$socket->expects($this->once()) |
->method('fgets') |
->willReturn("HTTP/1.1 200 OK\n\nRESPONSEBODY"); |
$socket->expects($this->exactly(2)) |
->method('feof') |
->will($this->onConsecutiveCalls(false, true)); |
$socket->expects($this->once()) |
->method('fclose') |
->willReturn(true); |
$ps = new SocketPost($socket); |
$response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version")); |
$this->assertEquals('RESPONSEBODY', $response); |
} |
public function testSubmitBadResponse() |
{ |
$socket = $this->getMock('\\ReCaptcha\\RequestMethod\\Socket', array('fsockopen', 'fwrite', 'fgets', 'feof', 'fclose')); |
$socket->expects($this->once()) |
->method('fsockopen') |
->willReturn(true); |
$socket->expects($this->once()) |
->method('fwrite'); |
$socket->expects($this->once()) |
->method('fgets') |
->willReturn("HTTP/1.1 500 NOPEn\\nBOBBINS"); |
$socket->expects($this->exactly(2)) |
->method('feof') |
->will($this->onConsecutiveCalls(false, true)); |
$socket->expects($this->once()) |
->method('fclose') |
->willReturn(true); |
$ps = new SocketPost($socket); |
$response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version")); |
$this->assertEquals(SocketPost::BAD_RESPONSE, $response); |
} |
public function testSubmitBadRequest() |
{ |
$socket = $this->getMock('\\ReCaptcha\\RequestMethod\\Socket', array('fsockopen')); |
$socket->expects($this->once()) |
->method('fsockopen') |
->willReturn(false); |
$ps = new SocketPost($socket); |
$response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version")); |
$this->assertEquals(SocketPost::BAD_REQUEST, $response); |
} |
} |
/trunk/includes/recaptcha/tests/ReCaptcha/RequestParametersTest.php |
---|
0,0 → 1,61 |
<?php |
/** |
* This is a PHP library that handles calling reCAPTCHA. |
* |
* @copyright Copyright (c) 2015, Google Inc. |
* @link http://www.google.com/recaptcha |
* |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
* of this software and associated documentation files (the "Software"), to deal |
* in the Software without restriction, including without limitation the rights |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
* copies of the Software, and to permit persons to whom the Software is |
* furnished to do so, subject to the following conditions: |
* |
* The above copyright notice and this permission notice shall be included in |
* all copies or substantial portions of the Software. |
* |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
* THE SOFTWARE. |
*/ |
namespace ReCaptcha; |
class RequestParametersTest extends \PHPUnit_Framework_TestCase |
{ |
public function provideValidData() |
{ |
return array( |
array('SECRET', 'RESPONSE', 'REMOTEIP', 'VERSION', |
array('secret' => 'SECRET', 'response' => 'RESPONSE', 'remoteip' => 'REMOTEIP', 'version' => 'VERSION'), |
'secret=SECRET&response=RESPONSE&remoteip=REMOTEIP&version=VERSION'), |
array('SECRET', 'RESPONSE', null, null, |
array('secret' => 'SECRET', 'response' => 'RESPONSE'), |
'secret=SECRET&response=RESPONSE'), |
); |
} |
/** |
* @dataProvider provideValidData |
*/ |
public function testToArray($secret, $response, $remoteIp, $version, $expectedArray, $expectedQuery) |
{ |
$params = new RequestParameters($secret, $response, $remoteIp, $version); |
$this->assertEquals($params->toArray(), $expectedArray); |
} |
/** |
* @dataProvider provideValidData |
*/ |
public function testToQueryString($secret, $response, $remoteIp, $version, $expectedArray, $expectedQuery) |
{ |
$params = new RequestParameters($secret, $response, $remoteIp, $version); |
$this->assertEquals($params->toQueryString(), $expectedQuery); |
} |
} |
/trunk/includes/recaptcha/tests/ReCaptcha/ResponseTest.php |
---|
0,0 → 1,84 |
<?php |
/** |
* This is a PHP library that handles calling reCAPTCHA. |
* |
* @copyright Copyright (c) 2015, Google Inc. |
* @link http://www.google.com/recaptcha |
* |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
* of this software and associated documentation files (the "Software"), to deal |
* in the Software without restriction, including without limitation the rights |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
* copies of the Software, and to permit persons to whom the Software is |
* furnished to do so, subject to the following conditions: |
* |
* The above copyright notice and this permission notice shall be included in |
* all copies or substantial portions of the Software. |
* |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
* THE SOFTWARE. |
*/ |
namespace ReCaptcha; |
class ResponseTest extends \PHPUnit_Framework_TestCase |
{ |
/** |
* @dataProvider provideJson |
*/ |
public function testFromJson($json, $success, $errorCodes, $hostname) |
{ |
$response = Response::fromJson($json); |
$this->assertEquals($success, $response->isSuccess()); |
$this->assertEquals($errorCodes, $response->getErrorCodes()); |
$this->assertEquals($hostname, $response->getHostname()); |
} |
public function provideJson() |
{ |
return array( |
array('{"success": true}', true, array(), null), |
array('{"success": true, "hostname": "google.com"}', true, array(), 'google.com'), |
array('{"success": false, "error-codes": ["test"]}', false, array('test'), null), |
array('{"success": false, "error-codes": ["test"], "hostname": "google.com"}', false, array('test'), 'google.com'), |
array('{"success": true, "error-codes": ["test"]}', true, array(), null), |
array('{"success": true, "error-codes": ["test"], "hostname": "google.com"}', true, array(), 'google.com'), |
array('{"success": false}', false, array(), null), |
array('{"success": false, "hostname": "google.com"}', false, array(), 'google.com'), |
array('BAD JSON', false, array('invalid-json'), null), |
); |
} |
public function testIsSuccess() |
{ |
$response = new Response(true); |
$this->assertTrue($response->isSuccess()); |
$response = new Response(false); |
$this->assertFalse($response->isSuccess()); |
$response = new Response(true, array(), 'example.com'); |
$this->assertEquals('example.com', $response->getHostName()); |
} |
public function testGetErrorCodes() |
{ |
$errorCodes = array('test'); |
$response = new Response(true, $errorCodes); |
$this->assertEquals($errorCodes, $response->getErrorCodes()); |
} |
public function testGetHostname() |
{ |
$hostname = 'google.com'; |
$errorCodes = array(); |
$response = new Response(true, $errorCodes, $hostname); |
$this->assertEquals($hostname, $response->getHostname()); |
} |
} |