Commit 4be8faa8 authored by Tino Goratsch's avatar Tino Goratsch

- throw an exception, when an unregistered validation rule is being called

- covered the Validator class with unit tests
- moved the related validation error exceptions into the Validation\Exceptions namespace
- fixed the URI alias validation by adding the service container tag
parent 3bd9a88e
<?php
namespace ACP3\Core\Exceptions;
/**
* Class InvalidFormToken
* @package ACP3\Core\Exceptions
*/
class InvalidFormToken extends \Exception
{
}
......@@ -88,9 +88,9 @@ abstract class FrontendController extends Core\Modules\Controller
{
try {
$callback();
} catch (Core\Exceptions\InvalidFormToken $e) {
} catch (Core\Validation\Exceptions\InvalidFormTokenException $e) {
$this->redirectMessages()->setMessage(false, $e->getMessage(), $path);
} catch (Core\Exceptions\ValidationFailed $e) {
} catch (Core\Validation\Exceptions\ValidationFailedException $e) {
$this->view->assign('error_msg', $this->get('core.helpers.alerts')->errorBox($e->getMessage()));
}
}
......
......@@ -70,10 +70,10 @@ class Action
{
try {
return $callback();
} catch (Core\Exceptions\InvalidFormToken $e) {
} catch (Core\Validation\Exceptions\InvalidFormTokenException $e) {
return $this->redirectMessages->setMessage(false, $this->translator->t('system', 'form_already_submitted'),
$path);
} catch (Core\Exceptions\ValidationFailed $e) {
} catch (Core\Validation\Exceptions\ValidationFailedException $e) {
return [
'error_msg' => $this->alerts->errorBox($e->getMessage())
];
......
......@@ -34,8 +34,8 @@ abstract class AbstractFormValidation
/**
* @param array $formData
*
* @throws Core\Exceptions\InvalidFormToken
* @throws Core\Exceptions\ValidationFailed
* @throws \ACP3\Core\Validation\Exceptions\InvalidFormTokenException
* @throws \ACP3\Core\Validation\Exceptions\ValidationFailedException
*/
abstract public function validate(array $formData);
}
\ No newline at end of file
<?php
namespace ACP3\Core\Validation\Exceptions;
/**
* Class InvalidFormTokenException
* @package ACP3\Core\Validation\Exceptions
*/
class InvalidFormTokenException extends \Exception
{
}
<?php
namespace ACP3\Core\Exceptions;
namespace ACP3\Core\Validation\Exceptions;
/**
* Class ValidationFailed
* @package ACP3\Core\Exceptions
* Class ValidationFailedException
* @package ACP3\Core\Validation\Exceptions
*/
class ValidationFailed extends \Exception
class ValidationFailedException extends \Exception
{
/**
* ValidationFailed constructor.
......
<?php
namespace ACP3\Core\Validation\Exceptions;
/**
* Class ValidationRuleNotFoundException
* @package ACP3\Core\Validation\Exceptions
*/
class ValidationRuleNotFoundException extends \Exception
{
}
\ No newline at end of file
<?php
namespace ACP3\Core\Validation\ValidationRules;
use ACP3\Core\Exceptions\InvalidFormToken;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\SessionHandler;
use ACP3\Core\Validation\Exceptions\InvalidFormTokenException;
use ACP3\Core\Validation\Validator;
/**
......@@ -42,7 +42,7 @@ class FormTokenValidationRule extends AbstractValidationRule
public function validate(Validator $validator, $data, $field = '', array $extra = [])
{
if (!$this->isValid($data, $field, $extra)) {
throw new InvalidFormToken();
throw new InvalidFormTokenException();
}
}
......
<?php
namespace ACP3\Core\Validation;
use ACP3\Core\Exceptions\ValidationFailed;
use ACP3\Core\Validation\Exceptions\ValidationFailedException;
use ACP3\Core\Validation\Exceptions\ValidationRuleNotFoundException;
use ACP3\Core\Validation\ValidationRules\ValidationRuleInterface;
/**
......@@ -115,11 +116,13 @@ class Validator
$params['field'],
$params['extra']
);
} else {
throw new ValidationRuleNotFoundException('Can not find the validation rule with the name ' . $constraint['rule'] . '.');
}
}
if ($this->hasErrors()) {
throw new ValidationFailed($this->errors);
throw new ValidationFailedException($this->errors);
}
}
......@@ -128,6 +131,7 @@ class Validator
* @param mixed $field
*
* @return bool
* @throws \ACP3\Core\Validation\Exceptions\ValidationRuleNotFoundException
*/
public function is($validationRuleName, $field)
{
......@@ -135,7 +139,7 @@ class Validator
return $this->validationRules[$validationRuleName]->isValid($field);
}
return false;
throw new ValidationRuleNotFoundException('Can not find the validation rule with the name ' . $validationRuleName . '.');
}
/**
......
......@@ -99,7 +99,7 @@ class Index extends Core\Modules\FrontendController
$this->setTemplate($this->get('core.helpers.alerts')->confirmBox($this->translator->t('newsletter',
$bool !== false ? 'activate_success' : 'activate_error'), $this->appPath->getWebRoot()));
} catch (Core\Exceptions\ValidationFailed $e) {
} catch (Core\Validation\Exceptions\ValidationFailedException $e) {
$this->setContent($this->get('core.helpers.alerts')->errorBox($e->getMessage()));
}
}
......
......@@ -42,6 +42,8 @@ services:
- @core.validation.validation_rules.internal_uri_validation_rule
- @core.validation.validation_rules.uri_safe_validation_rule
- @seo.model.seorepository
tags:
- { name: core.validation.validation_rule }
seo.installer.schema:
class: %seo.installer.schema.fqdn%
......
......@@ -13,7 +13,7 @@ class AdminSettingsFormValidation extends Core\Validation\AbstractFormValidation
/**
* @param array $formData
*
* @throws \ACP3\Core\Exceptions\ValidationFailed
* @throws \ACP3\Core\Validation\Exceptions\ValidationFailedException
*/
public function validate(array $formData)
{
......
......@@ -2,11 +2,11 @@
namespace ACP3\Installer\Modules\Install\Controller;
use ACP3\Core\Exceptions\ValidationFailed;
use ACP3\Core\Filesystem;
use ACP3\Core\Functions;
use ACP3\Core\Helpers\Secure;
use ACP3\Core\User;
use ACP3\Core\Validation\Exceptions\ValidationFailedException;
use ACP3\Installer\Core;
use ACP3\Installer\Core\Date;
use ACP3\Installer\Modules\Install\Helpers\Install as InstallerHelpers;
......@@ -128,7 +128,7 @@ class Install extends AbstractController
$this->setTemplate('install/install.result.tpl');
return;
} catch (ValidationFailed $e) {
} catch (ValidationFailedException $e) {
$this->view->assign('error_msg', $this->get('core.helpers.alerts')->errorBox($e->getMessage()));
} catch (\Exception $e) {
$this->setTemplate('install/install.error.tpl');
......
<?php
class ValidatorTest extends PHPUnit_Framework_TestCase
{
/**
* @var \ACP3\Core\Validation\Validator
*/
protected $validator;
protected function setUp()
{
parent::setUp();
$this->validator = new \ACP3\Core\Validation\Validator();
}
public function testValidateValidValidationRuleWithValidValue()
{
$this->validator->registerValidationRule(new ACP3\Core\Validation\ValidationRules\EmailValidationRule());
$this->validator->addConstraint(\ACP3\Core\Validation\ValidationRules\EmailValidationRule::NAME, [
'data' => 'test@example.com',
'field' => 'mail',
'message' => 'Invalid E-mail address'
]);
try {
$this->validator->validate();
} catch (\ACP3\Core\Validation\Exceptions\ValidationFailedException $e) {
$this->fail();
}
}
public function testValidateValidValidationRuleWithInvalidValue()
{
$this->validator->registerValidationRule(new ACP3\Core\Validation\ValidationRules\EmailValidationRule());
$this->validator->addConstraint(\ACP3\Core\Validation\ValidationRules\EmailValidationRule::NAME, [
'data' => 'testexample.com',
'field' => 'mail',
'message' => 'Invalid E-mail address'
]);
try {
$this->validator->validate();
$this->fail();
} catch (\ACP3\Core\Validation\Exceptions\ValidationFailedException $e) {
$expected = [
'mail' => 'Invalid E-mail address'
];
$errors = unserialize($e->getMessage());
$this->assertEquals($expected, $errors);
}
}
public function testValidateValidValidationRuleWithInvalidValueWithoutFormField()
{
$this->validator->registerValidationRule(new ACP3\Core\Validation\ValidationRules\EmailValidationRule());
$this->validator->addConstraint(\ACP3\Core\Validation\ValidationRules\EmailValidationRule::NAME, [
'data' => 'testexample.com',
'message' => 'Invalid E-mail address'
]);
try {
$this->validator->validate();
$this->fail();
} catch (\ACP3\Core\Validation\Exceptions\ValidationFailedException $e) {
$expected = [
'Invalid E-mail address'
];
$errors = unserialize($e->getMessage());
$this->assertEquals($expected, $errors);
}
}
/**
* @expectedException \ACP3\Core\Validation\Exceptions\ValidationRuleNotFoundException
*/
public function testValidateInvalidValidationRule()
{
$this->validator->registerValidationRule(new ACP3\Core\Validation\ValidationRules\EmailValidationRule());
$this->validator->addConstraint('invalid_validation_rule', [
'data' => 'testexample.com',
'message' => 'Invalid E-mail address'
]);
$this->validator->validate();
}
/**
* @dataProvider inlineValidationProvider
*
* @param string $value
* @param bool $expected
*/
public function testIs($value, $expected)
{
$this->validator->registerValidationRule(new ACP3\Core\Validation\ValidationRules\EmailValidationRule());
$actual = $this->validator->is(\ACP3\Core\Validation\ValidationRules\EmailValidationRule::NAME, $value);
$this->assertEquals($expected, $actual);
}
/**
* @expectedException \ACP3\Core\Validation\Exceptions\ValidationRuleNotFoundException
*/
public function testIsInvalidValidationRule()
{
$this->validator->registerValidationRule(new ACP3\Core\Validation\ValidationRules\EmailValidationRule());
$this->validator->is('invalid_validation_rule', 'test@example.com');
}
/**
* @return array
*/
public function inlineValidationProvider()
{
return [
['test@example.com', true],
['testexample.com', false]
];
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment