PHP tests: Move some test utils to a parent class

Change-Id: I6ae5aa85f8aaa02e1b9323820a841c06c5d62b64
This commit is contained in:
Ed Sanders 2020-05-11 16:52:06 +01:00
parent a000b8c7b1
commit 745101c02b
3 changed files with 97 additions and 34 deletions

View file

@ -229,6 +229,9 @@
"DiscussionToolsData": "includes/DiscussionToolsData.php",
"DiscussionToolsHooks": "includes/DiscussionToolsHooks.php"
},
"TestAutoloadClasses": {
"DiscussionToolsTestCase": "tests/phpunit/DiscussionToolsTestCase.php"
},
"Hooks": {
"BeforePageDisplay": "DiscussionToolsHooks::onBeforePageDisplay",
"ResourceLoaderGetConfigVars": "DiscussionToolsHooks::onResourceLoaderGetConfigVars",

View file

@ -1,21 +1,11 @@
<?php
use MediaWiki\MediaWikiServices;
use Wikimedia\TestingAccessWrapper;
/**
* @coversDefaultClass DiscussionToolsCommentParser
*/
class DiscussionToolsCommentParserTest extends MediaWikiTestCase {
private static function getJson( $relativePath ) {
$json = json_decode(
// TODO: Move cases out of /qunit
file_get_contents( __DIR__ . '/../qunit/' . $relativePath ),
true
);
return $json;
}
class DiscussionToolsCommentParserTest extends DiscussionToolsTestCase {
private static function getOffsetPath( $ancestor, $node, $nodeOffset ) {
$path = [ $nodeOffset ];
@ -173,36 +163,17 @@ class DiscussionToolsCommentParserTest extends MediaWikiTestCase {
* @covers ::groupThreads
*/
public function testGetComments( $name, $dom, $expected, $config, $data ) {
$dom = file_get_contents( __DIR__ . '/../qunit/' . $dom );
$dom = self::getHtml( $dom );
$expected = self::getJson( $expected );
$config = self::getJson( $config );
$data = self::getJson( $data );
// Remove all but the body tags from full Parsoid docs
if ( strpos( $dom, '<body' ) !== false ) {
preg_match( '`<body[^>]*>(.*)</body>`s', $dom, $match );
$dom = $match[1];
}
$this->setupEnv( $config, $data );
$parserOrig = self::createParser( $data );
$this->setMwGlobals( $config );
$this->setMwGlobals( [
'wgArticlePath' => $config['wgArticlePath'],
'wgNamespaceAliases' => $config['wgNamespaceIds'],
'wgLocaltimezone' => $data['localTimezone']
] );
$this->setUserLang( $config['wgContentLang'] );
$this->setContentLang( $config['wgContentLang'] );
$services = MediaWikiServices::getInstance();
$parserOrig = new DiscussionToolsCommentParser(
$services->getContentLanguage(),
$services->getMainConfig(),
$data
);
$parser = TestingAccessWrapper::newFromObject( $parserOrig );
$doc = new DOMDocument();
$doc->loadHTML( '<?xml encoding="utf-8" ?>' . $dom, LIBXML_NOERROR );
$doc = self::createDocument( $dom );
$container = $doc->documentElement->childNodes[0];
$comments = $parserOrig->getComments( $container );

View file

@ -0,0 +1,89 @@
<?php
use MediaWiki\MediaWikiServices;
/**
* @coversDefaultClass DiscussionToolsCommentModifier
*/
class DiscussionToolsTestCase extends MediaWikiTestCase {
/**
* Create a DOMDocument from a string
*
* @param string $html
* @return DOMDocument
*/
protected static function createDocument( string $html ) : DOMDocument {
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadHTML( '<?xml encoding="utf-8" ?>' . $html, LIBXML_NOERROR );
return $doc;
}
/**
* Get parsed JSON from path
*
* @param string $relativePath
* @return array
*/
protected static function getJson( string $relativePath ) : array {
$json = json_decode(
// TODO: Move cases out of /qunit
file_get_contents( __DIR__ . '/../qunit/' . $relativePath ),
true
);
return $json;
}
/**
* Get HTML from path
*
* @param string $relativePath
* @return string
*/
protected static function getHtml( string $relativePath ) : string {
// TODO: Move cases out of /qunit
$html = file_get_contents( __DIR__ . '/../qunit/' . $relativePath );
// Remove all but the body tags from full Parsoid docs
if ( strpos( $html, '<body' ) !== false ) {
preg_match( '`<body[^>]*>(.*)</body>`s', $html, $match );
$html = $match[1];
}
return $html;
}
/**
* Create a comment pareser
*
* @param array $data
* @return DiscussionToolsCommentParser
*/
protected static function createParser( array $data ) : DiscussionToolsCommentParser {
$services = MediaWikiServices::getInstance();
return new DiscussionToolsCommentParser(
$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'] );
}
}