From 3f93a5341ca0c49f1a3c1a9d7af42d1f6ece8552 Mon Sep 17 00:00:00 2001 From: Niklas Gerstner Date: Fri, 4 Oct 2024 00:20:20 +0200 Subject: [PATCH] Implement unit tests --- tests/Generator/HtmlGeneratorTest.php | 60 ++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/tests/Generator/HtmlGeneratorTest.php b/tests/Generator/HtmlGeneratorTest.php index 98e1d2d..c5e6647 100644 --- a/tests/Generator/HtmlGeneratorTest.php +++ b/tests/Generator/HtmlGeneratorTest.php @@ -275,4 +275,62 @@ public function testGenerateWithEmptyCollection() $this->assertEquals('', $result); } -} \ No newline at end of file + + public function testFilterInvalidHexColor() + { + $collection = new MotdItemCollection(); + + $item1 = new MotdItem(); + $item1->setText('Hello'); + $item1->setColor('#FF555'); + $collection->add($item1); + + $item2 = new MotdItem(); + $item2->setText("\n"); + $collection->add($item2); + + $item3 = new MotdItem(); + $item3->setText('Beautiful'); + $item3->setColor('#800080'); + $collection->add($item3); + + $item4 = new MotdItem(); + $item4->setText("\n"); + $collection->add($item4); + + $item5 = new MotdItem(); + $item5->setText('World'); + $item5->setColor('#42'); + $collection->add($item5); + + $item6 = new MotdItem(); + $item6->setText('!'); + $item6->setColor('#42a'); + $collection->add($item6); + + $generator = new HtmlGenerator(); + $result = $generator->generate($collection); + + $this->assertEquals('
Beautiful
!', $result); + } + + public function testEscapeInput() + { + $collection = new MotdItemCollection(); + + $item1 = new MotdItem(); + $item1->setText('Hover me'); + $item1->setColor('#000000" onmouseover="javascript:alert(\'XSS when mouse pointer enters the span element\')"'); + $collection->add($item1); + + $item2 = new MotdItem(); + $item2->setText(''); + $item2->setColor('#800080'); + $collection->add($item2); + + $generator = new HtmlGenerator(); + $result = $generator->generate($collection); + + $this->assertEquals('<script>alert("XSS on page load")</script>', $result); + } +}