Merge "assertValidHtml for checking html in test cases."
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index 6ce78b5..95b5047 100644 (file)
@@ -35,6 +35,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        private static $dbSetup = false;
        private static $oldTablePrefix = false;
 
+       /**
+        * Original value of PHP's error_reporting setting.
+        *
+        * @var int
+        */
+       private $phpErrorLevel;
+
        /**
         * Holds the paths of temporary files/directories created through getNewTempFile,
         * and getNewTempDirectory
@@ -46,7 +53,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        /**
         * Holds original values of MediaWiki configuration settings
         * to be restored in tearDown().
-        * See also setMwGlobal().
+        * See also setMwGlobals().
         * @var array
         */
        private $mwGlobals = array();
@@ -172,6 +179,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                parent::setUp();
                $this->called['setUp'] = 1;
 
+               $this->phpErrorLevel = intval( ini_get( 'error_reporting' ) );
+
                /*
                // @todo global variables to restore for *every* test
                array(
@@ -233,6 +242,18 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
                $this->mwGlobals = array();
 
+               $phpErrorLevel = intval( ini_get( 'error_reporting' ) );
+
+               if ( $phpErrorLevel !== $this->phpErrorLevel ) {
+                       ini_set( 'error_reporting', $this->phpErrorLevel );
+
+                       $oldHex = strtoupper( dechex( $this->phpErrorLevel ) );
+                       $newHex = strtoupper( dechex( $phpErrorLevel ) );
+                       $message = "PHP error_reporting setting was left dirty: was 0x$oldHex before test, 0x$newHex after test!";
+
+                       $this->fail( $message );
+               }
+
                parent::tearDown();
                wfProfileOut( __METHOD__ );
        }
@@ -945,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 = '<!DOCTYPE html><html><head><title>test</title></head><body>' . $html . '</body></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 );
+       }
 }