Skip to content

Commit

Permalink
Merge pull request #40059 from owncloud/flexible-TemplatesTest
Browse files Browse the repository at this point in the history
[tests-only] Make TemplatesTest more flexible
  • Loading branch information
phil-davis authored May 17, 2022
2 parents 6321f96 + c616421 commit 9e6ed2e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/templates/403.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
?>
<ul>
<li class='error'>
<?php p($l->t('Access forbidden')); ?><br>
<?php p($l->t('Access forbidden')); ?><br/>
<p class='hint'><?php if (isset($_['file'])) {
p($_['file']);
}?></p>
Expand Down
2 changes: 1 addition & 1 deletion core/templates/404.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<?php else: ?>
<ul>
<li class="error">
<?php p($l->t('File not found')); ?><br>
<?php p($l->t('File not found')); ?><br/>
<p class="hint"><?php p($l->t('The specified document has not been found on the server.')); ?></p>
<p class="hint"><a href="<?php p(\OC::$server->getURLGenerator()->linkTo('', 'index.php')) ?>"><?php p($l->t('You can click here to return to %s.', [$theme->getName()])); ?></a></p>
</li>
Expand Down
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: 17 additions & 30 deletions tests/lib/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,41 +485,28 @@ protected function assertTemplate($expectedHtml, $template, $vars = []) {

$t = new Base($template, $requestToken, $l10n, null, $theme);
$buf = $t->fetchPage($vars);
$this->assertHtmlStringEqualsHtmlString($expectedHtml, $buf);
$this->assertXmlStringEqualsXmlString(
$this->normalizeHTML($expectedHtml),
$this->normalizeHTML($buf)
);
}

/**
* @param string $expectedHtml
* @param string $actualHtml
* @param string $message
* 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
*/
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);
}

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);
}
}
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 9e6ed2e

Please sign in to comment.