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-06-16 20:13:31 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
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 {
|
2020-06-16 20:13:31 +00:00
|
|
|
$doc = DOMUtils::parseHTML( $html );
|
2020-05-11 15:52:06 +00:00
|
|
|
$doc->preserveWhiteSpace = false;
|
|
|
|
return $doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parsed JSON from path
|
|
|
|
*
|
|
|
|
* @param string $relativePath
|
2020-05-19 19:01:03 +00:00
|
|
|
* @param bool $assoc See json_decode()
|
2020-05-11 15:52:06 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-05-19 19:01:03 +00:00
|
|
|
protected static function getJson( string $relativePath, bool $assoc = true ) : array {
|
2020-05-11 15:52:06 +00:00
|
|
|
$json = json_decode(
|
2020-05-18 20:07:00 +00:00
|
|
|
file_get_contents( __DIR__ . '/' . $relativePath ),
|
2020-05-19 19:01:03 +00:00
|
|
|
$assoc
|
2020-05-11 15:52:06 +00:00
|
|
|
);
|
|
|
|
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'] );
|
|
|
|
}
|
|
|
|
}
|