2020-01-21 22:01:11 +00:00
|
|
|
var
|
2020-03-19 18:43:51 +00:00
|
|
|
testUtils = require( './testUtils.js' ),
|
2020-07-20 21:15:03 +00:00
|
|
|
Parser = require( 'ext.discussionTools.init' ).Parser,
|
2020-02-25 02:10:27 +00:00
|
|
|
modifier = require( 'ext.discussionTools.init' ).modifier;
|
2020-01-21 22:01:11 +00:00
|
|
|
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
QUnit.module( 'mw.dt.modifier', QUnit.newMwEnvironment() );
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2022-01-14 00:06:29 +00:00
|
|
|
require( '../cases/modified.json' ).forEach( function ( caseItem, i ) {
|
|
|
|
// This should be one test with many cases, rather than multiple tests, but the cases are large
|
|
|
|
// enough that processing all of them at once causes timeouts in Karma test runner.
|
|
|
|
QUnit.test( '#addListItem/#removeAddedListItem case ' + i, function ( assert ) {
|
|
|
|
var fixture = document.getElementById( 'qunit-fixture' );
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var dom = mw.template.get( 'test.DiscussionTools', caseItem.dom ).render(),
|
2020-05-11 14:25:01 +00:00
|
|
|
expected = mw.template.get( 'test.DiscussionTools', caseItem.expected ).render(),
|
|
|
|
config = require( caseItem.config ),
|
2022-01-11 15:50:44 +00:00
|
|
|
data = require( caseItem.data ),
|
|
|
|
title = mw.Title.newFromText( caseItem.title );
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
testUtils.overrideMwConfig( config );
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
$( fixture ).empty().append( expected );
|
2021-04-08 13:46:09 +00:00
|
|
|
var expectedHtml = fixture.innerHTML;
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2020-07-30 23:34:56 +00:00
|
|
|
$( fixture ).empty().append( dom );
|
2021-04-08 13:46:09 +00:00
|
|
|
var reverseExpectedHtml = fixture.innerHTML;
|
2020-02-24 21:58:51 +00:00
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
var threadItemSet = new Parser( data ).parse( fixture, title );
|
|
|
|
var comments = threadItemSet.getCommentItems();
|
2020-01-21 22:01:11 +00:00
|
|
|
|
|
|
|
// Add a reply to every comment. Note that this inserts *all* of the replies, unlike the real
|
|
|
|
// thing, which only deals with one at a time. This isn't ideal but resetting everything after
|
|
|
|
// every reply would be super slow.
|
2021-04-08 13:46:09 +00:00
|
|
|
var nodes = [];
|
|
|
|
comments.forEach( function ( comment ) {
|
2022-01-12 00:18:27 +00:00
|
|
|
var node = modifier.addListItem( comment, caseItem.replyIndentation || 'invisible' );
|
2021-04-08 13:46:09 +00:00
|
|
|
node.textContent = 'Reply to ' + comment.id;
|
2020-02-24 21:58:51 +00:00
|
|
|
nodes.push( node );
|
2021-04-08 13:46:09 +00:00
|
|
|
} );
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2020-03-04 20:25:35 +00:00
|
|
|
// Uncomment this to get updated content for the "modified HTML" files, for copy/paste:
|
2020-01-21 22:01:11 +00:00
|
|
|
// console.log( fixture.innerHTML );
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var actualHtml = fixture.innerHTML;
|
2020-01-21 22:01:11 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
actualHtml,
|
|
|
|
expectedHtml,
|
2020-05-11 14:25:01 +00:00
|
|
|
caseItem.name
|
2020-01-21 22:01:11 +00:00
|
|
|
);
|
2020-02-24 21:58:51 +00:00
|
|
|
|
|
|
|
// Now discard the replies and verify we get the original document back.
|
2021-04-08 13:46:09 +00:00
|
|
|
nodes.forEach( function ( node ) {
|
|
|
|
modifier.removeAddedListItem( node );
|
|
|
|
} );
|
2020-02-24 21:58:51 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var reverseActualHtml = fixture.innerHTML;
|
2020-02-24 21:58:51 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
reverseActualHtml,
|
|
|
|
reverseExpectedHtml,
|
2020-05-11 14:25:01 +00:00
|
|
|
caseItem.name + ' (discard replies)'
|
2020-02-24 21:58:51 +00:00
|
|
|
);
|
2020-05-11 14:25:01 +00:00
|
|
|
} );
|
2020-01-21 22:01:11 +00:00
|
|
|
} );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
|
|
|
QUnit.test( '#addReplyLink', function ( assert ) {
|
2020-05-18 20:07:00 +00:00
|
|
|
var cases = require( '../cases/reply.json' ),
|
2020-05-11 14:25:01 +00:00
|
|
|
fixture = document.getElementById( 'qunit-fixture' );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var dom = mw.template.get( 'test.DiscussionTools', caseItem.dom ).render(),
|
2020-05-11 14:25:01 +00:00
|
|
|
expected = mw.template.get( 'test.DiscussionTools', caseItem.expected ).render(),
|
|
|
|
config = require( caseItem.config ),
|
2022-01-11 15:50:44 +00:00
|
|
|
data = require( caseItem.data ),
|
|
|
|
title = mw.Title.newFromText( caseItem.title );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
testUtils.overrideMwConfig( config );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
$( fixture ).empty().append( expected );
|
2021-04-08 13:46:09 +00:00
|
|
|
var expectedHtml = fixture.innerHTML;
|
2020-03-02 18:50:36 +00:00
|
|
|
|
2020-07-30 23:34:56 +00:00
|
|
|
$( fixture ).empty().append( dom );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
var threadItemSet = new Parser( data ).parse( fixture, title );
|
|
|
|
var comments = threadItemSet.getCommentItems();
|
2020-03-02 18:50:36 +00:00
|
|
|
|
|
|
|
// Add a reply link to every comment.
|
2021-04-08 13:46:09 +00:00
|
|
|
comments.forEach( function ( comment ) {
|
|
|
|
var linkNode = document.createElement( 'a' );
|
2020-03-02 18:50:36 +00:00
|
|
|
linkNode.textContent = 'Reply';
|
|
|
|
linkNode.href = '#';
|
2021-04-08 13:46:09 +00:00
|
|
|
modifier.addReplyLink( comment, linkNode );
|
|
|
|
} );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
2020-03-04 20:25:35 +00:00
|
|
|
// Uncomment this to get updated content for the "reply HTML" files, for copy/paste:
|
2020-03-02 18:50:36 +00:00
|
|
|
// console.log( fixture.innerHTML );
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var actualHtml = fixture.innerHTML;
|
2020-03-02 18:50:36 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
actualHtml,
|
|
|
|
expectedHtml,
|
2020-05-11 14:25:01 +00:00
|
|
|
caseItem.name
|
2020-03-02 18:50:36 +00:00
|
|
|
);
|
2020-05-11 14:25:01 +00:00
|
|
|
} );
|
2020-03-02 18:50:36 +00:00
|
|
|
} );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
QUnit.test( '#unwrapList', function ( assert ) {
|
2020-05-18 20:07:00 +00:00
|
|
|
var cases = require( '../cases/unwrap.json' );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var container = document.createElement( 'div' );
|
|
|
|
|
|
|
|
container.innerHTML = caseItem.html;
|
2020-05-26 20:47:46 +00:00
|
|
|
modifier.unwrapList( container.childNodes[ caseItem.index || 0 ] );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
2020-06-03 12:53:36 +00:00
|
|
|
container.innerHTML,
|
2020-04-27 16:23:27 +00:00
|
|
|
caseItem.expected,
|
|
|
|
caseItem.name
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
2020-06-26 22:24:14 +00:00
|
|
|
|
2020-07-15 21:43:56 +00:00
|
|
|
QUnit.test( 'isWikitextSigned', function ( assert ) {
|
|
|
|
var cases = require( '../cases/isWikitextSigned.json' );
|
2020-06-26 22:24:14 +00:00
|
|
|
|
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
assert.strictEqual(
|
2020-07-15 21:43:56 +00:00
|
|
|
modifier.isWikitextSigned( caseItem.wikitext ),
|
|
|
|
caseItem.expected,
|
|
|
|
caseItem.msg
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'isHtmlSigned', function ( assert ) {
|
|
|
|
var cases = require( '../cases/isHtmlSigned.json' );
|
|
|
|
|
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var container = document.createElement( 'div' );
|
|
|
|
container.innerHTML = caseItem.html;
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
modifier.isHtmlSigned( container ),
|
2020-06-26 22:24:14 +00:00
|
|
|
caseItem.expected,
|
|
|
|
caseItem.msg
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'sanitizeWikitextLinebreaks', function ( assert ) {
|
|
|
|
var cases = require( '../cases/sanitize-wikitext-linebreaks.json' );
|
|
|
|
|
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
assert.strictEqual(
|
|
|
|
modifier.sanitizeWikitextLinebreaks( caseItem.wikitext ),
|
|
|
|
caseItem.expected,
|
|
|
|
caseItem.msg
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
2021-11-13 15:27:38 +00:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// * addSiblingListItem
|