/** * @dataProvider provideGetResultStatus() */ public function testGetResultStatus(Result $result) { $localizer = new ValidatorErrorLocalizer($this->getMockFormatter()); $status = $localizer->getResultStatus($result); $this->assertInstanceOf('Status', $status); $this->assertEquals($result->isValid(), $status->isOk(), 'isOK()'); $this->assertEquals(count($result->getErrors()), count($status->getErrorsArray()), 'Error count:'); }
protected function assertErrorCodeLocalization(Result $result) { $localizer = new ValidatorErrorLocalizer(); $errors = $result->getErrors(); $this->assertGreaterThanOrEqual(1, $errors); foreach ($errors as $error) { $msg = $localizer->getErrorMessage($error); $this->assertTrue($msg->exists(), 'message: ' . $msg); } }
/** * @dataProvider provideValidate_failure() */ public function testValidate_failure($value, $type, $errorCode) { $validator = new EntityExistsValidator($this->getEntityLookup(), $type); $result = $validator->validate($value); $this->assertFalse($result->isValid()); $errors = $result->getErrors(); $this->assertCount(1, $errors); $this->assertEquals($errorCode, $errors[0]->getCode()); $localizer = new ValidatorErrorLocalizer(); $msg = $localizer->getErrorMessage($errors[0]); $this->assertTrue($msg->exists(), $msg); }
/** * @see ExceptionLocalizer::getExceptionMessage() * * @param Exception $exception * * @throws InvalidArgumentException * @return Message */ public function getExceptionMessage(Exception $exception) { if (!$this->hasExceptionMessage($exception)) { throw new InvalidArgumentException('$exception is not a ChangeOpValidationException.'); } /** @var ChangeOpValidationException $exception */ $result = $exception->getValidationResult(); foreach ($result->getErrors() as $error) { $msg = $this->validatorErrorLocalizer->getErrorMessage($error); return $msg; } return wfMessage('wikibase-validator-invalid'); }
/** * @dataProvider provideValidate() */ public function testValidate($validators, $value, $expectedErrorCount, $message) { $validator = new AlternativeValidator($validators); $result = $validator->validate($value); $errors = $result->getErrors(); $this->assertEquals($expectedErrorCount === 0, $result->isValid(), $message); $this->assertCount($expectedErrorCount, $errors, $message); $localizer = new ValidatorErrorLocalizer(); foreach ($errors as $error) { $msg = $localizer->getErrorMessage($error); $this->assertTrue($msg->exists(), $msg); } }
/** * @dataProvider provideValidate() */ public function testValidate($values, $normalize, $value, $expected) { $validator = new MembershipValidator($values, 'not-allowed', $normalize); $result = $validator->validate($value); $this->assertEquals($expected, $result->isValid()); if (!$expected) { $errors = $result->getErrors(); $this->assertCount(1, $errors); $this->assertTrue(in_array($errors[0]->getCode(), array('not-allowed')), $errors[0]->getCode()); $localizer = new ValidatorErrorLocalizer(); $msg = $localizer->getErrorMessage($errors[0]); $this->assertTrue($msg->exists(), $msg); } }
/** * @dataProvider provideValidate() */ public function testValidate($type, $value, $expected, $message) { $validator = new TypeValidator($type); $result = $validator->validate($value); $this->assertEquals($expected, $result->isValid(), $message); if (!$expected) { $errors = $result->getErrors(); $this->assertCount(1, $errors, $message); $this->assertEquals('bad-type', $errors[0]->getCode(), $message); $localizer = new ValidatorErrorLocalizer(); $msg = $localizer->getErrorMessage($errors[0]); $this->assertTrue($msg->exists(), $msg); } }
/** * @dataProvider provideValidate() */ public function testValidate($minLength, $maxLength, $measure, $value, $expected, $message) { $validator = new StringLengthValidator($minLength, $maxLength, $measure); $result = $validator->validate($value); $this->assertEquals($expected, $result->isValid(), $message); if (!$expected) { $errors = $result->getErrors(); $this->assertCount(1, $errors, $message); $this->assertTrue(in_array($errors[0]->getCode(), array('too-long', 'too-short')), $message . "\n" . $errors[0]->getCode()); $localizer = new ValidatorErrorLocalizer(); $msg = $localizer->getErrorMessage($errors[0]); $this->assertTrue($msg->exists(), $msg); } }
/** * @dataProvider provideValidate() */ public function testValidate($schemes, $value, $expectedErrorCode) { $validator = new UrlValidator($schemes); $result = $validator->validate($value); if ($expectedErrorCode === null) { $this->assertTrue($result->isValid(), 'isValid'); } else { $this->assertFalse($result->isValid(), 'isValid'); $errors = $result->getErrors(); $this->assertCount(1, $errors); $this->assertEquals($expectedErrorCode, $errors[0]->getCode(), 'error code'); $localizer = new ValidatorErrorLocalizer(); $msg = $localizer->getErrorMessage($errors[0]); $this->assertTrue($msg->exists(), 'message: ' . $msg); } }
/** * @param ValueParser $parser * @param string $value * @param ValueValidator|null $validator * * @return array */ private function parseStringValue(ValueParser $parser, $value, ValueValidator $validator = null) { $result = array('raw' => $value); try { $parseResult = $parser->parse($value); } catch (ParseException $parseError) { $this->addParseErrorToResult($result, $parseError); return $result; } if ($parseResult instanceof DataValue) { $result['value'] = $parseResult->getArrayValue(); $result['type'] = $parseResult->getType(); } else { $result['value'] = $parseResult; } if ($validator) { $validatorResult = $validator->validate($parseResult); $validationStatus = $this->validatorErrorLocalizer->getResultStatus($validatorResult); $result['valid'] = $validationStatus->isOK(); if (!$validationStatus->isOK()) { $result['error'] = 'ValidationError'; $this->errorReporter->addStatusToResult($validationStatus, $result); $result['validation-errors'] = $this->getValidatorErrorCodes($validatorResult->getErrors()); } } return $result; }
/** * @dataProvider provideValidate() */ public function testValidate($validator, $value, $expected, $exception, $message) { if ($exception !== null) { $this->setExpectedException($exception); } $validator = new DataValueValidator($validator); $result = $validator->validate($value); $this->assertEquals($expected, $result->isValid(), $message); if (!$expected) { $errors = $result->getErrors(); $this->assertCount(1, $errors, $message); $localizer = new ValidatorErrorLocalizer(); $msg = $localizer->getErrorMessage($errors[0]); $this->assertTrue($msg->exists(), $msg); } }