public function testReCaptchaValidateFailed() { $this->reCaptcha = new ReCaptcha('secret', '127.0.0.1', 'google_response'); $this->assertInstanceOf('DS\\Library\\ReCaptcha\\ReCaptcha', $this->reCaptcha); $testDriver = new TestDriver(false); $response = $this->reCaptcha->buildRequest($testDriver)->send(); $this->assertInstanceOf('DS\\Library\\ReCaptcha\\Http\\Response', $response); $this->assertFalse($response->isSuccess()); $this->assertNotEmpty($response->getErrors()); $errors = $response->getErrors(); $this->assertEquals($errors[0], 'invalid-input-response'); }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (!$constraint instanceof ReCaptchaConstraint) { throw new InvalidArgumentException('Use ReCaptchaConstraint for ReCaptchaValidator.'); } if ($this->request->get('g-recaptcha-response', false)) { $reCaptcha = new ReCaptcha($this->privateKey, $this->request->getClientIp(), $this->request->get('g-recaptcha-response', false)); $response = $reCaptcha->buildRequest($this->driver)->send(); if (!$response->isSuccess()) { $this->context->addViolation($constraint->message); } } else { $this->context->addViolation($constraint->message); } }