mediawiki-extensions-Visual.../modules/ve-mw/test/dm/ve.dm.Transaction.test.js
Roan Kattouw cf17789985 Introduce newFromDocumentReplace() transaction builder
Replaces newFromNodeReplacement(). newFromNodeReplacement was very
simplistic and didn't support metadata or internal list items, so
if you had comments or references inside of the data you were editing
(reference contents or an image caption), they'd get mangled.

With this, you can do:
newDoc = doc.getDocumentSlice( node );
// Edit newDoc
tx = ve.dm.Transaction.newFromDocumentReplace( doc, node, newDoc );
surface.change( newDoc );

and that takes care of metadata, internal list items, and things like
references that reference internal list items.

ve.dm.Document.js:
* In getDocumentSlice(), store a reference to the original document
  and the number of items in its InternalList at the time of slicing
  in the created slice. This is used for reconciliation when the
  modified slice is injected back into the parent document with
  newFromDocumentReplace().

ve.dm.InternalList.js:
* Add a method for merging in another InternalList. This provides a
  mapping from old to new InternalList indexes so the linear model data
  being injected by newFromDocumentReplace() can have its InternalList
  indexes remapped.

ve.dm.Transaction.js:
* Replace newFromNodeReplacement() with newFromDocumentReplace()

ve.ui.MWMediaEditDialog.js, ve.ui.MWReferenceDialog.js:
* Use getDocumentSlice/newFromDocumentReplace for editing captions/refs
* Change insertion code path to insert an empty internalItem/caption, then
  newFromDocumentReplace into that
* Add empty internalList to new mini-documents

ve/test/dm/ve.dm.Transaction.test.js:
* Replace newFromNodeReplacement tests with newFromDocumentReplace tests

ve-mw/test/dm/ve.dm.Transaction.test.js (new):
* Add tests for newFromDocumentReplace with mwReference nodes

ve.dm.mwExample.js:
* Add data for newFromDocumentReplace with mwReference tests

VisualEditor.hooks.php:
* Add new test file

Bug: 52102
Change-Id: I4aa980780114b391924f04df588e81c990c32983
2013-09-25 21:46:38 +00:00

145 lines
4.9 KiB
JavaScript

/*!
* VisualEditor DataModel Transaction tests.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
QUnit.module( 've.dm.Transaction' );
// FIXME duplicates test runner; should be using a data provider
QUnit.test( 'newFromDocumentReplace with references', function ( assert ) {
var i, j, doc2, tx, actualStoreItems, expectedStoreItems,
doc = ve.dm.example.createExampleDocument( 'internalData' ),
complexDoc = ve.dm.mwExample.createExampleDocument( 'complexInternalData' ),
comment = { 'type': 'alienMeta', 'attributes': { 'domElements': $( '<!-- hello -->' ).get() } },
withReference = [
{ 'type': 'paragraph' },
'B', 'a', 'r',
{ 'type': 'mwReference', 'attributes': {
'mw': {},
'about': '#mwt4',
'listIndex': 0,
'listGroup': 'mwReference/',
'listKey': null,
'refGroup': '',
'contentsUsed': true
} },
{ 'type': '/mwReference' },
{ 'type': '/paragraph' },
{ 'type': 'internalList' },
{ 'type': 'internalItem' },
{ 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } },
'B',
'a',
'z',
{ 'type': '/paragraph' },
{ 'type': '/internalItem' },
{ 'type': '/internalList' }
],
cases = [
{
'msg': 'metadata insertion',
'doc': complexDoc,
'range': new ve.Range( 0, 7 ),
'modify': function ( newDoc ) {
newDoc.commit( ve.dm.Transaction.newFromMetadataInsertion(
newDoc, 5, 0, [ comment ]
) );
},
'expectedOps': [
{
'type': 'replace',
'remove': complexDoc.getData( new ve.Range( 0, 7 ) ),
'insert': complexDoc.getData( new ve.Range( 0, 7 ) ),
'removeMetadata': complexDoc.getMetadata( new ve.Range( 0, 7 ) ),
'insertMetadata': complexDoc.getMetadata( new ve.Range( 0, 5 ) )
.concat( [ [ comment ] ] )
.concat( complexDoc.getMetadata( new ve.Range( 6, 8 ) ) )
},
{ 'type': 'retain', 'length': 1 },
{
'type': 'replace',
'remove': complexDoc.getData( new ve.Range( 8, 32 ) ),
'insert': complexDoc.getData( new ve.Range( 8, 32 ) ),
'removeMetadata': complexDoc.getMetadata( new ve.Range( 8, 32 ) ),
'insertMetadata': complexDoc.getMetadata( new ve.Range( 8, 32 ) )
},
{ 'type': 'retain', 'length': 1 }
]
},
{
'msg': 'metadata removal',
'doc': complexDoc,
'range': new ve.Range( 24, 31 ),
'modify': function ( newDoc ) {
newDoc.commit( ve.dm.Transaction.newFromMetadataRemoval(
newDoc, 6, new ve.Range( 0, 1 )
) );
},
'expectedOps': [
{ 'type': 'retain', 'length': 8 },
{
'type': 'replace',
'remove': complexDoc.getData( new ve.Range( 8, 32 ) ),
'insert': complexDoc.getData( new ve.Range( 8, 32 ) ),
'removeMetadata': complexDoc.getMetadata( new ve.Range( 8, 32 ) ),
'insertMetadata': complexDoc.getMetadata( new ve.Range( 8, 30 ) )
.concat( [ [] ] )
.concat( complexDoc.getMetadata( new ve.Range( 31, 32 ) ) )
},
{ 'type': 'retain', 'length': 1 }
]
},
{
'msg': 'inserting a brand new document; internal lists are merged and items renumbered',
'doc': complexDoc,
'range': new ve.Range( 7, 7 ),
'newDocData': withReference,
'expectedOps': [
{ 'type': 'retain', 'length': 7 },
{
'type': 'replace',
'remove': [],
'insert': withReference.slice( 0, 4 )
// Renumber listIndex from 0 to 2
.concat( [ ve.extendObject( true, {}, withReference[4],
{ 'attributes': { 'listIndex': 2 } } ) ] )
.concat( withReference.slice( 5, 7 ) )
},
{ 'type': 'retain', 'length': 1 },
{
'type': 'replace',
'remove': complexDoc.getData( new ve.Range( 8, 32 ) ),
'insert': complexDoc.getData( new ve.Range( 8, 32 ) )
.concat( withReference.slice( 8, 15 ) ),
'removeMetadata': complexDoc.getMetadata( new ve.Range( 8, 32 ) ),
'insertMetadata': complexDoc.getMetadata( new ve.Range( 8, 32 ) )
.concat( new Array( 7 ) )
},
{ 'type': 'retain', 'length': 1 }
]
}
];
QUnit.expect( 2 * cases.length );
for ( i = 0; i < cases.length; i++ ) {
doc = cases[i].doc; // TODO deep copy?
if ( cases[i].newDocData ) {
doc2 = new ve.dm.Document( cases[i].newDocData );
} else {
doc2 = doc.getDocumentSlice( cases[i].range );
cases[i].modify( doc2 );
}
tx = ve.dm.Transaction.newFromDocumentReplace( doc, cases[i].range, doc2 );
assert.deepEqualWithDomElements( tx.getOperations(), cases[i].expectedOps, cases[i].msg + ': transaction' );
actualStoreItems = [];
expectedStoreItems = cases[i].expectedStoreItems || [];
for ( j = 0; j < expectedStoreItems.length; j++ ) {
actualStoreItems[j] = doc.store.value( doc.store.indexOfHash(
ve.getHash( expectedStoreItems[j] )
) );
}
assert.deepEqual( actualStoreItems, expectedStoreItems, cases[i].msg + ': store items' );
}
} );