Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)
This commit is contained in:
@@ -32,7 +32,7 @@ class ColorTest extends TestCase
|
||||
$globalActivity->setColor('#000001');
|
||||
self::assertEquals('#000001', $sut->getColor($globalActivity));
|
||||
|
||||
$customer = new Customer();
|
||||
$customer = new Customer('foo');
|
||||
self::assertNull($sut->getColor($customer));
|
||||
|
||||
$customer->setColor('#000004');
|
||||
@@ -82,7 +82,7 @@ class ColorTest extends TestCase
|
||||
$timesheet = new Timesheet();
|
||||
$timesheet->setActivity(new Activity());
|
||||
$project = new Project();
|
||||
$customer = new Customer();
|
||||
$customer = new Customer('foo');
|
||||
$customer->setColor('#123456');
|
||||
$project->setCustomer($customer);
|
||||
$timesheet->setProject($project);
|
||||
|
||||
@@ -1,48 +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\Utils;
|
||||
|
||||
use App\Utils\CommandStyle;
|
||||
use App\Validator\ValidationFailedException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\BufferedOutput;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\CommandStyle
|
||||
*/
|
||||
class CommandStyleTest extends TestCase
|
||||
{
|
||||
public function testStyles()
|
||||
{
|
||||
$input = new ArrayInput([]);
|
||||
$output = new BufferedOutput();
|
||||
|
||||
$sut = new CommandStyle($input, $output);
|
||||
$sut->error('FooBar-Error');
|
||||
$sut->warning('Hello-Warning');
|
||||
$sut->success('World-Success');
|
||||
|
||||
$exception = new ValidationFailedException(new ConstraintViolationList([
|
||||
new ConstraintViolation('123', null, [], $this, null, 123),
|
||||
new ConstraintViolation('456', null, [], $this, null, 456),
|
||||
]));
|
||||
$sut->validationError($exception);
|
||||
|
||||
$result = $output->fetch();
|
||||
self::assertStringContainsString('[ERROR] FooBar-Error', $result);
|
||||
self::assertStringContainsString('[WARNING] Hello-Warning', $result);
|
||||
self::assertStringContainsString('[OK] World-Success', $result);
|
||||
self::assertStringContainsString('[ERROR] (123)', $result);
|
||||
self::assertStringContainsString('[ERROR] (456)', $result);
|
||||
}
|
||||
}
|
||||
@@ -22,20 +22,7 @@ class DurationTest extends TestCase
|
||||
$sut = new Duration();
|
||||
|
||||
$this->assertNull($sut->format(null));
|
||||
$this->assertEquals('02:38', $sut->format(9494));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testParseDurationStringSpecials()
|
||||
{
|
||||
$sut = new Duration();
|
||||
$this->assertEquals(-1, $sut->parseDuration('-1', Duration::FORMAT_SECONDS));
|
||||
$this->assertEquals(0, $sut->parseDuration('0', Duration::FORMAT_SECONDS));
|
||||
$this->assertEquals(3600, $sut->parseDuration('3600', Duration::FORMAT_SECONDS));
|
||||
$this->assertEquals(0, $sut->parseDuration('', Duration::FORMAT_SECONDS));
|
||||
$this->assertEquals(-12, $sut->parseDuration('-12', Duration::FORMAT_SECONDS));
|
||||
$this->assertEquals('2:38', $sut->format(9494));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
192
tests/Utils/FormFormatConverterTest.php
Normal file
192
tests/Utils/FormFormatConverterTest.php
Normal file
@@ -0,0 +1,192 @@
|
||||
<?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\Utils;
|
||||
|
||||
use App\Utils\FormFormatConverter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\FormFormatConverter
|
||||
*/
|
||||
class FormFormatConverterTest extends TestCase
|
||||
{
|
||||
public function testConvert()
|
||||
{
|
||||
$sut = new FormFormatConverter();
|
||||
|
||||
$this->assertEquals('dd.MM.yyyy', $sut->convert('dd.MM.yy'));
|
||||
$this->assertEquals('d.M.yyyy', $sut->convert('d.M.y'));
|
||||
$this->assertEquals('g:i A', $sut->convert('h:mm a'));
|
||||
$this->assertEquals('G:i', $sut->convert('H:mm'));
|
||||
$this->assertEquals('H.i', $sut->convert('HH.mm'));
|
||||
$this->assertEquals('A g:i', $sut->convert('a h:mm'));
|
||||
$this->assertEquals('H \h i', $sut->convert('HH \'h\' mm'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getProblemPattern
|
||||
*/
|
||||
public function testProblemPattern($format, $example)
|
||||
{
|
||||
$sut = new FormFormatConverter();
|
||||
$format = $sut->convert($format);
|
||||
$pattern = $sut->convertToPattern($format, false);
|
||||
$this->assertMatchesRegularExpression($pattern, $example);
|
||||
}
|
||||
|
||||
public function testDayPattern()
|
||||
{
|
||||
for ($i = 1; $i < 32; $i++) {
|
||||
if ($i < 10) {
|
||||
$this->assertMatchesRegularExpression('/^' . FormFormatConverter::PATTERN_DAY_DOUBLE . '$/', '0' . $i);
|
||||
}
|
||||
$this->assertMatchesRegularExpression('/^' . FormFormatConverter::PATTERN_DAY_SINGLE . '$/', (string) $i);
|
||||
}
|
||||
}
|
||||
|
||||
public function testHourPattern()
|
||||
{
|
||||
for ($i = 0; $i < 24; $i++) {
|
||||
if ($i < 10) {
|
||||
$this->assertMatchesRegularExpression('/^' . FormFormatConverter::PATTERN_HOUR_DOUBLE . '$/', '0' . $i);
|
||||
}
|
||||
$this->assertMatchesRegularExpression('/^' . FormFormatConverter::PATTERN_HOUR_SINGLE . '$/', (string) $i);
|
||||
}
|
||||
}
|
||||
|
||||
public function testMinutePattern()
|
||||
{
|
||||
for ($i = 0; $i < 60; $i++) {
|
||||
if ($i < 10) {
|
||||
$i = '0' . $i;
|
||||
}
|
||||
$this->assertMatchesRegularExpression('/^' . FormFormatConverter::PATTERN_MINUTES . '$/', (string) $i);
|
||||
}
|
||||
}
|
||||
|
||||
public function testMonthPattern()
|
||||
{
|
||||
for ($i = 1; $i < 13; $i++) {
|
||||
if ($i < 10) {
|
||||
$this->assertMatchesRegularExpression('/^' . FormFormatConverter::PATTERN_MONTH_DOUBLE . '$/', '0' . $i);
|
||||
}
|
||||
$this->assertMatchesRegularExpression('/^' . FormFormatConverter::PATTERN_MONTH_SINGLE . '$/', (string) $i);
|
||||
}
|
||||
}
|
||||
|
||||
public function testYearPattern()
|
||||
{
|
||||
for ($i = 0; $i < 200; $i++) {
|
||||
$this->assertMatchesRegularExpression('/^' . FormFormatConverter::PATTERN_YEAR . '$/', (string) (1900 + $i));
|
||||
}
|
||||
}
|
||||
|
||||
public function getProblemPattern()
|
||||
{
|
||||
yield ["yy-MM-dd HH 'h' mm", '2009-08-06 17 h 45'];
|
||||
}
|
||||
|
||||
public function testPattern()
|
||||
{
|
||||
$sut = new FormFormatConverter();
|
||||
foreach ($this->getPossibleDateTimePattern() as $format => $example) {
|
||||
$pattern = $sut->convertToPattern($format, false);
|
||||
$this->assertMatchesRegularExpression($pattern, $example, sprintf('Invalid pattern %s for format %s, did not match %s', $pattern, $format, $example));
|
||||
}
|
||||
}
|
||||
|
||||
public function getPossibleDateTimePattern(): array
|
||||
{
|
||||
$timeFormat = [
|
||||
'ar' => 'h:mm a',
|
||||
'cs' => 'H:mm',
|
||||
'da' => 'HH.mm',
|
||||
'de' => 'HH:mm',
|
||||
'fi' => 'H.mm',
|
||||
'ko' => 'a h:mm',
|
||||
'fr_CA' => 'HH \'h\' mm',
|
||||
];
|
||||
|
||||
$timeExample = [
|
||||
'ar' => '8:13 AM',
|
||||
'cs' => '8:44',
|
||||
'da' => '08.44',
|
||||
'de' => '19:56',
|
||||
'fi' => '17.41',
|
||||
'ko' => 'AM 7:39',
|
||||
'fr_CA' => '17 h 45',
|
||||
];
|
||||
|
||||
$dateFormat = [
|
||||
//'ar' => 'd/M/y',
|
||||
'cs' => 'dd.MM.yy',
|
||||
'da' => 'dd.MM.y',
|
||||
'el' => 'd/M/yy',
|
||||
'en' => 'M/d/yy',
|
||||
'fi' => 'd.M.y',
|
||||
'fr' => 'dd/MM/y',
|
||||
'hu' => 'y. MM. dd.',
|
||||
'it' => 'dd/MM/yy',
|
||||
'nl' => 'dd-MM-y',
|
||||
'sk' => 'd. M. y',
|
||||
'sv' => 'y-MM-dd',
|
||||
'pl' => 'd.MM.y',
|
||||
'eo' => 'yy-MM-dd',
|
||||
'eu' => 'yy/M/d',
|
||||
'fa' => 'y/M/d',
|
||||
'hr' => 'dd. MM. y.',
|
||||
'ja' => 'y/MM/dd',
|
||||
'ko' => 'yy. M. d.',
|
||||
'en_HK' => 'd/M/y',
|
||||
'en_NZ' => 'd/MM/yy',
|
||||
'es_CL' => 'dd-MM-yy',
|
||||
'es_PA' => 'MM/dd/yy',
|
||||
'hr_BA' => 'd. M. yy.',
|
||||
'nl_BE' => 'd/MM/y',
|
||||
];
|
||||
|
||||
$dateExample = [
|
||||
'ar' => '6/8/2009',
|
||||
'cs' => '06.08.2009',
|
||||
'da' => '06.08.2009',
|
||||
'el' => '6/8/2009',
|
||||
'en' => '8/6/2009',
|
||||
'fi' => '6.8.2009',
|
||||
'fr' => '06/08/2009',
|
||||
'hu' => '2009. 08. 06.',
|
||||
'it' => '06/08/2009',
|
||||
'nl' => '06-08-2009',
|
||||
'sk' => '6. 8. 2009',
|
||||
'sv' => '2009-08-06',
|
||||
'pl' => '6.08.2009',
|
||||
'eo' => '2009-08-06',
|
||||
'eu' => '2009/8/6',
|
||||
'fa' => '2009/8/6',
|
||||
'hr' => '06. 08. 2009.',
|
||||
'ja' => '2009/08/06',
|
||||
'ko' => '2009. 8. 6.',
|
||||
'en_HK' => '6/8/2009',
|
||||
'en_NZ' => '6/08/2009',
|
||||
'es_CL' => '06-08-2009',
|
||||
'es_PA' => '08/06/2009',
|
||||
'hr_BA' => '6. 8. 2009.',
|
||||
'nl_BE' => '6/08/2009',
|
||||
];
|
||||
|
||||
$all = [];
|
||||
foreach ($dateFormat as $dateLocale => $date) {
|
||||
foreach ($timeFormat as $timeLocale => $time) {
|
||||
$all[$date . ' ' . $time] = $dateExample[$dateLocale] . ' ' . $timeExample[$timeLocale];
|
||||
}
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
}
|
||||
@@ -9,21 +9,23 @@
|
||||
|
||||
namespace App\Tests\Utils;
|
||||
|
||||
use App\Utils\MomentFormatConverter;
|
||||
use App\Utils\JavascriptFormatConverter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\MomentFormatConverter
|
||||
* @covers \App\Utils\JavascriptFormatConverter
|
||||
*/
|
||||
class MomentFormatConverterTest extends TestCase
|
||||
class JavascriptFormatConverterTest extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$sut = new MomentFormatConverter();
|
||||
$sut = new JavascriptFormatConverter();
|
||||
$this->assertEquals('DD.MM.YYYY HH:mm', $sut->convert('dd.MM.y HH:mm'));
|
||||
$this->assertEquals('DD.MM.YYYY HH:mm', $sut->convert('dd.MM.yy HH:mm'));
|
||||
$this->assertEquals('DD.MM.YYYY HH:mm', $sut->convert('dd.MM.yyyy HH:mm'));
|
||||
$this->assertEquals('DD-MM-YYYY HH:mm', $sut->convert('dd-MM-yyyy HH:mm'));
|
||||
$this->assertEquals('DD/MM/YYYY HH:mm', $sut->convert('dd/MM/yyyy HH:mm'));
|
||||
$this->assertEquals('D/MM/YYYY HH:mm', $sut->convert('d/MM/yyyy HH:mm'));
|
||||
$this->assertEquals('YYYY-MM-DD HH:mm', $sut->convert('yyyy-MM-dd HH:mm'));
|
||||
$this->assertEquals('YYYY.MM.DD. HH:mm', $sut->convert('yyyy.MM.dd. HH:mm'));
|
||||
$this->assertEquals('YYYY.MM.DD. HH:mm A', $sut->convert('yyyy.MM.dd. HH:mm a'));
|
||||
}
|
||||
}
|
||||
@@ -1,51 +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\Utils;
|
||||
|
||||
use App\Utils\LanguageService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\LanguageService
|
||||
*/
|
||||
class LanguageServiceTest extends TestCase
|
||||
{
|
||||
public function testDefaults()
|
||||
{
|
||||
$sut = new LanguageService('en');
|
||||
self::assertFalse($sut->isKnownLanguage('de'));
|
||||
self::assertTrue($sut->isKnownLanguage('en'));
|
||||
self::assertFalse($sut->isKnownLanguage('xx'));
|
||||
self::assertEquals('en', $sut->getDefaultLanguage());
|
||||
self::assertEquals(['en'], $sut->getAllLanguages());
|
||||
}
|
||||
|
||||
public function testOneLanguage()
|
||||
{
|
||||
$sut = new LanguageService('de');
|
||||
self::assertTrue($sut->isKnownLanguage('de'));
|
||||
self::assertFalse($sut->isKnownLanguage('en'));
|
||||
self::assertFalse($sut->isKnownLanguage('xx'));
|
||||
self::assertEquals('en', $sut->getDefaultLanguage());
|
||||
self::assertEquals(['de'], $sut->getAllLanguages());
|
||||
}
|
||||
|
||||
public function testMultipleLanguages()
|
||||
{
|
||||
$sut = new LanguageService('de|it|fr|de_CH|ru|hu|en|zh_CN');
|
||||
self::assertTrue($sut->isKnownLanguage('de'));
|
||||
self::assertTrue($sut->isKnownLanguage('en'));
|
||||
self::assertTrue($sut->isKnownLanguage('en'));
|
||||
self::assertFalse($sut->isKnownLanguage('xx'));
|
||||
self::assertEquals('en', $sut->getDefaultLanguage());
|
||||
// casing is important for locales!
|
||||
self::assertEquals(['de', 'it', 'fr', 'de_CH', 'ru', 'hu', 'en', 'zh_CN'], $sut->getAllLanguages());
|
||||
}
|
||||
}
|
||||
@@ -1,157 +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\Utils;
|
||||
|
||||
use App\Configuration\LanguageFormattings;
|
||||
use App\Utils\LocaleFormats;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\LocaleFormats
|
||||
* @covers \App\Configuration\LanguageFormattings
|
||||
*/
|
||||
class LocaleFormatsTest extends TestCase
|
||||
{
|
||||
protected function getSut(string $locale, array $settings)
|
||||
{
|
||||
return new LocaleFormats(new LanguageFormattings($settings), $locale);
|
||||
}
|
||||
|
||||
protected function getDefaultSettings()
|
||||
{
|
||||
return [
|
||||
'de' => [
|
||||
'date_type' => 'dd.MM.yyyy',
|
||||
'date' => 'd.m.Y',
|
||||
'date_time' => 'd.m. H:i',
|
||||
'duration' => '%h:%m h',
|
||||
'time' => 'H:i',
|
||||
],
|
||||
'en' => [
|
||||
'date_type' => 'yyyy-MM-dd',
|
||||
'date' => 'Y-m-d',
|
||||
'date_time' => 'm-d H:i',
|
||||
'duration' => '%h:%m h',
|
||||
'time' => 'H:i:s',
|
||||
],
|
||||
'pt_BR' => [
|
||||
'date_type' => 'dd-MM-yyyy',
|
||||
'date' => 'd-m-Y',
|
||||
'duration' => '%h:%m h',
|
||||
],
|
||||
'it' => [
|
||||
'date_type' => 'dd.MM.yyyy',
|
||||
'date' => 'd.m.Y',
|
||||
'duration' => '%h:%m h',
|
||||
],
|
||||
'fr' => [
|
||||
'date_type' => 'dd/MM/yyyy',
|
||||
'date' => 'd/m/Y',
|
||||
'duration' => '%h h %m',
|
||||
],
|
||||
'es' => [
|
||||
'date_type' => 'dd.MM.yyyy',
|
||||
'date' => 'd.m.Y',
|
||||
'duration' => '%h:%m h',
|
||||
],
|
||||
'ru' => [
|
||||
'date_type' => 'dd.MM.yyyy',
|
||||
'date' => 'd.m.Y',
|
||||
'duration' => '%h:%m h',
|
||||
],
|
||||
'ar' => [
|
||||
'date_type' => 'yyyy-MM-dd',
|
||||
'date' => 'Y-m-d',
|
||||
'duration' => '%h:%m h',
|
||||
],
|
||||
'hu' => [
|
||||
'date_type' => 'yyyy.MM.dd',
|
||||
'date' => 'Y.m.d.',
|
||||
'duration' => '%h:%m h',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function testGetLocale()
|
||||
{
|
||||
$sut = $this->getSut('en', []);
|
||||
$this->assertEquals('en', $sut->getLocale());
|
||||
$sut = $this->getSut('ar', []);
|
||||
$this->assertEquals('ar', $sut->getLocale());
|
||||
}
|
||||
|
||||
public function testGetAvailableLanguages()
|
||||
{
|
||||
$sut = $this->getSut('en', []);
|
||||
$this->assertEquals([], $sut->getAvailableLanguages());
|
||||
|
||||
$sut = $this->getSut('en', $this->getDefaultSettings());
|
||||
$this->assertEquals(['de', 'en', 'pt_BR', 'it', 'fr', 'es', 'ru', 'ar', 'hu'], $sut->getAvailableLanguages());
|
||||
}
|
||||
|
||||
public function testInvalidLocaleWithDefaultLocale()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
$sut = $this->getSut('en', []);
|
||||
$sut->getDateFormat();
|
||||
}
|
||||
|
||||
public function testInvalidLocaleWithGivenLocale()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Unknown locale given: xx');
|
||||
|
||||
$sut = $this->getSut('xx', $this->getDefaultSettings());
|
||||
$sut->getDateFormat();
|
||||
}
|
||||
|
||||
public function testGetDurationFormat()
|
||||
{
|
||||
$sut = $this->getSut('en', $this->getDefaultSettings());
|
||||
$this->assertEquals('%h:%m h', $sut->getDurationFormat());
|
||||
}
|
||||
|
||||
public function testGetDateFormat()
|
||||
{
|
||||
$sut = $this->getSut('de', $this->getDefaultSettings());
|
||||
$this->assertEquals('d.m.Y', $sut->getDateFormat());
|
||||
}
|
||||
|
||||
public function testGetDateTimeFormat()
|
||||
{
|
||||
$sut = $this->getSut('en', $this->getDefaultSettings());
|
||||
$this->assertEquals('m-d H:i', $sut->getDateTimeFormat());
|
||||
}
|
||||
|
||||
public function testGetDateTypeFormat()
|
||||
{
|
||||
$sut = $this->getSut('de', $this->getDefaultSettings());
|
||||
$this->assertEquals('dd.MM.yyyy', $sut->getDateTypeFormat());
|
||||
}
|
||||
|
||||
public function testGetDatePickerFormat()
|
||||
{
|
||||
$sut = $this->getSut('en', $this->getDefaultSettings());
|
||||
$this->assertEquals('YYYY-MM-DD', $sut->getDatePickerFormat());
|
||||
}
|
||||
|
||||
public function testGetDateTimeTypeFormat()
|
||||
{
|
||||
$sut = $this->getSut('en', $this->getDefaultSettings());
|
||||
$this->assertEquals('yyyy-MM-dd HH:mm', $sut->getDateTimeTypeFormat());
|
||||
}
|
||||
|
||||
public function testGetTimeFormat()
|
||||
{
|
||||
$sut = $this->getSut('en', $this->getDefaultSettings());
|
||||
$this->assertEquals('H:i:s', $sut->getTimeFormat());
|
||||
}
|
||||
}
|
||||
@@ -1,236 +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\Utils;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Utils\LocaleHelper;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Intl\Util\IntlTestHelper;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\LocaleHelper
|
||||
*/
|
||||
class LocaleHelperTest extends TestCase
|
||||
{
|
||||
protected function getSut(string $locale): LocaleHelper
|
||||
{
|
||||
return new LocaleHelper($locale);
|
||||
}
|
||||
|
||||
public function testCurrency()
|
||||
{
|
||||
$symbols = [
|
||||
'EUR' => '€',
|
||||
'USD' => '$',
|
||||
'RUB' => 'RUB',
|
||||
'rub' => 'RUB',
|
||||
123 => 123,
|
||||
];
|
||||
|
||||
$sut = $this->getSut('en');
|
||||
foreach ($symbols as $name => $symbol) {
|
||||
$this->assertEquals($symbol, $sut->currency($name));
|
||||
}
|
||||
}
|
||||
|
||||
public function testCountry()
|
||||
{
|
||||
$countries = [
|
||||
'DE' => 'Germany',
|
||||
'RU' => 'Russia',
|
||||
'ES' => 'Spain',
|
||||
'es' => 'Spain',
|
||||
'12' => '12',
|
||||
];
|
||||
|
||||
$sut = $this->getSut('en');
|
||||
foreach ($countries as $locale => $name) {
|
||||
$this->assertEquals($name, $sut->country($locale));
|
||||
}
|
||||
}
|
||||
|
||||
public function testLanguage()
|
||||
{
|
||||
$languages = [
|
||||
'de' => 'German',
|
||||
'ru' => 'Russian',
|
||||
'es' => 'Spanish',
|
||||
'ES' => 'Spanish',
|
||||
'12' => '12',
|
||||
];
|
||||
|
||||
$sut = $this->getSut('en');
|
||||
foreach ($languages as $locale => $name) {
|
||||
$this->assertEquals($name, $sut->language($locale));
|
||||
}
|
||||
}
|
||||
|
||||
public function testMoneyWithoutCurrency()
|
||||
{
|
||||
$sut = $this->getSut('en');
|
||||
$this->assertEquals('123.75', $sut->money(123.75));
|
||||
|
||||
$sut = $this->getSut('de');
|
||||
$this->assertEquals('123.234,76', $sut->money(123234.7554, null, true));
|
||||
$this->assertEquals('123.234,76', $sut->money(123234.7554, null, false));
|
||||
$this->assertEquals('123.234,76', $sut->money(123234.7554, 'EUR', false));
|
||||
$this->assertEquals('-123.234,76', $sut->money(-123234.7554, null, false));
|
||||
$this->assertEquals('-123.234,76', $sut->money(-123234.7554, 'EUR', false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getMoneyNoCurrencyData
|
||||
*/
|
||||
public function testMoneyNoCurrency($result, $amount, $currency, $locale)
|
||||
{
|
||||
$sut = $this->getSut($locale);
|
||||
$this->assertEquals($result, $sut->money($amount, $currency, false));
|
||||
}
|
||||
|
||||
public function getMoneyNoCurrencyData()
|
||||
{
|
||||
return [
|
||||
['0,00', null, 'EUR', 'de'],
|
||||
['2.345,00', 2345, 'EUR', 'de'],
|
||||
['2,345.00', 2345, 'EUR', 'en'],
|
||||
['2,345.01', 2345.009, 'EUR', 'en'],
|
||||
['2.345,01', 2345.009, 'EUR', 'de'],
|
||||
['13.75', 13.75, 'USD', 'en'],
|
||||
['13,75', 13.75, 'USD', 'de'],
|
||||
['13,75', 13.75, 'RUB', 'de'],
|
||||
['13,75', 13.75, 'JPY', 'de'],
|
||||
['13 933,49', 13933.49, 'JPY', 'ru'],
|
||||
['-13 933,49', -13933.49, 'JPY', 'ru'],
|
||||
['13,75', 13.75, 'CNY', 'de'],
|
||||
['13.933,00', 13933, 'CNY', 'de'],
|
||||
['13 933,00', 13933, 'CNY', 'ru'],
|
||||
['13,933.00', 13933, 'CNY', 'en'],
|
||||
['13,933.00', 13933, 'CNY', 'zh_CN'],
|
||||
['1.234.567,89', 1234567.891234567890000, 'USD', 'de'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getMoneyData
|
||||
*/
|
||||
public function testMoney($result, $amount, $currency, $locale)
|
||||
{
|
||||
$sut = $this->getSut($locale);
|
||||
$this->assertEquals($result, $sut->money($amount, $currency));
|
||||
}
|
||||
|
||||
public function getMoneyData()
|
||||
{
|
||||
return [
|
||||
['0,00 €', null, 'EUR', 'de'],
|
||||
['2.345,00 €', 2345, 'EUR', 'de'],
|
||||
['€2,345.00', 2345, 'EUR', 'en'],
|
||||
['€2,345.01', 2345.009, 'EUR', 'en'],
|
||||
['2.345,01 €', 2345.009, 'EUR', 'de'],
|
||||
['$13.75', 13.75, 'USD', 'en'],
|
||||
['13,75 $', 13.75, 'USD', 'de'],
|
||||
['13,75 RUB', 13.75, 'RUB', 'de'],
|
||||
['14 ¥', 13.75, 'JPY', 'de'],
|
||||
['13 933 ¥', 13933.49, 'JPY', 'ru'],
|
||||
['13,75 CN¥', 13.75, 'CNY', 'de'],
|
||||
['13.933,00 CN¥', 13933, 'CNY', 'de'],
|
||||
['13 933,00 CN¥', 13933, 'CNY', 'ru'],
|
||||
['CN¥13,933.00', 13933, 'CNY', 'en'],
|
||||
['1.234.567,89 $', 1234567.891234567890000, 'USD', 'de'],
|
||||
['-CN¥13,933.00', -13933, 'CNY', 'en'],
|
||||
['-1.234.567,89 $', -1234567.891234567890000, 'USD', 'de'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getAmountData
|
||||
*/
|
||||
public function testAmount($result, $amount, $locale)
|
||||
{
|
||||
$sut = $this->getSut($locale);
|
||||
$this->assertEquals($result, $sut->amount($amount));
|
||||
}
|
||||
|
||||
public function getAmountData()
|
||||
{
|
||||
return [
|
||||
['0', null, 'de'],
|
||||
['2.345,01', 2345.01, 'de'],
|
||||
['2.345', 2345, 'de'],
|
||||
['2,345', 2345, 'en'],
|
||||
['2,345.009', 2345.009, 'en'],
|
||||
['2.345,009', 2345.009, 'de'],
|
||||
['13.75', 13.75, 'en'],
|
||||
['13,75', 13.75, 'de'],
|
||||
['13 933,49', 13933.49, 'ru'],
|
||||
['1.234.567,891', 1234567.891234567890000, 'de'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getMoneyData62_1
|
||||
*/
|
||||
public function testMoney62_1($result, $amount, $currency, $locale)
|
||||
{
|
||||
IntlTestHelper::requireFullIntl($this, '62.1');
|
||||
|
||||
$sut = $this->getSut($locale);
|
||||
$this->assertEquals($result, $sut->money($amount, $currency));
|
||||
}
|
||||
|
||||
public function getMoneyData62_1()
|
||||
{
|
||||
return [
|
||||
['RUB 13.50', 13.50, 'RUB', 'en'],
|
||||
['13,75 ₽', 13.75, 'RUB', 'ru'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testDurationDecimal()
|
||||
{
|
||||
$record = $this->getTimesheet(9437);
|
||||
|
||||
$sut = $this->getSut('en');
|
||||
$this->assertEquals('2.62', $sut->durationDecimal($record->getDuration()));
|
||||
|
||||
// test extended format
|
||||
$sut = $this->getSut('de');
|
||||
$this->assertEquals('2,62', $sut->durationDecimal($record->getDuration()));
|
||||
$this->assertEquals('6.328,89', $sut->durationDecimal(22784012));
|
||||
$this->assertEquals('1,00', $sut->durationDecimal(3600));
|
||||
$this->assertEquals('1,01', $sut->durationDecimal(3630));
|
||||
$this->assertEquals('1,02', $sut->durationDecimal(3661));
|
||||
$this->assertEquals('1,10', $sut->durationDecimal(3960));
|
||||
|
||||
// test negative duration
|
||||
$sut = $this->getSut('en');
|
||||
$this->assertEquals('-0.00', $sut->durationDecimal(-1));
|
||||
$this->assertEquals('0.00', $sut->durationDecimal(-0));
|
||||
$this->assertEquals('-0.01', $sut->durationDecimal(-40));
|
||||
|
||||
// test zero duration
|
||||
$sut = $this->getSut('en');
|
||||
$this->assertEquals('0.00', $sut->durationDecimal(0));
|
||||
$this->assertEquals('6,328.89', $sut->durationDecimal(22784012));
|
||||
}
|
||||
|
||||
protected function getTimesheet($seconds)
|
||||
{
|
||||
$begin = new \DateTime();
|
||||
$end = clone $begin;
|
||||
$end->setTimestamp($begin->getTimestamp() + $seconds);
|
||||
$record = new Timesheet();
|
||||
$record->setBegin($begin);
|
||||
$record->setEnd($end);
|
||||
$record->setDuration($seconds);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +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\Utils;
|
||||
|
||||
use App\Configuration\LanguageFormattings;
|
||||
use App\Utils\LocaleSettings;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\LocaleSettings
|
||||
* @covers \App\Configuration\LanguageFormattings
|
||||
*/
|
||||
class LocaleSettingsTest extends LocaleFormatsTest
|
||||
{
|
||||
protected function getSut(string $locale, array $settings)
|
||||
{
|
||||
$request = new Request();
|
||||
$request->setLocale($locale);
|
||||
$requestStack = new RequestStack();
|
||||
$requestStack->push($request);
|
||||
|
||||
return new LocaleSettings($requestStack, new LanguageFormattings($settings));
|
||||
}
|
||||
}
|
||||
@@ -1,39 +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\Utils;
|
||||
|
||||
use App\Tests\Mocks\FileHelperFactory;
|
||||
use App\Utils\MPdfConverter;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\MPdfConverter
|
||||
* @group integration
|
||||
*/
|
||||
class MPdfConverterTest extends KernelTestCase
|
||||
{
|
||||
public function unicode_hex($unicode_dec)
|
||||
{
|
||||
return (sprintf('%05s', strtoupper(dechex($unicode_dec))));
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
$kernel = self::bootKernel();
|
||||
$cacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
|
||||
|
||||
$sut = new MPdfConverter((new FileHelperFactory($this))->create(), $cacheDir);
|
||||
$result = $sut->convertToPdf('<h1>Test</h1>');
|
||||
// Yeah, thats not a real test, I know ;-)
|
||||
$this->assertNotEmpty($result);
|
||||
preg_match('/\/Creator \((.*)\)/', $result, $matches);
|
||||
$this->assertCount(2, $matches);
|
||||
}
|
||||
}
|
||||
@@ -24,44 +24,44 @@ class MarkdownTest extends TestCase
|
||||
$this->assertEquals('<p><em>test</em></p>', $sut->toHtml('*test*'));
|
||||
$this->assertEquals('<p># foobar</p>', $sut->toHtml('# foobar'));
|
||||
$html = <<<'EOT'
|
||||
<p>foo bar</p>
|
||||
<ul>
|
||||
<li>sdfasdfasdf</li>
|
||||
<li>asdfasdfasdf</li>
|
||||
</ul>
|
||||
<p># test<br />
|
||||
asdfasdfa</p>
|
||||
<pre><code>ssdfsdf</code></pre>
|
||||
<p><a href="http://example.com/foo-bar.html" target="_blank">http://example.com/foo-bar.html</a><br />
|
||||
<a href="file:///home/kimai/images/beautiful-flower.png" target="_blank">file:///home/kimai/images/beautiful-flower.png</a></p>
|
||||
<p>sdfsdf <a href="#test-1">asdfasdf</a> asdfasdf</p>
|
||||
<p># test<br />
|
||||
aasdfasdf<br />
|
||||
1111<br />
|
||||
222</p>
|
||||
EOT;
|
||||
<p>foo bar</p>
|
||||
<ul>
|
||||
<li>sdfasdfasdf</li>
|
||||
<li>asdfasdfasdf</li>
|
||||
</ul>
|
||||
<p># test<br />
|
||||
asdfasdfa</p>
|
||||
<pre><code>ssdfsdf</code></pre>
|
||||
<p><a href="http://example.com/foo-bar.html" target="_blank">http://example.com/foo-bar.html</a><br />
|
||||
<a href="file:///home/kimai/images/beautiful-flower.png" target="_blank">file:///home/kimai/images/beautiful-flower.png</a></p>
|
||||
<p>sdfsdf <a href="#test-1">asdfasdf</a> asdfasdf</p>
|
||||
<p># test<br />
|
||||
aasdfasdf<br />
|
||||
1111<br />
|
||||
222</p>
|
||||
EOT;
|
||||
|
||||
$markdown = <<<EOT
|
||||
foo bar
|
||||
foo bar
|
||||
|
||||
- sdfasdfasdf
|
||||
- asdfasdfasdf
|
||||
- sdfasdfasdf
|
||||
- asdfasdfasdf
|
||||
|
||||
# test
|
||||
asdfasdfa
|
||||
# test
|
||||
asdfasdfa
|
||||
|
||||
ssdfsdf
|
||||
|
||||
http://example.com/foo-bar.html
|
||||
file:///home/kimai/images/beautiful-flower.png
|
||||
ssdfsdf
|
||||
|
||||
http://example.com/foo-bar.html
|
||||
file:///home/kimai/images/beautiful-flower.png
|
||||
|
||||
sdfsdf [asdfasdf](#test-1) asdfasdf
|
||||
sdfsdf [asdfasdf](#test-1) asdfasdf
|
||||
|
||||
# test
|
||||
aasdfasdf
|
||||
1111
|
||||
222
|
||||
EOT;
|
||||
# test
|
||||
aasdfasdf
|
||||
1111
|
||||
222
|
||||
EOT;
|
||||
$this->assertEquals($html, $sut->toHtml($markdown));
|
||||
}
|
||||
|
||||
@@ -70,18 +70,18 @@ EOT;
|
||||
$sut = new Markdown();
|
||||
|
||||
$html = <<<'EOT'
|
||||
<p># test<br />
|
||||
## test<br />
|
||||
### test<br />
|
||||
# test</p>
|
||||
EOT;
|
||||
<p># test<br />
|
||||
## test<br />
|
||||
### test<br />
|
||||
# test</p>
|
||||
EOT;
|
||||
|
||||
$markdown = <<<EOT
|
||||
# test
|
||||
## test
|
||||
### test
|
||||
# test
|
||||
EOT;
|
||||
# test
|
||||
## test
|
||||
### test
|
||||
# test
|
||||
EOT;
|
||||
$this->assertEquals($html, $sut->toHtml($markdown));
|
||||
}
|
||||
|
||||
@@ -90,16 +90,16 @@ EOT;
|
||||
$sut = new Markdown();
|
||||
|
||||
$html = <<<'EOT'
|
||||
<p><a href="javascript%3Aalert(`XSS`)">XSS</a><br />
|
||||
<a href="javascript%3Aalert("XSS")">XSS</a><br />
|
||||
<a href="javascript%3Aalert('XSS')">XSS</a></p>
|
||||
EOT;
|
||||
<p><a href="javascript%3Aalert(`XSS`)">XSS</a><br />
|
||||
<a href="javascript%3Aalert("XSS")">XSS</a><br />
|
||||
<a href="javascript%3Aalert('XSS')">XSS</a></p>
|
||||
EOT;
|
||||
|
||||
$markdown = <<<EOT
|
||||
[XSS](javascript:alert(`XSS`))
|
||||
[XSS](javascript:alert("XSS"))
|
||||
[XSS](javascript:alert('XSS'))
|
||||
EOT;
|
||||
[XSS](javascript:alert(`XSS`))
|
||||
[XSS](javascript:alert("XSS"))
|
||||
[XSS](javascript:alert('XSS'))
|
||||
EOT;
|
||||
$this->assertEquals($html, $sut->toHtml($markdown));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ class MenuItemModelTest extends TestCase
|
||||
{
|
||||
$sut = new MenuItemModel('test', 'foo', 'bar');
|
||||
|
||||
self::assertEquals('foo', $sut->getLabel());
|
||||
|
||||
self::assertFalse($sut->isChildRoute('blub'));
|
||||
self::assertFalse($sut->isChildRoute('bla'));
|
||||
|
||||
@@ -33,5 +35,48 @@ class MenuItemModelTest extends TestCase
|
||||
|
||||
self::assertFalse($sut->isChildRoute('blub'));
|
||||
self::assertTrue($sut->isChildRoute('bla'));
|
||||
|
||||
self::assertNull($sut->getIcon());
|
||||
$sut->setIcon('123456789');
|
||||
self::assertEquals('123456789', $sut->getIcon());
|
||||
|
||||
self::assertFalse($sut->isDivider());
|
||||
$sut->setDivider(true);
|
||||
self::assertTrue($sut->isDivider());
|
||||
|
||||
self::assertNull($sut->getBadge());
|
||||
$sut->setBadge('22');
|
||||
self::assertEquals('22', $sut->getBadge());
|
||||
|
||||
self::assertNull($sut->getBadgeColor());
|
||||
$sut->setBadgeColor('red');
|
||||
self::assertEquals('red', $sut->getBadgeColor());
|
||||
|
||||
self::assertEquals([], $sut->getRouteArgs());
|
||||
$sut->setRouteArgs(['sdf' => 'dfsgdfg', 'xxx' => 2345]);
|
||||
self::assertEquals(['sdf' => 'dfsgdfg', 'xxx' => 2345], $sut->getRouteArgs());
|
||||
|
||||
self::assertNull($sut->getParent());
|
||||
$parent = new MenuItemModel('a', 'b');
|
||||
self::assertEquals('b', $parent->getLabel());
|
||||
$parent->setLabel('x');
|
||||
self::assertEquals('x', $parent->getLabel());
|
||||
$sut->setParent($parent);
|
||||
self::assertSame($parent, $sut->getParent());
|
||||
|
||||
self::assertNull($sut->getChild('foo'));
|
||||
self::assertNull($sut->getActiveChild());
|
||||
|
||||
$child1 = new MenuItemModel('a1', 'a2');
|
||||
$child2 = new MenuItemModel('b1', 'b2');
|
||||
self::assertFalse($child2->getIsActive());
|
||||
$child2->setIsActive(true);
|
||||
self::assertTrue($child2->getIsActive());
|
||||
|
||||
$sut->setChildren([$child1, $child2]);
|
||||
self::assertEquals([$child1, $child2], $sut->getChildren());
|
||||
self::assertEquals($child1, $sut->getChild('a1'));
|
||||
self::assertEquals($child2, $sut->getChild('b1'));
|
||||
self::assertEquals($child2, $sut->getActiveChild());
|
||||
}
|
||||
}
|
||||
|
||||
58
tests/Utils/NumberGeneratorTest.php
Normal file
58
tests/Utils/NumberGeneratorTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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\Utils;
|
||||
|
||||
use App\Utils\NumberGenerator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\NumberGenerator
|
||||
*/
|
||||
class NumberGeneratorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getTestData
|
||||
* @param string $format
|
||||
* @param array<mixed> $options
|
||||
* @param string $expected
|
||||
* @param int $startWith
|
||||
* @return void
|
||||
*/
|
||||
public function testGenerateNumber(string $format, array $options, string $expected, int $startWith): void
|
||||
{
|
||||
$sut = new NumberGenerator($format, function (string $originalFormat, string $format, int $increaseBy) use ($options) {
|
||||
if (\array_key_exists($format, $options)) {
|
||||
$value = $options[$format];
|
||||
if (\is_int($value)) {
|
||||
$value += $increaseBy;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
return $originalFormat;
|
||||
});
|
||||
|
||||
self::assertEquals($expected, $sut->getNumber($startWith));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function getTestData(): array
|
||||
{
|
||||
return [
|
||||
['DEMO-{XXX}-{known,5}', ['known' => 123], 'DEMO-{XXX}-00124', 0],
|
||||
['DEMO-{XXX}-{known,4}', ['known' => 123, 'XXX' => 'FOO!'], 'DEMO-FOO!-0222', 99],
|
||||
['DEMO-{XXX}-{known+11}', ['known' => 123, 'XXX' => 'FOO!'], 'DEMO-FOO!-135', 1],
|
||||
['{XXX}-{known-11}', ['known' => 123, 'XXX' => 'A'], 'A-113', 1],
|
||||
];
|
||||
}
|
||||
}
|
||||
50
tests/Utils/PageSetupTest.php
Normal file
50
tests/Utils/PageSetupTest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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\Utils;
|
||||
|
||||
use App\Repository\Query\BaseQuery;
|
||||
use App\Utils\DataTable;
|
||||
use App\Utils\PageSetup;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\PageSetup
|
||||
*/
|
||||
class PageSetupTest extends TestCase
|
||||
{
|
||||
public function testDefaults(): void
|
||||
{
|
||||
$sut = new PageSetup('foo-bar');
|
||||
self::assertEquals('foo-bar', $sut->getTitle());
|
||||
self::assertNull($sut->getActionName());
|
||||
self::assertEquals([], $sut->getActionPayload());
|
||||
self::assertEquals('index', $sut->getActionView());
|
||||
self::assertNull($sut->getHelp());
|
||||
self::assertNull($sut->getDataTable());
|
||||
}
|
||||
|
||||
public function testSetter(): void
|
||||
{
|
||||
$dataTable = new DataTable('tadaaaaa', new BaseQuery());
|
||||
$sut = new PageSetup('foo-bar');
|
||||
$sut->setDataTable($dataTable);
|
||||
$sut->setActionName('some-action');
|
||||
$sut->setHelp('kjhgkjhgkjhg');
|
||||
$sut->setActionView('custom');
|
||||
$sut->setActionPayload(['foo' => 'bar']);
|
||||
|
||||
self::assertEquals('foo-bar', $sut->getTitle());
|
||||
self::assertEquals('some-action', $sut->getActionName());
|
||||
self::assertEquals(['foo' => 'bar'], $sut->getActionPayload());
|
||||
self::assertEquals('custom', $sut->getActionView());
|
||||
self::assertEquals('kjhgkjhgkjhg', $sut->getHelp());
|
||||
self::assertSame($dataTable, $sut->getDataTable());
|
||||
}
|
||||
}
|
||||
185
tests/Utils/ProfileManagerTest.php
Normal file
185
tests/Utils/ProfileManagerTest.php
Normal file
@@ -0,0 +1,185 @@
|
||||
<?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\Utils;
|
||||
|
||||
use App\Utils\ProfileManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
|
||||
|
||||
/**
|
||||
* @covers \App\Utils\ProfileManager
|
||||
*/
|
||||
class ProfileManagerTest extends TestCase
|
||||
{
|
||||
public function testEmpty()
|
||||
{
|
||||
$request = new Request();
|
||||
$session = new Session(new MockFileSessionStorage());
|
||||
$request->setSession($session);
|
||||
|
||||
$sut = new ProfileManager();
|
||||
self::assertEquals(ProfileManager::PROFILE_DESKTOP, $sut->getProfileFromCookie($request));
|
||||
self::assertEquals(ProfileManager::PROFILE_DESKTOP, $sut->getProfileFromSession($session));
|
||||
}
|
||||
|
||||
public function getInvalidProfiles()
|
||||
{
|
||||
return [
|
||||
['MOBILE'],
|
||||
['mobilE'],
|
||||
['mobile2'],
|
||||
['mobile2'],
|
||||
['foo'],
|
||||
['mobile '],
|
||||
[' desktop'],
|
||||
['DESKTOP'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidProfiles
|
||||
*/
|
||||
public function testIsInvalidProfile(string $profile)
|
||||
{
|
||||
$sut = new ProfileManager();
|
||||
self::assertFalse($sut->isValidProfile($profile));
|
||||
}
|
||||
|
||||
public function getDatatableNames()
|
||||
{
|
||||
return [
|
||||
['admin-timesheet_mobile', 'admin-timesheet', 'mobile'],
|
||||
['admin-timesheet_mobile', 'admin-timesheet', ProfileManager::PROFILE_MOBILE],
|
||||
['admin-timesheet', 'admin-timesheet', 'desktop'],
|
||||
['admin-timesheet', 'admin-timesheet', ProfileManager::PROFILE_DESKTOP],
|
||||
['admin-timesheet', 'admin-timesheet', ''],
|
||||
['admin-timesheet_', 'admin-timesheet', ' '],
|
||||
['admin-timesheet_', ' admin-timesheet', ' '],
|
||||
['admin-timesheet', 'admin-timesheet', null],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getDatatableNames
|
||||
*/
|
||||
public function testDatatableName(string $expected, string $datatable, ?string $prefix)
|
||||
{
|
||||
$sut = new ProfileManager();
|
||||
self::assertEquals($expected, $sut->getDatatableName($datatable, $prefix));
|
||||
}
|
||||
|
||||
public function getProfileNames()
|
||||
{
|
||||
return [
|
||||
['mobile', ProfileManager::PROFILE_MOBILE],
|
||||
[' mobile', ProfileManager::PROFILE_DESKTOP],
|
||||
['mobile ', ProfileManager::PROFILE_DESKTOP],
|
||||
['MOBILE', ProfileManager::PROFILE_DESKTOP],
|
||||
['mobilE', ProfileManager::PROFILE_DESKTOP],
|
||||
['mobile2', ProfileManager::PROFILE_DESKTOP],
|
||||
['mobile2', ProfileManager::PROFILE_DESKTOP],
|
||||
['foo', ProfileManager::PROFILE_DESKTOP],
|
||||
['mobile ', ProfileManager::PROFILE_DESKTOP],
|
||||
[' desktop', ProfileManager::PROFILE_DESKTOP],
|
||||
['DESKTOP', ProfileManager::PROFILE_DESKTOP],
|
||||
['', ProfileManager::PROFILE_DESKTOP],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getProfileNames
|
||||
*/
|
||||
public function testGetProfile(string $profile, string $expected)
|
||||
{
|
||||
$sut = new ProfileManager();
|
||||
self::assertEquals($expected, $sut->getProfile($profile));
|
||||
}
|
||||
|
||||
public function testSetProfile()
|
||||
{
|
||||
$request = new Request();
|
||||
$session = new Session(new MockFileSessionStorage());
|
||||
$request->setSession($session);
|
||||
|
||||
self::assertNull($session->get(ProfileManager::SESSION_PROFILE));
|
||||
|
||||
$sut = new ProfileManager();
|
||||
|
||||
$sut->setProfile($session, ProfileManager::PROFILE_DESKTOP);
|
||||
self::assertNull($session->get(ProfileManager::SESSION_PROFILE));
|
||||
|
||||
$sut->setProfile($session, ProfileManager::PROFILE_MOBILE);
|
||||
self::assertNotNull($session->get(ProfileManager::SESSION_PROFILE));
|
||||
self::assertEquals(ProfileManager::PROFILE_MOBILE, $session->get(ProfileManager::SESSION_PROFILE));
|
||||
|
||||
$sut->setProfile($session, 'foo');
|
||||
self::assertNull($session->get(ProfileManager::SESSION_PROFILE));
|
||||
}
|
||||
|
||||
public function getCookieProfiles()
|
||||
{
|
||||
return [
|
||||
['mobile', ProfileManager::PROFILE_MOBILE],
|
||||
['desktop', ProfileManager::PROFILE_DESKTOP],
|
||||
['foo', ProfileManager::PROFILE_DESKTOP],
|
||||
['MOBILE', ProfileManager::PROFILE_DESKTOP],
|
||||
['', ProfileManager::PROFILE_DESKTOP],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getCookieProfiles
|
||||
*/
|
||||
public function testGetProfileFromCookie(string $cookieValue, string $expected)
|
||||
{
|
||||
$request = new Request();
|
||||
self::assertFalse($request->cookies->has(ProfileManager::COOKIE_PROFILE));
|
||||
|
||||
$session = new Session(new MockFileSessionStorage());
|
||||
$request->setSession($session);
|
||||
|
||||
$request->cookies->set(ProfileManager::COOKIE_PROFILE, $cookieValue);
|
||||
|
||||
$sut = new ProfileManager();
|
||||
$profile = $sut->getProfileFromCookie($request);
|
||||
self::assertEquals($expected, $profile);
|
||||
}
|
||||
|
||||
public function getSessionProfiles()
|
||||
{
|
||||
return [
|
||||
['mobile', ProfileManager::PROFILE_MOBILE],
|
||||
['desktop', ProfileManager::PROFILE_DESKTOP],
|
||||
['foo', ProfileManager::PROFILE_DESKTOP],
|
||||
['MOBILE', ProfileManager::PROFILE_DESKTOP],
|
||||
['', ProfileManager::PROFILE_DESKTOP],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getSessionProfiles
|
||||
*/
|
||||
public function testGetProfileFromSession(string $sessionValue, string $expected)
|
||||
{
|
||||
$request = new Request();
|
||||
self::assertFalse($request->cookies->has(ProfileManager::COOKIE_PROFILE));
|
||||
|
||||
$session = new Session(new MockFileSessionStorage());
|
||||
$request->setSession($session);
|
||||
|
||||
$session->set(ProfileManager::SESSION_PROFILE, $sessionValue);
|
||||
|
||||
$sut = new ProfileManager();
|
||||
$profile = $sut->getProfileFromSession($session);
|
||||
self::assertEquals($expected, $profile);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user