Part-DB.Part-DB-server/tests/Services/LabelSystem/SandboxedTwigFactoryTest.php

148 lines
5.1 KiB
PHP
Raw Normal View History

<?php
2022-11-29 21:21:26 +01:00
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-05-10 21:39:31 +02:00
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LabelSystem;
use App\Entity\LabelSystem\LabelOptions;
2023-06-12 23:39:30 +02:00
use App\Entity\LabelSystem\LabelProcessMode;
use App\Entity\LabelSystem\LabelSupportedElement;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Entity\Parts\StorageLocation;
use App\Services\LabelSystem\SandboxedTwigFactory;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Twig\Sandbox\SecurityError;
class SandboxedTwigFactoryTest extends WebTestCase
{
private ?SandboxedTwigFactory $service = null;
2020-05-10 21:39:31 +02:00
protected function setUp(): void
{
self::bootKernel();
$this->service = self::getContainer()->get(SandboxedTwigFactory::class);
}
2024-06-22 00:31:43 +02:00
public function twigDataProvider(): \Iterator
{
2024-06-22 00:31:43 +02:00
yield [' {% for i in range(1, 3) %}
{{ part.id }}
{{ part.name }}
{{ part.lastModified | format_datetime }}
{% endfor %}
2024-06-22 00:31:43 +02:00
'];
yield [' {% if part.category %}
{{ part.category }}
{% endif %}
2024-06-22 00:31:43 +02:00
'];
yield [' {% set a = random(1, 3) %}
{{ 1 + 2 | abs }}
{{ "test" | capitalize | escape | lower | raw }}
{{ "\n" | nl2br | trim | title | url_encode | reverse }}
2024-06-22 00:31:43 +02:00
'];
yield ['
{{ location.isRoot}} {{ location.isChildOf(location) }} {{ location.comment }} {{ location.level }}
2024-11-08 23:32:14 +01:00
{{ location.fullPath }} {% set arr = location.pathArray %} {% set child = location.children %} {{location.notSelectable}}
2024-06-22 00:31:43 +02:00
'];
yield ['
2024-11-08 23:32:14 +01:00
{{ part.needsReview }} {{ part.tags }} {{ part.mass }}
2024-06-22 00:31:43 +02:00
'];
yield ['
{{ entity_type(part) is object }}
2024-06-22 00:31:43 +02:00
'];
yield ['
{% apply placeholders(part) %}[[NAME]]{% endapply %}</br>
{{ placeholder("[[NAME]]", part) }}
2024-06-22 00:31:43 +02:00
'];
}
2024-06-22 00:31:43 +02:00
public function twigNotAllowedDataProvider(): \Iterator
{
2024-06-22 00:31:43 +02:00
yield ['{% block test %} {% endblock %}'];
yield ['{% deprecated test %}'];
yield ['{% flush %}'];
yield ["{{ part.setName('test') }}"];
yield ['{{ part.setCategory(null) }}'];
}
/**
* @dataProvider twigDataProvider
*/
2020-05-10 21:39:31 +02:00
public function testTwigFeatures(string $twig): void
{
$options = new LabelOptions();
2023-06-12 23:39:30 +02:00
$options->setSupportedElement(LabelSupportedElement::PART);
$options->setLines($twig);
2023-06-12 23:39:30 +02:00
$options->setProcessMode(LabelProcessMode::TWIG);
$twig = $this->service->createTwig($options);
$str = $twig->render('lines', [
'part' => new Part(),
'lot' => new PartLot(),
'location' => new StorageLocation(),
]);
$this->assertIsString($str);
}
/**
* @dataProvider twigNotAllowedDataProvider
*/
2020-05-10 21:39:31 +02:00
public function testTwigForbidden(string $twig): void
{
$this->expectException(SecurityError::class);
$options = new LabelOptions();
2023-06-12 23:39:30 +02:00
$options->setSupportedElement(LabelSupportedElement::PART);
$options->setLines($twig);
2023-06-12 23:39:30 +02:00
$options->setProcessMode(LabelProcessMode::TWIG);
$twig = $this->service->createTwig($options);
$str = $twig->render('lines', [
'part' => new Part(),
'lot' => new PartLot(),
'location' => new StorageLocation(),
]);
2023-04-15 23:14:53 +02:00
$this->assertIsString($str);
}
}