Prevent bookings with same start / stop time (#3304)
This commit is contained in:
@@ -37,6 +37,7 @@ class SystemConfigurationTest extends TestCase
|
||||
'timesheet' => [
|
||||
'rules' => [
|
||||
'allow_future_times' => false,
|
||||
'allow_zero_duration' => true,
|
||||
'lockdown_period_start' => null,
|
||||
'lockdown_period_end' => null,
|
||||
'lockdown_grace_period' => null,
|
||||
|
||||
@@ -93,6 +93,7 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
|
||||
$configService = static::$kernel->getContainer()->get(SystemConfiguration::class);
|
||||
$this->assertEquals('default', $configService->find('timesheet.mode'));
|
||||
$this->assertTrue($configService->find('timesheet.rules.allow_future_times'));
|
||||
$this->assertTrue($configService->find('timesheet.rules.allow_zero_duration'));
|
||||
$this->assertEquals(1, $configService->find('timesheet.active_entries.hard_limit'));
|
||||
|
||||
$form = $client->getCrawler()->filter('form[name=system_configuration_form_timesheet]')->form();
|
||||
@@ -102,6 +103,7 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
|
||||
['name' => 'timesheet.mode', 'value' => 'duration_only'],
|
||||
['name' => 'timesheet.active_entries.default_begin', 'value' => '23:59'],
|
||||
['name' => 'timesheet.rules.allow_future_times', 'value' => false],
|
||||
['name' => 'timesheet.rules.allow_zero_duration', 'value' => true],
|
||||
['name' => 'timesheet.rules.allow_overlapping_records', 'value' => false],
|
||||
['name' => 'timesheet.rules.allow_overbooking_budget', 'value' => false],
|
||||
['name' => 'timesheet.active_entries.hard_limit', 'value' => 99],
|
||||
@@ -168,6 +170,7 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
|
||||
['name' => 'timesheet.mode', 'value' => 'foo'],
|
||||
['name' => 'timesheet.active_entries.default_begin', 'value' => '23:59'],
|
||||
['name' => 'timesheet.rules.allow_future_times', 'value' => 1],
|
||||
['name' => 'timesheet.rules.allow_zero_duration', 'value' => 1],
|
||||
['name' => 'timesheet.rules.allow_overlapping_records', 'value' => 1],
|
||||
['name' => 'timesheet.rules.allow_overbooking_budget', 'value' => 1],
|
||||
['name' => 'timesheet.active_entries.hard_limit', 'value' => -1],
|
||||
@@ -176,7 +179,7 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
|
||||
],
|
||||
[
|
||||
'#system_configuration_form_timesheet_configuration_0_value', // mode
|
||||
'#system_configuration_form_timesheet_configuration_5_value', // hard_limit
|
||||
'#system_configuration_form_timesheet_configuration_6_value', // hard_limit
|
||||
],
|
||||
true
|
||||
);
|
||||
|
||||
@@ -370,6 +370,7 @@ class TimesheetControllerTest extends ControllerBaseTest
|
||||
['name' => 'timesheet.mode', 'value' => 'default'],
|
||||
['name' => 'timesheet.active_entries.default_begin', 'value' => '08:00'],
|
||||
['name' => 'timesheet.rules.allow_future_times', 'value' => true],
|
||||
['name' => 'timesheet.rules.allow_zero_duration', 'value' => true],
|
||||
['name' => 'timesheet.rules.allow_overlapping_records', 'value' => false],
|
||||
['name' => 'timesheet.rules.allow_overbooking_budget', 'value' => true],
|
||||
['name' => 'timesheet.active_entries.hard_limit', 'value' => 1],
|
||||
@@ -458,6 +459,59 @@ class TimesheetControllerTest extends ControllerBaseTest
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreateActionWithEmptyDuration()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
|
||||
|
||||
$fixture = new ActivityFixtures();
|
||||
$fixture->setAmount(1);
|
||||
$fixture->setIsGlobal(true);
|
||||
$fixture->setIsVisible(true);
|
||||
$fixture->setCallback(function (Activity $activity) {
|
||||
$activity->setBudget(1000);
|
||||
$activity->setTimeBudget(3600);
|
||||
});
|
||||
$activities = $this->importFixture($fixture);
|
||||
/** @var Activity $activity */
|
||||
$activity = $activities[0];
|
||||
|
||||
$fixture = new TimesheetFixtures();
|
||||
$fixture->setAmount(1);
|
||||
$fixture->setActivities([$activity]);
|
||||
$fixture->setUser($this->getUserByRole(User::ROLE_USER));
|
||||
$timesheets = $this->importFixture($fixture);
|
||||
$id = $timesheets[0]->getId();
|
||||
|
||||
$this->request($client, '/timesheet/' . $id . '/edit');
|
||||
|
||||
$response = $client->getResponse();
|
||||
$this->assertTrue($response->isSuccessful());
|
||||
|
||||
/** @var ConfigurationRepository $repository */
|
||||
$repository = $this->getEntityManager()->getRepository(Configuration::class);
|
||||
$config = new Configuration();
|
||||
$config->setName('timesheet.rules.allow_zero_duration');
|
||||
$config->setValue(false);
|
||||
$repository->saveConfiguration($config);
|
||||
|
||||
$this->assertHasValidationError(
|
||||
$client,
|
||||
'/timesheet/' . $id . '/edit',
|
||||
'form[name=timesheet_edit_form]',
|
||||
[
|
||||
'timesheet_edit_form' => [
|
||||
'hourlyRate' => 100,
|
||||
'begin' => '2020-02-18 01:00',
|
||||
'end' => '2020-02-18 01:00',
|
||||
'duration' => '00:00',
|
||||
'project' => 1,
|
||||
'activity' => $activity->getId(),
|
||||
]
|
||||
],
|
||||
['#timesheet_edit_form_duration']
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreateActionWithBeginAndEndAndTagValues()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
|
||||
|
||||
@@ -202,6 +202,7 @@ class AppExtensionTest extends TestCase
|
||||
],
|
||||
'rules' => [
|
||||
'allow_future_times' => true,
|
||||
'allow_zero_duration' => true,
|
||||
'allow_overlapping_records' => true,
|
||||
'lockdown_period_start' => null,
|
||||
'lockdown_period_end' => null,
|
||||
|
||||
@@ -296,6 +296,7 @@ class ConfigurationTest extends TestCase
|
||||
],
|
||||
'rules' => [
|
||||
'allow_future_times' => true,
|
||||
'allow_zero_duration' => true,
|
||||
'allow_overlapping_records' => true,
|
||||
'lockdown_period_start' => null,
|
||||
'lockdown_period_end' => null,
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
<?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\Configuration\ConfigLoaderInterface;
|
||||
use App\Configuration\SystemConfiguration;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Validator\Constraints\TimesheetZeroDuration;
|
||||
use App\Validator\Constraints\TimesheetZeroDurationValidator;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Validator\Constraints\TimesheetZeroDuration
|
||||
* @covers \App\Validator\Constraints\TimesheetZeroDurationValidator
|
||||
*/
|
||||
class TimesheetZeroDurationValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
protected function createValidator()
|
||||
{
|
||||
return $this->createMyValidator(false);
|
||||
}
|
||||
|
||||
protected function createMyValidator(bool $allowZeroDuration = false)
|
||||
{
|
||||
$loader = $this->createMock(ConfigLoaderInterface::class);
|
||||
$config = new SystemConfiguration($loader, [
|
||||
'timesheet' => [
|
||||
'rules' => [
|
||||
'allow_zero_duration' => $allowZeroDuration,
|
||||
],
|
||||
]
|
||||
]);
|
||||
|
||||
return new TimesheetZeroDurationValidator($config);
|
||||
}
|
||||
|
||||
public function testConstraintIsInvalid()
|
||||
{
|
||||
$this->expectException(UnexpectedTypeException::class);
|
||||
|
||||
$this->validator->validate(new Timesheet(), new NotBlank());
|
||||
}
|
||||
|
||||
public function testInvalidValueThrowsException()
|
||||
{
|
||||
$this->expectException(UnexpectedTypeException::class);
|
||||
|
||||
$this->validator->validate(new NotBlank(), new TimesheetZeroDuration(['message' => 'Duration cannot be zero.']));
|
||||
}
|
||||
|
||||
private function prepareTimesheet()
|
||||
{
|
||||
// creates Timesheet with same begin and endtime
|
||||
$begin = new \DateTime();
|
||||
$timesheet = new Timesheet();
|
||||
$timesheet->setBegin(clone $begin);
|
||||
$timesheet->setEnd(clone $begin);
|
||||
$timesheet->setDuration(0);
|
||||
|
||||
return $timesheet;
|
||||
}
|
||||
|
||||
public function testZeroDurationIsDisallowed()
|
||||
{
|
||||
$timesheet = $this->prepareTimesheet();
|
||||
|
||||
$this->validator->validate($timesheet, new TimesheetZeroDuration(['message' => 'Duration cannot be zero.']));
|
||||
|
||||
$this->buildViolation('Duration cannot be zero.')
|
||||
->atPath('property.path.duration')
|
||||
->setCode(TimesheetZeroDuration::ZERO_DURATION_ERROR)
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testZeroDurationIsAllowed()
|
||||
{
|
||||
$this->validator = $this->createMyValidator(true);
|
||||
$this->validator->initialize($this->context);
|
||||
|
||||
$timesheet = $this->prepareTimesheet();
|
||||
|
||||
$this->validator->validate($timesheet, new TimesheetZeroDuration(['message' => 'Duration cannot be zero.']));
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user