Skip to content

Commit

Permalink
test methods return the same result
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jul 23, 2024
1 parent 04c9012 commit c82d8a1
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
88 changes: 88 additions & 0 deletions library/Icingadb/Util/PluginOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Icinga\Module\Icingadb\Util;

use DOMDocument;
use DOMNode;
use DOMText;
use Icinga\Module\Icingadb\Hook\PluginOutputHook;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
Expand All @@ -12,6 +15,8 @@
use ipl\Html\HtmlString;
use ipl\Orm\Model;
use LogicException;
use Icinga\Web\Dom\DomNodeIterator;
use RecursiveIteratorIterator;

class PluginOutput extends HtmlString
{
Expand Down Expand Up @@ -199,4 +204,87 @@ public function render()

return $output;

Check failure on line 205 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::render() should return string but returns string|null.

Check failure on line 205 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::render() should return string but returns string|null.

Check failure on line 205 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::render() should return string but returns string|null.
}


/** @var string[] Patterns to be replaced in html plugin output */
const HTML_PATTERNS = [

Check failure on line 210 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_PATTERNS.

Check failure on line 210 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_PATTERNS.

Check failure on line 210 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_PATTERNS.

Check failure on line 210 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_PATTERNS.

Check failure on line 210 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_PATTERNS.

Check failure on line 210 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_PATTERNS.

Check failure on line 210 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_PATTERNS.
'~\\\t~',
'~\\\n~'
];

/** @var string[] Replacements for {@see PluginOutput::HTML_PATTERNS} */
const HTML_REPLACEMENTS = [

Check failure on line 216 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_REPLACEMENTS.

Check failure on line 216 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_REPLACEMENTS.

Check failure on line 216 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_REPLACEMENTS.

Check failure on line 216 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_REPLACEMENTS.

Check failure on line 216 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_REPLACEMENTS.

Check failure on line 216 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_REPLACEMENTS.

Check failure on line 216 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Cannot redeclare constant Icinga\Module\Icingadb\Util\PluginOutput::HTML_REPLACEMENTS.
"\t",
"\n"
];
public static function test($output)

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has no return type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has parameter $output with no type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has no return type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has parameter $output with no type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has no return type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has parameter $output with no type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has no return type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has parameter $output with no type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has no return type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has parameter $output with no type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has no return type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has parameter $output with no type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has no return type specified.

Check failure on line 220 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method Icinga\Module\Icingadb\Util\PluginOutput::test() has parameter $output with no type specified.
{
$output = HtmlPurifier::process(preg_replace(
self::HTML_PATTERNS,
self::HTML_REPLACEMENTS,
$output
));

$output = trim($output);


$pattern = '/[([](OK|WARNING|CRITICAL|UNKNOWN|UP|DOWN)[)\]]/';
$doc = new DOMDocument();
$doc->loadXML('<div>' . $output . '</div>', LIBXML_NOERROR | LIBXML_NOWARNING);
$dom = new RecursiveIteratorIterator(new DomNodeIterator($doc), RecursiveIteratorIterator::SELF_FIRST);

$nodesToRemove = [];
foreach ($dom as $node) {
/** @var DOMNode $node */
if ($node->nodeType !== XML_TEXT_NODE) {
continue;
}

$start = 0;
while (preg_match($pattern, $node->nodeValue, $match, PREG_OFFSET_CAPTURE, $start)) {
$offsetLeft = $match[0][1];
$matchLength = strlen($match[0][0]);
$leftLength = $offsetLeft - $start;

// if there is text before the match
if ($leftLength) {
// create node for leading text
$text = new DOMText(substr($node->nodeValue, $start, $leftLength));

Check failure on line 252 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$string of function substr expects string, string\|null given\.$# in path /home/runner/work/icingadb-web/icingadb-web/library/Icingadb/Util/PluginOutput.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 252 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$string of function substr expects string, string\|null given\.$# in path /home/runner/work/icingadb-web/icingadb-web/library/Icingadb/Util/PluginOutput.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 252 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$string of function substr expects string, string\|null given\.$# in path /home/runner/work/icingadb-web/icingadb-web/library/Icingadb/Util/PluginOutput.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 252 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$string of function substr expects string, string\|null given\.$# in path /home/runner/work/icingadb-web/icingadb-web/library/Icingadb/Util/PluginOutput.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 252 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$string of function substr expects string, string\|null given\.$# in path /home/runner/work/icingadb-web/icingadb-web/library/Icingadb/Util/PluginOutput.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 252 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$string of function substr expects string, string\|null given\.$# in path /home/runner/work/icingadb-web/icingadb-web/library/Icingadb/Util/PluginOutput.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 252 in library/Icingadb/Util/PluginOutput.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$string of function substr expects string, string\|null given\.$# in path /home/runner/work/icingadb-web/icingadb-web/library/Icingadb/Util/PluginOutput.php is expected to occur 3 times, but occurred only 2 times.
$node->parentNode->insertBefore($text, $node);
}

// create the state ball for the match
$span = $doc->createElement('span');
$span->setAttribute(
'class',
'state-ball ball-size-m state-' . strtolower($match[1][0])
);
$node->parentNode->insertBefore($span, $node);

// start for next match
$start = $offsetLeft + $matchLength;
}

if ($start) {
// is there text left?
if (strlen($node->nodeValue) > $start) {
// create node for trailing text
$text = new DOMText(substr($node->nodeValue, $start));
$node->parentNode->insertBefore($text, $node);
}

// delete the old node later
$nodesToRemove[] = $node;
}
}

foreach ($nodesToRemove as $node) {
/** @var DOMNode $node */
$node->parentNode->removeChild($node);
}

$output = substr($doc->saveHTML(), 5, -7);

return preg_replace('/,(?=[^\s])/', ',&#8203;', $output);
}
}
21 changes: 21 additions & 0 deletions test/php/library/Icingadb/Util/PluginOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,25 @@ public function testRenderTextWithHtmlIncludingStatesAndSpecialChars()
'PluginOutput::render does not return expected values'
);
}

public function testRender()
{
$input = <<<'INPUT'
Hello <h3>World</h3>, this "is" a <strong>test</strong>.
[OK] Dummy state
special chars: !@#$%^&*()_+{}|:"<>?`-=[]\;',./
text <span> ends </span> here
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
INPUT;

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

0 comments on commit c82d8a1

Please sign in to comment.