From fdee771aca39f394c940edfcaa5a15826b078974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 12 Apr 2016 12:49:11 +0200 Subject: [PATCH] Add unit testing capabilities for templates (#23708) Add unit testing capabilities for templates --- core/templates/403.php | 2 + core/templates/404.php | 3 ++ lib/private/template.php | 8 ++-- lib/private/template/base.php | 58 ++++++++++++++----------- tests/core/templates/templates.php | 20 +++++++++ tests/lib/testcase.php | 68 ++++++++++++++++++++++++++++++ 6 files changed, 132 insertions(+), 27 deletions(-) create mode 100644 tests/core/templates/templates.php diff --git a/core/templates/403.php b/core/templates/403.php index a19009c354..7d07c72c87 100644 --- a/core/templates/403.php +++ b/core/templates/403.php @@ -1,4 +1,5 @@ printPage(); exit; } +// @codeCoverageIgnoreEnd ?> "; + $this->assertTemplate($expectedHtml, $template); + } + + public function test404() { + $template = \OC::$SERVERROOT . '/core/templates/404.php'; + $href = \OC::$server->getURLGenerator()->linkTo('', 'index.php'); + $expectedHtml = ""; + $this->assertTemplate($expectedHtml, $template); + } + +} diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index fb0db8cd86..d7573337f8 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -22,10 +22,15 @@ namespace Test; +use DOMDocument; +use DOMNode; use OC\Command\QueueBus; use OC\Files\Filesystem; +use OC\Template\Base; +use OC_Defaults; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\IL10N; use OCP\Security\ISecureRandom; abstract class TestCase extends \PHPUnit_Framework_TestCase { @@ -34,6 +39,8 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { /** @var IDBConnection */ static protected $realDatabase = null; + + /** @var bool */ static private $wasDatabaseAllowed = false; /** @var array */ @@ -408,4 +415,65 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { return false; } + + /** + * @param string $expectedHtml + * @param string $template + * @param array $vars + */ + protected function assertTemplate($expectedHtml, $template, $vars = []) { + + require_once __DIR__.'/../../lib/private/template/functions.php'; + + $requestToken = 12345; + $theme = new OC_Defaults(); + /** @var IL10N | \PHPUnit_Framework_MockObject_MockObject $l10n */ + $l10n = $this->getMockBuilder('\OCP\IL10N') + ->disableOriginalConstructor()->getMock(); + $l10n + ->expects($this->any()) + ->method('t') + ->will($this->returnCallback(function($text, $parameters = array()) { + return vsprintf($text, $parameters); + })); + + $t = new Base($template, $requestToken, $l10n, $theme); + $buf = $t->fetchPage($vars); + $this->assertHtmlStringEqualsHtmlString($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); + } + + + 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); + } + } + } + } }