Test for WikiPage::updateRedirectOn
authoraddshore <addshorewiki@gmail.com>
Mon, 4 Dec 2017 12:11:26 +0000 (13:11 +0100)
committeraddshore <addshorewiki@gmail.com>
Mon, 4 Dec 2017 12:11:26 +0000 (13:11 +0100)
Bug: T180989
Change-Id: I798ded72142503e52395b45fe9ec71a215d5ab92

tests/phpunit/includes/page/WikiPageDbTestBase.php

index 64ad644..fa48fb4 100644 (file)
@@ -11,6 +11,7 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase {
                        $this->tablesUsed,
                        [ 'page',
                                'revision',
+                               'redirect',
                                'archive',
                                'category',
                                'ip_changes',
@@ -1183,4 +1184,55 @@ more stuff
                $this->assertEquals( 1, Category::newFromName( 'C' )->getPageCount() );
        }
 
+       public function provideUpdateRedirectOn() {
+               yield [ '#REDIRECT [[Foo]]', true, null, true, true, 0 ];
+               yield [ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ];
+               yield [ 'SomeText', false, null, false, true, 0 ];
+               yield [ 'SomeText', false, 'Foo', false, false, 1 ];
+       }
+
+       /**
+        * @dataProvider provideUpdateRedirectOn
+        * @covers WikiPage::updateRedirectOn
+        *
+        * @param string $initialText
+        * @param bool $initialRedirectState
+        * @param string|null $redirectTitle
+        * @param bool|null $lastRevIsRedirect
+        * @param bool $expectedSuccess
+        * @param int $expectedRowCount
+        */
+       public function testUpdateRedirectOn(
+               $initialText,
+               $initialRedirectState,
+               $redirectTitle,
+               $lastRevIsRedirect,
+               $expectedSuccess,
+               $expectedRowCount
+       ) {
+               static $pageCounter = 0;
+               $pageCounter++;
+
+               $page = $this->createPage( Title::newFromText( __METHOD__ . $pageCounter ), $initialText );
+               $this->assertSame( $initialRedirectState, $page->isRedirect() );
+
+               $redirectTitle = is_string( $redirectTitle )
+                       ? Title::newFromText( $redirectTitle )
+                       : $redirectTitle;
+
+               $success = $page->updateRedirectOn( $this->db, $redirectTitle, $lastRevIsRedirect );
+               $this->assertSame( $expectedSuccess, $success, 'Success assertion' );
+               /**
+                * updateRedirectOn explicitly updates the redirect table (and not the page table).
+                * Most of core checks the page table for redirect status, so we have to be ugly and
+                * assert a select from the table here.
+                */
+               $this->assertSelect(
+                       'redirect',
+                       'COUNT(*)',
+                       [ 'rd_from' => $page->getId() ],
+                       [ [ strval( $expectedRowCount ) ] ]
+               );
+       }
+
 }