use dialog to duplicate timesheet (#2567)

* fix: display time budget in customer listing if set
* updated api methods with annotations
* do not require checkboxes for system configurations
* open dialog for duplicated timesheets
This commit is contained in:
Kevin Papst
2021-05-12 22:32:08 +02:00
committed by GitHub
parent f81f9ba492
commit f2b32b211b
16 changed files with 271 additions and 230 deletions

View File

@@ -120,13 +120,13 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
return $client->request($method, $this->createUrl($url), $parameters, [], $server, $content);
}
protected function assertEntityNotFound(string $role, string $url, string $method = 'GET')
protected function assertEntityNotFound(string $role, string $url, string $method = 'GET', ?string $message = null)
{
$client = $this->getClientForAuthenticatedUser($role);
$this->request($client, $url, $method);
$this->assertApiException($client->getResponse(), [
'code' => 404,
'message' => 'Not found'
'message' => $message ?? 'Not found'
]);
}
@@ -138,19 +138,19 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
]);
}
protected function assertEntityNotFoundForDelete(string $role, string $url)
protected function assertEntityNotFoundForDelete(string $role, string $url, ?string $message = null)
{
$this->assertExceptionForDeleteAction($role, $url, [], [
'code' => 404,
'message' => 'Not found'
'message' => $message ?? 'Not found'
]);
}
protected function assertEntityNotFoundForPatch(string $role, string $url, array $data)
protected function assertEntityNotFoundForPatch(string $role, string $url, array $data, ?string $message = null)
{
$this->assertExceptionForPatchAction($role, $url, $data, [
'code' => 404,
'message' => 'Not found'
'message' => $message ?? 'Not found',
]);
}
@@ -158,7 +158,7 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
{
$this->assertExceptionForPostAction($role, $url, $data, [
'code' => 404,
'message' => $message ?? 'Not found'
'message' => $message ?? 'Not found',
]);
}

View File

@@ -363,7 +363,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$timesheets = $this->importFixtureForUser(User::ROLE_ADMIN);
$this->assertCount(10, $timesheets);
$this->assertApiAccessDenied($client, '/api/timesheets/' . $timesheets[0]->getId(), 'You are not allowed to view this timesheet');
$this->assertApiAccessDenied($client, '/api/timesheets/' . $timesheets[0]->getId(), 'Access denied.');
}
public function testGetEntityAccessAllowedForAdmin()
@@ -379,7 +379,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testGetEntityNotFound()
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/' . PHP_INT_MAX);
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/' . PHP_INT_MAX, 'GET', 'App\\Entity\\Timesheet object not found by the @ParamConverter annotation.');
}
public function testPostAction()
@@ -569,12 +569,12 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertFalse($response->isSuccessful());
$this->assertEquals(Response::HTTP_FORBIDDEN, $response->getStatusCode());
$json = json_decode($response->getContent(), true);
$this->assertEquals('You are not allowed to update this timesheet', $json['message']);
$this->assertEquals('Access denied.', $json['message']);
}
public function testPatchActionWithUnknownTimesheet()
{
$this->assertEntityNotFoundForPatch(User::ROLE_USER, '/api/timesheets/255', []);
$this->assertEntityNotFoundForPatch(User::ROLE_USER, '/api/timesheets/255', [], 'App\\Entity\\Timesheet object not found by the @ParamConverter annotation.');
}
public function testInvalidPatchAction()
@@ -618,7 +618,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testDeleteActionWithUnknownTimesheet()
{
$this->assertEntityNotFoundForDelete(User::ROLE_ADMIN, '/api/timesheets/255');
$this->assertEntityNotFoundForDelete(User::ROLE_ADMIN, '/api/timesheets/255', 'App\\Entity\\Timesheet object not found by the @ParamConverter annotation.');
}
public function testDeleteActionForDifferentUser()
@@ -644,7 +644,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertFalse($response->isSuccessful());
$this->assertEquals(Response::HTTP_FORBIDDEN, $response->getStatusCode());
$json = json_decode($response->getContent(), true);
$this->assertEquals('You are not allowed to delete this timesheet', $json['message']);
$this->assertEquals('Access denied.', $json['message']);
}
public function testDeleteActionForExportedRecordIsNotAllowed()
@@ -661,7 +661,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$em->flush();
$this->request($client, '/api/timesheets/' . $id, 'DELETE');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to delete this timesheet');
$this->assertApiResponseAccessDenied($client->getResponse(), 'Access denied.');
}
public function testDeleteActionForExportedRecordIsAllowedForAdmin()
@@ -784,7 +784,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testStopThrowsNotFound()
{
$this->assertEntityNotFoundForPatch(User::ROLE_USER, '/api/timesheets/11/stop', []);
$this->assertEntityNotFoundForPatch(User::ROLE_USER, '/api/timesheets/11/stop', [], 'App\\Entity\\Timesheet object not found by the @ParamConverter annotation.');
}
public function testStopNotAllowedForUser()
@@ -807,7 +807,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$id = $timesheets[3]->getId();
$this->request($client, '/api/timesheets/' . $id . '/stop', 'PATCH');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to stop this timesheet');
$this->assertApiResponseAccessDenied($client->getResponse(), 'Access denied.');
}
public function testGetCollectionWithTags()
@@ -980,12 +980,12 @@ class TimesheetControllerTest extends APIControllerBaseTest
$id = $timesheets[0]->getId();
$this->request($client, '/api/timesheets/' . $id . '/restart', 'PATCH');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to re-start this timesheet');
$this->assertApiResponseAccessDenied($client->getResponse(), 'Access denied.');
}
public function testRestartThrowsNotFound()
{
$this->assertEntityNotFoundForPatch(User::ROLE_USER, '/api/timesheets/42/restart', []);
$this->assertEntityNotFoundForPatch(User::ROLE_USER, '/api/timesheets/42/restart', [], 'App\\Entity\\Timesheet object not found by the @ParamConverter annotation.');
}
public function testDuplicateAction()
@@ -1024,7 +1024,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testDuplicateThrowsNotFound()
{
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/11/duplicate', []);
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/11/duplicate', [], 'App\\Entity\\Timesheet object not found by the @ParamConverter annotation.');
}
public function testExportAction()
@@ -1063,19 +1063,17 @@ class TimesheetControllerTest extends APIControllerBaseTest
$id = $timesheets[0]->getId();
$this->request($client, '/api/timesheets/' . $id . '/export', 'PATCH');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to lock this timesheet');
$this->assertApiResponseAccessDenied($client->getResponse(), 'Access denied.');
}
public function testExportThrowsNotFound()
{
$id = PHP_INT_MAX;
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/' . $id . '/export', []);
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/' . PHP_INT_MAX . '/export', [], 'App\\Entity\\Timesheet object not found by the @ParamConverter annotation.');
}
public function testMetaActionThrowsNotFound()
{
$id = PHP_INT_MAX;
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/' . $id . '/meta', []);
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/timesheets/' . PHP_INT_MAX . '/meta', [], 'App\\Entity\\Timesheet object not found by the @ParamConverter annotation.');
}
public function testMetaActionThrowsExceptionOnMissingName()
@@ -1084,7 +1082,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
return $this->assertExceptionForMethod($client, '/api/timesheets/' . $id . '/meta', 'PATCH', ['value' => 'X'], [
$this->assertExceptionForMethod($client, '/api/timesheets/' . $id . '/meta', 'PATCH', ['value' => 'X'], [
'code' => 400,
'message' => 'Parameter "name" of value "NULL" violated a constraint "This value should not be null."'
]);
@@ -1096,7 +1094,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
return $this->assertExceptionForMethod($client, '/api/timesheets/' . $id . '/meta', 'PATCH', ['name' => 'X'], [
$this->assertExceptionForMethod($client, '/api/timesheets/' . $id . '/meta', 'PATCH', ['name' => 'X'], [
'code' => 400,
'message' => 'Parameter "value" of value "NULL" violated a constraint "This value should not be null."'
]);
@@ -1108,7 +1106,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$timesheets = $this->importFixtureForUser(User::ROLE_USER);
$id = $timesheets[0]->getId();
return $this->assertExceptionForMethod($client, '/api/timesheets/' . $id . '/meta', 'PATCH', ['name' => 'X', 'value' => 'Y'], [
$this->assertExceptionForMethod($client, '/api/timesheets/' . $id . '/meta', 'PATCH', ['name' => 'X', 'value' => 'Y'], [
'code' => 500,
'message' => 'Unknown meta-field requested'
]);

View File

@@ -19,6 +19,7 @@ use App\Repository\ConfigurationRepository;
use App\Tests\DataFixtures\ActivityFixtures;
use App\Tests\DataFixtures\TimesheetFixtures;
use App\Tests\Mocks\TimesheetTestMetaFieldSubscriberMock;
use App\Timesheet\DateTimeFactory;
/**
* @group integration
@@ -634,4 +635,51 @@ class TimesheetControllerTest extends ControllerBaseTest
self::assertEquals(13, $timesheet->getFixedRate());
}
}
public function testDuplicateAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$dateTime = new DateTimeFactory(new \DateTimeZone('Europe/London'));
$fixture = new TimesheetFixtures();
$fixture->setAmount(1);
$fixture->setAmountRunning(0);
$fixture->setUser($this->getUserByRole(User::ROLE_USER));
$fixture->setStartDate($dateTime->createDateTime());
$fixture->setCallback(function (Timesheet $timesheet) {
$timesheet->setDescription('Testing is fun!');
$end = clone $timesheet->getBegin();
$end->modify('+ 16 hours');
$timesheet->setEnd($end);
$timesheet->setFixedRate(2016);
$timesheet->setHourlyRate(127);
});
/** @var Timesheet[] $ids */
$ids = $this->importFixture($fixture);
$newId = $ids[0]->getId();
$this->request($client, '/timesheet/' . $newId . '/duplicate');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=timesheet_edit_form]')->form();
$client->submit($form, $form->getPhpValues());
$this->assertIsRedirect($client, $this->createUrl('/timesheet/'));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find($newId++);
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertEquals('Europe/London', $timesheet->getBegin()->getTimezone()->getName());
$this->assertEquals('Testing is fun!', $timesheet->getDescription());
$this->assertEquals(2016, $timesheet->getRate());
$this->assertEquals(127, $timesheet->getHourlyRate());
$this->assertEquals(2016, $timesheet->getFixedRate());
$this->assertTrue($timesheet->getDuration() == 57600 || $timesheet->getDuration() == 57660); // 1 minute rounding might be applied
$this->assertEquals(2016, $timesheet->getRate());
}
}

View File

@@ -14,6 +14,7 @@ use App\Entity\TimesheetMeta;
use App\Entity\User;
use App\Form\Type\DateRangeType;
use App\Tests\DataFixtures\TimesheetFixtures;
use App\Timesheet\DateTimeFactory;
use App\Timesheet\Util;
/**
@@ -405,4 +406,51 @@ class TimesheetTeamControllerTest extends ControllerBaseTest
self::assertEquals(Util::calculateRate(13.78, $timesheet->getDuration()), $timesheet->getRate());
}
}
public function testDuplicateAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$dateTime = new DateTimeFactory(new \DateTimeZone('Europe/London'));
$fixture = new TimesheetFixtures();
$fixture->setAmount(1);
$fixture->setAmountRunning(0);
$fixture->setUser($this->getUserByRole(User::ROLE_USER));
$fixture->setStartDate($dateTime->createDateTime());
$fixture->setCallback(function (Timesheet $timesheet) {
$timesheet->setDescription('Testing is fun!');
$end = clone $timesheet->getBegin();
$end->modify('+ 16 hours');
$timesheet->setEnd($end);
$timesheet->setFixedRate(2016);
$timesheet->setHourlyRate(127);
});
/** @var Timesheet[] $ids */
$ids = $this->importFixture($fixture);
$newId = $ids[0]->getId();
$this->request($client, '/team/timesheet/' . $newId . '/duplicate');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=timesheet_admin_edit_form]')->form();
$client->submit($form, $form->getPhpValues());
$this->assertIsRedirect($client, $this->createUrl('/team/timesheet/'));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$em = $this->getEntityManager();
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find($newId++);
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertEquals('Europe/London', $timesheet->getBegin()->getTimezone()->getName());
$this->assertEquals('Testing is fun!', $timesheet->getDescription());
$this->assertEquals(2016, $timesheet->getRate());
$this->assertEquals(127, $timesheet->getHourlyRate());
$this->assertEquals(2016, $timesheet->getFixedRate());
$this->assertTrue($timesheet->getDuration() == 57600 || $timesheet->getDuration() == 57660); // 1 minute rounding might be applied
$this->assertEquals(2016, $timesheet->getRate());
}
}

View File

@@ -45,9 +45,9 @@ final class TimesheetFixtures implements TestFixture
*/
private $projects = [];
/**
* @var string
* @var \DateTime
*/
private $startDate = '2018-04-01';
private $startDate;
/**
* @var bool
*/
@@ -121,8 +121,8 @@ final class TimesheetFixtures implements TestFixture
*/
public function setStartDate($date): TimesheetFixtures
{
if ($date instanceof \DateTime) {
$date = $date->format('Y-m-d');
if (!($date instanceof \DateTime)) {
$date = new \DateTime($date);
}
$this->startDate = $date;
@@ -312,7 +312,11 @@ final class TimesheetFixtures implements TestFixture
private function getDateTime(int $i): \DateTime
{
$start = \DateTime::createFromFormat('Y-m-d', $this->startDate);
if ($this->startDate === null) {
$this->startDate = new \DateTime('2018-04-01');
}
$start = clone $this->startDate;
$start->modify("+ $i days");
$start->modify('+ ' . rand(1, 172800) . ' seconds'); // up to 2 days

View File

@@ -210,12 +210,11 @@ class TimesheetVoterTest extends AbstractVoterTest
'lockdown_period_start' => $lockdownBegin,
'lockdown_period_end' => $lockdownEnd,
'lockdown_grace_period' => $lockdownGrace,
'allow_overlapping_records' => true,
],
]
]);
$voter = new TimesheetVoter($this->getRolePermissionManager(), new LockdownService($config), $config);
$voter = new TimesheetVoter($this->getRolePermissionManager(), new LockdownService($config));
self::assertInstanceOf(Voter::class, $voter);
return $voter;