From ccd55f562471a808bc11e7f635ccfd409df39574 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Mon, 9 May 2022 11:14:29 +0545 Subject: [PATCH 1/2] Make TemplatesTest more flexible --- tests/Core/Templates/TemplatesTest.php | 4 +-- tests/lib/TestCase.php | 47 ++++++++++++-------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/tests/Core/Templates/TemplatesTest.php b/tests/Core/Templates/TemplatesTest.php index 714c0d15e715..168695dbcff4 100644 --- a/tests/Core/Templates/TemplatesTest.php +++ b/tests/Core/Templates/TemplatesTest.php @@ -5,14 +5,14 @@ class TemplatesTest extends \Test\TestCase { public function test403() { $template = \OC::$SERVERROOT . '/core/templates/403.php'; - $expectedHtml = ""; + $expectedHtml = ""; $this->assertTemplate($expectedHtml, $template); } public function test404() { $template = \OC::$SERVERROOT . '/core/templates/404.php'; $href = \OC::$server->getURLGenerator()->linkTo('', 'index.php'); - $expectedHtml = ""; + $expectedHtml = ""; $this->assertTemplate($expectedHtml, $template); } } diff --git a/tests/lib/TestCase.php b/tests/lib/TestCase.php index 8abdfe521f18..f4c3ca47538a 100644 --- a/tests/lib/TestCase.php +++ b/tests/lib/TestCase.php @@ -485,7 +485,7 @@ protected function assertTemplate($expectedHtml, $template, $vars = []) { $t = new Base($template, $requestToken, $l10n, null, $theme); $buf = $t->fetchPage($vars); - $this->assertHtmlStringEqualsHtmlString($expectedHtml, $buf); + $this->assertHtmlStringIsEquivalentToHtmlString($expectedHtml, $buf); } /** @@ -493,33 +493,30 @@ protected function assertTemplate($expectedHtml, $template, $vars = []) { * @param string $actualHtml * @param string $message */ - protected function assertHtmlStringEqualsHtmlString($expectedHtml, $actualHtml, $message = '') { - $expected = new DOMDocument(); - $expected->preserveWhiteSpace = false; - $expected->formatOutput = true; - $expected->loadHTML($expectedHtml); - - $actual = new DOMDocument(); - $actual->preserveWhiteSpace = false; - $actual->formatOutput = true; - $actual->loadHTML($actualHtml); - $this->removeWhitespaces($actual); - - $expectedHtml1 = $expected->saveHTML(); - $actualHtml1 = $actual->saveHTML(); - self::assertEquals($expectedHtml1, $actualHtml1, $message); + protected function assertHtmlStringIsEquivalentToHtmlString($expectedHtml, $actualHtml, $message = '') { + self::assertSame( + $this->normalizeHTML($expectedHtml), + $this->normalizeHTML($actualHtml), + $message + ); } - private function removeWhitespaces(DOMNode $domNode) { - foreach ($domNode->childNodes as $node) { - if ($node->hasChildNodes()) { - $this->removeWhitespaces($node); - } else { - if ($node instanceof \DOMText && $node->isWhitespaceInElementContent()) { - $domNode->removeChild($node); - } - } + /** + * Takes a string that might be formatted with new-lines and indented with spaces or tabs. + * Returns a one-line string without the new-lines or indenting. + * The returned string has equivalent functionality as HTML. + * + * @param string $inputHtml + * + * @return string + */ + private function normalizeHTML(string $inputHtml):string { + $inputLines = \explode("\n", $inputHtml); + $trimmedLines = []; + foreach ($inputLines as $inputLine) { + $trimmedLines[] = \trim($inputLine); } + return \implode("", $trimmedLines); } public function getCurrentUser() { From c616421778536e625b2909adf16b09473239884e Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Mon, 16 May 2022 17:43:50 +0545 Subject: [PATCH 2/2] Use assertXmlStringEqualsXmlString and fix
elements --- core/templates/403.php | 2 +- core/templates/404.php | 2 +- tests/Core/Templates/TemplatesTest.php | 4 ++-- tests/lib/TestCase.php | 14 ++------------ 4 files changed, 6 insertions(+), 16 deletions(-) diff --git a/core/templates/403.php b/core/templates/403.php index 0c77b758f343..63419d59a5b9 100644 --- a/core/templates/403.php +++ b/core/templates/403.php @@ -11,7 +11,7 @@ ?>