javascript and api to stop and display active records (#772)

This commit is contained in:
Kevin Papst
2019-05-10 13:45:09 +02:00
committed by GitHub
parent e619b5fd31
commit 09da7cd242
86 changed files with 918 additions and 449 deletions

View File

@@ -152,11 +152,12 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
/**
* @param string $role
* @param string $url
* @param string $method
*/
protected function assertEntityNotFound(string $role, string $url)
protected function assertEntityNotFound(string $role, string $url, string $method = 'GET')
{
$client = $this->getClientForAuthenticatedUser($role);
$this->request($client, $url);
$this->request($client, $url, $method);
$expected = [
'code' => 404,
@@ -197,6 +198,17 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
);
}
/**
* @param Response $response
* @param string $message
*/
protected function assertApiException(Response $response, string $message)
{
$this->assertFalse($response->isSuccessful());
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals(['code' => 500, 'message' => $message], json_decode($response->getContent(), true));
}
/**
* @param Client $client
* @param string $url
@@ -205,7 +217,16 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
protected function assertApiAccessDenied(Client $client, string $url, string $message)
{
$this->request($client, $url);
$response = $client->getResponse();
$this->assertApiResponseAccessDenied($client->getResponse(), $message);
}
/**
* @param Client $client
* @param string $url
* @param string $message
*/
protected function assertApiResponseAccessDenied(Response $response, string $message)
{
$this->assertFalse($response->isSuccessful());
$this->assertEquals(Response::HTTP_FORBIDDEN, $response->getStatusCode());
$expected = ['code' => Response::HTTP_FORBIDDEN, 'message' => $message];

View File

@@ -12,6 +12,7 @@ namespace App\Tests\API;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Tests\DataFixtures\TimesheetFixtures;
use Symfony\Component\HttpFoundation\Response;
@@ -495,6 +496,96 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertHasSubresources($result[0]);
}
public function testActiveAction()
{
$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(0)
->setUser($this->getUserByRole($em, User::ROLE_USER))
->setStartDate($start)
->setAmountRunning(3)
;
$this->importFixture($em, $fixture);
$this->request($client, '/api/timesheets/active');
$this->assertTrue($client->getResponse()->isSuccessful());
$results = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals(3, count($results));
foreach ($results as $timesheet) {
$this->assertDefaultStructure($timesheet, false);
}
}
public function testStopAction()
{
$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(0)
->setUser($this->getUserByRole($em, User::ROLE_USER))
->setStartDate($start)
->setAmountRunning(1)
;
$this->importFixture($em, $fixture);
$this->request($client, '/api/timesheets/11/stop', 'PATCH');
$this->assertTrue($client->getResponse()->isSuccessful());
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$this->assertInstanceOf(\DateTime::class, $timesheet->getEnd());
}
public function testStopActionFailsOnStoppedEntry()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->request($client, '/api/timesheets/1/stop', 'PATCH');
$this->assertApiException($client->getResponse(), 'Timesheet entry already stopped');
}
public function testStopThrowsNotFound()
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/11/stop', 'PATCH');
}
public function testStopNotAllowedForUser()
{
$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/stop', 'PATCH');
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to stop this timesheet');
}
protected function assertDefaultStructure(array $result, $full = true)
{
$expectedKeys = [

View File

@@ -188,7 +188,7 @@ abstract class ControllerBaseTest extends WebTestCase
*/
protected function assertHasDataTable(Client $client)
{
$this->assertContains('<table class="table table-striped table-hover dataTable" role="grid">', $client->getResponse()->getContent());
$this->assertContains('<table class="table table-striped table-hover dataTable" role="grid" data-reload-event="', $client->getResponse()->getContent());
}
/**
@@ -285,7 +285,7 @@ abstract class ControllerBaseTest extends WebTestCase
*/
protected function assertHasFlashDeleteSuccess(Client $client)
{
$this->assertHasFlashSuccess($client, 'Entry was deleted successful');
$this->assertHasFlashSuccess($client, 'Entry was deleted');
}
/**
@@ -294,7 +294,7 @@ abstract class ControllerBaseTest extends WebTestCase
*/
protected function assertHasFlashSaveSuccess(Client $client)
{
$this->assertHasFlashSuccess($client, 'Saved changes successful');
$this->assertHasFlashSuccess($client, 'Saved changes');
}
/**

View File

@@ -51,6 +51,7 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
return [
['#system_configuration_form_timesheet', $this->createUrl('/admin/system-config/timesheet')],
['#system_configuration_form_form_customer', $this->createUrl('/admin/system-config/customer')],
['#system_configuration_form_theme', $this->createUrl('/admin/system-config/theme')],
];
}

View File

@@ -210,77 +210,6 @@ class TimesheetControllerTest extends ControllerBaseTest
$this->assertFalse($form->has('fixedRate'));
}
public function testStopAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->request($client, '/timesheet/create');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=timesheet_edit_form]')->form();
$client->submit($form, [
'timesheet_edit_form' => [
'description' => 'Testing is fun!',
'fixedRate' => 100,
'project' => 1,
'activity' => 1,
]
]);
$this->assertIsRedirect($client, $this->createUrl('/timesheet/'));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$this->request($client, '/timesheet/1/stop');
$this->assertIsRedirect($client, $this->createUrl('/timesheet/'));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
/** @var Timesheet $timesheet */
$timesheet = $em->getRepository(Timesheet::class)->find(1);
$this->assertInstanceOf(\DateTime::class, $timesheet->getBegin());
$this->assertInstanceOf(\DateTime::class, $timesheet->getEnd());
$this->assertEquals(100, $timesheet->getRate());
$this->assertEquals(100, $timesheet->getFixedRate());
$this->assertNull($timesheet->getHourlyRate());
}
public function testStopActionFailsOnStoppedEntry()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$this->request($client, '/timesheet/create');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=timesheet_edit_form]')->form();
$client->submit($form, [
'timesheet_edit_form' => [
'description' => 'Testing is fun!',
'fixedRate' => 100,
'project' => 1,
'activity' => 1,
]
]);
$this->assertIsRedirect($client, $this->createUrl('/timesheet/'));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$this->request($client, '/timesheet/1/stop');
$this->assertIsRedirect($client, $this->createUrl('/timesheet/'));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$this->request($client, '/timesheet/1/stop');
$this->assertIsRedirect($client, $this->createUrl('/timesheet/'));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashError($client, 'Time recording could not be stopped: Timesheet entry already stopped');
}
public function testCreateActionWithFromAndToValues()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);