new user-year reporting and data-type chooser (#3155)

This commit is contained in:
Kevin Papst
2022-02-16 12:18:04 +01:00
committed by GitHub
parent 1852d71974
commit 85a63c4b78
58 changed files with 1862 additions and 683 deletions

View File

@@ -0,0 +1,75 @@
<?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\Reporting;
use App\Entity\User;
use App\Tests\Controller\ControllerBaseTest;
use App\Tests\DataFixtures\TimesheetFixtures;
/**
* @group integration
*/
abstract class AbstractUserPeriodControllerTest extends ControllerBaseTest
{
protected function importReportingFixture(string $role)
{
$fixture = new TimesheetFixtures();
$fixture->setAmount(50);
$fixture->setAmountRunning(10);
$fixture->setUser($this->getUserByRole($role));
$fixture->setStartDate(new \DateTime());
$this->importFixture($fixture);
}
abstract protected function getReportUrl(): string;
abstract protected function getBoxId(): string;
public function testIsSecure()
{
$this->assertUrlIsSecured($this->getReportUrl());
}
public function getTestData(): array
{
return [
[4, 'duration', 'Working hours total'],
[4, 'rate', 'Total revenue'],
[4, 'internalRate', 'Internal rate'],
];
}
/**
* @dataProvider getTestData
*/
public function testUserPeriodReport(int $user, string $dataType, string $title)
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->importReportingFixture(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, sprintf('%s?user=%s&date=12999119191&sumType=%s', $this->getReportUrl(), $user, $dataType));
self::assertStringContainsString(sprintf('<div class="box-body %s', $this->getBoxId()), $client->getResponse()->getContent());
$option = $client->getCrawler()->filterXPath("//select[@id='user']/option[@selected]");
self::assertEquals($user, $option->attr('value'));
$cell = $client->getCrawler()->filterXPath("//th[contains(@class, 'reportDataTypeTitle')]");
self::assertEquals($title, $cell->text());
}
public function testUserPeriodReportAsTeamlead()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importReportingFixture(User::ROLE_USER);
$this->assertAccessIsGranted($client, sprintf('%s?date=12999119191', $this->getReportUrl()));
self::assertStringContainsString(sprintf('<div class="box-body %s', $this->getBoxId()), $client->getResponse()->getContent());
$select = $client->getCrawler()->filterXPath("//select[@id='user']");
self::assertEquals(0, $select->count());
$cell = $client->getCrawler()->filterXPath("//th[contains(@class, 'reportDataTypeTitle')]");
self::assertEquals('Working hours total', $cell->text());
}
}

View File

@@ -0,0 +1,95 @@
<?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\Reporting;
use App\Entity\User;
use App\Tests\Controller\ControllerBaseTest;
use App\Tests\DataFixtures\TimesheetFixtures;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* @group integration
*/
abstract class AbstractUsersPeriodControllerTest extends ControllerBaseTest
{
protected function importReportingFixture(string $role)
{
$fixture = new TimesheetFixtures();
$fixture->setAmount(50);
$fixture->setAmountRunning(10);
$fixture->setUser($this->getUserByRole($role));
$fixture->setStartDate(new \DateTime());
$this->importFixture($fixture);
}
abstract protected function getReportUrl(): string;
abstract protected function getReportExportUrl(): string;
abstract protected function getBoxId(): string;
public function testIsSecure()
{
$this->assertUrlIsSecured($this->getReportUrl());
}
public function getTestData(): array
{
return [
['duration', 'Working hours total'],
['rate', 'Total revenue'],
['internalRate', 'Internal rate'],
];
}
/**
* @dataProvider getTestData
*/
public function testUsersPeriodReport(string $dataType, string $title)
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->importReportingFixture(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, sprintf('%s?date=12999119191&sumType=%s', $this->getReportUrl(), $dataType));
self::assertStringContainsString(sprintf('<div class="box-body %s', $this->getBoxId()), $client->getResponse()->getContent());
$cell = $client->getCrawler()->filterXPath("//th[contains(@class, 'reportDataTypeTitle')]");
self::assertEquals($title, $cell->text());
}
/**
* @dataProvider getTestData
*/
public function testUsersPeriodReportAsTeamlead(string $dataType, string $title)
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importReportingFixture(User::ROLE_TEAMLEAD);
$this->assertAccessIsGranted($client, sprintf('%s?date=12999119191&sumType=%s', $this->getReportUrl(), $dataType));
self::assertStringContainsString(sprintf('<div class="box-body %s', $this->getBoxId()), $client->getResponse()->getContent());
$select = $client->getCrawler()->filterXPath("//select[@id='user']");
self::assertEquals(0, $select->count());
$cell = $client->getCrawler()->filterXPath("//th[contains(@class, 'reportDataTypeTitle')]");
self::assertEquals($title, $cell->text());
}
/**
* @dataProvider getTestData
*/
public function testUsersPeriodReportExport(string $dataType, string $title)
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->importReportingFixture(User::ROLE_SUPER_ADMIN);
$this->request($client, sprintf('%s?date=12999119191&sumType=%s', $this->getReportExportUrl(), $dataType));
$response = $client->getResponse();
$this->assertTrue($response->isSuccessful());
self::assertInstanceOf(BinaryFileResponse::class, $response);
self::assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $response->headers->get('Content-Type'));
self::assertStringContainsString('attachment; filename=kimai-export-users-', $response->headers->get('Content-Disposition'));
self::assertStringContainsString('.xlsx', $response->headers->get('Content-Disposition'));
}
}

View File

@@ -1,60 +0,0 @@
<?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\Reporting;
use App\Entity\User;
use App\Tests\Controller\ControllerBaseTest;
use App\Tests\DataFixtures\TimesheetFixtures;
/**
* @group integration
*/
class ReportByUserControllerTest extends ControllerBaseTest
{
protected function importReportingFixture(string $role)
{
$fixture = new TimesheetFixtures();
$fixture->setAmount(50);
$fixture->setAmountRunning(10);
$fixture->setUser($this->getUserByRole($role));
$fixture->setStartDate(new \DateTime());
$this->importFixture($fixture);
}
public function testWeekByUserIsSecure()
{
$this->assertUrlIsSecured('/reporting/week_by_user');
}
public function testMonthByUserIsSecure()
{
$this->assertUrlIsSecured('/reporting/month_by_user');
}
public function testUserWeekReport()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->importReportingFixture(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, '/reporting/week_by_user?user=4&date=12999119191');
self::assertStringContainsString('<div class="box-body user-week-reporting-box', $client->getResponse()->getContent());
$option = $client->getCrawler()->filterXPath("//select[@id='user']/option[@selected]");
self::assertEquals(4, $option->attr('value'));
}
public function testUserMonthReport()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->importReportingFixture(User::ROLE_USER);
$this->assertAccessIsGranted($client, '/reporting/month_by_user?user=4&date=12999119191');
self::assertStringContainsString('<div class="box-body user-month-reporting-box', $client->getResponse()->getContent());
$select = $client->getCrawler()->filterXPath("//select[@id='user']");
self::assertEquals(0, $select->count());
}
}

View File

@@ -1,90 +0,0 @@
<?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\Reporting;
use App\Entity\User;
use App\Tests\Controller\ControllerBaseTest;
use App\Tests\DataFixtures\TimesheetFixtures;
/**
* @group integration
*/
class ReportUsersListControllerTest extends ControllerBaseTest
{
protected function importReportingFixture(string $role)
{
$fixture = new TimesheetFixtures();
$fixture->setAmount(50);
$fixture->setAmountRunning(10);
$fixture->setUser($this->getUserByRole($role));
$fixture->setStartDate(new \DateTime());
$this->importFixture($fixture);
}
public function testYearlyListIsSecure()
{
$this->assertUrlIsSecured('/reporting/yearly_users_list');
}
public function testWeeklyListIsSecure()
{
$this->assertUrlIsSecured('/reporting/weekly_users_list');
}
public function testMonthlyListIsSecure()
{
$this->assertUrlIsSecured('/reporting/monthly_users_list');
}
public function testYearlyUsersListIsSecureForUserRole()
{
$this->assertUrlIsSecuredForRole(User::ROLE_USER, '/reporting/yearly_users_list');
}
public function testWeeklyUsersListIsSecureForUserRole()
{
$this->assertUrlIsSecuredForRole(User::ROLE_USER, '/reporting/weekly_users_list');
}
public function testMonthlyUsersListIsSecureForUserRole()
{
$this->assertUrlIsSecuredForRole(User::ROLE_USER, '/reporting/monthly_users_list');
}
public function testYearlyUsersReport()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importReportingFixture(User::ROLE_TEAMLEAD);
$this->assertAccessIsGranted($client, '/reporting/yearly_users_list');
self::assertStringContainsString('<div class="box-body yearly-user-list-reporting-box', $client->getResponse()->getContent());
$select = $client->getCrawler()->filterXPath("//select[@id='user']");
self::assertEquals(0, $select->count());
}
public function testWeeklyUsersReport()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importReportingFixture(User::ROLE_TEAMLEAD);
$this->assertAccessIsGranted($client, '/reporting/weekly_users_list');
self::assertStringContainsString('<div class="box-body weekly-user-list-reporting-box', $client->getResponse()->getContent());
$select = $client->getCrawler()->filterXPath("//select[@id='user']");
self::assertEquals(0, $select->count());
}
public function testMonthlyUsersReport()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importReportingFixture(User::ROLE_TEAMLEAD);
$this->assertAccessIsGranted($client, '/reporting/monthly_users_list');
self::assertStringContainsString('<div class="box-body monthly-user-list-reporting-box', $client->getResponse()->getContent());
$select = $client->getCrawler()->filterXPath("//select[@id='user']");
self::assertEquals(0, $select->count());
}
}

View File

@@ -0,0 +1,31 @@
<?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\Reporting;
/**
* @group integration
*/
class ReportUsersMonthControllerTest extends AbstractUsersPeriodControllerTest
{
protected function getReportUrl(): string
{
return '/reporting/users/month';
}
protected function getReportExportUrl(): string
{
return '/reporting/users/month_export';
}
protected function getBoxId(): string
{
return 'monthly-user-list-reporting-box';
}
}

View File

@@ -0,0 +1,31 @@
<?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\Reporting;
/**
* @group integration
*/
class ReportUsersWeekControllerTest extends AbstractUsersPeriodControllerTest
{
protected function getReportUrl(): string
{
return '/reporting/users/week';
}
protected function getReportExportUrl(): string
{
return '/reporting/users/week_export';
}
protected function getBoxId(): string
{
return 'weekly-user-list-reporting-box';
}
}

View File

@@ -0,0 +1,31 @@
<?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\Reporting;
/**
* @group integration
*/
class ReportUsersYearControllerTest extends AbstractUsersPeriodControllerTest
{
protected function getReportUrl(): string
{
return '/reporting/users/year';
}
protected function getReportExportUrl(): string
{
return '/reporting/users/year_export';
}
protected function getBoxId(): string
{
return 'yearly-user-list-reporting-box';
}
}

View File

@@ -0,0 +1,26 @@
<?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\Reporting;
/**
* @group integration
*/
class UserMonthControllerTest extends AbstractUserPeriodControllerTest
{
protected function getReportUrl(): string
{
return '/reporting/user/month';
}
protected function getBoxId(): string
{
return 'user-month-reporting-box';
}
}

View File

@@ -0,0 +1,26 @@
<?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\Reporting;
/**
* @group integration
*/
class UserWeekControllerTest extends AbstractUserPeriodControllerTest
{
protected function getReportUrl(): string
{
return '/reporting/user/week';
}
protected function getBoxId(): string
{
return 'user-week-reporting-box';
}
}

View File

@@ -0,0 +1,26 @@
<?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\Reporting;
/**
* @group integration
*/
class UserYearControllerTest extends AbstractUserPeriodControllerTest
{
protected function getReportUrl(): string
{
return '/reporting/user/year';
}
protected function getBoxId(): string
{
return 'user-year-reporting-box';
}
}

View File

@@ -25,7 +25,7 @@ class ReportingControllerTest extends ControllerBaseTest
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->request($client, '/reporting/');
$this->assertIsRedirect($client, $this->createUrl('/reporting/week_by_user'));
$this->assertIsRedirect($client, $this->createUrl('/reporting/user/week'));
$client->followRedirect();
self::assertStringContainsString('<div class="box-body user-week-reporting-box', $client->getResponse()->getContent());
}

View File

@@ -104,5 +104,10 @@ class MonthlyStatisticTest extends TestCase
self::assertNull($sut->getMonth('2019', '12'));
self::assertNull($sut->getMonth('2020', '1'));
self::assertNull($sut->getMonth('2020', '01'));
self::assertNull($sut->getMonthByDateTime(new \DateTime('2020-01-01')));
self::assertInstanceOf(StatisticDate::class, $sut->getMonthByDateTime(new \DateTime('2018-04-01')));
self::assertInstanceOf(StatisticDate::class, $sut->getByDateTime(new \DateTime('2018-04-01')));
self::assertSame($sut->getMonths(), $sut->getData());
}
}

View File

@@ -14,6 +14,7 @@ use App\Reporting\DateByUser;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Reporting\AbstractUserList
* @covers \App\Reporting\DateByUser
*/
abstract class AbstractDateByUserTest extends TestCase
@@ -25,6 +26,8 @@ abstract class AbstractDateByUserTest extends TestCase
$sut = $this->createSut();
self::assertNull($sut->getDate());
self::assertNull($sut->getUser());
self::assertEquals('duration', $sut->getSumType());
self::assertFalse($sut->isDecimal());
}
public function testSetter()
@@ -34,10 +37,32 @@ abstract class AbstractDateByUserTest extends TestCase
$user->setAlias('sdfsdfdsdf');
$sut = $this->createSut();
self::assertInstanceOf(DateByUser::class, $sut->setDate($date));
self::assertInstanceOf(DateByUser::class, $sut->setUser($user));
$sut->setDate($date);
$sut->setUser($user);
self::assertSame($date, $sut->getDate());
self::assertSame($user, $sut->getUser());
$sut->setSumType('rate');
self::assertEquals('rate', $sut->getSumType());
$sut->setSumType('internalRate');
self::assertEquals('internalRate', $sut->getSumType());
$sut->setSumType('duration');
self::assertEquals('duration', $sut->getSumType());
$sut->setDecimal(true);
self::assertTrue($sut->isDecimal());
$sut->setDecimal(false);
self::assertFalse($sut->isDecimal());
}
public function testInvalidSumType()
{
$this->expectException(\InvalidArgumentException::class);
$sut = $this->createSut();
$sut->setSumType('DURation');
}
}

View File

@@ -0,0 +1,61 @@
<?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\Reporting;
use App\Reporting\AbstractUserList;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Reporting\AbstractUserList
*/
abstract class AbstractUserListTest extends TestCase
{
abstract protected function createSut(): AbstractUserList;
public function testEmptyObject()
{
$sut = $this->createSut();
self::assertNull($sut->getDate());
self::assertEquals('duration', $sut->getSumType());
self::assertFalse($sut->isDecimal());
}
public function testSetter()
{
$date = new \DateTime('2019-05-27');
$sut = $this->createSut();
$sut->setDate($date);
self::assertSame($date, $sut->getDate());
$sut->setSumType('rate');
self::assertEquals('rate', $sut->getSumType());
$sut->setSumType('internalRate');
self::assertEquals('internalRate', $sut->getSumType());
$sut->setSumType('duration');
self::assertEquals('duration', $sut->getSumType());
$sut->setDecimal(true);
self::assertTrue($sut->isDecimal());
$sut->setDecimal(false);
self::assertFalse($sut->isDecimal());
}
public function testInvalidSumType()
{
$this->expectException(\InvalidArgumentException::class);
$sut = $this->createSut();
$sut->setSumType('DURation');
}
}

View File

@@ -0,0 +1,25 @@
<?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\Reporting;
use App\Reporting\AbstractUserList;
use App\Reporting\MonthlyUserList;
/**
* @covers \App\Reporting\MonthlyUserList
* @covers \App\Reporting\AbstractUserList
*/
class MonthlyUserListTest extends AbstractUserListTest
{
protected function createSut(): AbstractUserList
{
return new MonthlyUserList();
}
}

View File

@@ -47,6 +47,6 @@ class ReportingServiceTest extends TestCase
$sut = $this->getSut(true);
$reports = $sut->getAvailableReports(new User());
self::assertIsArray($reports);
self::assertCount(9, $reports);
self::assertCount(10, $reports);
}
}

View File

@@ -0,0 +1,25 @@
<?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\Reporting;
use App\Reporting\AbstractUserList;
use App\Reporting\WeeklyUserList;
/**
* @covers \App\Reporting\WeeklyUserList
* @covers \App\Reporting\AbstractUserList
*/
class WeeklyUserListTest extends AbstractUserListTest
{
protected function createSut(): AbstractUserList
{
return new WeeklyUserList();
}
}

View File

@@ -0,0 +1,25 @@
<?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\Reporting;
use App\Reporting\DateByUser;
use App\Reporting\YearByUser;
/**
* @covers \App\Reporting\YearByUser
* @covers \App\Reporting\DateByUser
*/
class YearByUserTest extends AbstractDateByUserTest
{
protected function createSut(): DateByUser
{
return new YearByUser();
}
}

View File

@@ -0,0 +1,25 @@
<?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\Reporting;
use App\Reporting\AbstractUserList;
use App\Reporting\YearlyUserList;
/**
* @covers \App\Reporting\YearlyUserList
* @covers \App\Reporting\AbstractUserList
*/
class YearlyUserListTest extends AbstractUserListTest
{
protected function createSut(): AbstractUserList
{
return new YearlyUserList();
}
}

View File

@@ -238,4 +238,25 @@ class DateTimeFactoryTest extends TestCase
self::assertEquals('01', $year->format('d'));
self::assertEquals('00:00:00', $year->format('H:i:s'));
}
public function testCreateEndOfYear()
{
$sut = $this->createDateTimeFactory(self::TEST_TIMEZONE);
$now = $sut->createDateTime();
$year = $sut->createEndOfYear();
self::assertEquals($now->format('Y'), $year->format('Y'));
self::assertEquals('12', $year->format('m'));
self::assertEquals('31', $year->format('d'));
self::assertEquals('23:59:59', $year->format('H:i:s'));
$now->setTime(23, 59, 59);
self::assertEquals($now->format('H:i:s'), $year->format('H:i:s'));
$begin = $sut->createDateTime('2017-12-31 23:59:59');
$year = $sut->createEndOfYear($begin);
self::assertEquals('2017', $year->format('Y'));
self::assertEquals('12', $year->format('m'));
self::assertEquals('31', $year->format('d'));
self::assertEquals('23:59:59', $year->format('H:i:s'));
}
}