Release 2.47 (#5784)
This commit is contained in:
@@ -55,7 +55,7 @@ abstract class AbstractUserPeriodControllerTestCase extends AbstractControllerBa
|
||||
$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="card-body %s', $this->getBoxId()), $client->getResponse()->getContent());
|
||||
self::assertStringContainsString(\sprintf('<div class="card-body p-0 %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')]");
|
||||
@@ -90,7 +90,7 @@ abstract class AbstractUserPeriodControllerTestCase extends AbstractControllerBa
|
||||
$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="card-body %s', $this->getBoxId()), $client->getResponse()->getContent());
|
||||
self::assertStringContainsString(\sprintf('<div class="card-body p-0 %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')]");
|
||||
|
||||
@@ -55,7 +55,7 @@ abstract class AbstractUsersPeriodControllerTestCase extends AbstractControllerB
|
||||
$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="card-body %s', $this->getBoxId()), $client->getResponse()->getContent());
|
||||
self::assertStringContainsString(\sprintf('<div class="card-body p-0 %s', $this->getBoxId()), $client->getResponse()->getContent());
|
||||
$cell = $client->getCrawler()->filterXPath("//th[contains(@class, 'reportDataTypeTitle')]");
|
||||
self::assertEquals($title, $cell->text());
|
||||
}
|
||||
@@ -66,7 +66,7 @@ abstract class AbstractUsersPeriodControllerTestCase extends AbstractControllerB
|
||||
$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="card-body %s', $this->getBoxId()), $client->getResponse()->getContent());
|
||||
self::assertStringContainsString(\sprintf('<div class="card-body p-0 %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')]");
|
||||
|
||||
@@ -294,9 +294,6 @@ class UserTest extends TestCase
|
||||
|
||||
self::assertInstanceOf(UserPreference::class, $prefs[2]);
|
||||
self::assertEquals('export_decimal', $prefs[2]->getName());
|
||||
|
||||
$user->setPreferences(new ArrayCollection([]));
|
||||
self::assertCount(0, $user->getPreferences());
|
||||
}
|
||||
|
||||
public function testDisplayName(): void
|
||||
|
||||
@@ -26,6 +26,8 @@ use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
use Twig\Environment;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
|
||||
#[CoversClass(ServiceExport::class)]
|
||||
#[CoversClass(CsvRenderer::class)]
|
||||
@@ -35,7 +37,7 @@ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
#[Group('integration')]
|
||||
class DefaultRendererTest extends AbstractRendererTestCase
|
||||
{
|
||||
private function createServiceExport(): ServiceExport
|
||||
private function createServiceExport(?Environment $environment = null): ServiceExport
|
||||
{
|
||||
$repository = $this->createMock(ExportTemplateRepository::class);
|
||||
$repository->expects($this->once())->method('findAll')->willReturn([]);
|
||||
@@ -43,8 +45,8 @@ class DefaultRendererTest extends AbstractRendererTestCase
|
||||
|
||||
return new ServiceExport(
|
||||
$this->createMock(EventDispatcherInterface::class),
|
||||
(new HtmlRendererFactoryMock($this))->create(),
|
||||
(new PdfRendererFactoryMock($this))->create(),
|
||||
(new HtmlRendererFactoryMock($this))->create($environment),
|
||||
(new PdfRendererFactoryMock($this))->create($environment),
|
||||
(new CsvRendererFactoryMock($this))->create(),
|
||||
(new XlsxRendererFactoryMock($this))->create(),
|
||||
$repository,
|
||||
@@ -54,7 +56,10 @@ class DefaultRendererTest extends AbstractRendererTestCase
|
||||
|
||||
public function testRenderDefaultTemplates(): void
|
||||
{
|
||||
$sut = $this->createServiceExport();
|
||||
/** @var Environment $twig */
|
||||
$twig = $this->getContainer()->get(Environment::class);
|
||||
|
||||
$sut = $this->createServiceExport($twig);
|
||||
|
||||
$renderer = $sut->getRenderer();
|
||||
self::assertCount(4, $renderer);
|
||||
@@ -98,12 +103,21 @@ class DefaultRendererTest extends AbstractRendererTestCase
|
||||
->files()
|
||||
;
|
||||
|
||||
/** @var Environment $twig */
|
||||
$twig = $this->getContainer()->get(Environment::class);
|
||||
|
||||
/** @var FilesystemLoader $loader */
|
||||
$loader = $twig->getLoader();
|
||||
|
||||
$files = [];
|
||||
$dirs = [];
|
||||
foreach ($finder->getIterator() as $filename => $splFile) {
|
||||
$files[] = $splFile->getRealPath();
|
||||
$dir = \dirname($splFile->getRealPath());
|
||||
$dirs[$dir] = $dir;
|
||||
if (!\array_key_exists($dir, $dirs)) {
|
||||
$dirs[$dir] = $dir;
|
||||
$loader->addPath($dir . '/', 'export');
|
||||
}
|
||||
}
|
||||
$dirs = array_keys($dirs);
|
||||
|
||||
@@ -113,7 +127,7 @@ class DefaultRendererTest extends AbstractRendererTestCase
|
||||
return;
|
||||
}
|
||||
|
||||
$sut = $this->createServiceExport();
|
||||
$sut = $this->createServiceExport($twig);
|
||||
foreach ($dirs as $dir) {
|
||||
$sut->addDirectory($dir);
|
||||
}
|
||||
|
||||
@@ -25,10 +25,10 @@ use Twig\Environment;
|
||||
#[Group('integration')]
|
||||
class HtmlRendererTest extends AbstractRendererTestCase
|
||||
{
|
||||
protected function getAbstractRenderer(): HtmlRenderer
|
||||
protected function getAbstractRenderer(?Environment $environment = null): HtmlRenderer
|
||||
{
|
||||
return new HtmlRenderer(
|
||||
$this->createMock(Environment::class),
|
||||
$environment ?? $this->createMock(Environment::class),
|
||||
$this->createMock(EventDispatcherInterface::class),
|
||||
$this->createMock(ProjectStatisticService::class),
|
||||
$this->createMock(ActivityStatisticService::class),
|
||||
@@ -60,7 +60,10 @@ class HtmlRendererTest extends AbstractRendererTestCase
|
||||
|
||||
public function testRender(): void
|
||||
{
|
||||
$sut = $this->getAbstractRenderer();
|
||||
/** @var Environment $twig */
|
||||
$twig = $this->getContainer()->get(Environment::class);
|
||||
|
||||
$sut = $this->getAbstractRenderer($twig);
|
||||
|
||||
$response = $this->render($sut);
|
||||
self::assertInstanceOf(Response::class, $response);
|
||||
|
||||
@@ -26,14 +26,13 @@ use Twig\Environment;
|
||||
#[Group('integration')]
|
||||
class PdfRendererTest extends AbstractRendererTestCase
|
||||
{
|
||||
protected function getAbstractRenderer(bool $exportDecimal = false): PDFRenderer
|
||||
protected function getAbstractRenderer(?Environment $environment = null): PDFRenderer
|
||||
{
|
||||
$twig = $this->createMock(Environment::class);
|
||||
$converter = $this->createMock(HtmlToPdfConverter::class);
|
||||
$projectStatisticService = $this->createMock(ProjectStatisticService::class);
|
||||
|
||||
return new PDFRenderer(
|
||||
$twig,
|
||||
$environment ?? $this->createMock(Environment::class),
|
||||
$converter,
|
||||
$projectStatisticService,
|
||||
'foo',
|
||||
@@ -70,7 +69,10 @@ class PdfRendererTest extends AbstractRendererTestCase
|
||||
|
||||
public function testRender(): void
|
||||
{
|
||||
$sut = $this->getAbstractRenderer();
|
||||
/** @var Environment $twig */
|
||||
$twig = $this->getContainer()->get(Environment::class);
|
||||
|
||||
$sut = $this->getAbstractRenderer($twig);
|
||||
|
||||
$response = $this->render($sut);
|
||||
self::assertInstanceOf(Response::class, $response);
|
||||
|
||||
@@ -131,6 +131,7 @@ abstract class AbstractRendererTestCase extends KernelTestCase
|
||||
|
||||
$currentUser = new User();
|
||||
$currentUser->setPreferenceValue('export_decimal', $exportDecimal);
|
||||
$currentUser->setUserIdentifier('foo-bar');
|
||||
|
||||
$query = new TimesheetQuery();
|
||||
$query->setActivities([$activity]);
|
||||
|
||||
@@ -59,7 +59,7 @@ class PdfRendererTest extends KernelTestCase
|
||||
|
||||
/** @var FilesystemLoader $loader */
|
||||
$loader = $twig->getLoader();
|
||||
$loader->addPath(__DIR__ . '/../templates/', 'invoice');
|
||||
$loader->addPath($this->getInvoiceTemplatePath(), 'invoice');
|
||||
|
||||
$sut = new PdfRenderer($twig, new MPdfConverter((new FileHelperFactory($this))->create(), $cacheDir));
|
||||
$model = $this->getInvoiceModel();
|
||||
@@ -115,46 +115,85 @@ class PdfRendererTest extends KernelTestCase
|
||||
|
||||
/** @var FilesystemLoader $loader */
|
||||
$loader = $twig->getLoader();
|
||||
$loader->addPath(__DIR__ . '/../templates/', 'invoice');
|
||||
|
||||
$dirs = [
|
||||
__DIR__ . '/../../../templates/invoice/renderer/',
|
||||
];
|
||||
|
||||
$files = [];
|
||||
|
||||
$additionalTemplatesDir = __DIR__ . '/../../../var/templates';
|
||||
if (is_dir($additionalTemplatesDir)) {
|
||||
$dirs = [
|
||||
realpath($this->getInvoiceTemplatePath()),
|
||||
realpath(__DIR__ . '/../templates/'),
|
||||
realpath(__DIR__ . '/../../../var/invoices/'),
|
||||
];
|
||||
|
||||
foreach ($dirs as $dir) {
|
||||
if ($dir === false || !is_dir($dir)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$finder = new Finder();
|
||||
$finder
|
||||
->in($additionalTemplatesDir)
|
||||
->in($dir)
|
||||
->name('*.pdf.twig')
|
||||
->path('invoice-tpl/')
|
||||
->sortByName()
|
||||
->files();
|
||||
|
||||
foreach ($finder->getIterator() as $splFile) {
|
||||
$filename = $splFile->getRealPath();
|
||||
$files[] = $filename;
|
||||
$loader->addPath(\dirname($filename) . '/', 'invoice');
|
||||
if ($filename === false) {
|
||||
continue;
|
||||
}
|
||||
$dir = \dirname($filename) . '/';
|
||||
if (!\array_key_exists($dir, $files)) {
|
||||
$loader->addPath($dir . '/', 'invoice');
|
||||
}
|
||||
$files[$dir][] = $filename;
|
||||
}
|
||||
}
|
||||
|
||||
// search for custom templates, that shall not be shipped
|
||||
$dirs = [
|
||||
realpath(__DIR__ . '/../../../var/templates/'),
|
||||
];
|
||||
|
||||
foreach ($dirs as $dir) {
|
||||
if (!is_dir($dir)) {
|
||||
if ($dir === false || !is_dir($dir)) {
|
||||
continue;
|
||||
}
|
||||
$dir = realpath($dir);
|
||||
$loader->addPath($dir . '/', 'invoice');
|
||||
$found = glob($dir . '/*.pdf.twig');
|
||||
if ($found !== false) {
|
||||
$files = array_merge($files, $found);
|
||||
|
||||
$finder = new Finder();
|
||||
$finder
|
||||
->in($dir)
|
||||
->name('*.pdf.twig')
|
||||
->path('invoice-tpl/')
|
||||
->sortByName()
|
||||
->files();
|
||||
|
||||
foreach ($finder->getIterator() as $splFile) {
|
||||
$filename = $splFile->getRealPath();
|
||||
if ($filename === false) {
|
||||
continue;
|
||||
}
|
||||
$dir = \dirname($filename) . '/';
|
||||
if (!\array_key_exists($dir, $files)) {
|
||||
$loader->addPath($dir . '/', 'invoice');
|
||||
}
|
||||
$files[$dir][] = $filename;
|
||||
}
|
||||
}
|
||||
|
||||
$sut = new PdfRenderer($twig, new MPdfConverter((new FileHelperFactory($this))->create(), $cacheDir));
|
||||
$model = $this->getInvoiceModel();
|
||||
|
||||
foreach ($files as $filename) {
|
||||
$allFiles = [];
|
||||
|
||||
foreach ($files as $templates) {
|
||||
foreach ($templates as $filename) {
|
||||
$allFiles[] = $filename;
|
||||
}
|
||||
}
|
||||
|
||||
self::assertGreaterThanOrEqual(3, \count($allFiles));
|
||||
|
||||
foreach ($allFiles as $filename) {
|
||||
$document = new InvoiceDocument(new \SplFileInfo($filename));
|
||||
|
||||
$response = $sut->render($document, $model);
|
||||
|
||||
@@ -15,6 +15,7 @@ use App\Model\InvoiceDocument;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\Environment;
|
||||
@@ -93,34 +94,85 @@ class TwigRendererTest extends KernelTestCase
|
||||
|
||||
/** @var FilesystemLoader $loader */
|
||||
$loader = $twig->getLoader();
|
||||
$loader->addPath($this->getInvoiceTemplatePath(), 'invoice');
|
||||
|
||||
$dirs = [
|
||||
__DIR__ . '/../../../templates/invoice/renderer/',
|
||||
__DIR__ . '/../../../var/invoices/',
|
||||
__DIR__ . '/../../../var/invoices_customer/',
|
||||
__DIR__ . '/../../../var/invoices_old/',
|
||||
];
|
||||
|
||||
$files = [];
|
||||
|
||||
$dirs = [
|
||||
realpath($this->getInvoiceTemplatePath()),
|
||||
realpath(__DIR__ . '/../templates/'),
|
||||
realpath(__DIR__ . '/../../../var/invoices/'),
|
||||
];
|
||||
|
||||
foreach ($dirs as $dir) {
|
||||
if (!is_dir($dir)) {
|
||||
if ($dir === false || !is_dir($dir)) {
|
||||
continue;
|
||||
}
|
||||
$dir = realpath($dir);
|
||||
$loader->addPath($dir . '/', 'invoice');
|
||||
$found = glob($dir . '/*.html.twig');
|
||||
if ($found !== false) {
|
||||
$files = array_merge($files, $found);
|
||||
|
||||
$finder = new Finder();
|
||||
$finder
|
||||
->in($dir)
|
||||
->name('*.html.twig')
|
||||
->sortByName()
|
||||
->files();
|
||||
|
||||
foreach ($finder->getIterator() as $splFile) {
|
||||
$filename = $splFile->getRealPath();
|
||||
if ($filename === false) {
|
||||
continue;
|
||||
}
|
||||
$dir = \dirname($filename) . '/';
|
||||
if (!\array_key_exists($dir, $files)) {
|
||||
$loader->addPath($dir . '/', 'invoice');
|
||||
}
|
||||
$files[$dir][] = $filename;
|
||||
}
|
||||
}
|
||||
|
||||
// search for custom templates, that shall not be shipped
|
||||
$dirs = [
|
||||
realpath(__DIR__ . '/../../../var/templates/'),
|
||||
];
|
||||
|
||||
foreach ($dirs as $dir) {
|
||||
if ($dir === false || !is_dir($dir)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$finder = new Finder();
|
||||
$finder
|
||||
->in($dir)
|
||||
->name('*.html.twig')
|
||||
->path('invoice-tpl/')
|
||||
->sortByName()
|
||||
->files();
|
||||
|
||||
foreach ($finder->getIterator() as $splFile) {
|
||||
$filename = $splFile->getRealPath();
|
||||
if ($filename === false) {
|
||||
continue;
|
||||
}
|
||||
$dir = \dirname($filename) . '/';
|
||||
if (!\array_key_exists($dir, $files)) {
|
||||
$loader->addPath($dir . '/', 'invoice');
|
||||
}
|
||||
$files[$dir][] = $filename;
|
||||
}
|
||||
}
|
||||
|
||||
$allFiles = [];
|
||||
|
||||
foreach ($files as $templates) {
|
||||
foreach ($templates as $filename) {
|
||||
$allFiles[] = $filename;
|
||||
}
|
||||
}
|
||||
|
||||
self::assertGreaterThanOrEqual(2, \count($allFiles));
|
||||
|
||||
$sut = new TwigRenderer($twig);
|
||||
|
||||
$model = $this->getInvoiceModel();
|
||||
$model->getTemplate()->setLanguage('de');
|
||||
|
||||
foreach ($files as $filename) {
|
||||
foreach ($allFiles as $filename) {
|
||||
$document = new InvoiceDocument(new \SplFileInfo($filename));
|
||||
|
||||
$response = $sut->render($document, $model);
|
||||
|
||||
@@ -18,10 +18,10 @@ use Twig\Environment;
|
||||
|
||||
class HtmlRendererFactoryMock extends AbstractMockFactory
|
||||
{
|
||||
public function create(): HtmlRendererFactory
|
||||
public function create(?Environment $environment = null): HtmlRendererFactory
|
||||
{
|
||||
return new HtmlRendererFactory(
|
||||
$this->createMock(Environment::class),
|
||||
$environment ?? $this->createMock(Environment::class),
|
||||
$this->createMock(EventDispatcherInterface::class),
|
||||
$this->createMock(ProjectStatisticService::class),
|
||||
$this->createMock(ActivityStatisticService::class)
|
||||
|
||||
@@ -22,7 +22,7 @@ use Twig\Environment;
|
||||
|
||||
class PdfRendererFactoryMock extends AbstractMockFactory
|
||||
{
|
||||
public function create(): PdfRendererFactory
|
||||
public function create(?Environment $environment = null): PdfRendererFactory
|
||||
{
|
||||
$converter = new ColumnConverter(
|
||||
$this->createMock(EventDispatcherInterface::class),
|
||||
@@ -31,7 +31,7 @@ class PdfRendererFactoryMock extends AbstractMockFactory
|
||||
);
|
||||
|
||||
return new PdfRendererFactory(
|
||||
$this->createMock(Environment::class),
|
||||
$environment ?? $this->createMock(Environment::class),
|
||||
$this->createMock(HtmlToPdfConverter::class),
|
||||
$this->createMock(ProjectStatisticService::class),
|
||||
$this->createMock(LocaleSwitcher::class),
|
||||
|
||||
@@ -1,66 +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\Twig\SecurityPolicy;
|
||||
|
||||
use App\Twig\SecurityPolicy\ChainPolicy;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Twig\Sandbox\SecurityPolicyInterface;
|
||||
|
||||
#[CoversClass(ChainPolicy::class)]
|
||||
class ChainPolicyTest extends TestCase
|
||||
{
|
||||
public function testCheckSecurity(): void
|
||||
{
|
||||
$policy1 = $this->createMock(SecurityPolicyInterface::class);
|
||||
$policy1->expects(self::once())->method('checkSecurity')->with(['tag'], ['filter'], ['function']);
|
||||
|
||||
$policy2 = $this->createMock(SecurityPolicyInterface::class);
|
||||
$policy2->expects(self::once())->method('checkSecurity')->with(['tag'], ['filter'], ['function']);
|
||||
|
||||
$sut = new ChainPolicy();
|
||||
$sut->addPolicy($policy1);
|
||||
$sut->addPolicy($policy2);
|
||||
|
||||
$sut->checkSecurity(['tag'], ['filter'], ['function']);
|
||||
}
|
||||
|
||||
public function testCheckMethodAllowed(): void
|
||||
{
|
||||
$obj = new \stdClass();
|
||||
$policy1 = $this->createMock(SecurityPolicyInterface::class);
|
||||
$policy1->expects(self::once())->method('checkMethodAllowed')->with($obj, 'method');
|
||||
|
||||
$policy2 = $this->createMock(SecurityPolicyInterface::class);
|
||||
$policy2->expects(self::once())->method('checkMethodAllowed')->with($obj, 'method');
|
||||
|
||||
$sut = new ChainPolicy();
|
||||
$sut->addPolicy($policy1);
|
||||
$sut->addPolicy($policy2);
|
||||
|
||||
$sut->checkMethodAllowed($obj, 'method');
|
||||
}
|
||||
|
||||
public function testCheckPropertyAllowed(): void
|
||||
{
|
||||
$obj = new \stdClass();
|
||||
$policy1 = $this->createMock(SecurityPolicyInterface::class);
|
||||
$policy1->expects(self::once())->method('checkPropertyAllowed')->with($obj, 'property');
|
||||
|
||||
$policy2 = $this->createMock(SecurityPolicyInterface::class);
|
||||
$policy2->expects(self::once())->method('checkPropertyAllowed')->with($obj, 'property');
|
||||
|
||||
$sut = new ChainPolicy();
|
||||
$sut->addPolicy($policy1);
|
||||
$sut->addPolicy($policy2);
|
||||
|
||||
$sut->checkPropertyAllowed($obj, 'property');
|
||||
}
|
||||
}
|
||||
@@ -1,23 +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\Twig\SecurityPolicy;
|
||||
|
||||
use App\Twig\SecurityPolicy\DefaultPolicy;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Twig\Sandbox\SecurityPolicyInterface;
|
||||
|
||||
#[CoversClass(DefaultPolicy::class)]
|
||||
class DefaultPolicyTest extends AbstractPolicyTestCase
|
||||
{
|
||||
protected function createPolicy(): SecurityPolicyInterface
|
||||
{
|
||||
return new DefaultPolicy();
|
||||
}
|
||||
}
|
||||
@@ -1,23 +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\Twig\SecurityPolicy;
|
||||
|
||||
use App\Twig\SecurityPolicy\ExportPolicy;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Twig\Sandbox\SecurityPolicyInterface;
|
||||
|
||||
#[CoversClass(ExportPolicy::class)]
|
||||
class ExportPolicyTest extends AbstractPolicyTestCase
|
||||
{
|
||||
protected function createPolicy(): SecurityPolicyInterface
|
||||
{
|
||||
return new ExportPolicy();
|
||||
}
|
||||
}
|
||||
@@ -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\Twig\SecurityPolicy;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Pdf\PdfContext;
|
||||
use App\Twig\SecurityPolicy\InvoicePolicy;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\Twig\AppVariable;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\ServerBag;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\String\UnicodeString;
|
||||
use Twig\Sandbox\SecurityNotAllowedMethodError;
|
||||
use Twig\Sandbox\SecurityPolicyInterface;
|
||||
|
||||
#[CoversClass(InvoicePolicy::class)]
|
||||
class InvoicePolicyTest extends TestCase
|
||||
{
|
||||
protected function createPolicy(): SecurityPolicyInterface
|
||||
{
|
||||
return new InvoicePolicy();
|
||||
}
|
||||
|
||||
public function testCheckSecurity(): void
|
||||
{
|
||||
$sut = $this->createPolicy();
|
||||
$sut->checkSecurity([], [], []);
|
||||
$this->expectNotToPerformAssertions();
|
||||
}
|
||||
|
||||
#[DataProvider('getCheckMethodAllowedData')]
|
||||
public function testCheckMethodAllowed(object $obj, string $method, ?string $expectedExceptionMessage = null): void
|
||||
{
|
||||
$sut = $this->createPolicy();
|
||||
|
||||
if ($expectedExceptionMessage !== null) {
|
||||
$this->expectException(SecurityNotAllowedMethodError::class);
|
||||
$this->expectExceptionMessage($expectedExceptionMessage);
|
||||
}
|
||||
|
||||
$sut->checkMethodAllowed($obj, $method);
|
||||
|
||||
if ($expectedExceptionMessage === null) {
|
||||
$this->expectNotToPerformAssertions();
|
||||
}
|
||||
}
|
||||
|
||||
public static function getCheckMethodAllowedData(): array
|
||||
{
|
||||
return [
|
||||
[new ServerBag(), 'get', 'Tried to access server environment'],
|
||||
[self::createStub(SessionInterface::class), 'getId', 'Tried to access session'],
|
||||
[new \stdClass(), 'foo', 'Tried to access non-read method'],
|
||||
[new \stdClass(), 'setFoo', 'Tried to access non-read method'],
|
||||
[new \stdClass(), 'getFoo'],
|
||||
[new \stdClass(), 'hasFoo'],
|
||||
[new \stdClass(), 'isFoo'],
|
||||
[new UnicodeString(), '__toString'],
|
||||
// Request
|
||||
[new Request(), 'get', null],
|
||||
[new Request(), 'isXmlHttpRequest', 'Tried to call setter() of app variable'],
|
||||
[new Request(), 'hasSession', 'Tried to call setter() of app variable'],
|
||||
// PdfContext
|
||||
[new PdfContext(), 'setOption'],
|
||||
[new PdfContext(), 'getOption', 'Tried to access forbidden method on PdfContext'],
|
||||
// AppVariable
|
||||
[new AppVariable(), 'getRequest'],
|
||||
[new AppVariable(), 'getUser'],
|
||||
[new AppVariable(), 'getLocale'],
|
||||
[new AppVariable(), 'getCharset', 'Tried to access forbidden app variable method'],
|
||||
// User
|
||||
[new User(), 'getUsername'],
|
||||
[new User(), 'getPassword', 'Tried to access user secrets'],
|
||||
[new User(), 'getTotpSecret', 'Tried to access user secrets'],
|
||||
[new User(), 'getPlainPassword', 'Tried to access user secrets'],
|
||||
[new User(), 'getConfirmationToken', 'Tried to access user secrets'],
|
||||
[new User(), 'getTotpAuthenticationConfiguration', 'Tried to access user secrets'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ namespace App\Tests\Twig\SecurityPolicy;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Pdf\PdfContext;
|
||||
use App\Twig\SecurityPolicy\StrictPolicy;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\Twig\AppVariable;
|
||||
@@ -21,9 +23,13 @@ use Symfony\Component\String\UnicodeString;
|
||||
use Twig\Sandbox\SecurityNotAllowedMethodError;
|
||||
use Twig\Sandbox\SecurityPolicyInterface;
|
||||
|
||||
abstract class AbstractPolicyTestCase extends TestCase
|
||||
#[CoversClass(StrictPolicy::class)]
|
||||
class StrictPolicyTestCase extends TestCase
|
||||
{
|
||||
abstract protected function createPolicy(): SecurityPolicyInterface;
|
||||
private function createPolicy(): SecurityPolicyInterface
|
||||
{
|
||||
return new StrictPolicy();
|
||||
}
|
||||
|
||||
public function testCheckSecurity(): void
|
||||
{
|
||||
Reference in New Issue
Block a user