Skip to content

Commit

Permalink
PluginOutputTest: Migrate tests from monitoring/PluginOutputTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jul 23, 2024
1 parent c4728c3 commit 97a9680
Showing 1 changed file with 104 additions and 38 deletions.
142 changes: 104 additions & 38 deletions test/php/library/Icingadb/Util/PluginOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@

class PluginOutputTest extends TestCase
{
public function testRenderPlainText()
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';
$expectedOutput = $input;

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

public function testRenderTextWithStates()
public function testRenderTextWithStates(): void
{
$input = <<<'INPUT'
[OK] Dummy state
Expand All @@ -35,14 +42,10 @@ public function testRenderTextWithStates()
\_ <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'
);
$this->checkOutput($expectedOutput, $input);
}

public function testRenderTextWithStatesAndCharacterLimit()
public function testRenderTextWithStatesAndCharacterLimit(): void
{
$input = <<<'INPUT'
[OK] Dummy state
Expand All @@ -54,14 +57,10 @@ public function testRenderTextWithStatesAndCharacterLimit()
<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'
);
$this->checkOutput($expectedOutput, $input, 10);
}

public function testRenderTextWithHtml()
public function testRenderTextWithHtml(): void
{
$input = <<<'INPUT'
Hello <h3>World</h3>, this "is" 'a <strong>test</strong>.
Expand All @@ -71,14 +70,10 @@ public function testRenderTextWithHtml()
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'
);
$this->checkOutput($expectedOutput, $input);
}

public function testRenderTextWithHtmlAndStates()
public function testRenderTextWithHtmlAndStates(): void
{
$input = <<<'INPUT'
Hello <h3>World</h3>, this "is" a <strong>test</strong>.
Expand All @@ -96,14 +91,10 @@ public function testRenderTextWithHtmlAndStates()
text <span> ends </span> here
EXPECTED_OUTPUT;

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

public function testRenderTextWithHtmlIncludingStatesAndSpecialChars()
public function testRenderTextWithHtmlIncludingStatesAndSpecialChars(): void
{
$input = <<<'INPUT'
Hello <h3>World</h3>, this "is" a <strong>test</strong>.
Expand All @@ -115,14 +106,89 @@ public function testRenderTextWithHtmlIncludingStatesAndSpecialChars()
$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;./
special chars: !@#$%^&amp;*()_+{}|:"&lt;&gt;?`-=[]\;',&#8203;./
text <span> ends </span> here
EXPECTED_OUTPUT;

$this->assertSame(
$expectedOutput,
(new PluginOutput($input))->render(),
'PluginOutput::render does not return expected values'
);
$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&nbsp;&amp;&nbsp;bar';
$expectedOutput = $input;

$this->checkOutput($expectedOutput, $input);
}

public function testSimpleHtmlOutput(): void
{
$input = 'OK - Teststatus <a href="http://localhost/test.php" target="_blank">Info</a>';
$expectedOutput = 'OK - Teststatus <a href="http://localhost/test.php" target="_blank" rel="noreferrer noopener">Info</a>';

$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);
}
}

0 comments on commit 97a9680

Please sign in to comment.