2020-05-11 15:52:06 +00:00
|
|
|
<?php
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
|
|
|
use DOMDocument;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentParser;
|
2020-05-11 15:52:06 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-05-14 22:44:49 +00:00
|
|
|
use MediaWikiTestCase;
|
2020-05-11 15:52:06 +00:00
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
abstract class CommentTestCase extends MediaWikiTestCase {
|
2020-05-11 15:52:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
2020-05-18 20:07:00 +00:00
|
|
|
file_get_contents( __DIR__ . '/' . $relativePath ),
|
2020-05-11 15:52:06 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
return $json;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get HTML from path
|
|
|
|
*
|
|
|
|
* @param string $relativePath
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getHtml( string $relativePath ) : string {
|
2020-05-18 20:07:00 +00:00
|
|
|
$html = file_get_contents( __DIR__ . '/../' . $relativePath );
|
2020-05-11 15:52:06 +00:00
|
|
|
|
|
|
|
// Remove all but the body tags from full Parsoid docs
|
|
|
|
if ( strpos( $html, '<body' ) !== false ) {
|
|
|
|
preg_match( '`<body[^>]*>(.*)</body>`s', $html, $match );
|
2020-05-13 20:19:43 +00:00
|
|
|
$html = "<div>$match[1]</div>";
|
2020-05-11 15:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a comment pareser
|
|
|
|
*
|
|
|
|
* @param array $data
|
2020-05-14 22:44:49 +00:00
|
|
|
* @return CommentParser
|
2020-05-11 15:52:06 +00:00
|
|
|
*/
|
2020-05-14 22:44:49 +00:00
|
|
|
protected static function createParser( array $data ) : CommentParser {
|
2020-05-11 15:52:06 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2020-05-14 22:44:49 +00:00
|
|
|
return new CommentParser(
|
2020-05-11 15:52:06 +00:00
|
|
|
$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'] );
|
|
|
|
}
|
|
|
|
}
|