added export module (#538)

This commit is contained in:
Kevin Papst
2019-02-05 21:37:33 +01:00
committed by GitHub
parent 062735c9e3
commit 816866549c
104 changed files with 3770 additions and 608 deletions

View File

@@ -123,8 +123,8 @@ class ActivityControllerTest extends ControllerBaseTest
$this->request($client, '/admin/activity/1/delete');
$this->assertIsRedirect($client, $this->createUrl('/admin/activity/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
$this->request($client, '/admin/activity/1/edit');
$this->assertFalse($client->getResponse()->isSuccessful());
@@ -157,8 +157,8 @@ class ActivityControllerTest extends ControllerBaseTest
$this->assertIsRedirect($client, $this->createUrl('/admin/activity/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();

View File

@@ -130,8 +130,8 @@ class CustomerControllerTest extends ControllerBaseTest
$this->assertIsRedirect($client, $this->createUrl('/admin/customer/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();

View File

@@ -153,8 +153,8 @@ class ProjectControllerTest extends ControllerBaseTest
$this->assertIsRedirect($client, $this->createUrl('/admin/project/'));
$client->followRedirect();
$this->assertHasDataTable($client);
$this->assertHasFlashSuccess($client);
$this->assertHasFlashDeleteSuccess($client);
$this->assertHasNoEntriesWithFilter($client);
// SQLIte does not necessarly support onCascade delete, so these timesheet will stay after deletion
// $em->clear();

View File

@@ -31,7 +31,10 @@ class TimesheetControllerTest extends ControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->assertAccessIsGranted($client, '/team/timesheet/');
$this->assertHasDataTable($client);
$this->assertTrue($client->getResponse()->isSuccessful());
// there are no records by default in the test database
$this->assertHasNoEntriesWithFilter($client);
$result = $client->getCrawler()->filter('div.breadcrumb div.box-tools div.btn-group a.btn');
$this->assertEquals(4, count($result));
@@ -47,9 +50,10 @@ class TimesheetControllerTest extends ControllerBaseTest
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$user = $this->getUserByRole($em, User::ROLE_USER);
$fixture = new TimesheetFixtures();
$fixture->setAmount(10);
$fixture->setUser($this->getUserByRole($em, User::ROLE_USER));
$fixture->setUser($user);
$fixture->setStartDate(new \DateTime('-10 days'));
$this->importFixture($em, $fixture);
@@ -61,7 +65,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$form = $client->getCrawler()->filter('form.navbar-form')->form();
$client->submit($form, [
'state' => 1,
'user' => 1,
'user' => $user->getId(),
'pageSize' => 25,
'daterange' => $dateRange,
'customer' => null,
@@ -69,8 +73,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasDataTable($client);
// TODO more assertions
$this->assertDataTableRowCount($client, 'datatable_timesheet_admin', 10);
}
public function testExportAction()

View File

@@ -178,6 +178,17 @@ abstract class ControllerBaseTest extends WebTestCase
$this->assertContains('<table class="table table-striped table-hover dataTable" role="grid">', $client->getResponse()->getContent());
}
/**
* @param Client $client
* @param string $id
* @param int $count
*/
protected function assertDataTableRowCount(Client $client, string $id, int $count)
{
$node = $client->getCrawler()->filter('section.content div#' . $id . ' table.table-striped tbody tr');
$this->assertEquals($count, $node->count());
}
/**
* @param string $role the USER role to use for the request
* @param string $url the URL of the page displaying the initial form to submit
@@ -221,13 +232,32 @@ abstract class ControllerBaseTest extends WebTestCase
}
}
protected function assertHasNoEntriesWithFilter(Client $client)
{
$node = $client->getCrawler()->filter('div.callout.callout-warning.lead');
$this->assertContains('No entries were found based on your selected filters.', $node->text());
}
/**
* @param Client $client
* @param string|null $message
*/
protected function assertHasFlashSuccess(Client $client)
protected function assertHasFlashDeleteSuccess(Client $client)
{
$this->assertHasFlashSuccess($client, 'Entry was deleted successful');
}
/**
* @param Client $client
* @param string|null $message
*/
protected function assertHasFlashSuccess(Client $client, string $message = null)
{
$node = $client->getCrawler()->filter('div.alert.alert-success.alert-dismissible');
$this->assertNotEmpty($node->text());
if (null !== $message) {
$this->assertContains($message, $node->text());
}
}
/**

View File

@@ -0,0 +1,134 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Controller;
use App\Entity\User;
use App\Tests\DataFixtures\TimesheetFixtures;
/**
* @coversDefaultClass \App\Controller\ExportController
* @group integration
*/
class ExportControllerTest extends ControllerBaseTest
{
public function testIsSecure()
{
$this->assertUrlIsSecured('/export/');
$this->assertUrlIsSecuredForRole(User::ROLE_USER, '/export/');
}
public function testIndexActionHasErrorMessageOnEmptyQuery()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->request($client, '/export/');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasNoEntriesWithFilter($client);
}
public function testIndexActionWithEntries()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$begin = new \DateTime('first day of this month');
$end = new \DateTime('last day of this month');
$fixture = new TimesheetFixtures();
$fixture
->setUser($this->getUserByRole($em, User::ROLE_USER))
->setAmount(20)
->setStartDate($begin)
;
$this->importFixture($em, $fixture);
$this->request($client, '/export/');
$this->assertTrue($client->getResponse()->isSuccessful());
// make sure all existing records are displayed
$this->assertHasDataTable($client);
$this->assertDataTableRowCount($client, 'datatable_export', 20);
// assert export type buttons are available
$expected = ['csv', 'html', 'pdf', 'ods', 'xlsx'];
$node = $client->getCrawler()->filter('#export-buttons button');
$this->assertEquals(count($expected), $node->count());
foreach ($node->getIterator() as $button) {
$type = $button->getAttribute('data-type');
$this->assertContains($type, $expected);
}
}
public function testExportActionWithMissingRenderer()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->request($client, '/export/data');
$response = $client->getResponse();
$this->assertFalse($response->isSuccessful());
$this->assertEquals(404, $response->getStatusCode());
}
public function testExportActionWithInvalidRenderer()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->request($client, '/export/');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('#export-form')->form();
$form->getFormNode()->setAttribute('action', $this->createUrl('/export/data'));
$client->submit($form, [
'type' => 'default'
]);
$response = $client->getResponse();
$this->assertFalse($response->isSuccessful());
$this->assertEquals(404, $response->getStatusCode());
}
public function testExportAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$begin = new \DateTime('first day of this month');
$fixture = new TimesheetFixtures();
$fixture
->setUser($this->getUserByRole($em, User::ROLE_USER))
->setAmount(20)
->setStartDate($begin)
;
$this->importFixture($em, $fixture);
$this->request($client, '/export/');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('#export-form')->form();
$form->getFormNode()->setAttribute('action', $this->createUrl('/export/data'));
// don't add daterange to make sure the current month is the default range
$client->submit($form, [
'type' => 'html'
]);
$response = $client->getResponse();
$this->assertTrue($response->isSuccessful());
$node = $client->getCrawler()->filter('body');
$this->assertEquals(1, $node->count());
// poor mans assertions ;-)
$this->assertContains('export_print', $node->getIterator()[0]->getAttribute('class'));
$this->assertContains('<h2>List of expenses</h2>', $response->getContent());
$this->assertContains('<h3>Summary</h3>', $response->getContent());
$node = $client->getCrawler()->filter('section.export div#export-records table.dataTable tbody tr');
$this->assertEquals(20, $node->count());
}
}

View File

@@ -46,9 +46,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$this->request($client, '/invoice/');
$this->assertTrue($client->getResponse()->isSuccessful());
$node = $client->getCrawler()->filter('div.callout.callout-warning.lead');
$this->assertNotEmpty($node->text());
$this->assertContains('No invoice entries were found based on your selected filters.', $node->text());
$this->assertHasNoEntriesWithFilter($client);
}
public function testListTemplateAction()
@@ -150,6 +148,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
// no datatable should be displayed
$node = $client->getCrawler()->filter('div.callout.callout-warning.lead');
$this->assertEquals(0, $node->count());
@@ -157,8 +156,7 @@ class InvoiceControllerTest extends ControllerBaseTest
$this->assertNotEmpty($node->text());
$this->assertContains('This is a preview of the data that will show up in your invoice document.', $node->text());
$node = $client->getCrawler()->filter('section.invoice div.table-responsive table.table-striped tbody tr');
$this->assertEquals(20, $node->count());
$this->assertDataTableRowCount($client, 'datatable_invoice', 20);
$form = $client->getCrawler()->filter('#invoice-print-form')->form();
$form->getFormNode()->setAttribute('action', $this->createUrl('/invoice/print'));

View File

@@ -30,7 +30,9 @@ class TimesheetControllerTest extends ControllerBaseTest
$client = $this->getClientForAuthenticatedUser();
$this->request($client, '/timesheet/');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasDataTable($client);
// there are no records by default in the test database
$this->assertHasNoEntriesWithFilter($client);
$result = $client->getCrawler()->filter('div.breadcrumb div.box-tools div.btn-group a.btn');
$this->assertEquals(5, count($result));
@@ -43,7 +45,7 @@ class TimesheetControllerTest extends ControllerBaseTest
public function testIndexActionWithQuery()
{
$client = $this->getClientForAuthenticatedUser();
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$fixture = new TimesheetFixtures();
@@ -67,8 +69,7 @@ class TimesheetControllerTest extends ControllerBaseTest
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasDataTable($client);
// TODO more assertions
$this->assertDataTableRowCount($client, 'datatable_timesheet', 5);
}
public function testExportAction()