Move project validator methods
This commit is contained in:
@@ -44,7 +44,7 @@ class Me extends Base
|
||||
'is_private' => 1,
|
||||
);
|
||||
|
||||
list($valid, ) = $this->project->validateCreation($values);
|
||||
list($valid, ) = $this->projectValidator->validateCreation($values);
|
||||
return $valid ? $this->project->create($values, $this->userSession->getId(), true) : false;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ class Project extends Base
|
||||
'description' => $description
|
||||
);
|
||||
|
||||
list($valid, ) = $this->project->validateCreation($values);
|
||||
list($valid, ) = $this->projectValidator->validateCreation($values);
|
||||
return $valid ? $this->project->create($values) : false;
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ class Project extends Base
|
||||
'description' => $description
|
||||
);
|
||||
|
||||
list($valid, ) = $this->project->validateModification($values);
|
||||
list($valid, ) = $this->projectValidator->validateModification($values);
|
||||
return $valid && $this->project->update($values);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ class Project extends Base
|
||||
}
|
||||
}
|
||||
|
||||
list($valid, $errors) = $this->project->validateModification($values);
|
||||
list($valid, $errors) = $this->projectValidator->validateModification($values);
|
||||
|
||||
if ($valid) {
|
||||
if ($this->project->update($values)) {
|
||||
@@ -329,7 +329,7 @@ class Project extends Base
|
||||
public function save()
|
||||
{
|
||||
$values = $this->request->getValues();
|
||||
list($valid, $errors) = $this->project->validateCreation($values);
|
||||
list($valid, $errors) = $this->projectValidator->validateCreation($values);
|
||||
|
||||
if ($valid) {
|
||||
$project_id = $this->project->create($values, $this->userSession->getId(), true);
|
||||
|
||||
@@ -114,6 +114,7 @@ use Pimple\Container;
|
||||
* @property \Kanboard\Model\UserMetadata $userMetadata
|
||||
* @property \Kanboard\Model\Webhook $webhook
|
||||
* @property \Kanboard\Validator\PasswordResetValidator $passwordResetValidator
|
||||
* @property \Kanboard\Validator\ProjectValidator $projectValidator
|
||||
* @property \Kanboard\Validator\SubtaskValidator $subtaskValidator
|
||||
* @property \Kanboard\Validator\SwimlaneValidator $swimlaneValidator
|
||||
* @property \Kanboard\Validator\TaskLinkValidator $taskLinkValidator
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
namespace Kanboard\Model;
|
||||
|
||||
use SimpleValidator\Validator;
|
||||
use SimpleValidator\Validators;
|
||||
use Kanboard\Core\Security\Token;
|
||||
use Kanboard\Core\Security\Role;
|
||||
|
||||
@@ -510,71 +508,4 @@ class Project extends Base
|
||||
->eq('id', $project_id)
|
||||
->save(array('is_public' => 0, 'token' => ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Common validation rules
|
||||
*
|
||||
* @access private
|
||||
* @return array
|
||||
*/
|
||||
private function commonValidationRules()
|
||||
{
|
||||
return array(
|
||||
new Validators\Integer('id', t('This value must be an integer')),
|
||||
new Validators\Integer('is_active', t('This value must be an integer')),
|
||||
new Validators\Required('name', t('The project name is required')),
|
||||
new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50),
|
||||
new Validators\MaxLength('identifier', t('The maximum length is %d characters', 50), 50),
|
||||
new Validators\MaxLength('start_date', t('The maximum length is %d characters', 10), 10),
|
||||
new Validators\MaxLength('end_date', t('The maximum length is %d characters', 10), 10),
|
||||
new Validators\AlphaNumeric('identifier', t('This value must be alphanumeric')) ,
|
||||
new Validators\Unique('identifier', t('The identifier must be unique'), $this->db->getConnection(), self::TABLE),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate project creation
|
||||
*
|
||||
* @access public
|
||||
* @param array $values Form values
|
||||
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
|
||||
*/
|
||||
public function validateCreation(array $values)
|
||||
{
|
||||
if (! empty($values['identifier'])) {
|
||||
$values['identifier'] = strtoupper($values['identifier']);
|
||||
}
|
||||
|
||||
$v = new Validator($values, $this->commonValidationRules());
|
||||
|
||||
return array(
|
||||
$v->execute(),
|
||||
$v->getErrors()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate project modification
|
||||
*
|
||||
* @access public
|
||||
* @param array $values Form values
|
||||
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
|
||||
*/
|
||||
public function validateModification(array $values)
|
||||
{
|
||||
if (! empty($values['identifier'])) {
|
||||
$values['identifier'] = strtoupper($values['identifier']);
|
||||
}
|
||||
|
||||
$rules = array(
|
||||
new Validators\Required('id', t('This value is required')),
|
||||
);
|
||||
|
||||
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
|
||||
|
||||
return array(
|
||||
$v->execute(),
|
||||
$v->getErrors()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ class ClassProvider implements ServiceProviderInterface
|
||||
),
|
||||
'Validator' => array(
|
||||
'PasswordResetValidator',
|
||||
'ProjectValidator',
|
||||
'SubtaskValidator',
|
||||
'SwimlaneValidator',
|
||||
'TaskValidator',
|
||||
|
||||
83
app/Validator/ProjectValidator.php
Normal file
83
app/Validator/ProjectValidator.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Validator;
|
||||
|
||||
use SimpleValidator\Validator;
|
||||
use SimpleValidator\Validators;
|
||||
use Kanboard\Model\Project;
|
||||
|
||||
/**
|
||||
* Project Validator
|
||||
*
|
||||
* @package validator
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class ProjectValidator extends Base
|
||||
{
|
||||
/**
|
||||
* Common validation rules
|
||||
*
|
||||
* @access private
|
||||
* @return array
|
||||
*/
|
||||
private function commonValidationRules()
|
||||
{
|
||||
return array(
|
||||
new Validators\Integer('id', t('This value must be an integer')),
|
||||
new Validators\Integer('is_active', t('This value must be an integer')),
|
||||
new Validators\Required('name', t('The project name is required')),
|
||||
new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50),
|
||||
new Validators\MaxLength('identifier', t('The maximum length is %d characters', 50), 50),
|
||||
new Validators\MaxLength('start_date', t('The maximum length is %d characters', 10), 10),
|
||||
new Validators\MaxLength('end_date', t('The maximum length is %d characters', 10), 10),
|
||||
new Validators\AlphaNumeric('identifier', t('This value must be alphanumeric')) ,
|
||||
new Validators\Unique('identifier', t('The identifier must be unique'), $this->db->getConnection(), Project::TABLE),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate project creation
|
||||
*
|
||||
* @access public
|
||||
* @param array $values Form values
|
||||
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
|
||||
*/
|
||||
public function validateCreation(array $values)
|
||||
{
|
||||
if (! empty($values['identifier'])) {
|
||||
$values['identifier'] = strtoupper($values['identifier']);
|
||||
}
|
||||
|
||||
$v = new Validator($values, $this->commonValidationRules());
|
||||
|
||||
return array(
|
||||
$v->execute(),
|
||||
$v->getErrors()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate project modification
|
||||
*
|
||||
* @access public
|
||||
* @param array $values Form values
|
||||
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
|
||||
*/
|
||||
public function validateModification(array $values)
|
||||
{
|
||||
if (! empty($values['identifier'])) {
|
||||
$values['identifier'] = strtoupper($values['identifier']);
|
||||
}
|
||||
|
||||
$rules = array(
|
||||
new Validators\Required('id', t('This value is required')),
|
||||
);
|
||||
|
||||
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
|
||||
|
||||
return array(
|
||||
$v->execute(),
|
||||
$v->getErrors()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -277,30 +277,5 @@ class ProjectTest extends Base
|
||||
|
||||
$project = $p->getByIdentifier('');
|
||||
$this->assertFalse($project);
|
||||
|
||||
// Validation rules
|
||||
$r = $p->validateCreation(array('name' => 'test', 'identifier' => 'TEST1'));
|
||||
$this->assertFalse($r[0]);
|
||||
|
||||
$r = $p->validateCreation(array('name' => 'test', 'identifier' => 'test1'));
|
||||
$this->assertFalse($r[0]);
|
||||
|
||||
$r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST1'));
|
||||
$this->assertTrue($r[0]);
|
||||
|
||||
$r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'test3'));
|
||||
$this->assertTrue($r[0]);
|
||||
|
||||
$r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => ''));
|
||||
$this->assertTrue($r[0]);
|
||||
|
||||
$r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST2'));
|
||||
$this->assertFalse($r[0]);
|
||||
|
||||
$r = $p->validateCreation(array('name' => 'test', 'identifier' => 'a-b-c'));
|
||||
$this->assertFalse($r[0]);
|
||||
|
||||
$r = $p->validateCreation(array('name' => 'test', 'identifier' => 'test 123'));
|
||||
$this->assertFalse($r[0]);
|
||||
}
|
||||
}
|
||||
|
||||
67
tests/units/Validator/ProjectValidatorTest.php
Normal file
67
tests/units/Validator/ProjectValidatorTest.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__.'/../Base.php';
|
||||
|
||||
use Kanboard\Validator\ProjectValidator;
|
||||
use Kanboard\Model\Project;
|
||||
|
||||
class ProjectValidatorTest extends Base
|
||||
{
|
||||
public function testValidateCreation()
|
||||
{
|
||||
$validator = new ProjectValidator($this->container);
|
||||
$p = new Project($this->container);
|
||||
|
||||
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1', 'identifier' => 'test1')));
|
||||
$this->assertEquals(2, $p->create(array('name' => 'UnitTest2')));
|
||||
|
||||
$project = $p->getById(1);
|
||||
$this->assertNotEmpty($project);
|
||||
$this->assertEquals('TEST1', $project['identifier']);
|
||||
|
||||
$project = $p->getById(2);
|
||||
$this->assertNotEmpty($project);
|
||||
$this->assertEquals('', $project['identifier']);
|
||||
|
||||
$r = $validator->validateCreation(array('name' => 'test', 'identifier' => 'TEST1'));
|
||||
$this->assertFalse($r[0]);
|
||||
|
||||
$r = $validator->validateCreation(array('name' => 'test', 'identifier' => 'test1'));
|
||||
$this->assertFalse($r[0]);
|
||||
|
||||
$r = $validator->validateCreation(array('name' => 'test', 'identifier' => 'a-b-c'));
|
||||
$this->assertFalse($r[0]);
|
||||
|
||||
$r = $validator->validateCreation(array('name' => 'test', 'identifier' => 'test 123'));
|
||||
$this->assertFalse($r[0]);
|
||||
}
|
||||
|
||||
public function testValidateModification()
|
||||
{
|
||||
$validator = new ProjectValidator($this->container);
|
||||
$p = new Project($this->container);
|
||||
|
||||
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1', 'identifier' => 'test1')));
|
||||
$this->assertEquals(2, $p->create(array('name' => 'UnitTest2', 'identifier' => 'TEST2')));
|
||||
|
||||
$project = $p->getById(1);
|
||||
$this->assertNotEmpty($project);
|
||||
$this->assertEquals('TEST1', $project['identifier']);
|
||||
|
||||
$project = $p->getById(2);
|
||||
$this->assertNotEmpty($project);
|
||||
$this->assertEquals('TEST2', $project['identifier']);
|
||||
|
||||
$r = $validator->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST1'));
|
||||
$this->assertTrue($r[0]);
|
||||
|
||||
$r = $validator->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'test3'));
|
||||
$this->assertTrue($r[0]);
|
||||
|
||||
$r = $validator->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => ''));
|
||||
$this->assertTrue($r[0]);
|
||||
|
||||
$r = $validator->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST2'));
|
||||
$this->assertFalse($r[0]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user