Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests-only] Make TemplatesTest more flexible #40059

Merged
merged 2 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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