diff --git a/library/Icingadb/Util/PluginOutput.php b/library/Icingadb/Util/PluginOutput.php index 03055a9e..14d9cbf4 100644 --- a/library/Icingadb/Util/PluginOutput.php +++ b/library/Icingadb/Util/PluginOutput.php @@ -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; diff --git a/test/php/library/Icingadb/Util/PluginOutputTest.php b/test/php/library/Icingadb/Util/PluginOutputTest.php new file mode 100644 index 00000000..821be98a --- /dev/null +++ b/test/php/library/Icingadb/Util/PluginOutputTest.php @@ -0,0 +1,128 @@ +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' + Dummy state + \_ Fake "state" + \_ 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' + 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