public function provideGetExceptionMessage() { $result0 = Result::newError(array()); $result1 = Result::newError(array(Error::newError('Eeek!', null, 'too-long', array(8)))); $result2 = Result::newError(array(Error::newError('Eeek!', null, 'too-long', array(array('eekwiki', 'Eek'))), Error::newError('Foo!', null, 'too-short', array(array('foowiki', 'Foo'))))); return array('ChangeOpValidationException(0)' => array(new ChangeOpValidationException($result0), 'wikibase-validator-invalid', array()), 'ChangeOpValidationException(1)' => array(new ChangeOpValidationException($result1), 'wikibase-validator-too-long', array('8')), 'ChangeOpValidationException(2)' => array(new ChangeOpValidationException($result2), 'wikibase-validator-too-long', array('eekwiki|Eek'))); }
/** * @see ValueValidator::validate() * * @param mixed $value The value to validate * * @return Result */ public function validate($value) { $isValid = is_int($value) || is_float($value); if ($isValid) { return Result::newSuccess(); } return Result::newError(array(Error::newError('Bad type, expected an integer or float value', null, 'bad-type', array('number', gettype($value))))); }
/** * @see ValueValidator::validate * * @param string $value The value to validate * * @return Result */ public function validate($value) { if ($this->normalizer !== null) { $value = call_user_func($this->normalizer, $value); } if (!in_array($value, $this->allowed, true)) { return Result::newError(array(Error::newError('Not a legal value: ' . $value, null, $this->errorCode, array($value)))); } return Result::newSuccess(); }
public function validEntityProvider() { $success = Result::newSuccess(); $failure = Result::newError(array(Error::newError('Foo!'))); $good = $this->getMock('Wikibase\\Repo\\Validators\\EntityValidator'); $good->expects($this->any())->method('validateEntity')->will($this->returnValue($success)); $bad = $this->getMock('Wikibase\\Repo\\Validators\\EntityValidator'); $bad->expects($this->any())->method('validateEntity')->will($this->returnValue($failure)); return array(array(array($good, $bad), false), array(array($bad, $good), false), array(array($good, $good), true), array(array(), true)); }
/** * Returns a Message representing the given error. * This can be used for reporting validation failures. * * @param Error $error * * @return Message */ public function getErrorMessage(Error $error) { // Messages: // wikibase-validator-bad-type, wikibase-validator-too-long, wikibase-validator-too-short, // wikibase-validator-too-high, wikibase-validator-too-low, // wikibase-validator-malformed-value, wikibase-validator-bad-entity-id, // wikibase-validator-bad-entity-type, wikibase-validator-no-such-entity, // wikibase-validator-no-such-property, wikibase-validator-bad-value, // wikibase-validator-bad-value-type, wikibase-validator-bad-url, // wikibase-validator-bad-url-scheme, wikibase-validator-unknown-unit $key = 'wikibase-validator-' . $error->getCode(); $params = $error->getParameters(); foreach ($params as &$param) { if (!is_string($param)) { $param = $this->paramToString($param); } } return wfMessage($key)->params($params); }
/** * @see ValueValidator::validate() * * @param int|float $value The numeric value to validate * * @return Result */ public function validate($value) { if ($value < $this->min) { // XXX: having to provide an array is quite inconvenient return Result::newError(array(Error::newError('Value out of range, the minimum value is ' . $this->min, null, 'too-low', array($this->min, $value)))); } if ($value > $this->max) { return Result::newError(array(Error::newError('Value out of range, the maximum value is ' . $this->max, null, 'too-high', array($this->max, $value)))); } return Result::newSuccess(); }
/** * @param string|DataValue $value * * @return Result */ public function validate($value) { if ($value instanceof DataValue) { $value = $value->getArrayValue(); } if (preg_match($this->regex, $value)) { return Result::newSuccess(); } else { return Result::newError(array(Error::newError("doesn't match " . $this->regex))); } }
/** * @see ValueValidator::validate() * * @param string $value The value to validate * * @return Result */ public function validate($value) { $length = call_user_func($this->measure, $value); if ($length < $this->minLength) { // XXX: having to provide an array is quite inconvenient return Result::newError(array(Error::newError('Too short, minimum length is ' . $this->minLength, null, 'too-short', array($this->minLength, $value)))); } if ($length > $this->maxLength) { return Result::newError(array(Error::newError('Too long, maximum length is ' . $this->maxLength, null, 'too-long', array($this->maxLength, $this->truncateValue($value))))); } return Result::newSuccess(); }
/** * @see ValueValidator::validate() * * @param string $value The value to validate * * @return Result */ public function validate($value) { $match = preg_match($this->expression, $value); if ($match === 0 && !$this->inverse) { // XXX: having to provide an array is quite inconvenient return Result::newError(array(Error::newError('Pattern match failed: ' . $this->expression, null, $this->errorCode, array($value)))); } if ($match === 1 && $this->inverse) { // XXX: having to provide an array is quite inconvenient return Result::newError(array(Error::newError('Negative pattern matched: ' . $this->expression, null, $this->errorCode, array($value)))); } return Result::newSuccess(); }
/** * @see ValueValidator::validate() * * @param mixed $value The value to validate * * @return Result */ public function validate($value) { $type = gettype($value); if ($type === $this->type) { return Result::newSuccess(); } if (is_object($value)) { $type = get_class($value); if (is_a($value, $this->type)) { return Result::newSuccess(); } } return Result::newError(array(Error::newError('Bad type, expected ' . $this->type, null, 'bad-type', array($this->type, $type)))); }
/** * @see ValueValidator::validate() * * @param array $data The data array to validate * * @return Result * @throws InvalidArgumentException */ public function validate($data) { if (!is_array($data)) { //XXX: or should this just be reported as invalid? throw new InvalidArgumentException('DataValue is not represented as an array'); } if (!isset($data[$this->field])) { return Result::newError(array(Error::newError('Required field ' . $this->field . ' not set', $this->field, 'missing-field', array($this->field)))); } $fieldValue = $data[$this->field]; $result = $this->validator->validate($fieldValue); // TODO: include the field name in the error report return $result; }
/** * @see ValueValidator::validate() * * @param string $value The value to validate * * @return Result */ public function validate($value) { $result = Result::newSuccess(); try { $entityId = $this->idParser->parse($value); if ($this->forbiddenTypes === null || in_array($entityId->getEntityType(), $this->forbiddenTypes)) { // The label looks like a valid ID - we don't like that! $error = Error::newError('Looks like an Entity ID: ' . $value, null, $this->errorCode, array($value)); $result = Result::newError(array($error)); } } catch (EntityIdParsingException $parseException) { // All fine, the parsing did not work, so there is no entity id :) } return $result; }
/** * @see ValueValidator::validate() * * @param mixed $value The value to validate * * @return Result */ public function validate($value) { $result = null; foreach ($this->validators as $validator) { $subResult = $validator->validate($value); if ($subResult->isValid()) { return $subResult; } else { $result = $result ? Result::merge($result, $subResult) : $subResult; } } if (!$result) { $result = Result::newError(array(Error::newError("No validators", null, 'no-validators'))); } return $result; }
/** * @see ValueValidator::validate() * * @param EntityIdValue|EntityId $value The ID to validate * * @return Result * @throws InvalidArgumentException */ public function validate($value) { if ($value instanceof EntityIdValue) { $value = $value->getEntityId(); } if (!$value instanceof EntityId) { throw new InvalidArgumentException("Expected an EntityId object"); } $actualType = $value->getEntityType(); $errors = array(); if ($this->entityType !== null && $actualType !== $this->entityType) { $errors[] = Error::newError("Wrong entity type: " . $actualType, null, 'bad-entity-type', array($actualType)); } if (!$this->entityLookup->hasEntity($value)) { $errors[] = Error::newError("Entity not found: " . $value, null, 'no-such-entity', array($value)); } return empty($errors) ? Result::newSuccess() : Result::newError($errors); }
/** * @see ValueValidator::validate() * * @param string $url * * @throws InvalidArgumentException * @return Result */ public function validate($url) { if (!is_string($url)) { throw new InvalidArgumentException('$url must be a string.'); } // See RFC 3986, section-3.1. if (!preg_match('/^([-+.a-z\\d]+):/i', $url, $matches)) { return Result::newError(array(Error::newError('Malformed URL, can\'t find scheme name.', null, 'bad-url', array($url)))); } // Should we also check for and fail on whitespace in $value? $scheme = strtolower($matches[1]); if (isset($this->validators[$scheme])) { $validator = $this->validators[$scheme]; } elseif (isset($this->validators['*'])) { $validator = $this->validators['*']; } else { return Result::newError(array(Error::newError('Unsupported URL scheme', null, 'bad-url-scheme', array($scheme)))); } return $validator->validate($url); }
protected function makeChangeOpsMerge(Item $fromItem, Item $toItem, array $ignoreConflicts = array(), $siteLookup = null) { if ($siteLookup === null) { $siteLookup = MockSiteStore::newFromTestSites(); } // A validator which makes sure that no site link is for page 'DUPE' $siteLinkUniquenessValidator = $this->getMock('Wikibase\\Repo\\Validators\\EntityValidator'); $siteLinkUniquenessValidator->expects($this->any())->method('validateEntity')->will($this->returnCallback(function (Item $item) { $siteLinks = $item->getSiteLinkList(); foreach ($siteLinks as $siteLink) { if ($siteLink->getPageName() === 'DUPE') { return Result::newError(array(Error::newError('SiteLink conflict'))); } } return Result::newSuccess(); })); $constraintProvider = $this->getMockBuilder('Wikibase\\Repo\\Validators\\EntityConstraintProvider')->disableOriginalConstructor()->getMock(); $constraintProvider->expects($this->any())->method('getUpdateValidators')->will($this->returnValue(array($siteLinkUniquenessValidator))); $changeOpFactoryProvider = new ChangeOpFactoryProvider($constraintProvider, $this->mockProvider->getMockGuidGenerator(), $this->mockProvider->getMockGuidValidator(), $this->mockProvider->getMockGuidParser($toItem->getId()), $this->mockProvider->getMockSnakValidator(), $this->mockProvider->getMockTermValidatorFactory(), $siteLookup); return new ChangeOpsMerge($fromItem, $toItem, $ignoreConflicts, $constraintProvider, $changeOpFactoryProvider, $siteLookup); }
public function detectLabelDescriptionConflicts($entityType, array $labels, array $descriptions = null, EntityId $entityId = null) { if ($entityId && $entityId->getSerialization() === 'P666') { // simulated conflicts always conflict with P666, so if these are // ignored as self-conflicts, we don't need to check any labels. $labels = array(); } foreach ($labels as $lang => $text) { if ($descriptions !== null && (!isset($descriptions[$lang]) || $descriptions[$lang] !== 'DUPE')) { continue; } if ($text === 'DUPE') { return Result::newError(array(Error::newError('found conflicting terms', 'label', 'label-with-description-conflict', array('label', $lang, $text, 'P666')))); } } return Result::newSuccess(); }
public function provideGetResultStatus() { return array(array(Result::newSuccess()), array(Result::newError(array())), array(Result::newError(array(Error::newError('Bla bla')))), array(Result::newError(array(Error::newError('Foo'), Error::newError('Bar'))))); }
public function provideValidate() { $p1 = new PropertyId('P1'); // numeric $p2 = new PropertyId('P2'); // alphabetic $p3 = new PropertyId('P3'); // bad $p4 = new PropertyId('P4'); // property with bad data type $cases = array(); $snak = new PropertyNoValueSnak($p1); $cases[] = array($snak, 'PropertyNoValueSnak'); $snak = new PropertySomeValueSnak($p2); $cases[] = array($snak, 'PropertySomeValueSnak'); $snak = new PropertyValueSnak($p1, new StringValue('123')); $cases[] = array($snak, 'valid numeric value'); $snak = new PropertyValueSnak($p2, new StringValue('abc')); $cases[] = array($snak, 'valid alphabetic value'); $snak = new PropertyValueSnak($p2, new StringValue('123')); $cases[] = array($snak, 'invalid alphabetic value', Error::newError('doesn\'t match /^[A-Z]+$/i', null, 'invalid', array())); $snak = new PropertyValueSnak($p1, new StringValue('abc')); $cases[] = array($snak, 'invalid numeric value', Error::newError('doesn\'t match /^\\d+$/', null, 'invalid', array())); $snak = new PropertyValueSnak($p1, new UnDeserializableValue('abc', 'string', 'ooops')); $cases[] = array($snak, 'bad value', Error::newError('Bad snak value: ooops', null, 'bad-value', array('ooops'))); $snak = new PropertyValueSnak($p1, new UnknownValue('abc')); $cases[] = array($snak, 'wrong value type', Error::newError('Bad value type: unknown, expected string', null, 'bad-value-type', array('unknown', 'string'))); $snak = new PropertyValueSnak($p3, new StringValue('abc')); $cases[] = array($snak, 'bad property', Error::newError('Property P3 not found!', null, 'no-such-property', array('P3'))); $snak = new PropertyValueSnak($p4, new StringValue('abc')); $cases[] = array($snak, 'bad data type', Error::newError('Bad data type: fiddlediddle', null, 'bad-data-type', array('fiddlediddle'))); return $cases; }
/** * @see Error::__construct() * * @param EntityId $conflictingEntity The entity causing the conflict * @param string $text * @param string $code * @param mixed[] $params */ public function __construct(EntityId $conflictingEntity, $text, $code, array $params) { parent::__construct($text, Error::SEVERITY_ERROR, null, $code, $params); $this->conflictingEntity = $conflictingEntity; }
public function testValidate() { $item = new Item(); $guid = 'guid'; $snak = new PropertyValueSnak(new PropertyId('P7'), new StringValue('INVALID')); $guidGenerator = new GuidGenerator(); $error = Error::newError('Testing', 'test', 'test-error', array()); $result = Result::newError(array($error)); $snakValidator = $this->getMockBuilder('Wikibase\\Repo\\Validators\\SnakValidator')->disableOriginalConstructor()->getMock(); $snakValidator->expects($this->any())->method('validate')->will($this->returnValue($result)); $changeOps = new ChangeOps(); $changeOps->add(new ChangeOpMainSnak($guid, $snak, $guidGenerator, $snakValidator)); $result = $changeOps->validate($item); $this->assertFalse($result->isValid(), 'isValid()'); }
/** * Validates the given data value using the given data type. * * @param DataValue $dataValue * @param string $dataTypeId * * @return Result */ public function validateDataValue(DataValue $dataValue, $dataTypeId) { $dataValueType = $this->dataTypeFactory->getType($dataTypeId)->getDataValueType(); if ($dataValue instanceof UnDeserializableValue) { $result = Result::newError(array(Error::newError('Bad snak value: ' . $dataValue->getReason(), null, 'bad-value', array($dataValue->getReason())))); } elseif ($dataValueType != $dataValue->getType()) { $result = Result::newError(array(Error::newError('Bad value type: ' . $dataValue->getType() . ', expected ' . $dataValueType, null, 'bad-value-type', array($dataValue->getType(), $dataValueType)))); } else { $result = Result::newSuccess(); } //XXX: DataTypeValidatorFactory should expose only one validator, which would be a CompositeValidator foreach ($this->validatorFactory->getValidators($dataTypeId) as $validator) { $subResult = $validator->validate($dataValue); //XXX: Some validators should be fatal and cause us to abort the loop. // Others shouldn't. if (!$subResult->isValid()) { //TODO: Don't bail out immediately. Accumulate errors from all validators. // We need Result::merge() for this. return $subResult; } } return $result; }