mirror of
https://github.com/Icinga/icingadb-web.git
synced 2026-05-28 04:36:06 -04:00
PluginOutput::render(): Shorten the output by characterLimit before processing it
If the shorten output contained (not properly closed) HTML element, it merged the next list-item into the same html tag, because the closing tag was missing. So we therefor shorten the output by characterLimit before proccessing it
This commit is contained in:
parent
cdbaea02a6
commit
2fefc75552
2 changed files with 130 additions and 4 deletions
|
|
@ -174,6 +174,8 @@ class PluginOutput extends HtmlString
|
|||
$output = PluginOutputHook::processOutput($output, $this->commandName, $this->enrichOutput);
|
||||
}
|
||||
|
||||
$output = substr($output, 0, $this->characterLimit);
|
||||
|
||||
if (preg_match('~<\w+(?>\s\w+=[^>]*)?>~', $output)) {
|
||||
// HTML
|
||||
$output = HtmlPurifier::process(preg_replace(
|
||||
|
|
@ -202,10 +204,6 @@ class PluginOutput extends HtmlString
|
|||
$output = $this->processHtml($output);
|
||||
}
|
||||
|
||||
if ($this->characterLimit) {
|
||||
$output = substr($output, 0, $this->characterLimit);
|
||||
}
|
||||
|
||||
$this->renderedOutput = $output;
|
||||
|
||||
return $output;
|
||||
|
|
|
|||
128
test/php/library/Icingadb/Util/PluginOutputTest.php
Normal file
128
test/php/library/Icingadb/Util/PluginOutputTest.php
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
|
||||
/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */
|
||||
|
||||
namespace Tests\Icinga\Module\Icingadb\Util;
|
||||
|
||||
use Icinga\Module\Icingadb\Util\PluginOutput;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PluginOutputTest extends TestCase
|
||||
{
|
||||
public function testRenderPlainText()
|
||||
{
|
||||
$input = 'This is a plain text';
|
||||
$expectedOutput = $input;
|
||||
|
||||
$this->assertSame(
|
||||
$expectedOutput,
|
||||
(new PluginOutput($input))->render(),
|
||||
'PluginOutput::render does not return expected values'
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenderTextWithStates()
|
||||
{
|
||||
$input = <<<'INPUT'
|
||||
[OK] Dummy state
|
||||
\_ [OK] Fake "state"
|
||||
\_ [WARNING] Fake state again
|
||||
INPUT;
|
||||
|
||||
$expectedOutput = <<<'EXPECTED_OUTPUT'
|
||||
<span class="state-ball ball-size-m state-ok"></span> Dummy state
|
||||
\_ <span class="state-ball ball-size-m state-ok"></span> Fake "state"
|
||||
\_ <span class="state-ball ball-size-m state-warning"></span> Fake state again
|
||||
EXPECTED_OUTPUT;
|
||||
|
||||
$this->assertSame(
|
||||
$expectedOutput,
|
||||
(new PluginOutput($input))->render(),
|
||||
'PluginOutput::render does not return expected values'
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenderTextWithStatesAndCharacterLimit()
|
||||
{
|
||||
$input = <<<'INPUT'
|
||||
[OK] Dummy state
|
||||
\_ [OK] Fake "state"
|
||||
\_ [WARNING] Fake state again
|
||||
INPUT;
|
||||
|
||||
$expectedOutput = <<<'EXPECTED_OUTPUT'
|
||||
<span class="state-ball ball-size-m state-ok"></span> Dummy
|
||||
EXPECTED_OUTPUT;
|
||||
|
||||
$this->assertSame(
|
||||
$expectedOutput,
|
||||
(new PluginOutput($input))->setCharacterLimit(10)->render(),
|
||||
'PluginOutput::render does not return expected values'
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenderTextWithHtml()
|
||||
{
|
||||
$input = <<<'INPUT'
|
||||
Hello <h3>World</h3>, this "is" 'a <strong>test</strong>.
|
||||
INPUT;
|
||||
|
||||
$expectedOutput = <<<'EXPECTED_OUTPUT'
|
||||
Hello <h3>World</h3>, this "is" 'a <strong>test</strong>.
|
||||
EXPECTED_OUTPUT;
|
||||
|
||||
$this->assertSame(
|
||||
$expectedOutput,
|
||||
(new PluginOutput($input))->render(),
|
||||
'PluginOutput::render does not return expected values'
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenderTextWithHtmlAndStates()
|
||||
{
|
||||
$input = <<<'INPUT'
|
||||
Hello <h3>World</h3>, this "is" a <strong>test</strong>.
|
||||
[OK] Dummy state
|
||||
\_ [OK] Fake "state"
|
||||
\_ [WARNING] Fake state again
|
||||
text <span> ends </span> here
|
||||
INPUT;
|
||||
|
||||
$expectedOutput = <<<'EXPECTED_OUTPUT'
|
||||
Hello <h3>World</h3>, this "is" a <strong>test</strong>.
|
||||
<span class="state-ball ball-size-m state-ok"></span> Dummy state
|
||||
\_ <span class="state-ball ball-size-m state-ok"></span> Fake "state"
|
||||
\_ <span class="state-ball ball-size-m state-warning"></span> Fake state again
|
||||
text <span> ends </span> here
|
||||
EXPECTED_OUTPUT;
|
||||
|
||||
$this->assertSame(
|
||||
$expectedOutput,
|
||||
(new PluginOutput($input))->render(),
|
||||
'PluginOutput::render does not return expected values'
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenderTextWithHtmlIncludingStatesAndSpecialChars()
|
||||
{
|
||||
$input = <<<'INPUT'
|
||||
Hello <h3>World</h3>, this "is" a <strong>test</strong>.
|
||||
[OK] Dummy state
|
||||
special chars: !@#$%^&*()_+{}|:"<>?`-=[]\;',./
|
||||
text <span> ends </span> here
|
||||
INPUT;
|
||||
|
||||
$expectedOutput = <<<'EXPECTED_OUTPUT'
|
||||
Hello <h3>World</h3>, this "is" a <strong>test</strong>.
|
||||
<span class="state-ball ball-size-m state-ok"></span> Dummy state
|
||||
special chars: !@#$%^&*()_+{}|:"<>?`-=[]\;',​./
|
||||
text <span> ends </span> here
|
||||
EXPECTED_OUTPUT;
|
||||
|
||||
$this->assertSame(
|
||||
$expectedOutput,
|
||||
(new PluginOutput($input))->render(),
|
||||
'PluginOutput::render does not return expected values'
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue