Skip to content

Commit

Permalink
Make TemplatesTest more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-davis committed May 9, 2022
1 parent 95c36e7 commit fdded82
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions tests/Core/Templates/TemplatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
class TemplatesTest extends \Test\TestCase {
public function test403() {
$template = \OC::$SERVERROOT . '/core/templates/403.php';
$expectedHtml = "<ul><li class='error'>\n\t\tAccess forbidden<br><p class='hint'></p></li></ul>";
$expectedHtml = "<ul><li class='error'>Access forbidden<br><p class='hint'></p></li></ul>";
$this->assertTemplate($expectedHtml, $template);
}

public function test404() {
$template = \OC::$SERVERROOT . '/core/templates/404.php';
$href = \OC::$server->getURLGenerator()->linkTo('', 'index.php');
$expectedHtml = "<ul><li class='error'>\n\t\t\tFile not found<br><p class='hint'>The specified document has not been found on the server.</p>\n<p class='hint'><a href='$href'>You can click here to return to ownCloud.</a></p>\n\t\t</li></ul>";
$expectedHtml = "<ul><li class=\"error\">File not found<br><p class=\"hint\">The specified document has not been found on the server.</p><p class=\"hint\"><a href=\"$href\">You can click here to return to ownCloud.</a></p></li></ul>";
$this->assertTemplate($expectedHtml, $template);
}
}
47 changes: 22 additions & 25 deletions tests/lib/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,41 +485,38 @@ 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);
}

/**
* @param string $expectedHtml
* @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() {
Expand Down

0 comments on commit fdded82

Please sign in to comment.