From 2fefc755523783ae3face92e356df1002ae4b1a1 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Mon, 17 Jun 2024 13:07:02 +0200 Subject: [PATCH] `PluginOutput::render()`: Shorten the output by characterLimit before 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 --- library/Icingadb/Util/PluginOutput.php | 6 +- .../Icingadb/Util/PluginOutputTest.php | 128 ++++++++++++++++++ 2 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 test/php/library/Icingadb/Util/PluginOutputTest.php 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

World

, this "is" 'a test. +INPUT; + + $expectedOutput = <<<'EXPECTED_OUTPUT' +Hello

World

, this "is" 'a test. +EXPECTED_OUTPUT; + + $this->assertSame( + $expectedOutput, + (new PluginOutput($input))->render(), + 'PluginOutput::render does not return expected values' + ); + } + + public function testRenderTextWithHtmlAndStates() + { + $input = <<<'INPUT' +Hello

World

, this "is" a test. +[OK] Dummy state + \_ [OK] Fake "state" + \_ [WARNING] Fake state again +text ends here +INPUT; + + $expectedOutput = <<<'EXPECTED_OUTPUT' +Hello

World

, this "is" a test. + Dummy state + \_ Fake "state" + \_ Fake state again +text ends here +EXPECTED_OUTPUT; + + $this->assertSame( + $expectedOutput, + (new PluginOutput($input))->render(), + 'PluginOutput::render does not return expected values' + ); + } + + public function testRenderTextWithHtmlIncludingStatesAndSpecialChars() + { + $input = <<<'INPUT' +Hello

World

, this "is" a test. +[OK] Dummy state + special chars: !@#$%^&*()_+{}|:"<>?`-=[]\;',./ +text ends here +INPUT; + + $expectedOutput = <<<'EXPECTED_OUTPUT' +Hello

World

, this "is" a test. + Dummy state + special chars: !@#$%^&*()_+{}|:"<>?`-=[]\;',​./ +text ends here +EXPECTED_OUTPUT; + + $this->assertSame( + $expectedOutput, + (new PluginOutput($input))->render(), + 'PluginOutput::render does not return expected values' + ); + } +}