Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^13.4.6",
"phpunit/phpunit": "^11.0 || ^13.4.6",
"rector/rector": "^2.0",
"xoops/rector-xoops": "^1.0@alpha"
},
Expand Down
29 changes: 18 additions & 11 deletions include/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ function _debugbar_patch_vendor_assets(string $destDir): bool
[
" if (v._vd) return (v._vd[2] || '') + ' {…}';",
" if (v._vd) return this.esc(v._vd[2] || '') + ' {…}';",
" if (vd) return (typeof vd[2] === 'string' ? this.esc(vd[2]) : '') + ' {…}';",
],
],
'widgets/mails/widget.js' => [
Expand All @@ -423,7 +424,10 @@ function _debugbar_patch_vendor_assets(string $destDir): bool
],
'widgets/http/widget.js' => [
[
' PhpDebugBar.Widgets.renderValueInto(valueTd, request.details[key]);',
[
' PhpDebugBar.Widgets.renderValueInto(valueTd, request.details[key]);',
' PhpDebugBar.Widgets.renderValueInto(valueTd, request.details[key], false, request.details_html);',
],
' PhpDebugBar.Widgets.renderSafeValueInto(valueTd, request.details[key]);',
],
],
Expand All @@ -433,7 +437,10 @@ function _debugbar_patch_vendor_assets(string $destDir): bool
" name.textContent = String(tpl.name ?? tpl.html ?? '');",
],
[
' PhpDebugBar.Widgets.renderValueInto(valueTd, tpl.params[key]);',
[
' PhpDebugBar.Widgets.renderValueInto(valueTd, tpl.params[key]);',
' PhpDebugBar.Widgets.renderValueInto(valueTd, tpl.params[key], false, tpl.params_html);',
],
' PhpDebugBar.Widgets.renderSafeValueInto(valueTd, tpl.params[key]);',
],
],
Expand All @@ -458,15 +465,15 @@ function _debugbar_patch_vendor_assets(string $destDir): bool
}

$patched = $contents;
foreach ($replacements as [$search, $replacement]) {
if (str_contains($patched, $search)) {
$patched = str_replace($search, $replacement, $patched);

continue;
}

// Keep direct calls idempotent while still detecting vendor drift.
if (str_contains($patched, $replacement)) {
foreach ($replacements as $patch) {
[$search, $replacement] = $patch;
$patched = str_replace($search, $replacement, $patched, $matches);

// Recognize our replacement and the upstream VarDumper fix in 3.8.1.
$upstreamReplacement = $patch[2] ?? null;
if ($matches > 0 || str_contains($patched, $replacement)
|| ($upstreamReplacement !== null && str_contains($patched, $upstreamReplacement))
) {
continue;
}

Expand Down
96 changes: 96 additions & 0 deletions tests/Unit/InstallAssetsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace XoopsModules\Debugbar\Tests\Unit;

use FilesystemIterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

final class InstallAssetsTest extends TestCase
{
/** @return iterable<string, array{bool}> */
public static function vendorLayouts(): iterable
{
yield '3.8.0' => [false];
yield '3.8.1 (XOOPS 2.7.5 Beta 2)' => [true];
}

#[DataProvider('vendorLayouts')]
#[RunInSeparateProcess]
#[PreserveGlobalState(false)]
public function testPatchesVendorAssets(bool $hasUpstreamFix): void
{
$root = dirname(__DIR__, 2);
$assets = sys_get_temp_dir() . '/debugbar-assets-' . bin2hex(random_bytes(8));
if (! defined('XOOPS_ROOT_PATH')) {
define('XOOPS_ROOT_PATH', $root);
}
require_once $root . '/include/install.php';

self::assertTrue(mkdir($assets));

try {
$vendor = $root . '/vendor/php-debugbar/php-debugbar/resources';
foreach (['openhandler.js', 'highlight.css', 'vardumper.css', 'widgets/mails/widget.js'] as $file) {
$dir = dirname($assets . '/' . $file);
if (! is_dir($dir)) {
self::assertTrue(mkdir($dir, 0755, true));
}
self::assertTrue(copy($vendor . '/' . $file, $assets . '/' . $file));
}

$preview = $hasUpstreamFix
? " if (vd) return (typeof vd[2] === 'string' ? this.esc(vd[2]) : '') + ' {…}';"
: " if (v._vd) return (v._vd[2] || '') + ' {…}';";
file_put_contents($assets . '/vardumper.js', $preview);
$templateTitle = " if (tpl.html) {\n name.innerHTML = tpl.html;\n"
. " } else {\n name.textContent = tpl.name;\n }\n";
foreach ([
'http' => ['request.details[key]', 'request.details_html', ''],
'templates' => ['tpl.params[key]', 'tpl.params_html', $templateTitle],
] as $widget => [$value, $htmlFlag, $source]) {
self::assertTrue(mkdir($assets . '/widgets/' . $widget));
$args = $hasUpstreamFix ? ", false, $htmlFlag" : '';
file_put_contents(
$assets . '/widgets/' . $widget . '/widget.js',
$source . " PhpDebugBar.Widgets.renderValueInto(valueTd, $value$args);"
);
}

self::assertTrue(_debugbar_patch_vendor_assets($assets));
self::assertTrue(_debugbar_patch_vendor_assets($assets));
self::assertStringEqualsFile(
$assets . '/vardumper.js',
$hasUpstreamFix ? $preview : " if (v._vd) return this.esc(v._vd[2] || '') + ' {…}';"
);
self::assertStringEqualsFile(
$assets . '/widgets/http/widget.js',
' PhpDebugBar.Widgets.renderSafeValueInto(valueTd, request.details[key]);'
);
self::assertStringEqualsFile(
$assets . '/widgets/templates/widget.js',
" name.textContent = String(tpl.name ?? tpl.html ?? '');\n"
. ' PhpDebugBar.Widgets.renderSafeValueInto(valueTd, tpl.params[key]);'
);
} finally {
$entries = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($assets, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($entries as $entry) {
if ($entry->isDir()) {
rmdir($entry->getPathname());
} else {
unlink($entry->getPathname());
}
}
rmdir($assets);
}
}
}
Loading