From df927f86c24cc41cf5bd67bd19db969b55769669 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 3 Dec 2013 18:42:48 +0100 Subject: [PATCH] assertValidHtml for checking html in test cases. implemented using tidy. Change-Id: Idb98af785ca07ecd7afeebadf7396ecdc03a91bc --- includes/parser/Tidy.php | 3 ++ tests/phpunit/MediaWikiTestCase.php | 40 ++++++++++++++++++++ tests/phpunit/includes/HtmlFormatterTest.php | 2 + 3 files changed, 45 insertions(+) diff --git a/includes/parser/Tidy.php b/includes/parser/Tidy.php index 32b16aafa8..dbfab34335 100644 --- a/includes/parser/Tidy.php +++ b/includes/parser/Tidy.php @@ -206,6 +206,9 @@ class MWTidy { $process = proc_open( "$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes ); + //NOTE: At least on linux, the process will be created even if tidy is not installed. + // This means that missing tidy will be treated as a validation failure. + if ( is_resource( $process ) ) { // Theoretically, this style of communication could cause a deadlock // here. If the stdout buffer fills up, then writes to stdin could diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 7a7ec8f0c1..1210845499 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -966,4 +966,44 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { $this->assertInstanceOf( $expected, $pokemons, $message ); } + + /** + * Asserts that the given string is a valid HTML snippet. + * Wraps the given string in the required top level tags and + * then calls assertValidHtmlDocument(). + * The snippet is expected to be HTML 5. + * + * @note: Will mark the test as skipped if the "tidy" module is not installed. + * @note: This ignores $wgUseTidy, so we can check for valid HTML even (and especially) + * when automatic tidying is disabled. + * + * @param string $html An HTML snippet (treated as the contents of the body tag). + */ + protected function assertValidHtmlSnippet( $html ) { + $html = 'test' . $html . ''; + $this->assertValidHtmlDocument( $html ); + } + + /** + * Asserts that the given string is valid HTML document. + * + * @note: Will mark the test as skipped if the "tidy" module is not installed. + * @note: This ignores $wgUseTidy, so we can check for valid HTML even (and especially) + * when automatic tidying is disabled. + * + * @param string $html A complete HTML document + */ + protected function assertValidHtmlDocument( $html ) { + // Note: we only validate if the tidy PHP extension is available. + // In case wgTidyInternal is false, MWTidy would fall back to the command line version + // of tidy. In that case however, we can not reliably detect whether a failing validation + // is due to malformed HTML, or caused by tidy not being installed as a command line tool. + // That would cause all HTML assertions to fail on a system that has no tidy installed. + if ( !$GLOBALS['wgTidyInternal'] ) { + $this->markTestSkipped( 'Tidy extension not installed' ); + } + + $ok = MWTidy::checkErrors( $html, $errors ); + $this->assertTrue( $ok, 'HTML validation errors: ' . $errors ); + } } diff --git a/tests/phpunit/includes/HtmlFormatterTest.php b/tests/phpunit/includes/HtmlFormatterTest.php index 7ef0b60c23..99a6efd659 100644 --- a/tests/phpunit/includes/HtmlFormatterTest.php +++ b/tests/phpunit/includes/HtmlFormatterTest.php @@ -16,6 +16,8 @@ class HtmlFormatterTest extends MediaWikiTestCase { } $formatter->filterContent(); $html = $formatter->getText(); + + $this->assertValidHtmlSnippet( $html ); $this->assertEquals( self::normalize( $expected ), self::normalize( $html ) ); } -- 2.20.1