Type and escape Twig asset tags, improve timeago plural forms, and cover installer debug-mode installed check (#3631)

* Initial plan

* Harden Twig asset tags and improve installer debug test coverage

* Finalize Twig escaping, pluralization, and installer debug coverage fixes

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
Copilot authored and GitHub committed 2026-05-28 18:40:16 +01:00
1 parent 146d43dfcf
commit 6a01a07ffd
3 files changed
+59 -21

No files matched your search

@@ -304,7 +304,7 @@ class FOSSBillingExtension
}
#[AsTwigFilter('script_tag', isSafe: ['html'])]
public function scriptTag($path): string
public function scriptTag(string $path): string
{
if ($this->isAssetLoaded($path)) {
return '';
@@ -312,11 +312,14 @@ class FOSSBillingExtension
$this->markAssetAsLoaded($path);
return sprintf('<script src="%s?%s"></script>', $path, $this->getCacheBuster($path));
$escapedPath = htmlspecialchars($path, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$escapedCacheBuster = htmlspecialchars($this->getCacheBuster($path), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
return sprintf('<script src="%s?%s"></script>', $escapedPath, $escapedCacheBuster);
}
#[AsTwigFilter('stylesheet_tag', isSafe: ['html'])]
public function stylesheetTag($path, $media = 'screen'): string
public function stylesheetTag(string $path, string $media = 'screen'): string
{
if ($this->isAssetLoaded($path)) {
return '';
@@ -324,7 +327,11 @@ class FOSSBillingExtension
$this->markAssetAsLoaded($path);
return sprintf('<link rel="stylesheet" type="text/css" href="%s?v=%s" media="%s" />', $path, $this->getCacheBuster($path), $media);
$escapedPath = htmlspecialchars($path, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$escapedCacheBuster = htmlspecialchars($this->getCacheBuster($path), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$escapedMedia = htmlspecialchars($media, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
return sprintf('<link rel="stylesheet" type="text/css" href="%s?v=%s" media="%s" />', $escapedPath, $escapedCacheBuster, $escapedMedia);
}
#[AsTwigFilter('timeago')]
@@ -340,22 +347,23 @@ class FOSSBillingExtension
return '-';
}
$tokens = [
315_705_600 => __trans('decade'),
31_570_560 => __trans('year'),
2_630_880 => __trans('month'),
604_800 => __trans('week'),
86400 => __trans('day'),
3600 => __trans('hour'),
60 => __trans('minute'),
1 => __trans('second'),
315_705_600 => ['one' => __trans('decade'), 'other' => __trans('decades')],
31_570_560 => ['one' => __trans('year'), 'other' => __trans('years')],
2_630_880 => ['one' => __trans('month'), 'other' => __trans('months')],
604_800 => ['one' => __trans('week'), 'other' => __trans('weeks')],
86400 => ['one' => __trans('day'), 'other' => __trans('days')],
3600 => ['one' => __trans('hour'), 'other' => __trans('hours')],
60 => ['one' => __trans('minute'), 'other' => __trans('minutes')],
1 => ['one' => __trans('second'), 'other' => __trans('seconds')],
];
foreach ($tokens as $unit => $text) {
foreach ($tokens as $unit => $forms) {
if ($timeAgo < $unit) {
continue;
}
$numberOfUnits = floor($timeAgo / $unit);
$numberOfUnits = (int) floor($timeAgo / $unit);
$text = ($numberOfUnits === 1) ? $forms['one'] : $forms['other'];
return sprintf('%d %s%s', $numberOfUnits, $text, ($numberOfUnits > 1) ? 's' : '');
return sprintf('%d %s', $numberOfUnits, $text);
}
return '';
@@ -3,6 +3,7 @@
declare(strict_types=1);
use FOSSBilling\Twig\Extension\FOSSBillingExtension;
use Pimple\Container;
#[PHPUnit\Framework\Attributes\Group('Core')]
final class FOSSBillingExtensionTest extends PHPUnit\Framework\TestCase
@@ -11,7 +12,7 @@ final class FOSSBillingExtensionTest extends PHPUnit\Framework\TestCase
protected function setUp(): void
{
$this->extension = new FOSSBillingExtension(null);
$this->extension = new FOSSBillingExtension(new Container());
}
public function testTransWithoutValues(): void
@@ -64,4 +65,26 @@ final class FOSSBillingExtensionTest extends PHPUnit\Framework\TestCase
$this->assertSame('&lt;strong&gt;Fallback&lt;/strong&gt;', $result);
}
public function testScriptTagEscapesPath(): void
{
$result = $this->extension->scriptTag('js/app".js');
$this->assertStringContainsString('src="js/app&quot;.js?', $result);
}
public function testStylesheetTagEscapesPathAndMedia(): void
{
$result = $this->extension->stylesheetTag('css/app".css', 'screen" media');
$this->assertStringContainsString('href="css/app&quot;.css?v=', $result);
$this->assertStringContainsString('media="screen&quot; media"', $result);
}
public function testTimeagoUsesPluralFormsWithoutSuffixingS(): void
{
$result = $this->extension->timeago(date('Y-m-d H:i:s', time() - 120));
$this->assertSame('2 minutes', $result);
}
}
@@ -91,6 +91,13 @@ final class FOSSBilling_ConfigTest extends PHPUnit\Framework\TestCase
$this->assertSame('installed', $this->runInstallerInstalledCheck());
}
public function testInstallerTreatsExistingConfigAsNotInstalledInDebugMode(): void
{
$this->writeRawConfig("<?php return ['debug_and_monitoring' => ['debug' => true]];");
$this->assertSame('not-installed', $this->runInstallerInstalledCheck());
}
public function testInstallerAllowsInstallWhenConfigFileIsMissing(): void
{
$this->removeConfig();
@@ -128,11 +135,11 @@ final class FOSSBilling_ConfigTest extends PHPUnit\Framework\TestCase
private function runInstallerInstalledCheck(): string
{
$code = <<<'PHP'
require 'src/install/install.php';
$request = Symfony\Component\HttpFoundation\Request::create('http://localhost/install/install.php', 'GET');
$installer = new FOSSBilling_Installer($request);
echo $installer->isAlreadyInstalled() ? 'installed' : 'not-installed';
PHP;
require 'src/install/install.php';
$request = Symfony\Component\HttpFoundation\Request::create('http://localhost/install/install.php', 'GET');
$installer = new FOSSBilling_Installer($request);
echo $installer->isAlreadyInstalled() ? 'installed' : 'not-installed';
PHP;
$pipes = [];
$process = proc_open(