2024-06-17 07:07:02 -04:00
|
|
|
<?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
|
|
|
|
|
{
|
2024-07-23 09:21:28 -04:00
|
|
|
public function checkOutput(string $expected, string $input, int $characterLimit = 0): void
|
|
|
|
|
{
|
|
|
|
|
$p = new PluginOutput($input);
|
|
|
|
|
|
|
|
|
|
if ($characterLimit) {
|
|
|
|
|
$p->setCharacterLimit($characterLimit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertSame($expected, $p->render(), 'PluginOutput::render does not return expected values');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function testRenderPlainText(): void
|
2024-06-17 07:07:02 -04:00
|
|
|
{
|
|
|
|
|
$input = 'This is a plain text';
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
$this->checkOutput($input, $input);
|
2024-06-17 07:07:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
public function testRenderTextWithStates(): void
|
2024-06-17 07:07:02 -04:00
|
|
|
{
|
|
|
|
|
$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;
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
$this->checkOutput($expectedOutput, $input);
|
2024-06-17 07:07:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
public function testRenderTextWithStatesAndCharacterLimit(): void
|
2024-06-17 07:07:02 -04:00
|
|
|
{
|
|
|
|
|
$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;
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
$this->checkOutput($expectedOutput, $input, 10);
|
2024-06-17 07:07:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
public function testRenderTextWithHtml(): void
|
2024-06-17 07:07:02 -04:00
|
|
|
{
|
|
|
|
|
$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;
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
$this->checkOutput($expectedOutput, $input);
|
2024-06-17 07:07:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
public function testRenderTextWithHtmlAndStates(): void
|
2024-06-17 07:07:02 -04:00
|
|
|
{
|
|
|
|
|
$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;
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
$this->checkOutput($expectedOutput, $input);
|
2024-06-17 07:07:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
public function testRenderTextWithHtmlIncludingStatesAndSpecialChars(): void
|
2024-06-17 07:07:02 -04:00
|
|
|
{
|
|
|
|
|
$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
|
2024-07-23 09:21:28 -04:00
|
|
|
special chars: !@#$%^&*()_+{}|:"<>?`-=[]\;',​./
|
2024-06-17 07:07:02 -04:00
|
|
|
text <span> ends </span> here
|
|
|
|
|
EXPECTED_OUTPUT;
|
|
|
|
|
|
2024-07-23 09:21:28 -04:00
|
|
|
$this->checkOutput($expectedOutput, $input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testOutputWithNewlines(): void
|
|
|
|
|
{
|
|
|
|
|
$input = 'foo\nbar\n\nraboof';
|
|
|
|
|
$expectedOutput = "foo\nbar\n\nraboof";
|
|
|
|
|
|
|
|
|
|
$this->checkOutput($expectedOutput, $input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testOutputWithHtmlEntities(): void
|
|
|
|
|
{
|
|
|
|
|
$input = 'foo & bar';
|
|
|
|
|
$expectedOutput = $input;
|
|
|
|
|
|
|
|
|
|
$this->checkOutput($expectedOutput, $input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSimpleHtmlOutput(): void
|
|
|
|
|
{
|
|
|
|
|
$input = <<<'INPUT'
|
|
|
|
|
OK - Teststatus <a href="http://localhost/test.php" target="_blank">Info</a>
|
|
|
|
|
INPUT;
|
|
|
|
|
|
|
|
|
|
$expectedOutput = <<<'EXPECTED_OUTPUT'
|
|
|
|
|
OK - Teststatus <a href="http://localhost/test.php" target="_blank" rel="noreferrer noopener">Info</a>
|
|
|
|
|
EXPECTED_OUTPUT;
|
|
|
|
|
|
|
|
|
|
$this->checkOutput($expectedOutput, $input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTextStatusTags(): void
|
|
|
|
|
{
|
|
|
|
|
foreach (['OK', 'WARNING', 'CRITICAL', 'UNKNOWN', 'UP', 'DOWN'] as $s) {
|
|
|
|
|
$l = strtolower($s);
|
|
|
|
|
|
|
|
|
|
$input = sprintf('[%s] Test', $s);
|
|
|
|
|
$expectedOutput = sprintf('<span class="state-ball ball-size-m state-%s"></span> Test', $l);
|
|
|
|
|
|
|
|
|
|
$this->checkOutput($expectedOutput, $input);
|
|
|
|
|
|
|
|
|
|
$input = sprintf('(%s) Test', $s);
|
|
|
|
|
|
|
|
|
|
$this->checkOutput($expectedOutput, $input);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHtmlStatusTags(): void
|
|
|
|
|
{
|
|
|
|
|
$dummyHtml = '<a href="#"></a>';
|
|
|
|
|
|
|
|
|
|
foreach (['OK', 'WARNING', 'CRITICAL', 'UNKNOWN', 'UP', 'DOWN'] as $s) {
|
|
|
|
|
$l = strtolower($s);
|
|
|
|
|
|
|
|
|
|
$input = sprintf('%s [%s] Test', $dummyHtml, $s);
|
|
|
|
|
$expectedOutput = sprintf('%s <span class="state-ball ball-size-m state-%s"></span> Test', $dummyHtml, $l);
|
|
|
|
|
|
|
|
|
|
$this->checkOutput($expectedOutput, $input);
|
|
|
|
|
|
|
|
|
|
$input = sprintf('%s (%s) Test', $dummyHtml, $s);
|
|
|
|
|
|
|
|
|
|
$this->checkOutput($expectedOutput, $input);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNewlineProcessingInHtmlOutput(): void
|
|
|
|
|
{
|
|
|
|
|
$input = 'This is plugin output\n\n<ul>\n <li>with a HTML list</li>\n</ul>\n\n'
|
|
|
|
|
. 'and more text that\nis split onto multiple\n\nlines';
|
|
|
|
|
|
|
|
|
|
$expectedOutput = <<<'EXPECTED_OUTPUT'
|
|
|
|
|
This is plugin output
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
<li>with a HTML list</li>
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
and more text that
|
|
|
|
|
is split onto multiple
|
|
|
|
|
|
|
|
|
|
lines
|
|
|
|
|
EXPECTED_OUTPUT;
|
|
|
|
|
|
|
|
|
|
$this->checkOutput($expectedOutput, $input);
|
2024-06-17 07:07:02 -04:00
|
|
|
}
|
|
|
|
|
}
|