From 5c227888f20ca81352ba1e44d724b0932ebed3cb Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Sun, 12 May 2019 03:26:31 +0200 Subject: [PATCH] delete timesheets via api (#776) --- src/API/TimesheetController.php | 4 +- src/Controller/TimesheetController.php | 46 +------------------ src/Controller/TimesheetTeamController.php | 43 +---------------- templates/macros/actions.html.twig | 4 +- templates/timesheet-team/delete.html.twig | 17 ------- templates/timesheet/delete.html.twig | 17 ------- tests/Controller/TimesheetControllerTest.php | 30 ------------ .../TimesheetTeamControllerTest.php | 42 +++++++++++------ 8 files changed, 34 insertions(+), 169 deletions(-) delete mode 100644 templates/timesheet-team/delete.html.twig delete mode 100644 templates/timesheet/delete.html.twig diff --git a/src/API/TimesheetController.php b/src/API/TimesheetController.php index 0aa5be7f..46fac58b 100644 --- a/src/API/TimesheetController.php +++ b/src/API/TimesheetController.php @@ -401,9 +401,7 @@ class TimesheetController extends BaseApiController throw $this->createAccessDeniedException('You are not allowed to delete this timesheet'); } - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->remove($timesheet); - $entityManager->flush(); + $this->repository->delete($timesheet); $view = new View(null, Response::HTTP_NO_CONTENT); diff --git a/src/Controller/TimesheetController.php b/src/Controller/TimesheetController.php index acf504ee..2292bc2a 100644 --- a/src/Controller/TimesheetController.php +++ b/src/Controller/TimesheetController.php @@ -17,7 +17,6 @@ use App\Repository\ActivityRepository; use App\Repository\ProjectRepository; use App\Repository\Query\TimesheetQuery; use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\ORM\ORMException; use Pagerfanta\Pagerfanta; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\HttpFoundation\Request; @@ -193,9 +192,7 @@ class TimesheetController extends AbstractController { $route = 'timesheet'; - if (null !== $request->get('page')) { - $route = 'timesheet_paginated'; - } elseif ('calendar' === $request->get('origin')) { + if ('calendar' === $request->get('origin')) { $route = 'calendar'; } @@ -221,47 +218,6 @@ class TimesheetController extends AbstractController return $this->create($request, $route, 'timesheet/edit.html.twig', $projectRepository, $activityRepository); } - /** - * @Route(path="/{id}/delete", name="timesheet_delete", methods={"GET", "POST"}) - * @Security("is_granted('delete', entry)") - * - * @param Timesheet $entry - * @param Request $request - * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response - * @throws \Exception - */ - public function deleteAction(Timesheet $entry, Request $request) - { - $deleteForm = $this->createFormBuilder(null, [ - 'attr' => [ - 'data-form-event' => 'kimai.timesheetUpdate kimai.timesheetDelete', - 'data-msg-success' => 'action.delete.success', - 'data-msg-error' => 'action.delete.error', - ], - ]) - ->setAction($this->generateUrl('timesheet_delete', ['id' => $entry->getId()])) - ->setMethod('POST') - ->getForm(); - - $deleteForm->handleRequest($request); - - if ($deleteForm->isSubmitted() && $deleteForm->isValid()) { - try { - $this->getRepository()->delete($entry); - $this->flashSuccess('action.delete.success'); - } catch (ORMException $ex) { - $this->flashError('action.delete.error', ['%reason%' => $ex->getMessage()]); - } - - return $this->redirectToRoute('timesheet'); - } - - return $this->render('timesheet/delete.html.twig', [ - 'timesheet' => $entry, - 'form' => $deleteForm->createView(), - ]); - } - /** * @param Timesheet $entry * @param string $redirectRoute diff --git a/src/Controller/TimesheetTeamController.php b/src/Controller/TimesheetTeamController.php index 0b243afb..4b8a0d2e 100644 --- a/src/Controller/TimesheetTeamController.php +++ b/src/Controller/TimesheetTeamController.php @@ -17,7 +17,6 @@ use App\Repository\ActivityRepository; use App\Repository\ProjectRepository; use App\Repository\Query\TimesheetQuery; use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\ORM\ORMException; use Pagerfanta\Pagerfanta; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\HttpFoundation\Request; @@ -128,7 +127,7 @@ class TimesheetTeamController extends AbstractController */ public function editAction(Timesheet $entry, Request $request) { - return $this->edit($entry, $request, 'admin_timesheet_paginated', 'timesheet-team/edit.html.twig'); + return $this->edit($entry, $request, 'admin_timesheet', 'timesheet-team/edit.html.twig'); } /** @@ -145,46 +144,6 @@ class TimesheetTeamController extends AbstractController return $this->create($request, 'admin_timesheet', 'timesheet-team/edit.html.twig', $projectRepository, $activityRepository); } - /** - * @Route(path="/{id}/delete", name="admin_timesheet_delete", methods={"GET", "POST"}) - * @Security("is_granted('delete', entry)") - * - * @param Timesheet $entry - * @param Request $request - * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response - */ - public function deleteAction(Timesheet $entry, Request $request) - { - $deleteForm = $this->createFormBuilder(null, [ - 'attr' => [ - 'data-form-event' => 'kimai.timesheetUpdate kimai.timesheetDelete', - 'data-msg-success' => 'action.delete.success', - 'data-msg-error' => 'action.delete.error', - ] - ]) - ->setAction($this->generateUrl('admin_timesheet_delete', ['id' => $entry->getId()])) - ->setMethod('POST') - ->getForm(); - - $deleteForm->handleRequest($request); - - if ($deleteForm->isSubmitted() && $deleteForm->isValid()) { - try { - $this->getRepository()->delete($entry); - $this->flashSuccess('action.delete.success'); - } catch (ORMException $ex) { - $this->flashError('action.delete.error', ['%reason%' => $ex->getMessage()]); - } - - return $this->redirectToRoute('admin_timesheet'); - } - - return $this->render('timesheet-team/delete.html.twig', [ - 'timesheet' => $entry, - 'form' => $deleteForm->createView(), - ]); - } - /** * @param Timesheet $entry * @param string $redirectRoute diff --git a/templates/macros/actions.html.twig b/templates/macros/actions.html.twig index c7f4a6a4..fa1941e7 100644 --- a/templates/macros/actions.html.twig +++ b/templates/macros/actions.html.twig @@ -231,7 +231,7 @@ {% endif %} {% if view == 'index' and is_granted('delete', timesheet) %} - {% set actions = actions|merge({'trash': {'url': path('timesheet_delete', {'id' : timesheet.id}), 'class': 'modal-ajax-form'}}) %} + {% set actions = actions|merge({'trash': {'url': path('delete_timesheet', {'id' : timesheet.id}), 'class': 'api-link', 'attr': {'data-event': 'kimai.timesheetDelete kimai.timesheetUpdate', 'data-method': 'DELETE', 'data-question': 'confirm.delete', 'data-msg-error': 'action.delete.error', 'data-msg-success': 'action.delete.success'}}}) %} {% endif %} {% endif %} @@ -286,7 +286,7 @@ {% endif %} {% if view == 'index' and is_granted('delete', timesheet) %} - {% set actions = actions|merge({'trash': {'url': path('admin_timesheet_delete', {'id' : timesheet.id}), 'class': 'modal-ajax-form'}}) %} + {% set actions = actions|merge({'trash': {'url': path('delete_timesheet', {'id' : timesheet.id}), 'class': 'api-link', 'attr': {'data-event': 'kimai.timesheetDelete kimai.timesheetUpdate', 'data-method': 'DELETE', 'data-question': 'confirm.delete', 'data-msg-error': 'action.delete.error', 'data-msg-success': 'action.delete.success'}}}) %} {% endif %} {% endif %} diff --git a/templates/timesheet-team/delete.html.twig b/templates/timesheet-team/delete.html.twig deleted file mode 100644 index fc36929a..00000000 --- a/templates/timesheet-team/delete.html.twig +++ /dev/null @@ -1,17 +0,0 @@ -{% extends app.request.xmlHttpRequest ? 'form.html.twig' : 'base.html.twig' %} -{% import "macros/actions.html.twig" as actions %} - -{% block page_title %}{{ 'admin_timesheet.title'|trans }}{% endblock %} -{% block page_subtitle %}{{ 'admin_timesheet.subtitle'|trans }}{% endblock %} -{% block page_actions %}{{ actions.timesheet_team(timesheet, 'delete') }}{% endblock %} - -{% block main %} - - {{ include(app.request.xmlHttpRequest ? 'default/_form_delete_modal.html.twig' : 'default/_form_delete.html.twig', { - 'message': "delete.not_in_use"|trans|raw, - 'form': form, - 'used': false, - 'back': path('admin_timesheet') - }) }} - -{% endblock %} diff --git a/templates/timesheet/delete.html.twig b/templates/timesheet/delete.html.twig deleted file mode 100644 index e546fe61..00000000 --- a/templates/timesheet/delete.html.twig +++ /dev/null @@ -1,17 +0,0 @@ -{% extends app.request.xmlHttpRequest ? 'form.html.twig' : 'base.html.twig' %} -{% import "macros/actions.html.twig" as actions %} - -{% block page_title %}{{ 'timesheet.title'|trans }}{% endblock %} -{% block page_subtitle %}{{ 'timesheet.subtitle'|trans }}{% endblock %} -{% block page_actions %}{{ actions.timesheet(timesheet, 'delete') }}{% endblock %} - -{% block main %} - - {{ include(app.request.xmlHttpRequest ? 'default/_form_delete_modal.html.twig' : 'default/_form_delete.html.twig', { - 'message': "delete.not_in_use"|trans|raw, - 'form': form, - 'used': false, - 'back': path('timesheet') - }) }} - -{% endblock %} diff --git a/tests/Controller/TimesheetControllerTest.php b/tests/Controller/TimesheetControllerTest.php index 2a28affb..2a03f5c1 100644 --- a/tests/Controller/TimesheetControllerTest.php +++ b/tests/Controller/TimesheetControllerTest.php @@ -143,36 +143,6 @@ class TimesheetControllerTest extends ControllerBaseTest $this->assertNull($timesheet->getFixedRate()); } - public function testDeleteAction() - { - $client = $this->getClientForAuthenticatedUser(User::ROLE_USER); - - $em = $client->getContainer()->get('doctrine.orm.entity_manager'); - $fixture = new TimesheetFixtures(); - $fixture->setAmount(10); - $fixture->setUser($this->getUserByRole($em, User::ROLE_USER)); - $fixture->setStartDate('2017-05-01'); - $this->importFixture($em, $fixture); - - $this->request($client, '/timesheet/1/edit'); - $this->assertTrue($client->getResponse()->isSuccessful()); - - $this->request($client, '/timesheet/1/delete'); - - $this->assertTrue($client->getResponse()->isSuccessful()); - $form = $client->getCrawler()->filter('form[name=form]')->form(); - $this->assertStringEndsWith($this->createUrl('/timesheet/1/delete'), $form->getUri()); - $client->submit($form); - - $client->followRedirect(); - $this->assertTrue($client->getResponse()->isSuccessful()); - $this->assertHasFlashDeleteSuccess($client); - $this->assertHasDataTable($client); - - $this->request($client, '/timesheet/1/edit'); - $this->assertFalse($client->getResponse()->isSuccessful()); - } - public function testStartAction() { $client = $this->getClientForAuthenticatedUser(User::ROLE_USER); diff --git a/tests/Controller/TimesheetTeamControllerTest.php b/tests/Controller/TimesheetTeamControllerTest.php index d499c06e..256da66e 100644 --- a/tests/Controller/TimesheetTeamControllerTest.php +++ b/tests/Controller/TimesheetTeamControllerTest.php @@ -146,33 +146,49 @@ class TimesheetTeamControllerTest extends ControllerBaseTest $this->assertNull($timesheet->getFixedRate()); } - public function testDeleteAction() + public function testEditAction() { - $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); + $client = $this->getClientForAuthenticatedUser(); $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + $user = $this->getUserByRole($em, User::ROLE_USER); + $teamlead = $this->getUserByRole($em, User::ROLE_TEAMLEAD); $fixture = new TimesheetFixtures(); $fixture->setAmount(10); - $fixture->setUser($this->getUserByRole($em, User::ROLE_USER)); + $fixture->setUser($user); $fixture->setStartDate('2017-05-01'); $this->importFixture($em, $fixture); + $client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD); $this->request($client, '/team/timesheet/1/edit'); - $this->assertTrue($client->getResponse()->isSuccessful()); - $this->request($client, '/team/timesheet/1/delete'); + $response = $client->getResponse(); + $this->assertTrue($response->isSuccessful()); - $this->assertTrue($client->getResponse()->isSuccessful()); - $form = $client->getCrawler()->filter('form[name=form]')->form(); - $this->assertStringEndsWith($this->createUrl('/team/timesheet/1/delete'), $form->getUri()); - $client->submit($form); + $this->assertContains( + 'href="https://www.kimai.org/documentation/timesheet.html"', + $response->getContent(), + 'Could not find link to documentation' + ); + $form = $client->getCrawler()->filter('form[name=timesheet_edit_form]')->form(); + $client->submit($form, [ + 'timesheet_edit_form' => [ + 'description' => 'foo-bar', + 'tags' => 'foo,bar, testing, hello world,,', + 'user' => $teamlead->getId() + ] + ]); + + $this->assertIsRedirect($client, $this->createUrl('/team/timesheet/')); $client->followRedirect(); $this->assertTrue($client->getResponse()->isSuccessful()); - $this->assertHasFlashDeleteSuccess($client); - $this->assertHasDataTable($client); + $this->assertHasFlashSaveSuccess($client); - $this->request($client, '/team/timesheet/1/edit'); - $this->assertFalse($client->getResponse()->isSuccessful()); + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + /** @var Timesheet $timesheet */ + $timesheet = $em->getRepository(Timesheet::class)->find(1); + $this->assertEquals('foo-bar', $timesheet->getDescription()); + $this->assertEquals($teamlead->getId(), $timesheet->getUser()->getId()); } }