diff options
Diffstat (limited to 'www/wiki/tests/phpunit/includes/deferred')
7 files changed, 1056 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/includes/deferred/CdnCacheUpdateTest.php b/www/wiki/tests/phpunit/includes/deferred/CdnCacheUpdateTest.php new file mode 100644 index 00000000..f3c949d3 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/deferred/CdnCacheUpdateTest.php @@ -0,0 +1,31 @@ +<?php + +use Wikimedia\TestingAccessWrapper; + +class CdnCacheUpdateTest extends MediaWikiTestCase { + + /** + * @covers CdnCacheUpdate::merge + */ + public function testPurgeMergeWeb() { + $this->setMwGlobals( 'wgCommandLineMode', false ); + + $urls1 = []; + $title = Title::newMainPage(); + $urls1[] = $title->getCanonicalURL( '?x=1' ); + $urls1[] = $title->getCanonicalURL( '?x=2' ); + $urls1[] = $title->getCanonicalURL( '?x=3' ); + $update1 = new CdnCacheUpdate( $urls1 ); + DeferredUpdates::addUpdate( $update1 ); + + $urls2 = []; + $urls2[] = $title->getCanonicalURL( '?x=2' ); + $urls2[] = $title->getCanonicalURL( '?x=3' ); + $urls2[] = $title->getCanonicalURL( '?x=4' ); + $update2 = new CdnCacheUpdate( $urls2 ); + DeferredUpdates::addUpdate( $update2 ); + + $wrapper = TestingAccessWrapper::newFromObject( $update1 ); + $this->assertEquals( array_merge( $urls1, $urls2 ), $wrapper->urls ); + } +} diff --git a/www/wiki/tests/phpunit/includes/deferred/DeferredUpdatesTest.php b/www/wiki/tests/phpunit/includes/deferred/DeferredUpdatesTest.php new file mode 100644 index 00000000..6b417073 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/deferred/DeferredUpdatesTest.php @@ -0,0 +1,338 @@ +<?php + +use MediaWiki\MediaWikiServices; + +class DeferredUpdatesTest extends MediaWikiTestCase { + + /** + * @covers DeferredUpdates::addUpdate + * @covers DeferredUpdates::push + * @covers DeferredUpdates::doUpdates + * @covers DeferredUpdates::execute + * @covers DeferredUpdates::runUpdate + */ + public function testAddAndRun() { + $update = $this->getMockBuilder( DeferrableUpdate::class ) + ->setMethods( [ 'doUpdate' ] )->getMock(); + $update->expects( $this->once() )->method( 'doUpdate' ); + + DeferredUpdates::addUpdate( $update ); + DeferredUpdates::doUpdates(); + } + + /** + * @covers DeferredUpdates::addUpdate + * @covers DeferredUpdates::push + */ + public function testAddMergeable() { + $this->setMwGlobals( 'wgCommandLineMode', false ); + + $update1 = $this->getMockBuilder( MergeableUpdate::class ) + ->setMethods( [ 'merge', 'doUpdate' ] )->getMock(); + $update1->expects( $this->once() )->method( 'merge' ); + $update1->expects( $this->never() )->method( 'doUpdate' ); + + $update2 = $this->getMockBuilder( MergeableUpdate::class ) + ->setMethods( [ 'merge', 'doUpdate' ] )->getMock(); + $update2->expects( $this->never() )->method( 'merge' ); + $update2->expects( $this->never() )->method( 'doUpdate' ); + + DeferredUpdates::addUpdate( $update1 ); + DeferredUpdates::addUpdate( $update2 ); + } + + /** + * @covers DeferredUpdates::addCallableUpdate + * @covers MWCallableUpdate::getOrigin + */ + public function testAddCallableUpdate() { + $this->setMwGlobals( 'wgCommandLineMode', true ); + + $ran = 0; + DeferredUpdates::addCallableUpdate( function () use ( &$ran ) { + $ran++; + } ); + DeferredUpdates::doUpdates(); + + $this->assertSame( 1, $ran, 'Update ran' ); + } + + /** + * @covers DeferredUpdates::getPendingUpdates + * @covers DeferredUpdates::clearPendingUpdates + */ + public function testGetPendingUpdates() { + // Prevent updates from running + $this->setMwGlobals( 'wgCommandLineMode', false ); + + $pre = DeferredUpdates::PRESEND; + $post = DeferredUpdates::POSTSEND; + $all = DeferredUpdates::ALL; + + $update = $this->getMock( DeferrableUpdate::class ); + $update->expects( $this->never() ) + ->method( 'doUpdate' ); + + DeferredUpdates::addUpdate( $update, $pre ); + $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $pre ) ); + $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $post ) ); + $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) ); + $this->assertCount( 1, DeferredUpdates::getPendingUpdates() ); + DeferredUpdates::clearPendingUpdates(); + $this->assertCount( 0, DeferredUpdates::getPendingUpdates() ); + + DeferredUpdates::addUpdate( $update, $post ); + $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $pre ) ); + $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $post ) ); + $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) ); + $this->assertCount( 1, DeferredUpdates::getPendingUpdates() ); + DeferredUpdates::clearPendingUpdates(); + $this->assertCount( 0, DeferredUpdates::getPendingUpdates() ); + } + + /** + * @covers DeferredUpdates::doUpdates + * @covers DeferredUpdates::execute + * @covers DeferredUpdates::addUpdate + */ + public function testDoUpdatesWeb() { + $this->setMwGlobals( 'wgCommandLineMode', false ); + + $updates = [ + '1' => "deferred update 1;\n", + '2' => "deferred update 2;\n", + '2-1' => "deferred update 1 within deferred update 2;\n", + '2-2' => "deferred update 2 within deferred update 2;\n", + '3' => "deferred update 3;\n", + '3-1' => "deferred update 1 within deferred update 3;\n", + '3-2' => "deferred update 2 within deferred update 3;\n", + '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n", + '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n", + ]; + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['1']; + } + ); + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['2']; + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['2-1']; + } + ); + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['2-2']; + } + ); + } + ); + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3']; + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3-1']; + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3-1-1']; + } + ); + } + ); + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3-2']; + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3-2-1']; + } + ); + } + ); + } + ); + + $this->assertEquals( 3, DeferredUpdates::pendingUpdatesCount() ); + + $this->expectOutputString( implode( '', $updates ) ); + + DeferredUpdates::doUpdates(); + + $x = null; + $y = null; + DeferredUpdates::addCallableUpdate( + function () use ( &$x ) { + $x = 'Sherity'; + }, + DeferredUpdates::PRESEND + ); + DeferredUpdates::addCallableUpdate( + function () use ( &$y ) { + $y = 'Marychu'; + }, + DeferredUpdates::POSTSEND + ); + + $this->assertNull( $x, "Update not run yet" ); + $this->assertNull( $y, "Update not run yet" ); + + DeferredUpdates::doUpdates( 'run', DeferredUpdates::PRESEND ); + $this->assertEquals( "Sherity", $x, "PRESEND update ran" ); + $this->assertNull( $y, "POSTSEND update not run yet" ); + + DeferredUpdates::doUpdates( 'run', DeferredUpdates::POSTSEND ); + $this->assertEquals( "Marychu", $y, "POSTSEND update ran" ); + } + + /** + * @covers DeferredUpdates::doUpdates + * @covers DeferredUpdates::execute + * @covers DeferredUpdates::addUpdate + */ + public function testDoUpdatesCLI() { + $this->setMwGlobals( 'wgCommandLineMode', true ); + $updates = [ + '1' => "deferred update 1;\n", + '2' => "deferred update 2;\n", + '2-1' => "deferred update 1 within deferred update 2;\n", + '2-2' => "deferred update 2 within deferred update 2;\n", + '3' => "deferred update 3;\n", + '3-1' => "deferred update 1 within deferred update 3;\n", + '3-2' => "deferred update 2 within deferred update 3;\n", + '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n", + '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n", + ]; + + // clear anything + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $lbFactory->commitMasterChanges( __METHOD__ ); + + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['1']; + } + ); + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['2']; + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['2-1']; + } + ); + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['2-2']; + } + ); + } + ); + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3']; + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3-1']; + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3-1-1']; + } + ); + } + ); + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3-2']; + DeferredUpdates::addCallableUpdate( + function () use ( $updates ) { + echo $updates['3-2-1']; + } + ); + } + ); + } + ); + + $this->expectOutputString( implode( '', $updates ) ); + + DeferredUpdates::doUpdates(); + } + + /** + * @covers DeferredUpdates::doUpdates + * @covers DeferredUpdates::execute + * @covers DeferredUpdates::addUpdate + */ + public function testPresendAddOnPostsendRun() { + $this->setMwGlobals( 'wgCommandLineMode', true ); + + $x = false; + $y = false; + // clear anything + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $lbFactory->commitMasterChanges( __METHOD__ ); + + DeferredUpdates::addCallableUpdate( + function () use ( &$x, &$y ) { + $x = true; + DeferredUpdates::addCallableUpdate( + function () use ( &$y ) { + $y = true; + }, + DeferredUpdates::PRESEND + ); + }, + DeferredUpdates::POSTSEND + ); + + DeferredUpdates::doUpdates(); + + $this->assertTrue( $x, "Outer POSTSEND update ran" ); + $this->assertTrue( $y, "Nested PRESEND update ran" ); + } + + /** + * @covers DeferredUpdates::runUpdate + */ + public function testRunUpdateTransactionScope() { + $this->setMwGlobals( 'wgCommandLineMode', false ); + + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' ); + + $ran = 0; + DeferredUpdates::addCallableUpdate( function () use ( &$ran, $lbFactory ) { + $ran++; + $this->assertTrue( $lbFactory->hasTransactionRound(), 'Has transaction' ); + } ); + DeferredUpdates::doUpdates(); + + $this->assertSame( 1, $ran, 'Update ran' ); + $this->assertFalse( $lbFactory->hasTransactionRound(), 'Final state' ); + } + + /** + * @covers DeferredUpdates::runUpdate + * @covers TransactionRoundDefiningUpdate::getOrigin + */ + public function testRunOuterScopeUpdate() { + $this->setMwGlobals( 'wgCommandLineMode', false ); + + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' ); + + $ran = 0; + DeferredUpdates::addUpdate( new TransactionRoundDefiningUpdate( + function () use ( &$ran, $lbFactory ) { + $ran++; + $this->assertFalse( $lbFactory->hasTransactionRound(), 'No transaction' ); + } ) + ); + DeferredUpdates::doUpdates(); + + $this->assertSame( 1, $ran, 'Update ran' ); + } +} diff --git a/www/wiki/tests/phpunit/includes/deferred/LinksUpdateTest.php b/www/wiki/tests/phpunit/includes/deferred/LinksUpdateTest.php new file mode 100644 index 00000000..ddc0798f --- /dev/null +++ b/www/wiki/tests/phpunit/includes/deferred/LinksUpdateTest.php @@ -0,0 +1,422 @@ +<?php + +/** + * @covers LinksUpdate + * @group LinksUpdate + * @group Database + * ^--- make sure temporary tables are used. + */ +class LinksUpdateTest extends MediaWikiLangTestCase { + protected static $testingPageId; + + function __construct( $name = null, array $data = [], $dataName = '' ) { + parent::__construct( $name, $data, $dataName ); + + $this->tablesUsed = array_merge( $this->tablesUsed, + [ + 'interwiki', + 'page_props', + 'pagelinks', + 'categorylinks', + 'langlinks', + 'externallinks', + 'imagelinks', + 'templatelinks', + 'iwlinks', + 'recentchanges', + ] + ); + } + + protected function setUp() { + parent::setUp(); + $dbw = wfGetDB( DB_MASTER ); + $dbw->replace( + 'interwiki', + [ 'iw_prefix' ], + [ + 'iw_prefix' => 'linksupdatetest', + 'iw_url' => 'http://testing.com/wiki/$1', + 'iw_api' => 'http://testing.com/w/api.php', + 'iw_local' => 0, + 'iw_trans' => 0, + 'iw_wikiid' => 'linksupdatetest', + ] + ); + $this->setMwGlobals( 'wgRCWatchCategoryMembership', true ); + } + + public function addDBDataOnce() { + $res = $this->insertPage( 'Testing' ); + self::$testingPageId = $res['id']; + $this->insertPage( 'Some_other_page' ); + $this->insertPage( 'Template:TestingTemplate' ); + } + + protected function makeTitleAndParserOutput( $name, $id ) { + $t = Title::newFromText( $name ); + $t->mArticleID = $id; # XXX: this is fugly + + $po = new ParserOutput(); + $po->setTitleText( $t->getPrefixedText() ); + + return [ $t, $po ]; + } + + /** + * @covers ParserOutput::addLink + */ + public function testUpdate_pagelinks() { + /** @var Title $t */ + /** @var ParserOutput $po */ + list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId ); + + $po->addLink( Title::newFromText( "Foo" ) ); + $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored + $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored + $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored + + $update = $this->assertLinksUpdate( + $t, + $po, + 'pagelinks', + 'pl_namespace, + pl_title', + 'pl_from = ' . self::$testingPageId, + [ [ NS_MAIN, 'Foo' ] ] + ); + $this->assertArrayEquals( [ + Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state.... + ], $update->getAddedLinks() ); + + $po = new ParserOutput(); + $po->setTitleText( $t->getPrefixedText() ); + + $po->addLink( Title::newFromText( "Bar" ) ); + $po->addLink( Title::newFromText( "Talk:Bar" ) ); + + $update = $this->assertLinksUpdate( + $t, + $po, + 'pagelinks', + 'pl_namespace, + pl_title', + 'pl_from = ' . self::$testingPageId, + [ + [ NS_MAIN, 'Bar' ], + [ NS_TALK, 'Bar' ], + ] + ); + $this->assertArrayEquals( [ + Title::makeTitle( NS_MAIN, 'Bar' ), + Title::makeTitle( NS_TALK, 'Bar' ), + ], $update->getAddedLinks() ); + $this->assertArrayEquals( [ + Title::makeTitle( NS_MAIN, 'Foo' ), + ], $update->getRemovedLinks() ); + } + + /** + * @covers ParserOutput::addExternalLink + */ + public function testUpdate_externallinks() { + /** @var ParserOutput $po */ + list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId ); + + $po->addExternalLink( "http://testing.com/wiki/Foo" ); + + $this->assertLinksUpdate( + $t, + $po, + 'externallinks', + 'el_to, el_index', + 'el_from = ' . self::$testingPageId, + [ + [ 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ], + ] + ); + } + + /** + * @covers ParserOutput::addCategory + */ + public function testUpdate_categorylinks() { + /** @var ParserOutput $po */ + $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' ); + + list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId ); + + $po->addCategory( "Foo", "FOO" ); + + $this->assertLinksUpdate( + $t, + $po, + 'categorylinks', + 'cl_to, cl_sortkey', + 'cl_from = ' . self::$testingPageId, + [ [ 'Foo', "FOO\nTESTING" ] ] + ); + } + + public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() { + $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' ); + + $title = Title::newFromText( 'Testing' ); + $wikiPage = new WikiPage( $title ); + $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' ); + $this->runAllRelatedJobs(); + + $this->assertRecentChangeByCategorization( + $title, + $wikiPage->getParserOutput( ParserOptions::newCanonical() ), + Title::newFromText( 'Category:Foo' ), + [ [ 'Foo', '[[:Testing]] added to category' ] ] + ); + + $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' ); + $this->runAllRelatedJobs(); + + $this->assertRecentChangeByCategorization( + $title, + $wikiPage->getParserOutput( ParserOptions::newCanonical() ), + Title::newFromText( 'Category:Foo' ), + [ + [ 'Foo', '[[:Testing]] added to category' ], + [ 'Foo', '[[:Testing]] removed from category' ], + ] + ); + + $this->assertRecentChangeByCategorization( + $title, + $wikiPage->getParserOutput( ParserOptions::newCanonical() ), + Title::newFromText( 'Category:Bar' ), + [ + [ 'Bar', '[[:Testing]] added to category' ], + ] + ); + } + + public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() { + $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' ); + + $templateTitle = Title::newFromText( 'Template:TestingTemplate' ); + $templatePage = new WikiPage( $templateTitle ); + + $wikiPage = new WikiPage( Title::newFromText( 'Testing' ) ); + $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' ); + $this->runAllRelatedJobs(); + + $otherWikiPage = new WikiPage( Title::newFromText( 'Some_other_page' ) ); + $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' ); + $this->runAllRelatedJobs(); + + $this->assertRecentChangeByCategorization( + $templateTitle, + $templatePage->getParserOutput( ParserOptions::newCanonical() ), + Title::newFromText( 'Baz' ), + [] + ); + + $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' ); + $this->runAllRelatedJobs(); + + $this->assertRecentChangeByCategorization( + $templateTitle, + $templatePage->getParserOutput( ParserOptions::newCanonical() ), + Title::newFromText( 'Baz' ), + [ [ + 'Baz', + '[[:Template:TestingTemplate]] added to category, ' . + '[[Special:WhatLinksHere/Template:TestingTemplate|this page is included within other pages]]' + ] ] + ); + } + + /** + * @covers ParserOutput::addInterwikiLink + */ + public function testUpdate_iwlinks() { + /** @var ParserOutput $po */ + list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId ); + + $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' ); + $po->addInterwikiLink( $target ); + + $this->assertLinksUpdate( + $t, + $po, + 'iwlinks', + 'iwl_prefix, iwl_title', + 'iwl_from = ' . self::$testingPageId, + [ [ 'linksupdatetest', 'Foo' ] ] + ); + } + + /** + * @covers ParserOutput::addTemplate + */ + public function testUpdate_templatelinks() { + /** @var ParserOutput $po */ + list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId ); + + $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 ); + + $this->assertLinksUpdate( + $t, + $po, + 'templatelinks', + 'tl_namespace, + tl_title', + 'tl_from = ' . self::$testingPageId, + [ [ NS_TEMPLATE, 'Foo' ] ] + ); + } + + /** + * @covers ParserOutput::addImage + */ + public function testUpdate_imagelinks() { + /** @var ParserOutput $po */ + list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId ); + + $po->addImage( "Foo.png" ); + + $this->assertLinksUpdate( + $t, + $po, + 'imagelinks', + 'il_to', + 'il_from = ' . self::$testingPageId, + [ [ 'Foo.png' ] ] + ); + } + + /** + * @covers ParserOutput::addLanguageLink + */ + public function testUpdate_langlinks() { + $this->setMwGlobals( [ + 'wgCapitalLinks' => true, + ] ); + + /** @var ParserOutput $po */ + list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId ); + + $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() ); + + $this->assertLinksUpdate( + $t, + $po, + 'langlinks', + 'll_lang, ll_title', + 'll_from = ' . self::$testingPageId, + [ [ 'En', 'Foo' ] ] + ); + } + + /** + * @covers ParserOutput::setProperty + */ + public function testUpdate_page_props() { + global $wgPagePropsHaveSortkey; + + /** @var ParserOutput $po */ + list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId ); + + $fields = [ 'pp_propname', 'pp_value' ]; + $expected = []; + + $po->setProperty( "bool", true ); + $expected[] = [ "bool", true ]; + + $po->setProperty( "float", 4.0 + 1.0 / 4.0 ); + $expected[] = [ "float", 4.0 + 1.0 / 4.0 ]; + + $po->setProperty( "int", -7 ); + $expected[] = [ "int", -7 ]; + + $po->setProperty( "string", "33 bar" ); + $expected[] = [ "string", "33 bar" ]; + + // compute expected sortkey values + if ( $wgPagePropsHaveSortkey ) { + $fields[] = 'pp_sortkey'; + + foreach ( $expected as &$row ) { + $value = $row[1]; + + if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) { + $row[] = floatval( $value ); + } else { + $row[] = null; + } + } + } + + $this->assertLinksUpdate( + $t, $po, 'page_props', $fields, 'pp_page = ' . self::$testingPageId, $expected ); + } + + public function testUpdate_page_props_without_sortkey() { + $this->setMwGlobals( 'wgPagePropsHaveSortkey', false ); + + $this->testUpdate_page_props(); + } + + // @todo test recursive, too! + + protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, + $table, $fields, $condition, array $expectedRows + ) { + $update = new LinksUpdate( $title, $parserOutput ); + + $update->doUpdate(); + + $this->assertSelect( $table, $fields, $condition, $expectedRows ); + return $update; + } + + protected function assertRecentChangeByCategorization( + Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows + ) { + global $wgCommentTableSchemaMigrationStage; + + if ( $wgCommentTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ) { + $this->assertSelect( + 'recentchanges', + 'rc_title, rc_comment', + [ + 'rc_type' => RC_CATEGORIZE, + 'rc_namespace' => NS_CATEGORY, + 'rc_title' => $categoryTitle->getDBkey() + ], + $expectedRows + ); + } + if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH ) { + $this->assertSelect( + [ 'recentchanges', 'comment' ], + 'rc_title, comment_text', + [ + 'rc_type' => RC_CATEGORIZE, + 'rc_namespace' => NS_CATEGORY, + 'rc_title' => $categoryTitle->getDBkey(), + 'comment_id = rc_comment_id', + ], + $expectedRows + ); + } + } + + private function runAllRelatedJobs() { + $queueGroup = JobQueueGroup::singleton(); + while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) { + $job->run(); + $queueGroup->ack( $job ); + } + while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) { + $job->run(); + $queueGroup->ack( $job ); + } + } +} diff --git a/www/wiki/tests/phpunit/includes/deferred/MWCallableUpdateTest.php b/www/wiki/tests/phpunit/includes/deferred/MWCallableUpdateTest.php new file mode 100644 index 00000000..3ab9b565 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/deferred/MWCallableUpdateTest.php @@ -0,0 +1,82 @@ +<?php + +/** + * @covers MWCallableUpdate + */ +class MWCallableUpdateTest extends PHPUnit\Framework\TestCase { + + use MediaWikiCoversValidator; + + public function testDoUpdate() { + $ran = 0; + $update = new MWCallableUpdate( function () use ( &$ran ) { + $ran++; + } ); + $this->assertSame( 0, $ran ); + $update->doUpdate(); + $this->assertSame( 1, $ran ); + } + + public function testCancel() { + // Prepare update and DB + $db = new DatabaseTestHelper( __METHOD__ ); + $db->begin( __METHOD__ ); + $ran = 0; + $update = new MWCallableUpdate( function () use ( &$ran ) { + $ran++; + }, __METHOD__, $db ); + + // Emulate rollback + $db->rollback( __METHOD__ ); + + $update->doUpdate(); + + // Ensure it was cancelled + $this->assertSame( 0, $ran ); + } + + public function testCancelSome() { + // Prepare update and DB + $db1 = new DatabaseTestHelper( __METHOD__ ); + $db1->begin( __METHOD__ ); + $db2 = new DatabaseTestHelper( __METHOD__ ); + $db2->begin( __METHOD__ ); + $ran = 0; + $update = new MWCallableUpdate( function () use ( &$ran ) { + $ran++; + }, __METHOD__, [ $db1, $db2 ] ); + + // Emulate rollback + $db1->rollback( __METHOD__ ); + + $update->doUpdate(); + + // Prevents: "Notice: DB transaction writes or callbacks still pending" + $db2->rollback( __METHOD__ ); + + // Ensure it was cancelled + $this->assertSame( 0, $ran ); + } + + public function testCancelAll() { + // Prepare update and DB + $db1 = new DatabaseTestHelper( __METHOD__ ); + $db1->begin( __METHOD__ ); + $db2 = new DatabaseTestHelper( __METHOD__ ); + $db2->begin( __METHOD__ ); + $ran = 0; + $update = new MWCallableUpdate( function () use ( &$ran ) { + $ran++; + }, __METHOD__, [ $db1, $db2 ] ); + + // Emulate rollbacks + $db1->rollback( __METHOD__ ); + $db2->rollback( __METHOD__ ); + + $update->doUpdate(); + + // Ensure it was cancelled + $this->assertSame( 0, $ran ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/deferred/SearchUpdateTest.php b/www/wiki/tests/phpunit/includes/deferred/SearchUpdateTest.php new file mode 100644 index 00000000..9e4dbea2 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/deferred/SearchUpdateTest.php @@ -0,0 +1,87 @@ +<?php + +class MockSearch extends SearchEngine { + public static $id; + public static $title; + public static $text; + + public function __construct( $db ) { + } + + public function update( $id, $title, $text ) { + self::$id = $id; + self::$title = $title; + self::$text = $text; + } +} + +/** + * @group Search + */ +class SearchUpdateTest extends MediaWikiTestCase { + + /** + * @var SearchUpdate + */ + private $su; + + protected function setUp() { + parent::setUp(); + $this->setMwGlobals( 'wgSearchType', 'MockSearch' ); + $this->su = new SearchUpdate( 0, "" ); + } + + public function updateText( $text ) { + return trim( $this->su->updateText( $text ) ); + } + + /** + * @covers SearchUpdate::updateText + */ + public function testUpdateText() { + $this->assertEquals( + 'test', + $this->updateText( '<div>TeSt</div>' ), + 'HTML stripped, text lowercased' + ); + + $this->assertEquals( + 'foo bar boz quux', + $this->updateText( <<<EOT +<table style="color:red; font-size:100px"> + <tr class="scary"><td><div>foo</div></td><tr>bar</td></tr> + <tr><td>boz</td><tr>quux</td></tr> +</table> +EOT + ), 'Stripping HTML tables' ); + + $this->assertEquals( + 'a b', + $this->updateText( 'a > b' ), + 'Handle unclosed tags' + ); + + $text = str_pad( "foo <barbarbar \n", 10000, 'x' ); + + $this->assertNotEquals( + '', + $this->updateText( $text ), + 'T20609' + ); + } + + /** + * @covers SearchUpdate::updateText + * Test T34712 + * Test if unicode quotes in article links make its search index empty + */ + public function testUnicodeLinkSearchIndexError() { + $text = "text „http://example.com“ text"; + $result = $this->updateText( $text ); + $processed = preg_replace( '/Q/u', 'Q', $result ); + $this->assertTrue( + $processed != '', + 'Link surrounded by unicode quotes should not fail UTF-8 validation' + ); + } +} diff --git a/www/wiki/tests/phpunit/includes/deferred/SiteStatsUpdateTest.php b/www/wiki/tests/phpunit/includes/deferred/SiteStatsUpdateTest.php new file mode 100644 index 00000000..83e9a47c --- /dev/null +++ b/www/wiki/tests/phpunit/includes/deferred/SiteStatsUpdateTest.php @@ -0,0 +1,77 @@ +<?php + +use Wikimedia\TestingAccessWrapper; + +/** + * @group Database + */ +class SiteStatsUpdateTest extends MediaWikiTestCase { + /** + * @covers SiteStatsUpdate::factory + * @covers SiteStatsUpdate::merge + */ + public function testFactoryAndMerge() { + $update1 = SiteStatsUpdate::factory( [ 'pages' => 1, 'users' => 2 ] ); + $update2 = SiteStatsUpdate::factory( [ 'users' => 1, 'images' => 1 ] ); + + $update1->merge( $update2 ); + $wrapped = TestingAccessWrapper::newFromObject( $update1 ); + + $this->assertEquals( 1, $wrapped->pages ); + $this->assertEquals( 3, $wrapped->users ); + $this->assertEquals( 1, $wrapped->images ); + $this->assertEquals( 0, $wrapped->edits ); + $this->assertEquals( 0, $wrapped->articles ); + } + + /** + * @covers SiteStatsUpdate::doUpdate() + * @covers SiteStatsInit::refresh() + */ + public function testDoUpdate() { + $this->setMwGlobals( 'wgSiteStatsAsyncFactor', false ); + $this->setMwGlobals( 'wgCommandLineMode', false ); // disable opportunistic updates + + $dbw = wfGetDB( DB_MASTER ); + $statsInit = new SiteStatsInit( $dbw ); + $statsInit->refresh(); + + $ei = SiteStats::edits(); // trigger load + $pi = SiteStats::pages(); + $ui = SiteStats::users(); + $fi = SiteStats::images(); + $ai = SiteStats::articles(); + + $dbw->begin( __METHOD__ ); // block opportunistic updates + + $update = SiteStatsUpdate::factory( [ 'pages' => 2, 'images' => 1, 'edits' => 2 ] ); + $this->assertEquals( 0, DeferredUpdates::pendingUpdatesCount() ); + $update->doUpdate(); + $this->assertEquals( 1, DeferredUpdates::pendingUpdatesCount() ); + + // Still the same + SiteStats::unload(); + $this->assertEquals( $pi, SiteStats::pages(), 'page count' ); + $this->assertEquals( $ei, SiteStats::edits(), 'edit count' ); + $this->assertEquals( $ui, SiteStats::users(), 'user count' ); + $this->assertEquals( $fi, SiteStats::images(), 'file count' ); + $this->assertEquals( $ai, SiteStats::articles(), 'article count' ); + $this->assertEquals( 1, DeferredUpdates::pendingUpdatesCount() ); + + $dbw->commit( __METHOD__ ); + + $this->assertEquals( 1, DeferredUpdates::pendingUpdatesCount() ); + DeferredUpdates::doUpdates(); + $this->assertEquals( 0, DeferredUpdates::pendingUpdatesCount() ); + + SiteStats::unload(); + $this->assertEquals( $pi + 2, SiteStats::pages(), 'page count' ); + $this->assertEquals( $ei + 2, SiteStats::edits(), 'edit count' ); + $this->assertEquals( $ui, SiteStats::users(), 'user count' ); + $this->assertEquals( $fi + 1, SiteStats::images(), 'file count' ); + $this->assertEquals( $ai, SiteStats::articles(), 'article count' ); + + $statsInit = new SiteStatsInit(); + $statsInit->refresh(); + } +} diff --git a/www/wiki/tests/phpunit/includes/deferred/TransactionRoundDefiningUpdateTest.php b/www/wiki/tests/phpunit/includes/deferred/TransactionRoundDefiningUpdateTest.php new file mode 100644 index 00000000..693897e6 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/deferred/TransactionRoundDefiningUpdateTest.php @@ -0,0 +1,19 @@ +<?php + +/** + * @covers TransactionRoundDefiningUpdate + */ +class TransactionRoundDefiningUpdateTest extends PHPUnit\Framework\TestCase { + + use MediaWikiCoversValidator; + + public function testDoUpdate() { + $ran = 0; + $update = new TransactionRoundDefiningUpdate( function () use ( &$ran ) { + $ran++; + } ); + $this->assertSame( 0, $ran ); + $update->doUpdate(); + $this->assertSame( 1, $ran ); + } +} |