2013-05-19 04:15:45 +00:00
|
|
|
<?php
|
|
|
|
|
2014-07-25 20:19:15 +00:00
|
|
|
/**
|
2019-10-23 10:23:09 +00:00
|
|
|
* @covers \EchoDiffParser
|
2014-07-25 20:19:15 +00:00
|
|
|
* @group Echo
|
|
|
|
*/
|
2019-07-10 20:47:28 +00:00
|
|
|
class EchoDiffParserTest extends MediaWikiUnitTestCase {
|
2013-05-19 04:15:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provider_getChangeSet
|
|
|
|
*/
|
|
|
|
public function testGetChangeSet( $message, array $expect, $leftText, $rightText ) {
|
|
|
|
$changeSet = EchoDiscussionParser::getMachineReadableDiff( $leftText, $rightText );
|
|
|
|
unset( $changeSet['_info'] );
|
|
|
|
$this->assertEquals( $expect, $changeSet, $message );
|
|
|
|
}
|
|
|
|
|
2015-10-29 11:25:14 +00:00
|
|
|
public static function provider_getChangeSet() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'Duplicate content must generate no changes',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"a\nb\nc",
|
|
|
|
// Right text
|
|
|
|
"a\nb\nc",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'Removing blank lines must generate no changes',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"a\n\nb\n\nc",
|
|
|
|
// Right text
|
|
|
|
"a\nb\nc\n",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'Must generate a single add change with only lines added',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[ self::mockAction( 'add', "foo\nbar", 1 ) ],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left
|
|
|
|
"something",
|
|
|
|
// Right
|
|
|
|
"foo\nbar\nsomething",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'Must generate a single subtract change with only lines subtracted',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[ self::mockAction( 'subtract', "Zomg\nHiHiHi", 2 ) ],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left
|
|
|
|
"dummy\nZomg\nHiHiHi",
|
|
|
|
// Right
|
|
|
|
"dummy",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2018-08-14 11:23:26 +00:00
|
|
|
'Adding content separated by no change must generate multiple changes',
|
2013-05-19 04:15:45 +00:00
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
self::mockAction( 'add', 'b1', 3 ),
|
|
|
|
self::mockAction( 'add', "d1\nd2", 5, 6 ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"a\nb\nc\nd\ne\nf",
|
|
|
|
// Right text
|
|
|
|
"a\nb\nb1\nc\nd\nd1\nd2\ne\nf",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2017-01-24 10:33:37 +00:00
|
|
|
[
|
|
|
|
'Additon with empty line should be one action',
|
|
|
|
// Expected change set
|
|
|
|
[ self::mockAction( 'add', "User\n\nSignature", 1 ) ],
|
|
|
|
// Left text
|
|
|
|
"",
|
|
|
|
// Right text
|
|
|
|
"User\n\nSignature",
|
|
|
|
],
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'Extra blank lines on the edges must be trimmed',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[ self::mockAction( 'add', "Zomg\nHiHiHi", 1 ) ],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"",
|
|
|
|
// Right text
|
|
|
|
"\nZomg\nHiHiHi\n",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'Extra blank lines inside the content must not be trimmed',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[ self::mockAction( 'add', "\nZomg\nHiHiHi\n", 2 ) ],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"foo\nbar",
|
|
|
|
// Right text
|
|
|
|
"foo\n\nZomg\nHiHiHi\n\nbar",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'A blank line replaced with content must be an add',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[ self::mockAction( 'add', 'cowbell', 1 ) ],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left Text
|
|
|
|
"",
|
|
|
|
// Right Text
|
|
|
|
"cowbell",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'A blanked out line must be a subtraction',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[ self::mockAction( 'subtract', 'cowbell', 1 ) ],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"cowbell",
|
|
|
|
// Right text
|
|
|
|
"",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'A line with its content replaced must be a change',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[ self::mockChange( 'Its all about the journey', 'dummy', 1 ) ],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"Its all about the journey",
|
|
|
|
// Right text
|
|
|
|
"dummy",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'Changing lines and adding more must result in two changes',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
self::mockChange( 'Must be in a hurry to finish this thing', 'Must be in a hurry', 1 ),
|
|
|
|
self::mockAction( 'add', 'Finish this thing', 2 ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"Must be in a hurry to finish this thing",
|
|
|
|
// Right text
|
|
|
|
"Must be in a hurry\nFinish this thing",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'Changing multiple lines and adding more must result in two changes',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
self::mockChange( "Must not be\nin much of a hurry", "Must be\nin a hurry", 2 ),
|
|
|
|
self::mockAction( 'subtract', "to finish\nthis thing", 4 ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"abc\nMust not be\nin much of a hurry\nto finish\nthis thing",
|
|
|
|
// Right text
|
|
|
|
"abc\nMust be\nin a hurry",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
'Must generate multiple add, change, and subtract actions',
|
|
|
|
// Expected change set
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2013-05-19 04:15:45 +00:00
|
|
|
self::mockChange( "abc\nSome", "Other\nThings", 1 ),
|
|
|
|
self::mockAction( 'subtract', "Stuff", 3 ),
|
|
|
|
self::mockChange( "And\nThen", "There\nWas", 6, 5 ),
|
|
|
|
self::mockAction( 'add', 'Fencing', 8, 7 ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-19 04:15:45 +00:00
|
|
|
// Left text
|
|
|
|
"abc\nSome\nStuff\ndef\nghi\nAnd\nThen\njkl\nmno",
|
|
|
|
// Right text
|
|
|
|
"\nOther\nThings\ndef\nghi\nThere\nWas\nFencing\njkl\nmno",
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2013-05-19 04:15:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 11:25:14 +00:00
|
|
|
protected static function mockAction( $action, $content, $left, $right = null ) {
|
2013-05-19 04:15:45 +00:00
|
|
|
if ( $right === null ) {
|
|
|
|
$right = $left;
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2013-05-19 04:15:45 +00:00
|
|
|
'action' => $action,
|
|
|
|
'content' => $content,
|
|
|
|
'left-pos' => $left,
|
|
|
|
'right-pos' => $right,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2013-05-19 04:15:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 11:25:14 +00:00
|
|
|
public static function mockChange( $oldContent, $newContent, $left, $right = null ) {
|
2013-05-19 04:15:45 +00:00
|
|
|
if ( $right === null ) {
|
|
|
|
$right = $left;
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2013-05-19 04:15:45 +00:00
|
|
|
'action' => 'change',
|
|
|
|
'old_content' => $oldContent,
|
|
|
|
'new_content' => $newContent,
|
|
|
|
'left-pos' => $left,
|
|
|
|
'right-pos' => $right,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2013-05-19 04:15:45 +00:00
|
|
|
}
|
|
|
|
}
|