2015-09-17 09:51:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-03-06 06:16:49 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
|
|
|
|
2015-09-17 09:51:25 +00:00
|
|
|
/**
|
|
|
|
|
* @covers CategoryMembershipChange
|
|
|
|
|
*
|
|
|
|
|
* @group Database
|
|
|
|
|
*
|
2016-01-27 09:59:31 +00:00
|
|
|
* @author Addshore
|
2015-09-17 09:51:25 +00:00
|
|
|
*/
|
|
|
|
|
class CategoryMembershipChangeTest extends MediaWikiLangTestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array|Title[]|User[]
|
|
|
|
|
*/
|
|
|
|
|
private static $lastNotifyArgs;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private static $notifyCallCounter = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var RecentChange
|
|
|
|
|
*/
|
|
|
|
|
private static $mockRecentChange;
|
|
|
|
|
|
2016-03-07 17:26:25 +00:00
|
|
|
/**
|
2020-04-24 03:51:03 +00:00
|
|
|
* @var RevisionRecord
|
2016-03-07 17:26:25 +00:00
|
|
|
*/
|
|
|
|
|
private static $pageRev = null;
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
/**
|
|
|
|
|
* @var User
|
|
|
|
|
*/
|
|
|
|
|
private static $revUser = null;
|
|
|
|
|
|
2016-03-07 17:26:25 +00:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private static $pageName = 'CategoryMembershipChangeTestPage';
|
|
|
|
|
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
public static function newForCategorizationCallback( ...$args ) {
|
|
|
|
|
self::$lastNotifyArgs = $args;
|
2015-09-17 09:51:25 +00:00
|
|
|
self::$notifyCallCounter += 1;
|
|
|
|
|
return self::$mockRecentChange;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 10:51:39 +00:00
|
|
|
protected function setUp() : void {
|
2015-09-17 09:51:25 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
self::$notifyCallCounter = 0;
|
2019-10-22 21:00:57 +00:00
|
|
|
self::$mockRecentChange = $this->createMock( RecentChange::class );
|
2016-03-07 17:26:25 +00:00
|
|
|
|
|
|
|
|
$this->setContentLang( 'qqx' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function addDBDataOnce() {
|
|
|
|
|
$info = $this->insertPage( self::$pageName );
|
|
|
|
|
$title = $info['title'];
|
|
|
|
|
|
|
|
|
|
$page = WikiPage::factory( $title );
|
2020-04-24 03:51:03 +00:00
|
|
|
self::$pageRev = $page->getRevisionRecord();
|
2020-07-03 00:20:38 +00:00
|
|
|
self::$revUser = User::newFromIdentity(
|
|
|
|
|
self::$pageRev->getUser( RevisionRecord::RAW )
|
|
|
|
|
);
|
2015-09-17 09:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-06 06:16:49 +00:00
|
|
|
private function newChange( RevisionRecord $revision = null ) {
|
2016-03-07 17:26:25 +00:00
|
|
|
$change = new CategoryMembershipChange( Title::newFromText( self::$pageName ), $revision );
|
2015-09-17 09:51:25 +00:00
|
|
|
$change->overrideNewForCategorizationCallback(
|
|
|
|
|
'CategoryMembershipChangeTest::newForCategorizationCallback'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $change;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChangeAddedNoRev() {
|
|
|
|
|
$change = $this->newChange();
|
|
|
|
|
$change->triggerCategoryAddedNotification( Title::newFromText( 'CategoryName', NS_CATEGORY ) );
|
|
|
|
|
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, self::$notifyCallCounter );
|
2015-09-17 09:51:25 +00:00
|
|
|
|
|
|
|
|
$this->assertTrue( strlen( self::$lastNotifyArgs[0] ) === 14 );
|
|
|
|
|
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
|
2016-03-07 17:26:25 +00:00
|
|
|
$this->assertEquals( '(autochange-username)', self::$lastNotifyArgs[2]->getName() );
|
2016-04-02 16:06:40 +00:00
|
|
|
$this->assertEquals( '(recentchanges-page-added-to-category: ' . self::$pageName . ')',
|
2016-03-07 17:26:25 +00:00
|
|
|
self::$lastNotifyArgs[3] );
|
|
|
|
|
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[5] );
|
|
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[6] );
|
2019-09-17 14:03:28 +00:00
|
|
|
$this->assertNull( self::$lastNotifyArgs[7] );
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, self::$lastNotifyArgs[8] );
|
2019-09-17 14:03:28 +00:00
|
|
|
$this->assertSame( '', self::$lastNotifyArgs[9] );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[10] );
|
2015-09-17 09:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChangeRemovedNoRev() {
|
|
|
|
|
$change = $this->newChange();
|
|
|
|
|
$change->triggerCategoryRemovedNotification( Title::newFromText( 'CategoryName', NS_CATEGORY ) );
|
|
|
|
|
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, self::$notifyCallCounter );
|
2015-09-17 09:51:25 +00:00
|
|
|
|
|
|
|
|
$this->assertTrue( strlen( self::$lastNotifyArgs[0] ) === 14 );
|
|
|
|
|
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
|
2016-03-07 17:26:25 +00:00
|
|
|
$this->assertEquals( '(autochange-username)', self::$lastNotifyArgs[2]->getName() );
|
2016-04-02 16:06:40 +00:00
|
|
|
$this->assertEquals( '(recentchanges-page-removed-from-category: ' . self::$pageName . ')',
|
2016-03-07 17:26:25 +00:00
|
|
|
self::$lastNotifyArgs[3] );
|
|
|
|
|
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[5] );
|
|
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[6] );
|
2019-09-17 14:03:28 +00:00
|
|
|
$this->assertNull( self::$lastNotifyArgs[7] );
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, self::$lastNotifyArgs[8] );
|
2019-09-17 14:03:28 +00:00
|
|
|
$this->assertSame( '', self::$lastNotifyArgs[9] );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[10] );
|
2015-09-17 09:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChangeAddedWithRev() {
|
2020-03-06 06:16:49 +00:00
|
|
|
$revision = MediaWikiServices::getInstance()
|
|
|
|
|
->getRevisionLookup()
|
|
|
|
|
->getRevisionByTitle( Title::newFromText( self::$pageName ) );
|
2015-09-17 09:51:25 +00:00
|
|
|
$change = $this->newChange( $revision );
|
|
|
|
|
$change->triggerCategoryAddedNotification( Title::newFromText( 'CategoryName', NS_CATEGORY ) );
|
|
|
|
|
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, self::$notifyCallCounter );
|
2015-09-17 09:51:25 +00:00
|
|
|
|
|
|
|
|
$this->assertTrue( strlen( self::$lastNotifyArgs[0] ) === 14 );
|
|
|
|
|
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
|
2016-05-18 09:19:20 +00:00
|
|
|
$this->assertEquals( self::$revUser->getName(), self::$lastNotifyArgs[2]->getName() );
|
2016-04-02 16:06:40 +00:00
|
|
|
$this->assertEquals( '(recentchanges-page-added-to-category: ' . self::$pageName . ')',
|
2016-03-07 17:26:25 +00:00
|
|
|
self::$lastNotifyArgs[3] );
|
|
|
|
|
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
|
|
|
|
|
$this->assertEquals( self::$pageRev->getParentId(), self::$lastNotifyArgs[5] );
|
2015-09-17 09:51:25 +00:00
|
|
|
$this->assertEquals( $revision->getId(), self::$lastNotifyArgs[6] );
|
2019-09-17 14:03:28 +00:00
|
|
|
$this->assertNull( self::$lastNotifyArgs[7] );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[8] );
|
2015-09-17 09:51:25 +00:00
|
|
|
$this->assertEquals( '127.0.0.1', self::$lastNotifyArgs[9] );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[10] );
|
2015-09-17 09:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChangeRemovedWithRev() {
|
2020-03-06 06:16:49 +00:00
|
|
|
$revision = MediaWikiServices::getInstance()
|
|
|
|
|
->getRevisionLookup()
|
|
|
|
|
->getRevisionByTitle( Title::newFromText( self::$pageName ) );
|
2015-09-17 09:51:25 +00:00
|
|
|
$change = $this->newChange( $revision );
|
|
|
|
|
$change->triggerCategoryRemovedNotification( Title::newFromText( 'CategoryName', NS_CATEGORY ) );
|
|
|
|
|
|
2020-05-30 10:36:42 +00:00
|
|
|
$this->assertSame( 1, self::$notifyCallCounter );
|
2015-09-17 09:51:25 +00:00
|
|
|
|
|
|
|
|
$this->assertTrue( strlen( self::$lastNotifyArgs[0] ) === 14 );
|
|
|
|
|
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
|
2016-05-18 09:19:20 +00:00
|
|
|
$this->assertEquals( self::$revUser->getName(), self::$lastNotifyArgs[2]->getName() );
|
2016-04-02 16:06:40 +00:00
|
|
|
$this->assertEquals( '(recentchanges-page-removed-from-category: ' . self::$pageName . ')',
|
2016-03-07 17:26:25 +00:00
|
|
|
self::$lastNotifyArgs[3] );
|
|
|
|
|
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
|
|
|
|
|
$this->assertEquals( self::$pageRev->getParentId(), self::$lastNotifyArgs[5] );
|
2015-09-17 09:51:25 +00:00
|
|
|
$this->assertEquals( $revision->getId(), self::$lastNotifyArgs[6] );
|
2019-09-17 14:03:28 +00:00
|
|
|
$this->assertNull( self::$lastNotifyArgs[7] );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[8] );
|
2015-09-17 09:51:25 +00:00
|
|
|
$this->assertEquals( '127.0.0.1', self::$lastNotifyArgs[9] );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, self::$lastNotifyArgs[10] );
|
2015-09-17 09:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|