summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/diff
diff options
context:
space:
mode:
Diffstat (limited to 'tests/phpunit/includes/diff')
-rw-r--r--tests/phpunit/includes/diff/ArrayDiffFormatterTest.php135
-rw-r--r--tests/phpunit/includes/diff/DiffOpTest.php73
-rw-r--r--tests/phpunit/includes/diff/DiffTest.php20
-rw-r--r--tests/phpunit/includes/diff/DifferenceEngineTest.php121
-rw-r--r--tests/phpunit/includes/diff/FakeDiffOp.php11
5 files changed, 360 insertions, 0 deletions
diff --git a/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php b/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php
new file mode 100644
index 00000000..188ad3fd
--- /dev/null
+++ b/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php
@@ -0,0 +1,135 @@
+<?php
+
+/**
+ * @licence GNU GPL v2+
+ * @author Adam Shorland
+ *
+ * @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' )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $diff->expects( $this->any() )
+ ->method( 'getEdits' )
+ ->will( $this->returnValue( $edits ) );
+ return $diff;
+ }
+
+ private function getMockDiffOp( $type = null, $orig = array(), $closing = array() ) {
+ $diffOp = $this->getMockBuilder( 'DiffOp' )
+ ->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 = array(
+ $this->getMockDiff( array() ),
+ $this->getMockDiff( array( $this->getMockDiffOp( 'add' ) ) ),
+ $this->getMockDiff( array( $this->getMockDiffOp( 'delete' ) ) ),
+ $this->getMockDiff( array( $this->getMockDiffOp( 'change' ) ) ),
+ $this->getMockDiff( array( $this->getMockDiffOp( 'copy' ) ) ),
+ $this->getMockDiff( array( $this->getMockDiffOp( 'FOOBARBAZ' ) ) ),
+ $this->getMockDiff( array( $this->getMockDiffOp( 'add', 'line' ) ) ),
+ $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array(), array( 'line' ) ) ) ),
+ $this->getMockDiff( array( $this->getMockDiffOp( 'copy', array(), array( 'line' ) ) ) ),
+ );
+
+ $otherTestCases = array();
+ $otherTestCases[] = array(
+ $this->getMockDiff( array( $this->getMockDiffOp( 'add', array( ), array( 'a1' ) ) ) ),
+ array( array( 'action' => 'add', 'new' => 'a1', 'newline' => 1 ) ),
+ );
+ $otherTestCases[] = array(
+ $this->getMockDiff( array( $this->getMockDiffOp( 'add', array( ), array( 'a1', 'a2' ) ) ) ),
+ array(
+ array( 'action' => 'add', 'new' => 'a1', 'newline' => 1 ),
+ array( 'action' => 'add', 'new' => 'a2', 'newline' => 2 ),
+ ),
+ );
+ $otherTestCases[] = array(
+ $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1' ) ) ) ),
+ array( array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ) ),
+ );
+ $otherTestCases[] = array(
+ $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1', 'd2' ) ) ) ),
+ array(
+ array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ),
+ array( 'action' => 'delete', 'old' => 'd2', 'oldline' => 2 ),
+ ),
+ );
+ $otherTestCases[] = array(
+ $this->getMockDiff( array( $this->getMockDiffOp( 'change', array( 'd1' ), array( 'a1' ) ) ) ),
+ array( array(
+ 'action' => 'change',
+ 'old' => 'd1',
+ 'new' => 'mockLine',
+ 'newline' => 1, 'oldline' => 1
+ ) ),
+ );
+ $otherTestCases[] = array(
+ $this->getMockDiff( array( $this->getMockDiffOp(
+ 'change',
+ array( 'd1', 'd2' ),
+ array( 'a1', 'a2' )
+ ) ) ),
+ array(
+ array(
+ 'action' => 'change',
+ 'old' => 'd1',
+ 'new' => 'mockLine',
+ 'newline' => 1, 'oldline' => 1
+ ),
+ array(
+ 'action' => 'change',
+ 'old' => 'd2',
+ 'new' => 'mockLine',
+ 'newline' => 2, 'oldline' => 2
+ ),
+ ),
+ );
+
+ $testCases = array();
+ foreach ( $emptyArrayTestCases as $testCase ) {
+ $testCases[] = array( $testCase, array() );
+ }
+ foreach ( $otherTestCases as $testCase ) {
+ $testCases[] = array( $testCase[0], $testCase[1] );
+ }
+ return $testCases;
+ }
+
+}
diff --git a/tests/phpunit/includes/diff/DiffOpTest.php b/tests/phpunit/includes/diff/DiffOpTest.php
new file mode 100644
index 00000000..d89b89fe
--- /dev/null
+++ b/tests/phpunit/includes/diff/DiffOpTest.php
@@ -0,0 +1,73 @@
+<?php
+
+//Load our FakeDiffOp
+require_once __DIR__ . DIRECTORY_SEPARATOR . 'FakeDiffOp.php';
+
+/**
+ * @licence GNU GPL v2+
+ * @author Adam Shorland
+ *
+ * @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 = array( 'foo' );
+ $this->assertEquals( array( 'foo' ), $obj->getOrig() );
+ }
+
+ /**
+ * @covers DiffOp::getClosing
+ */
+ public function testGetClosing() {
+ $obj = new FakeDiffOp();
+ $obj->closing = array( 'foo' );
+ $this->assertEquals( array( 'foo' ), $obj->getClosing() );
+ }
+
+ /**
+ * @covers DiffOp::getClosing
+ */
+ public function testGetClosingWithParameter() {
+ $obj = new FakeDiffOp();
+ $obj->closing = array( '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 = array( 'foo' );
+ $this->assertEquals( 1, $obj->norig() );
+ }
+
+ /**
+ * @covers DiffOp::nclosing
+ */
+ public function testNclosing() {
+ $obj = new FakeDiffOp();
+ $this->assertEquals( 0, $obj->nclosing() );
+ $obj->closing = array( 'foo' );
+ $this->assertEquals( 1, $obj->nclosing() );
+ }
+
+}
diff --git a/tests/phpunit/includes/diff/DiffTest.php b/tests/phpunit/includes/diff/DiffTest.php
new file mode 100644
index 00000000..1911c82a
--- /dev/null
+++ b/tests/phpunit/includes/diff/DiffTest.php
@@ -0,0 +1,20 @@
+<?php
+
+/**
+ * @licence GNU GPL v2+
+ * @author Adam Shorland
+ *
+ * @group Diff
+ */
+class DiffTest extends MediaWikiTestCase {
+
+ /**
+ * @covers Diff::getEdits
+ */
+ public function testGetEdits() {
+ $obj = new Diff( array(), array() );
+ $obj->edits = 'FooBarBaz';
+ $this->assertEquals( 'FooBarBaz', $obj->getEdits() );
+ }
+
+}
diff --git a/tests/phpunit/includes/diff/DifferenceEngineTest.php b/tests/phpunit/includes/diff/DifferenceEngineTest.php
new file mode 100644
index 00000000..5474b963
--- /dev/null
+++ b/tests/phpunit/includes/diff/DifferenceEngineTest.php
@@ -0,0 +1,121 @@
+<?php
+
+/**
+ * @covers DifferenceEngine
+ *
+ * @todo tests for the rest of DifferenceEngine!
+ *
+ * @group Database
+ * @group Diff
+ *
+ * @licence GNU GPL v2+
+ * @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 = array( "it is a kitten", "two kittens", "three kittens", "four kittens" );
+ $revisions = array();
+
+ 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 array(
+ array( array( $revs[1], $revs[2] ), $revs[2], 'prev', 'diff=prev' ),
+ array( array( $revs[2], $revs[3] ), $revs[2], 'next', 'diff=next' ),
+ array( array( $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 array(
+ array( $revs[2], $revs[3], $revs[3], 'prev', 'diff=prev' ),
+ array( $revs[2], $revs[3], $revs[2], 'next', 'diff=next' ),
+ array( $revs[1], $revs[3], $revs[1], $revs[3], 'diff=' . $revs[3] ),
+ array( $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' );
+ }
+
+}
diff --git a/tests/phpunit/includes/diff/FakeDiffOp.php b/tests/phpunit/includes/diff/FakeDiffOp.php
new file mode 100644
index 00000000..70c8f64a
--- /dev/null
+++ b/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;
+ }
+}