Files
kimai/tests/Export/Base/CsvRendererTest.php
Kevin Papst 677f8d776c [3.0] Update Symfony to 7.4 and dependencies, removed deprecated code, removed API password (#5622)
* deactivate deprecated auto-mapping
* use native month-picker with fallback on old browser
* bump dependencies to new major versions
* replace deprecated ARRAY column type with JSON
* named arguments
* nullable arguments
* native lazy ghosts
* update routes to use php config
* disable PHP < 8.4
* use static RTL configuration
* new qr code package
* clear out env file
* recipe updates
* fix deprecations
* preset new env variable if not existing
* bump maria db version examples
* remove deprecated api-token support
* fix validator deprecations
* remove timesheet category column
* fix broken validator autoconfiguration
* stabilize tests
* fix migration syntax compatibility
* fix doctrine deprecation
* fix datetime format
* remove constructor dependency
* only include visible preferences in invoice templates
* fix test container dependency
* removed ProjectConstraint multi-constraint logic
* allow to disable plugins in certain environments
* refactor TimesheetConstraint to non-service
* activate new interface methods
* prevent update issues with .env
* improved customer fixture
* removed $user->isExportDecimal()
2026-01-04 15:04:25 +01:00

203 lines
6.9 KiB
PHP

<?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\Export\Base;
use App\Export\Base\AbstractSpreadsheetRenderer;
use App\Export\Base\CsvRenderer;
use App\Export\ColumnConverter;
use App\Export\DefaultTemplate;
use App\Export\Package\SpoutSpreadsheet;
use App\Tests\Export\Renderer\AbstractRendererTestCase;
use App\Tests\Mocks\MetaFieldColumnSubscriberMock;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Contracts\Translation\TranslatorInterface;
#[CoversClass(AbstractSpreadsheetRenderer::class)]
#[CoversClass(SpoutSpreadsheet::class)]
#[CoversClass(CsvRenderer::class)]
#[Group('integration')]
class CsvRendererTest extends AbstractRendererTestCase
{
protected function getAbstractRenderer(?string $locale): CsvRenderer
{
$security = $this->createMock(Security::class);
$security->expects($this->any())->method('getUser')->willReturn(null);
$security->expects($this->any())->method('isGranted')->willReturn(true);
$translator = $this->getContainer()->get(TranslatorInterface::class);
self::assertInstanceOf(TranslatorInterface::class, $translator);
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new MetaFieldColumnSubscriberMock());
$converter = new ColumnConverter($dispatcher, $security);
$template = new DefaultTemplate($dispatcher, 'csv', $locale);
return new CsvRenderer($converter, $translator, $template);
}
public function testConfigurationFromTemplate(): void
{
$sut = $this->getAbstractRenderer('en');
self::assertEquals('csv', $sut->getType());
self::assertEquals('csv', $sut->getId());
self::assertEquals('default', $sut->getTitle());
self::assertFalse($sut->isInternal());
$sut->setInternal(true);
self::assertTrue($sut->isInternal());
}
public static function getTestModel(): array
{
$en = [
'Date', 'From', 'To', 'Duration', 'Currency', 'Price', 'Internal price', 'Hourly price', 'Fixed price', 'Name',
'User', 'E-mail', 'Staff number', 'Customer', 'Project', 'Activity', 'Description', 'Billable', 'Tags',
'Type', 'category', 'Account', 'Project number', 'VAT-ID', 'Order number',
'Working place', 'Working place', 'Working place', 'Working place', 'Working place', 'Working place', 'mypref',
];
$de = [
'Datum', 'Von', 'Bis', 'Dauer', 'Währung', 'Preis', 'Interner Preis', 'Preis pro Stunde', 'Festpreis', 'Name',
'Benutzer', 'E-Mail', 'Personalnummer', 'Kunde', 'Projekt', 'Tätigkeit', 'Beschreibung', 'Abrechenbar', 'Schlagworte',
'Typ', 'category', 'Kundennummer', 'Projektnummer', 'Umsatzsteuer-ID', 'Bestellnummer',
'Working place', 'Working place', 'Working place', 'Working place', 'Working place', 'Working place', 'mypref',
];
return [
['1947.99', 7, 6, 1, 2, 2, null, $en],
['1947.99', 7, 6, 1, 2, 2, 'de', $de]
];
}
#[DataProvider('getTestModel')]
public function testRender(string $expectedRate, int $expectedRows, int $expectedDescriptions, int $expectedUser1, int $expectedUser2, int $expectedUser3, ?string $locale, array $header): void
{
$sut = $this->getAbstractRenderer($locale);
$response = $this->render($sut);
self::assertInstanceOf(BinaryFileResponse::class, $response);
$file = $response->getFile();
$prefix = date('Ymd');
self::assertEquals('text/csv', $response->headers->get('Content-Type'));
self::assertEquals('attachment; filename=' . $prefix . '-Customer_Name-project_name.csv', $response->headers->get('Content-Disposition'));
self::assertTrue(file_exists($file->getRealPath()));
$content = file_get_contents($file->getRealPath());
self::assertIsString($content);
self::assertStringContainsString($expectedRate, $content);
self::assertEquals($expectedRows, substr_count($content, PHP_EOL));
self::assertEquals($expectedDescriptions, substr_count($content, '"activity description"'));
self::assertEquals($expectedUser1, substr_count($content, ',kevin,'));
self::assertEquals($expectedUser3, substr_count($content, ',hello-world,'));
self::assertEquals($expectedUser2, substr_count($content, ',foo-bar,'));
ob_start();
$response->sendContent();
$content2 = ob_get_clean();
self::assertIsString($content2);
self::assertEquals($content, $content2);
self::assertFalse(file_exists($file->getRealPath()));
$all = [];
$rows = array_filter(explode(PHP_EOL, $content2), function (string $line) { return $line !== ''; });
foreach ($rows as $row) {
self::assertIsString($row);
$all[] = str_getcsv($row, ',', '"', '\\');
}
self::assertEquals($header, $all[0]);
$expected = [
'2019-06-16',
'12:00',
'12:06',
'0.11',
'EUR',
'0',
'0',
'0',
'84',
'Kevin',
'kevin',
'',
'',
'Customer Name',
'project name',
'activity description',
'',
'1',
'foo, bar',
'timesheet',
'work',
'A-0123456789',
'',
'DE-9876543210',
'ORDER-123',
'meta-bar',
'meta-bar2',
'customer-bar',
'',
'project-foo2',
'activity-bar',
'',
];
$expected2 = [
'2019-06-16',
'12:00',
'12:06',
'0.11',
'EUR',
'0',
'0',
'0',
'-100.92',
'niveK',
'nivek',
'',
'',
'Customer Name',
'project name',
'activity description',
'',
'1',
'',
'timesheet',
'work',
'A-0123456789',
'',
'DE-9876543210',
'ORDER-123',
'',
'',
'customer-bar',
'',
'project-foo2',
'activity-bar',
'',
];
self::assertEquals(7, \count($all));
self::assertEquals($expected, $all[5]);
self::assertEquals($expected2, $all[6]);
self::assertEquals(\count($expected), \count($all[0]));
self::assertEquals('foo', $all[4][18]);
}
}