Files
kimai/tests/Entity/TimesheetValidationTest.php
Kevin Papst 677f8d776c [3.0] Update Symfony to 7.4 and dependencies, removed deprecated code, removed API password (#5622)
* deactivate deprecated auto-mapping
* use native month-picker with fallback on old browser
* bump dependencies to new major versions
* replace deprecated ARRAY column type with JSON
* named arguments
* nullable arguments
* native lazy ghosts
* update routes to use php config
* disable PHP < 8.4
* use static RTL configuration
* new qr code package
* clear out env file
* recipe updates
* fix deprecations
* preset new env variable if not existing
* bump maria db version examples
* remove deprecated api-token support
* fix validator deprecations
* remove timesheet category column
* fix broken validator autoconfiguration
* stabilize tests
* fix migration syntax compatibility
* fix doctrine deprecation
* fix datetime format
* remove constructor dependency
* only include visible preferences in invoice templates
* fix test container dependency
* removed ProjectConstraint multi-constraint logic
* allow to disable plugins in certain environments
* refactor TimesheetConstraint to non-service
* activate new interface methods
* prevent update issues with .env
* improved customer fixture
* removed $user->isExportDecimal()
2026-01-04 15:04:25 +01:00

300 lines
8.8 KiB
PHP

<?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\Entity;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
#[CoversClass(Timesheet::class)]
#[Group('integration')]
class TimesheetValidationTest extends KernelTestCase
{
use EntityValidationTestTrait;
protected function getEntity(): Timesheet
{
$customer = new Customer('Test Customer');
$project = new Project();
$project->setName('Test Project');
$project->setCustomer($customer);
$activity = new Activity();
$activity->setName('Test');
$activity->setProject($project);
$entity = new Timesheet();
$entity->setUser(new User());
$entity->setActivity($activity);
$entity->setProject($project);
return $entity;
}
public function testValidationNeedsActivity(): void
{
$project = new Project();
$project->setCustomer(new Customer('foo'));
$entity = new Timesheet();
$entity->setUser(new User());
$entity->setProject($project);
$entity->setBegin(new \DateTime());
$this->assertHasViolationForField($entity, 'activity');
}
public function testValidationNeedsProject(): void
{
$entity = new Timesheet();
$entity->setUser(new User());
$entity->setActivity(new Activity());
$entity->setBegin(new \DateTime());
$this->assertHasViolationForField($entity, 'project');
}
public function testValidationProjectMismatch(): void
{
$customer = new Customer('foo');
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$project2 = new Project();
$project2->setName('bar');
$project2->setCustomer($customer);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$entity = new Timesheet();
$entity->setUser(new User());
$entity->setActivity($activity);
$entity->setProject($project2);
$entity->setBegin(new \DateTime());
$this->assertHasViolationForField($entity, 'project');
}
public function testValidationCustomerInvisible(): void
{
$customer = new Customer('foo');
$customer->setVisible(false);
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$entity = new Timesheet();
$entity->setUser(new User());
$entity->setActivity($activity);
$entity->setProject($project);
$entity->setBegin(new \DateTime());
$entity->setEnd(new \DateTime());
$this->assertHasViolationForField($entity, 'customer');
}
private function createStoppedTimesheet(Project $project, Activity $activity, ?int $id = null): Timesheet
{
$entity = new Timesheet();
$entity->setUser(new User());
$entity->setActivity($activity);
$entity->setProject($project);
$entity->setBegin(new \DateTime());
$entity->setEnd(new \DateTime());
if ($id !== null) {
$o = new \ReflectionClass($entity);
$p = $o->getProperty('id');
$p->setValue($entity, $id);
}
return $entity;
}
public function testValidationCustomerInvisibleDoesNotTriggerOnStoppedEntities(): void
{
$customer = new Customer('foo');
$customer->setVisible(false);
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$entity = $this->createStoppedTimesheet($project, $activity, 99);
$this->assertHasNoViolations($entity);
}
public function testValidationCustomerInvisibleDoesTriggerOnNewEntities(): void
{
$customer = new Customer('foo');
$customer->setVisible(false);
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$entity = $this->createStoppedTimesheet($project, $activity);
$this->assertHasViolationForField($entity, 'customer');
}
public function testValidationProjectInvisible(): void
{
$customer = new Customer('foo');
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$project->setVisible(false);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$entity = new Timesheet();
$entity->setUser(new User());
$entity->setActivity($activity);
$entity->setProject($project);
$entity->setBegin(new \DateTime());
$entity->setEnd(new \DateTime());
$this->assertHasViolationForField($entity, 'project');
}
public function testValidationProjectInvisibleDoesNotTriggerOnStoppedEntities(): void
{
$customer = new Customer('foo');
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$project->setVisible(false);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$entity = $this->createStoppedTimesheet($project, $activity, 1);
$this->assertHasNoViolations($entity);
}
public function testValidationProjectInvisibleDoesTriggerOnNewEntities(): void
{
$customer = new Customer('foo');
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$project->setVisible(false);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$entity = $this->createStoppedTimesheet($project, $activity);
$this->assertHasViolationForField($entity, 'project');
}
public function testValidationActivityInvisible(): void
{
$customer = new Customer('foo');
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$activity->setVisible(false);
$entity = new Timesheet();
$entity->setUser(new User());
$entity->setActivity($activity);
$entity->setProject($project);
$entity->setBegin(new \DateTime());
$entity->setEnd(new \DateTime());
$this->assertHasViolationForField($entity, 'activity');
}
public function testValidationActivityInvisibleDoesNotTriggerOnStoppedEntities(): void
{
$customer = new Customer('foo');
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$activity->setVisible(false);
$entity = $this->createStoppedTimesheet($project, $activity, 2);
$this->assertHasNoViolations($entity);
}
public function testValidationActivityInvisibleDoesTriggerOnNewEntities(): void
{
$customer = new Customer('foo');
$project = new Project();
$project->setName('foo');
$project->setCustomer($customer);
$activity = new Activity();
$activity->setName('hello-world');
$activity->setProject($project);
$activity->setVisible(false);
$entity = $this->createStoppedTimesheet($project, $activity);
$this->assertHasViolationForField($entity, 'activity');
}
public function testValidationEndNotEarlierThanBegin(): void
{
$entity = $this->getEntity();
$begin = new \DateTime();
$end = clone $begin;
$end = $end->modify('-1 second');
$entity->setBegin($begin);
$entity->setEnd($end);
$this->assertHasViolationForField($entity, ['end_date', 'duration']);
// allow same begin and end
$entity = $this->getEntity();
$begin = new \DateTime();
$end = clone $begin;
$entity->setBegin($begin);
$entity->setEnd($end);
$this->assertHasViolationForField($entity, []);
}
}