/** * Creates and returns a captcha object from a row * @param $row array * @return Captcha object */ function &_returnCaptchaFromRow($row) { $captcha = new Captcha(); $captcha->setId($row['captcha_id']); $captcha->setSessionId($row['session_id']); $captcha->setValue($row['value']); $captcha->setDateCreated($this->datetimeFromDB($row['date_created'])); HookRegistry::call('CaptchaDAO::_returnCaptchaFromRow', array(&$captcha, &$row)); return $captcha; }
/** * Create a CAPTCHA object. * @param $length int The length, in characters, of the CAPTCHA test to create * @return object Captcha */ function &createCaptcha($length = 6) { $captchaDao =& DAORegistry::getDAO('CaptchaDAO'); $session =& Request::getSession(); if ($session && $this->isEnabled()) { $captcha = new Captcha(); $captcha->setSessionId($session->getId()); $captcha->setValue(Validation::generatePassword($length)); $captchaDao->insertCaptcha($captcha); } else { $captcha = null; } return $captcha; }
/** * @covers FormValidatorCaptcha * @covers FormValidator */ public function testIsValid() { // Test form $form = new Form('some template'); $form->setData('testCaptchaId', 'test captcha id'); // Create a test Captcha import('lib.pkp.classes.captcha.Captcha'); $captcha = new Captcha(); $captcha->setValue('expected captcha value'); $this->registerMockCaptchaDAO($captcha); // Instantiate validator $validator = new FormValidatorCaptcha($form, 'testData', 'testCaptchaId', 'some.message.key'); // Test valid captcha $form->setData('testData', 'expected captcha value'); self::assertTrue($validator->isValid()); // Simulate invalid captcha value $form->setData('testData', 'unexpected captcha value'); self::assertFalse($validator->isValid()); // Simulate invalid captcha id $this->registerMockCaptchaDAO(null); $form->setData('testData', 'expected captcha value'); self::assertFalse($validator->isValid()); }