split timesheet validator to simplify imports (#3974)

This commit is contained in:
Kevin Papst
2023-04-27 13:56:07 +02:00
committed by GitHub
parent 046ed313c9
commit f60065d089
11 changed files with 213 additions and 77 deletions

View File

@@ -6750,11 +6750,6 @@ parameters:
count: 2
path: src/Validator/Constraints/TeamValidator.php
-
message: "#^Cannot call method isVisible\\(\\) on App\\\\Entity\\\\Customer\\|null\\.$#"
count: 1
path: src/Validator/Constraints/TimesheetBasicValidator.php
-
message: "#^Cannot call method format\\(\\) on DateTime\\|null\\.$#"
count: 2

View File

@@ -45,6 +45,7 @@ use Symfony\Component\Validator\Constraints as Assert;
#[Serializer\VirtualProperty('UserAsId', exp: 'object.getUser().getId()', options: [new Serializer\SerializedName('user'), new Serializer\Type(name: 'integer'), new Serializer\Groups(['Not_Expanded'])])]
#[Serializer\VirtualProperty('TagsAsArray', exp: 'object.getTagsAsArray()', options: [new Serializer\SerializedName('tags'), new Serializer\Type(name: 'array<string>'), new Serializer\Groups(['Default'])])]
#[Constraints\Timesheet]
#[Constraints\TimesheetDeactivated]
class Timesheet implements EntityWithMetaFields, ExportableItem
{
/**

View File

@@ -16,9 +16,6 @@ final class TimesheetBasic extends TimesheetConstraint
public const MISSING_ACTIVITY_ERROR = 'kimai-timesheet-84';
public const MISSING_PROJECT_ERROR = 'kimai-timesheet-85';
public const ACTIVITY_PROJECT_MISMATCH_ERROR = 'kimai-timesheet-86';
public const DISABLED_ACTIVITY_ERROR = 'kimai-timesheet-87';
public const DISABLED_PROJECT_ERROR = 'kimai-timesheet-88';
public const DISABLED_CUSTOMER_ERROR = 'kimai-timesheet-89';
public const PROJECT_NOT_STARTED = 'kimai-timesheet-91';
public const PROJECT_ALREADY_ENDED = 'kimai-timesheet-92';
public const PROJECT_DISALLOWS_GLOBAL_ACTIVITY = 'kimai-timesheet-93';
@@ -29,9 +26,6 @@ final class TimesheetBasic extends TimesheetConstraint
self::MISSING_ACTIVITY_ERROR => 'An activity needs to be selected.',
self::MISSING_PROJECT_ERROR => 'A project needs to be selected.',
self::ACTIVITY_PROJECT_MISMATCH_ERROR => 'Project mismatch, project specific activity and timesheet project are different.',
self::DISABLED_ACTIVITY_ERROR => 'Cannot start a disabled activity.',
self::DISABLED_PROJECT_ERROR => 'Cannot start a disabled project.',
self::DISABLED_CUSTOMER_ERROR => 'Cannot start a disabled customer.',
self::PROJECT_NOT_STARTED => 'The project has not started at that time.',
self::PROJECT_ALREADY_ENDED => 'The project is finished at that time.',
self::PROJECT_DISALLOWS_GLOBAL_ACTIVITY => 'Global activities are forbidden for the selected project.',

View File

@@ -102,33 +102,6 @@ final class TimesheetBasicValidator extends ConstraintValidator
->addViolation();
}
$timesheetEnd = $timesheet->getEnd();
$newOrStarted = null === $timesheetEnd || $timesheet->getId() === null;
if ($newOrStarted && $hasActivity && !$activity->isVisible()) {
$context->buildViolation(TimesheetBasic::getErrorName(TimesheetBasic::DISABLED_ACTIVITY_ERROR))
->atPath('activity')
->setTranslationDomain('validators')
->setCode(TimesheetBasic::DISABLED_ACTIVITY_ERROR)
->addViolation();
}
if ($newOrStarted && !$project->isVisible()) {
$context->buildViolation(TimesheetBasic::getErrorName(TimesheetBasic::DISABLED_PROJECT_ERROR))
->atPath('project')
->setTranslationDomain('validators')
->setCode(TimesheetBasic::DISABLED_PROJECT_ERROR)
->addViolation();
}
if ($newOrStarted && !$project->getCustomer()->isVisible()) {
$context->buildViolation(TimesheetBasic::getErrorName(TimesheetBasic::DISABLED_CUSTOMER_ERROR))
->atPath('customer')
->setTranslationDomain('validators')
->setCode(TimesheetBasic::DISABLED_CUSTOMER_ERROR)
->addViolation();
}
if ($hasActivity && !$project->isGlobalActivities() && $activity->isGlobal()) {
$context->buildViolation(TimesheetBasic::getErrorName(TimesheetBasic::PROJECT_DISALLOWS_GLOBAL_ACTIVITY))
->atPath('activity')

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
#[\Attribute(\Attribute::TARGET_CLASS)]
final class TimesheetDeactivated extends Constraint
{
public const DISABLED_ACTIVITY_ERROR = 'kimai-timesheet-deactivated-activity';
public const DISABLED_PROJECT_ERROR = 'kimai-timesheet-deactivated-project';
public const DISABLED_CUSTOMER_ERROR = 'kimai-timesheet-deactivated-customer';
protected const ERROR_NAMES = [
self::DISABLED_ACTIVITY_ERROR => 'Cannot start a disabled activity.',
self::DISABLED_PROJECT_ERROR => 'Cannot start a disabled project.',
self::DISABLED_CUSTOMER_ERROR => 'Cannot start a disabled customer.',
];
public string $message = 'This timesheet has invalid settings.';
public function getTargets(): string|array
{
return self::CLASS_CONSTRAINT;
}
}

View File

@@ -0,0 +1,84 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Validator\Constraints;
use App\Entity\Timesheet as TimesheetEntity;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* This validator is separate, so it can be easily deactivated in import scenarios.
*/
final class TimesheetDeactivatedValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!($constraint instanceof TimesheetDeactivated)) {
throw new UnexpectedTypeException($constraint, TimesheetDeactivated::class);
}
if (!\is_object($value) || !($value instanceof TimesheetEntity)) {
throw new UnexpectedTypeException($value, TimesheetEntity::class);
}
$this->validateActivityAndProject($value, $this->context);
}
/**
* @param TimesheetEntity $timesheet
* @param ExecutionContextInterface $context
*/
protected function validateActivityAndProject(TimesheetEntity $timesheet, ExecutionContextInterface $context): void
{
$timesheetEnd = $timesheet->getEnd();
$newOrStarted = null === $timesheetEnd || $timesheet->getId() === null;
if (!$newOrStarted) {
return;
}
$activity = $timesheet->getActivity();
if (null !== $activity && !$activity->isVisible()) {
$context->buildViolation(TimesheetDeactivated::getErrorName(TimesheetDeactivated::DISABLED_ACTIVITY_ERROR))
->atPath('activity')
->setTranslationDomain('validators')
->setCode(TimesheetDeactivated::DISABLED_ACTIVITY_ERROR)
->addViolation();
}
$project = $timesheet->getProject();
if ($project === null) {
return;
}
if (!$project->isVisible()) {
$context->buildViolation(TimesheetDeactivated::getErrorName(TimesheetDeactivated::DISABLED_PROJECT_ERROR))
->atPath('project')
->setTranslationDomain('validators')
->setCode(TimesheetDeactivated::DISABLED_PROJECT_ERROR)
->addViolation();
}
$customer = $project->getCustomer();
if ($customer === null) {
return;
}
if (!$customer->isVisible()) {
$context->buildViolation(TimesheetDeactivated::getErrorName(TimesheetDeactivated::DISABLED_CUSTOMER_ERROR))
->atPath('customer')
->setTranslationDomain('validators')
->setCode(TimesheetDeactivated::DISABLED_CUSTOMER_ERROR)
->addViolation();
}
}
}

View File

@@ -38,11 +38,16 @@ final class TimesheetValidator extends ConstraintValidator
throw new UnexpectedTypeException($timesheet, TimesheetEntity::class);
}
$groups = [Constraint::DEFAULT_GROUP];
if ($this->context->getGroup() !== null) {
$groups = [$this->context->getGroup()];
}
foreach ($this->constraints as $innerConstraint) {
$this->context
->getValidator()
->inContext($this->context)
->validate($timesheet, $innerConstraint, [Constraint::DEFAULT_GROUP]);
->validate($timesheet, $innerConstraint, $groups);
}
}
}

View File

@@ -99,6 +99,7 @@ class TimesheetValidationTest extends KernelTestCase
$entity->setActivity($activity);
$entity->setProject($project);
$entity->setBegin(new \DateTime());
$entity->setEnd(new \DateTime());
$this->assertHasViolationForField($entity, 'customer');
}
@@ -166,6 +167,7 @@ class TimesheetValidationTest extends KernelTestCase
$entity->setActivity($activity);
$entity->setProject($project);
$entity->setBegin(new \DateTime());
$entity->setEnd(new \DateTime());
$this->assertHasViolationForField($entity, 'project');
}
@@ -203,6 +205,7 @@ class TimesheetValidationTest extends KernelTestCase
$entity->setActivity($activity);
$entity->setProject($project);
$entity->setBegin(new \DateTime());
$entity->setEnd(new \DateTime());
$this->assertHasViolationForField($entity, 'activity');
}

View File

@@ -143,39 +143,6 @@ class TimesheetBasicValidatorTest extends ConstraintValidatorTestCase
->assertRaised();
}
public function testDisabledValuesDuringStart()
{
$begin = new \DateTime('-10 hour');
$customer = new Customer('foo');
$customer->setVisible(false);
$activity = new Activity();
$activity->setVisible(false);
$project = new Project();
$project->setVisible(false);
$project->setCustomer($customer);
$activity->setProject($project);
$timesheet = new Timesheet();
$timesheet
->setBegin($begin)
->setActivity($activity)
->setProject($project)
;
$this->validator->validate($timesheet, new TimesheetBasic(['message' => 'myMessage']));
$this->buildViolation('Cannot start a disabled activity.')
->atPath('property.path.activity')
->setCode(TimesheetBasic::DISABLED_ACTIVITY_ERROR)
->buildNextViolation('Cannot start a disabled project.')
->atPath('property.path.project')
->setCode(TimesheetBasic::DISABLED_PROJECT_ERROR)
->buildNextViolation('Cannot start a disabled customer.')
->atPath('property.path.customer')
->setCode(TimesheetBasic::DISABLED_CUSTOMER_ERROR)
->assertRaised();
}
public function getProjectStartEndTestData()
{
yield [new \DateTime(), new \DateTime(), [

View File

@@ -0,0 +1,86 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Validator\Constraints;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Timesheet;
use App\Validator\Constraints\TimesheetDeactivated;
use App\Validator\Constraints\TimesheetDeactivatedValidator;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* @covers \App\Validator\Constraints\TimesheetDeactivated
* @covers \App\Validator\Constraints\TimesheetDeactivatedValidator
* @extends ConstraintValidatorTestCase<TimesheetDeactivatedValidator>
*/
class TimesheetDeactivatedValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): TimesheetDeactivatedValidator
{
return new TimesheetDeactivatedValidator();
}
public function testConstraintIsInvalid(): void
{
$this->expectException(UnexpectedTypeException::class);
$this->validator->validate(new Timesheet(), new NotBlank());
}
public function testInvalidValueThrowsException(): void
{
$this->expectException(UnexpectedTypeException::class);
$this->validator->validate(new NotBlank(), new TimesheetDeactivated(['message' => 'myMessage']));
}
public function testDisabledValuesDuringStart(): void
{
$begin = new \DateTime('-10 hour');
$customer = new Customer('foo');
$customer->setVisible(false);
$activity = new Activity();
$activity->setVisible(false);
$project = new Project();
$project->setVisible(false);
$project->setCustomer($customer);
$activity->setProject($project);
$timesheet = new Timesheet();
$timesheet
->setBegin($begin)
->setActivity($activity)
->setProject($project)
;
$this->validator->validate($timesheet, new TimesheetDeactivated(['message' => 'myMessage']));
$this->buildViolation(TimesheetDeactivated::getErrorName(TimesheetDeactivated::DISABLED_ACTIVITY_ERROR))
->atPath('property.path.activity')
->setCode(TimesheetDeactivated::DISABLED_ACTIVITY_ERROR)
->buildNextViolation(TimesheetDeactivated::getErrorName(TimesheetDeactivated::DISABLED_PROJECT_ERROR))
->atPath('property.path.project')
->setCode(TimesheetDeactivated::DISABLED_PROJECT_ERROR)
->buildNextViolation(TimesheetDeactivated::getErrorName(TimesheetDeactivated::DISABLED_CUSTOMER_ERROR))
->atPath('property.path.customer')
->setCode(TimesheetDeactivated::DISABLED_CUSTOMER_ERROR)
->assertRaised();
}
public function testGetTargets(): void
{
$constraint = new TimesheetDeactivated();
self::assertEquals('class', $constraint->getTargets());
}
}

View File

@@ -10522,11 +10522,6 @@ parameters:
count: 1
path: Validator/Constraints/TimesheetBasicValidatorTest.php
-
message: "#^Method App\\\\Tests\\\\Validator\\\\Constraints\\\\TimesheetBasicValidatorTest\\:\\:testDisabledValuesDuringStart\\(\\) has no return type specified\\.$#"
count: 1
path: Validator/Constraints/TimesheetBasicValidatorTest.php
-
message: "#^Method App\\\\Tests\\\\Validator\\\\Constraints\\\\TimesheetBasicValidatorTest\\:\\:testEmptyTimesheet\\(\\) has no return type specified\\.$#"
count: 1