X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FMediaWikiTestCase.php;h=87e214cb9e03f90d0508428dbc1ee32e4133f1eb;hb=a51ae81f11bd9abb4bb8e2a4924b6117f0ae2537;hp=9ebca3f4f90ffae288c99a8c3886f9b8c628c191;hpb=a5a6f595db5a5b9d942e822bede27b326798a5a8;p=lhc%2Fweb%2Fwiklou.git
diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php
index 9ebca3f4f9..87e214cb9e 100644
--- a/tests/phpunit/MediaWikiTestCase.php
+++ b/tests/phpunit/MediaWikiTestCase.php
@@ -966,4 +966,55 @@ 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' );
+ }
+
+ $errorBuffer = '';
+ MWTidy::checkErrors( $html, $errorBuffer );
+ $allErrors = preg_split( '/[\r\n]+/', $errorBuffer );
+
+ // Filter Tidy warnings which aren't useful for us.
+ // Tidy eg. often cries about parameters missing which have actually
+ // been deprecated since HTML4, thus we should not care about them.
+ $errors = preg_grep(
+ '/^(.*Warning: (trimming empty|.* lacks ".*?" attribute).*|\s*)$/m',
+ $allErrors, PREG_GREP_INVERT
+ );
+
+ $this->assertEmpty( $errors, implode( "\n", $errors ) );
+ }
}