1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
<?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 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
{
$input = 'This is a plain text';
$this->checkOutput($input, $input);
}
public function testRenderTextWithStates(): void
{
$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->checkOutput($expectedOutput, $input);
}
public function testRenderTextWithStatesAndCharacterLimit(): void
{
$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->checkOutput($expectedOutput, $input, 10);
}
public function testRenderTextWithHtml(): void
{
$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->checkOutput($expectedOutput, $input);
}
public function testRenderTextWithHtmlAndStates(): void
{
$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->checkOutput($expectedOutput, $input);
}
public function testRenderTextWithHtmlIncludingStatesAndSpecialChars(): void
{
$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->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);
}
}
|