mediawiki-extensions-Discus.../tests/phpunit/CommentTestCase.php
Bartosz Dziewoński ccd9e411d2 Allow updating the expected results when running PHP tests
This is similar to the code we already have in JS tests, but instead
of printing to the console where you have to copy-paste from, it just
overwrites the files.

Also, update all of the expected results by this method.

Changes in the expected outputs:
* In JSON files, the "warnings" are now always in the same place
  regardless of the type of the warning.
* In all HTML files, self-closing tags now include the trailing slash,
  some characters are no longer encoded as entities when not necessary,
  and attributes may be single-quoted when that makes them shorter.
* In Parsoid HTML files, the header is no longer terribly mangled.

Other notes:
* CommentParserTest.php: Change the output of serializeComments()
  to be in similar order as in JS, to reduce the diffs in this commit
  and because it's a better order for humans.
* modifier.test.js: Remove some hacks that were working around small
  inconsistencies between the previous expected outputs and the actual
  outputs.

Change-Id: I9f764640dae823321c0ac35898fa4db03f1ca364
2020-08-04 03:05:28 +02:00

136 lines
3.7 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
use DOMDocument;
use DOMElement;
use MediaWiki\Extension\DiscussionTools\CommentParser;
use MediaWiki\MediaWikiServices;
use MediaWikiTestCase;
use Wikimedia\Parsoid\Utils\DOMCompat;
use Wikimedia\Parsoid\Utils\DOMUtils;
abstract class CommentTestCase extends MediaWikiTestCase {
/**
* Create a DOMDocument from a string
*
* @param string $html
* @return DOMDocument
*/
protected static function createDocument( string $html ) : DOMDocument {
$doc = DOMUtils::parseHTML( $html );
$doc->preserveWhiteSpace = false;
return $doc;
}
/**
* Get parsed JSON from path
*
* @param string $relativePath
* @param bool $assoc See json_decode()
* @return array
*/
protected static function getJson( string $relativePath, bool $assoc = true ) : array {
$json = json_decode(
file_get_contents( __DIR__ . '/' . $relativePath ),
$assoc
);
return $json;
}
/**
* Write JSON to path
*
* @param string $relativePath
* @param array $data
*/
protected static function overwriteJsonFile( string $relativePath, array $data ) : void {
$json = json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
// 2 spaces instead of 4
$json = preg_replace( '/^( +)\1/m', '$1', $json );
file_put_contents( __DIR__ . '/' . $relativePath, $json . "\n" );
}
/**
* Get HTML from path
*
* @param string $relativePath
* @return string
*/
protected static function getHtml( string $relativePath ) : string {
$html = file_get_contents( __DIR__ . '/../' . $relativePath );
// Remove all but the body tags from full Parsoid docs
if ( strpos( $html, '<body' ) !== false ) {
preg_match( '`(<body[^>]*>)(.*)(</body>)`s', $html, $match );
$html = "<div>$match[2]</div>";
}
return $html;
}
/**
* Write HTML to path
*
* @param string $relPath
* @param DOMDocument $doc
* @param string $origRelPath
*/
protected static function overwriteHtmlFile( string $relPath, DOMDocument $doc, string $origRelPath ) : void {
// Do not use $doc->saveHtml(), it outputs an awful soup of HTML entities for documents with
// non-ASCII characters
$html = file_get_contents( __DIR__ . '/../' . $origRelativePath );
// Replace the body tag only in full Parsoid docs
if ( strpos( $html, '<body' ) !== false ) {
$innerHtml = DOMCompat::getInnerHTML( $doc->getElementsByTagName( 'body' )->item( 0 )->firstChild );
$html = preg_replace(
'`(<body[^>]*>)(.*)(</body>)`s',
// Quote \ and $ in the replacement text
'$1' . strtr( $innerHtml, [ '\\' => '\\\\', '$' => '\\$' ] ) . '$3',
$html
);
} else {
$html = DOMCompat::getInnerHTML( $doc->getElementsByTagName( 'body' )->item( 0 ) );
}
file_put_contents( __DIR__ . '/../' . $relativePath, $html );
}
/**
* Create a comment pareser
*
* @param DOMElement $rootNode
* @param array $data
* @return CommentParser
*/
protected static function createParser( DOMElement $rootNode, array $data ) : CommentParser {
$services = MediaWikiServices::getInstance();
return new CommentParser(
$rootNode,
$services->getContentLanguage(),
$services->getMainConfig(),
$data
);
}
/**
* Setup the MW environment
*
* @param array $config
* @param array $data
*/
protected function setupEnv( array $config, array $data ) : void {
$this->setMwGlobals( $config );
$this->setMwGlobals( [
'wgArticlePath' => $config['wgArticlePath'],
'wgNamespaceAliases' => $config['wgNamespaceIds'],
// TODO: Move this to $config
'wgLocaltimezone' => $data['localTimezone']
] );
$this->setUserLang( $config['wgContentLang'] );
$this->setContentLang( $config['wgContentLang'] );
}
}