delete timesheets via api (#776)

This commit is contained in:
Kevin Papst
2019-05-12 03:26:31 +02:00
committed by GitHub
parent e29e183e84
commit 5c227888f2
8 changed files with 34 additions and 169 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -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

View File

@@ -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 %}

View File

@@ -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 %}

View File

@@ -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 %}

View File

@@ -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);

View File

@@ -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());
}
}