|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 4 | +require_once __DIR__ . '/../lib/captcha/ReCaptcha.php'; |
| 5 | + |
| 6 | +class ReCaptchaTest extends \PHPUnit\Framework\TestCase |
| 7 | +{ |
| 8 | + |
| 9 | + use \phpmock\phpunit\PHPMock; |
| 10 | + |
| 11 | + public function test_construct(): void |
| 12 | + { |
| 13 | + $recaptcha_url = 'http://127.0.0.1/'; |
| 14 | + $recaptcha_sitekey = 'sitekey'; |
| 15 | + $recaptcha_secretkey = 'secret'; |
| 16 | + $recaptcha_minscore = 0.5; |
| 17 | + |
| 18 | + $captchaInstance = new captcha\ReCaptcha($recaptcha_url, |
| 19 | + $recaptcha_sitekey, |
| 20 | + $recaptcha_secretkey, |
| 21 | + $recaptcha_minscore); |
| 22 | + |
| 23 | + $this->assertEquals('captcha\ReCaptcha', get_class($captchaInstance), "Wrong class"); |
| 24 | + } |
| 25 | + |
| 26 | + public function test_generate_js_captcha(): void |
| 27 | + { |
| 28 | + $recaptcha_url = 'http://127.0.0.1/'; |
| 29 | + $recaptcha_sitekey = 'sitekey'; |
| 30 | + $recaptcha_secretkey = 'secret'; |
| 31 | + $recaptcha_minscore = 0.5; |
| 32 | + |
| 33 | + $captchaInstance = new captcha\ReCaptcha($recaptcha_url, |
| 34 | + $recaptcha_sitekey, |
| 35 | + $recaptcha_secretkey, |
| 36 | + $recaptcha_minscore); |
| 37 | + |
| 38 | + $js = $captchaInstance->generate_js_captcha(); |
| 39 | + |
| 40 | + $this->assertMatchesRegularExpression('/https:\/\/www.google.com\/recaptcha\/api.js/i',$js, "dummy js code returned"); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_generate_html_captcha(): void |
| 44 | + { |
| 45 | + $messages = array(); |
| 46 | + |
| 47 | + $recaptcha_url = 'http://127.0.0.1/'; |
| 48 | + $recaptcha_sitekey = 'sitekey'; |
| 49 | + $recaptcha_secretkey = 'secret'; |
| 50 | + $recaptcha_minscore = 0.5; |
| 51 | + |
| 52 | + $captchaInstance = new captcha\ReCaptcha($recaptcha_url, |
| 53 | + $recaptcha_sitekey, |
| 54 | + $recaptcha_secretkey, |
| 55 | + $recaptcha_minscore); |
| 56 | + |
| 57 | + $html = $captchaInstance->generate_html_captcha($messages); |
| 58 | + |
| 59 | + $this->assertMatchesRegularExpression('/<input type="hidden" autocomplete="new-password" name="captchaphrase" id="captchaphrase" class="form-control"/',$html, "dummy challenge in html code"); |
| 60 | + } |
| 61 | + |
| 62 | + public function test_verify_captcha_challenge_ok(): void |
| 63 | + { |
| 64 | + |
| 65 | + $recaptcha_url = 'http://127.0.0.1/'; |
| 66 | + $recaptcha_sitekey = 'sitekey'; |
| 67 | + $recaptcha_secretkey = 'secret'; |
| 68 | + $recaptcha_minscore = 0.5; |
| 69 | + $http_response = '{"success": "true", "score": "0.9"}'; |
| 70 | + |
| 71 | + $captchaInstance = new captcha\ReCaptcha($recaptcha_url, |
| 72 | + $recaptcha_sitekey, |
| 73 | + $recaptcha_secretkey, |
| 74 | + $recaptcha_minscore); |
| 75 | + |
| 76 | + $error_log = $this->getFunctionMock("captcha", "error_log"); |
| 77 | + $error_log->expects($this->any())->willReturn(""); |
| 78 | + $stream_context_create = $this->getFunctionMock("captcha", "stream_context_create"); |
| 79 | + $stream_context_create->expects($this->once())->willReturn("stream_context_create"); |
| 80 | + $file_get_contents = $this->getFunctionMock("captcha", "file_get_contents"); |
| 81 | + $file_get_contents->expects($this->once())->willReturn($http_response); |
| 82 | + |
| 83 | + $_POST["captchaphrase"] = "ABCDE"; |
| 84 | + $captcha = $captchaInstance->verify_captcha_challenge(); |
| 85 | + $this->assertEquals('',$captcha, "unexpected return response during verify_captcha_challenge"); |
| 86 | + } |
| 87 | + |
| 88 | + public function test_verify_captcha_challenge_badcaptcha(): void |
| 89 | + { |
| 90 | + |
| 91 | + $recaptcha_url = 'http://127.0.0.1/'; |
| 92 | + $recaptcha_sitekey = 'sitekey'; |
| 93 | + $recaptcha_secretkey = 'secret'; |
| 94 | + $recaptcha_minscore = 0.5; |
| 95 | + $http_response = '{"success": "false"}'; |
| 96 | + |
| 97 | + $captchaInstance = new captcha\ReCaptcha($recaptcha_url, |
| 98 | + $recaptcha_sitekey, |
| 99 | + $recaptcha_secretkey, |
| 100 | + $recaptcha_minscore); |
| 101 | + |
| 102 | + $error_log = $this->getFunctionMock("captcha", "error_log"); |
| 103 | + $error_log->expects($this->any())->willReturn(""); |
| 104 | + $stream_context_create = $this->getFunctionMock("captcha", "stream_context_create"); |
| 105 | + $stream_context_create->expects($this->once())->willReturn("stream_context_create"); |
| 106 | + $file_get_contents = $this->getFunctionMock("captcha", "file_get_contents"); |
| 107 | + $file_get_contents->expects($this->once())->willReturn($http_response); |
| 108 | + |
| 109 | + $_POST["captchaphrase"] = "ABCDE"; |
| 110 | + $captcha = $captchaInstance->verify_captcha_challenge(); |
| 111 | + $this->assertEquals('badcaptcha',$captcha, "unexpected return response during verify_captcha_challenge"); |
| 112 | + } |
| 113 | + |
| 114 | + public function test_verify_captcha_challenge_insufficientscore(): void |
| 115 | + { |
| 116 | + |
| 117 | + $recaptcha_url = 'http://127.0.0.1/'; |
| 118 | + $recaptcha_sitekey = 'sitekey'; |
| 119 | + $recaptcha_secretkey = 'secret'; |
| 120 | + $recaptcha_minscore = 0.5; |
| 121 | + $http_response = '{"success": "true", "score": "0.4"}'; |
| 122 | + |
| 123 | + $captchaInstance = new captcha\ReCaptcha($recaptcha_url, |
| 124 | + $recaptcha_sitekey, |
| 125 | + $recaptcha_secretkey, |
| 126 | + $recaptcha_minscore); |
| 127 | + |
| 128 | + $error_log = $this->getFunctionMock("captcha", "error_log"); |
| 129 | + $error_log->expects($this->any())->willReturn(""); |
| 130 | + $stream_context_create = $this->getFunctionMock("captcha", "stream_context_create"); |
| 131 | + $stream_context_create->expects($this->once())->willReturn("stream_context_create"); |
| 132 | + $file_get_contents = $this->getFunctionMock("captcha", "file_get_contents"); |
| 133 | + $file_get_contents->expects($this->once())->willReturn($http_response); |
| 134 | + |
| 135 | + $_POST["captchaphrase"] = "ABCDE"; |
| 136 | + $captcha = $captchaInstance->verify_captcha_challenge(); |
| 137 | + $this->assertEquals('badcaptcha',$captcha, "unexpected return response during verify_captcha_challenge"); |
| 138 | + } |
| 139 | + |
| 140 | + public function test_verify_captcha_challenge_nocaptcha(): void |
| 141 | + { |
| 142 | + |
| 143 | + $recaptcha_url = 'http://127.0.0.1/'; |
| 144 | + $recaptcha_sitekey = 'sitekey'; |
| 145 | + $recaptcha_secretkey = 'secret'; |
| 146 | + $recaptcha_minscore = 0.5; |
| 147 | + |
| 148 | + $captchaInstance = new captcha\ReCaptcha($recaptcha_url, |
| 149 | + $recaptcha_sitekey, |
| 150 | + $recaptcha_secretkey, |
| 151 | + $recaptcha_minscore); |
| 152 | + |
| 153 | + $error_log = $this->getFunctionMock("captcha", "error_log"); |
| 154 | + $error_log->expects($this->any())->willReturn(""); |
| 155 | + |
| 156 | + unset($_POST); |
| 157 | + $captcha = $captchaInstance->verify_captcha_challenge(); |
| 158 | + $this->assertEquals('captcharequired',$captcha, "unexpected return response during verify_captcha_challenge"); |
| 159 | + } |
| 160 | + |
| 161 | +} |
| 162 | + |
0 commit comments