summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/diff
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/tests/phpunit/includes/diff')
-rw-r--r--www/wiki/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php134
-rw-r--r--www/wiki/tests/phpunit/includes/diff/DiffOpTest.php68
-rw-r--r--www/wiki/tests/phpunit/includes/diff/DiffTest.php19
-rw-r--r--www/wiki/tests/phpunit/includes/diff/DifferenceEngineTest.php148
-rw-r--r--www/wiki/tests/phpunit/includes/diff/FakeDiffOp.php11
5 files changed, 380 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php b/www/wiki/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php
new file mode 100644
index 00000000..8d94404c
--- /dev/null
+++ b/www/wiki/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php
@@ -0,0 +1,134 @@
+<?php
+
+/**
+ * @author Addshore
+ *
+ * @group Diff
+ */
+class ArrayDiffFormatterTest extends MediaWikiTestCase {
+
+ /**
+ * @param Diff $input
+ * @param array $expectedOutput
+ * @dataProvider provideTestFormat
+ * @covers ArrayDiffFormatter::format
+ */
+ public function testFormat( $input, $expectedOutput ) {
+ $instance = new ArrayDiffFormatter();
+ $output = $instance->format( $input );
+ $this->assertEquals( $expectedOutput, $output );
+ }
+
+ private function getMockDiff( $edits ) {
+ $diff = $this->getMockBuilder( Diff::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $diff->expects( $this->any() )
+ ->method( 'getEdits' )
+ ->will( $this->returnValue( $edits ) );
+ return $diff;
+ }
+
+ private function getMockDiffOp( $type = null, $orig = [], $closing = [] ) {
+ $diffOp = $this->getMockBuilder( DiffOp::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $diffOp->expects( $this->any() )
+ ->method( 'getType' )
+ ->will( $this->returnValue( $type ) );
+ $diffOp->expects( $this->any() )
+ ->method( 'getOrig' )
+ ->will( $this->returnValue( $orig ) );
+ if ( $type === 'change' ) {
+ $diffOp->expects( $this->any() )
+ ->method( 'getClosing' )
+ ->with( $this->isType( 'integer' ) )
+ ->will( $this->returnCallback( function () {
+ return 'mockLine';
+ } ) );
+ } else {
+ $diffOp->expects( $this->any() )
+ ->method( 'getClosing' )
+ ->will( $this->returnValue( $closing ) );
+ }
+ return $diffOp;
+ }
+
+ public function provideTestFormat() {
+ $emptyArrayTestCases = [
+ $this->getMockDiff( [] ),
+ $this->getMockDiff( [ $this->getMockDiffOp( 'add' ) ] ),
+ $this->getMockDiff( [ $this->getMockDiffOp( 'delete' ) ] ),
+ $this->getMockDiff( [ $this->getMockDiffOp( 'change' ) ] ),
+ $this->getMockDiff( [ $this->getMockDiffOp( 'copy' ) ] ),
+ $this->getMockDiff( [ $this->getMockDiffOp( 'FOOBARBAZ' ) ] ),
+ $this->getMockDiff( [ $this->getMockDiffOp( 'add', 'line' ) ] ),
+ $this->getMockDiff( [ $this->getMockDiffOp( 'delete', [], [ 'line' ] ) ] ),
+ $this->getMockDiff( [ $this->getMockDiffOp( 'copy', [], [ 'line' ] ) ] ),
+ ];
+
+ $otherTestCases = [];
+ $otherTestCases[] = [
+ $this->getMockDiff( [ $this->getMockDiffOp( 'add', [], [ 'a1' ] ) ] ),
+ [ [ 'action' => 'add', 'new' => 'a1', 'newline' => 1 ] ],
+ ];
+ $otherTestCases[] = [
+ $this->getMockDiff( [ $this->getMockDiffOp( 'add', [], [ 'a1', 'a2' ] ) ] ),
+ [
+ [ 'action' => 'add', 'new' => 'a1', 'newline' => 1 ],
+ [ 'action' => 'add', 'new' => 'a2', 'newline' => 2 ],
+ ],
+ ];
+ $otherTestCases[] = [
+ $this->getMockDiff( [ $this->getMockDiffOp( 'delete', [ 'd1' ] ) ] ),
+ [ [ 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ] ],
+ ];
+ $otherTestCases[] = [
+ $this->getMockDiff( [ $this->getMockDiffOp( 'delete', [ 'd1', 'd2' ] ) ] ),
+ [
+ [ 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ],
+ [ 'action' => 'delete', 'old' => 'd2', 'oldline' => 2 ],
+ ],
+ ];
+ $otherTestCases[] = [
+ $this->getMockDiff( [ $this->getMockDiffOp( 'change', [ 'd1' ], [ 'a1' ] ) ] ),
+ [ [
+ 'action' => 'change',
+ 'old' => 'd1',
+ 'new' => 'mockLine',
+ 'newline' => 1, 'oldline' => 1
+ ] ],
+ ];
+ $otherTestCases[] = [
+ $this->getMockDiff( [ $this->getMockDiffOp(
+ 'change',
+ [ 'd1', 'd2' ],
+ [ 'a1', 'a2' ]
+ ) ] ),
+ [
+ [
+ 'action' => 'change',
+ 'old' => 'd1',
+ 'new' => 'mockLine',
+ 'newline' => 1, 'oldline' => 1
+ ],
+ [
+ 'action' => 'change',
+ 'old' => 'd2',
+ 'new' => 'mockLine',
+ 'newline' => 2, 'oldline' => 2
+ ],
+ ],
+ ];
+
+ $testCases = [];
+ foreach ( $emptyArrayTestCases as $testCase ) {
+ $testCases[] = [ $testCase, [] ];
+ }
+ foreach ( $otherTestCases as $testCase ) {
+ $testCases[] = [ $testCase[0], $testCase[1] ];
+ }
+ return $testCases;
+ }
+
+}
diff --git a/www/wiki/tests/phpunit/includes/diff/DiffOpTest.php b/www/wiki/tests/phpunit/includes/diff/DiffOpTest.php
new file mode 100644
index 00000000..3026fad6
--- /dev/null
+++ b/www/wiki/tests/phpunit/includes/diff/DiffOpTest.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * @author Addshore
+ *
+ * @group Diff
+ */
+class DiffOpTest extends MediaWikiTestCase {
+
+ /**
+ * @covers DiffOp::getType
+ */
+ public function testGetType() {
+ $obj = new FakeDiffOp();
+ $obj->type = 'foo';
+ $this->assertEquals( 'foo', $obj->getType() );
+ }
+
+ /**
+ * @covers DiffOp::getOrig
+ */
+ public function testGetOrig() {
+ $obj = new FakeDiffOp();
+ $obj->orig = [ 'foo' ];
+ $this->assertEquals( [ 'foo' ], $obj->getOrig() );
+ }
+
+ /**
+ * @covers DiffOp::getClosing
+ */
+ public function testGetClosing() {
+ $obj = new FakeDiffOp();
+ $obj->closing = [ 'foo' ];
+ $this->assertEquals( [ 'foo' ], $obj->getClosing() );
+ }
+
+ /**
+ * @covers DiffOp::getClosing
+ */
+ public function testGetClosingWithParameter() {
+ $obj = new FakeDiffOp();
+ $obj->closing = [ 'foo', 'bar', 'baz' ];
+ $this->assertEquals( 'foo', $obj->getClosing( 0 ) );
+ $this->assertEquals( 'bar', $obj->getClosing( 1 ) );
+ $this->assertEquals( 'baz', $obj->getClosing( 2 ) );
+ $this->assertEquals( null, $obj->getClosing( 3 ) );
+ }
+
+ /**
+ * @covers DiffOp::norig
+ */
+ public function testNorig() {
+ $obj = new FakeDiffOp();
+ $this->assertEquals( 0, $obj->norig() );
+ $obj->orig = [ 'foo' ];
+ $this->assertEquals( 1, $obj->norig() );
+ }
+
+ /**
+ * @covers DiffOp::nclosing
+ */
+ public function testNclosing() {
+ $obj = new FakeDiffOp();
+ $this->assertEquals( 0, $obj->nclosing() );
+ $obj->closing = [ 'foo' ];
+ $this->assertEquals( 1, $obj->nclosing() );
+ }
+
+}
diff --git a/www/wiki/tests/phpunit/includes/diff/DiffTest.php b/www/wiki/tests/phpunit/includes/diff/DiffTest.php
new file mode 100644
index 00000000..da6d7d95
--- /dev/null
+++ b/www/wiki/tests/phpunit/includes/diff/DiffTest.php
@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * @author Addshore
+ *
+ * @group Diff
+ */
+class DiffTest extends MediaWikiTestCase {
+
+ /**
+ * @covers Diff::getEdits
+ */
+ public function testGetEdits() {
+ $obj = new Diff( [], [] );
+ $obj->edits = 'FooBarBaz';
+ $this->assertEquals( 'FooBarBaz', $obj->getEdits() );
+ }
+
+}
diff --git a/www/wiki/tests/phpunit/includes/diff/DifferenceEngineTest.php b/www/wiki/tests/phpunit/includes/diff/DifferenceEngineTest.php
new file mode 100644
index 00000000..57aeb200
--- /dev/null
+++ b/www/wiki/tests/phpunit/includes/diff/DifferenceEngineTest.php
@@ -0,0 +1,148 @@
+<?php
+
+use Wikimedia\TestingAccessWrapper;
+
+/**
+ * @covers DifferenceEngine
+ *
+ * @todo tests for the rest of DifferenceEngine!
+ *
+ * @group Database
+ * @group Diff
+ *
+ * @author Katie Filbert < aude.wiki@gmail.com >
+ */
+class DifferenceEngineTest extends MediaWikiTestCase {
+
+ protected $context;
+
+ private static $revisions;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $title = $this->getTitle();
+
+ $this->context = new RequestContext();
+ $this->context->setTitle( $title );
+
+ if ( !self::$revisions ) {
+ self::$revisions = $this->doEdits();
+ }
+ }
+
+ /**
+ * @return Title
+ */
+ protected function getTitle() {
+ $namespace = $this->getDefaultWikitextNS();
+ return Title::newFromText( 'Kitten', $namespace );
+ }
+
+ /**
+ * @return int[] Revision ids
+ */
+ protected function doEdits() {
+ $title = $this->getTitle();
+ $page = WikiPage::factory( $title );
+
+ $strings = [ "it is a kitten", "two kittens", "three kittens", "four kittens" ];
+ $revisions = [];
+
+ foreach ( $strings as $string ) {
+ $content = ContentHandler::makeContent( $string, $title );
+ $page->doEditContent( $content, 'edit page' );
+ $revisions[] = $page->getLatest();
+ }
+
+ return $revisions;
+ }
+
+ public function testMapDiffPrevNext() {
+ $cases = $this->getMapDiffPrevNextCases();
+
+ foreach ( $cases as $case ) {
+ list( $expected, $old, $new, $message ) = $case;
+
+ $diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
+ $diffMap = $diffEngine->mapDiffPrevNext( $old, $new );
+ $this->assertEquals( $expected, $diffMap, $message );
+ }
+ }
+
+ private function getMapDiffPrevNextCases() {
+ $revs = self::$revisions;
+
+ return [
+ [ [ $revs[1], $revs[2] ], $revs[2], 'prev', 'diff=prev' ],
+ [ [ $revs[2], $revs[3] ], $revs[2], 'next', 'diff=next' ],
+ [ [ $revs[1], $revs[3] ], $revs[1], $revs[3], 'diff=' . $revs[3] ]
+ ];
+ }
+
+ public function testLoadRevisionData() {
+ $cases = $this->getLoadRevisionDataCases();
+
+ foreach ( $cases as $case ) {
+ list( $expectedOld, $expectedNew, $old, $new, $message ) = $case;
+
+ $diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
+ $diffEngine->loadRevisionData();
+
+ $this->assertEquals( $diffEngine->getOldid(), $expectedOld, $message );
+ $this->assertEquals( $diffEngine->getNewid(), $expectedNew, $message );
+ }
+ }
+
+ private function getLoadRevisionDataCases() {
+ $revs = self::$revisions;
+
+ return [
+ [ $revs[2], $revs[3], $revs[3], 'prev', 'diff=prev' ],
+ [ $revs[2], $revs[3], $revs[2], 'next', 'diff=next' ],
+ [ $revs[1], $revs[3], $revs[1], $revs[3], 'diff=' . $revs[3] ],
+ [ $revs[1], $revs[3], $revs[1], 0, 'diff=0' ]
+ ];
+ }
+
+ public function testGetOldid() {
+ $revs = self::$revisions;
+
+ $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
+ $this->assertEquals( $revs[1], $diffEngine->getOldid(), 'diff get old id' );
+ }
+
+ public function testGetNewid() {
+ $revs = self::$revisions;
+
+ $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
+ $this->assertEquals( $revs[2], $diffEngine->getNewid(), 'diff get new id' );
+ }
+
+ public function provideLocaliseTitleTooltipsTestData() {
+ return [
+ 'moved paragraph left shoud get new location title' => [
+ '<a class="mw-diff-movedpara-left">⚫</a>',
+ '<a class="mw-diff-movedpara-left" title="(diff-paragraph-moved-tonew)">⚫</a>',
+ ],
+ 'moved paragraph right shoud get old location title' => [
+ '<a class="mw-diff-movedpara-right">⚫</a>',
+ '<a class="mw-diff-movedpara-right" title="(diff-paragraph-moved-toold)">⚫</a>',
+ ],
+ 'nothing changed when key not hit' => [
+ '<a class="mw-diff-movedpara-rightis">⚫</a>',
+ '<a class="mw-diff-movedpara-rightis">⚫</a>',
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideLocaliseTitleTooltipsTestData
+ */
+ public function testAddLocalisedTitleTooltips( $input, $expected ) {
+ $this->setContentLang( 'qqx' );
+ $diffEngine = TestingAccessWrapper::newFromObject( new DifferenceEngine() );
+ $this->assertEquals( $expected, $diffEngine->addLocalisedTitleTooltips( $input ) );
+ }
+
+}
diff --git a/www/wiki/tests/phpunit/includes/diff/FakeDiffOp.php b/www/wiki/tests/phpunit/includes/diff/FakeDiffOp.php
new file mode 100644
index 00000000..70c8f64a
--- /dev/null
+++ b/www/wiki/tests/phpunit/includes/diff/FakeDiffOp.php
@@ -0,0 +1,11 @@
+<?php
+
+/**
+ * Class FakeDiffOp used to test abstract class DiffOp
+ */
+class FakeDiffOp extends DiffOp {
+
+ public function reverse() {
+ return null;
+ }
+}