restart via API allows to copy description (#782)

This commit is contained in:
Kevin Papst
2019-05-13 17:32:01 +02:00
committed by GitHub
parent 1f5710eaba
commit bbe1f9beda
28 changed files with 250 additions and 118 deletions

View File

@@ -198,6 +198,32 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
);
}
/**
* @param string $role
* @param string $url
* @param array $data
*/
protected function assertEntityNotFoundForDelete(string $role, string $url, array $data)
{
$client = $this->getClientForAuthenticatedUser($role);
$this->request($client, $url, 'DELETE', [], json_encode($data));
$response = $client->getResponse();
$this->assertFalse($response->isSuccessful());
$expected = [
'code' => 404,
'message' => 'Not found'
];
$this->assertEquals(404, $client->getResponse()->getStatusCode());
$this->assertEquals(
$expected,
json_decode($client->getResponse()->getContent(), true)
);
}
/**
* @param Response $response
* @param string $message

View File

@@ -86,4 +86,9 @@ class TagControllerTest extends APIControllerBaseTest
$this->assertEquals(9, count($result));
}
public function testDeleteActionWithUnknownTimesheet()
{
$this->assertEntityNotFoundForDelete(User::ROLE_ADMIN, '/api/tags/255', []);
}
}

View File

@@ -430,16 +430,15 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/' . $id);
}
public function testDeleteActionWithUnknownTimesheet()
{
$this->assertEntityNotFoundForDelete(User::ROLE_ADMIN, '/api/timesheets/255', []);
}
public function testDeleteActionForDifferentUser()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->assertAccessIsGranted($client, '/api/timesheets/1');
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertDefaultStructure($result);
$this->assertNotEmpty($result['id']);
$id = $result['id'];
$id = 1;
$this->request($client, '/api/timesheets/' . $id, 'DELETE');
$this->assertTrue($client->getResponse()->isSuccessful());
@@ -631,6 +630,91 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertDefaultStructure($result[0], false);
}
public function testRestartAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$data = [
'description' => 'foo',
'tags' => 'another,testing,bar'
];
$this->request($client, '/api/timesheets/1', 'PATCH', [], json_encode($data));
$this->request($client, '/api/timesheets/1/restart', 'PATCH');
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertDefaultStructure($result, true);
$this->assertEmpty($result['description']);
$this->assertEmpty($result['tags']);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find($result['id']);
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertNull($timesheet->getEnd());
$this->assertEquals(1, $timesheet->getActivity()->getId());
$this->assertEquals(1, $timesheet->getProject()->getId());
$this->assertEmpty($timesheet->getDescription());
$this->assertEmpty($timesheet->getTags());
}
public function testRestartActionWithCopyData()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$data = [
'description' => 'foo',
'tags' => 'another,testing,bar'
];
$this->request($client, '/api/timesheets/1', 'PATCH', [], json_encode($data));
$this->assertTrue($client->getResponse()->isSuccessful());
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$this->assertEquals('foo', $timesheet->getDescription());
$this->request($client, '/api/timesheets/1/restart', 'PATCH', ['copy' => 'all']);
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertDefaultStructure($result, true);
$this->assertEquals('foo', $result['description']);
$this->assertEquals(['another', 'testing', 'bar'], $result['tags']);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find($result['id']);
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertNull($timesheet->getEnd());
$this->assertEquals(1, $timesheet->getActivity()->getId());
$this->assertEquals(1, $timesheet->getProject()->getId());
$this->assertEquals('foo', $timesheet->getDescription());
$this->assertEquals(['another', 'testing', 'bar'], $timesheet->getTagsAsArray());
}
public function testRestartNotAllowedForUser()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$start = new \DateTime('-10 days');
$fixture = new TimesheetFixtures();
$fixture
->setFixedRate(true)
->setHourlyRate(true)
->setAmount(2)
->setUser($this->getUserByRole($em, User::ROLE_ADMIN))
->setStartDate($start)
->setAmountRunning(3)
;
$this->importFixture($em, $fixture);
$this->request($client, '/api/timesheets/12/restart', 'PATCH');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to re-start this timesheet');
}
protected function assertDefaultStructure(array $result, $full = true)
{
$expectedKeys = [

View File

@@ -143,32 +143,6 @@ class TimesheetControllerTest extends ControllerBaseTest
$this->assertNull($timesheet->getFixedRate());
}
public function testStartAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$fixture = new TimesheetFixtures();
$fixture->setUser($this->getUserByRole($em, User::ROLE_USER));
$fixture->setAmount(1);
$this->importFixture($em, $fixture);
$this->request($client, '/timesheet/start/1');
$this->assertIsRedirect($client, $this->createUrl('/timesheet/'));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client, 'Time recording was started');
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(2);
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertNull($timesheet->getEnd());
$this->assertEquals(1, $timesheet->getActivity()->getId());
$this->assertEquals(1, $timesheet->getProject()->getId());
}
public function testCreateActionDoesNotShowRateFieldsForUser()
{
$client = $this->getClientForAuthenticatedUser();