Skip to content

Commit

Permalink
PluginOutput::render(): Shorten the output by characterLimit before…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
sukhwinder33445 committed Jun 24, 2024
1 parent 6029cc1 commit caaa390
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 4 deletions.
8 changes: 4 additions & 4 deletions library/Icingadb/Util/PluginOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ public function render()
$output = PluginOutputHook::processOutput($output, $this->commandName, $this->enrichOutput);
}

if (strlen($output) > $this->characterLimit) {
$output = substr($output, 0, $this->characterLimit);
}

if (preg_match('~<\w+(?>\s\w+=[^>]*)?>~', $output)) {
// HTML
$output = HtmlPurifier::process(preg_replace(
Expand Down Expand Up @@ -202,10 +206,6 @@ public function render()
$output = $this->processHtml($output);
}

if ($this->characterLimit) {
$output = substr($output, 0, $this->characterLimit);
}

$this->renderedOutput = $output;

return $output;
Expand Down
128 changes: 128 additions & 0 deletions test/php/library/Icingadb/Util/PluginOutputTest.php
Original file line number Diff line number Diff line change
@@ -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 &quot;state&quot;
\_ <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: !@#$%^&amp;*()_+{}|:"&lt;&gt;?`-=[]\;',&#x200B;./
text <span> ends </span> here
EXPECTED_OUTPUT;

$this->assertSame(
$expectedOutput,
(new PluginOutput($input))->render(),
'PluginOutput::render does not return expected values'
);
}
}

0 comments on commit caaa390

Please sign in to comment.