1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
<?php
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Title\Title;
use MediaWiki\User\UserIdentity;
/**
* @covers \CategoryMembershipChange
*
* @group Database
*
* @author Addshore
*/
class CategoryMembershipChangeTest extends MediaWikiLangTestCase {
/**
* @var array
*/
private static $lastNotifyArgs;
/**
* @var int
*/
private static $notifyCallCounter = 0;
/**
* @var RecentChange
*/
private static $mockRecentChange;
/**
* @var RevisionRecord
*/
private static $pageRev = null;
/**
* @var UserIdentity
*/
private static $revUser = null;
/**
* @var string
*/
private static $pageName = 'CategoryMembershipChangeTestPage';
public static function newForCategorizationCallback( ...$args ) {
self::$lastNotifyArgs = $args;
self::$notifyCallCounter += 1;
return self::$mockRecentChange;
}
protected function setUp(): void {
parent::setUp();
self::$notifyCallCounter = 0;
self::$mockRecentChange = $this->createMock( RecentChange::class );
$this->setContentLang( 'qqx' );
}
public function addDBDataOnce() {
$info = $this->insertPage( self::$pageName );
$title = $info['title'];
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
self::$pageRev = $page->getRevisionRecord();
self::$revUser = self::$pageRev->getUser( RevisionRecord::RAW );
}
private function newChange( ?RevisionRecord $revision = null ) {
$title = Title::makeTitle( NS_MAIN, self::$pageName );
$blcFactory = $this->getServiceContainer()->getBacklinkCacheFactory();
$change = new CategoryMembershipChange(
$title, $blcFactory->getBacklinkCache( $title ), $revision
);
$change->overrideNewForCategorizationCallback(
'CategoryMembershipChangeTest::newForCategorizationCallback'
);
return $change;
}
public function testChangeAddedNoRev() {
$change = $this->newChange();
$change->triggerCategoryAddedNotification( Title::makeTitle( NS_CATEGORY, 'CategoryName' ) );
$this->assertSame( 1, self::$notifyCallCounter );
$this->assertSame( 14, strlen( self::$lastNotifyArgs[0] ) );
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
$this->assertEquals( '(autochange-username)', self::$lastNotifyArgs[2]->getName() );
$this->assertEquals( '(recentchanges-page-added-to-category: ' . self::$pageName . ')',
self::$lastNotifyArgs[3] );
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
$this->assertSame( 0, self::$lastNotifyArgs[5] );
$this->assertSame( 0, self::$lastNotifyArgs[6] );
$this->assertNull( self::$lastNotifyArgs[7] );
$this->assertSame( 1, self::$lastNotifyArgs[8] );
$this->assertSame( '', self::$lastNotifyArgs[9] );
$this->assertSame( 0, self::$lastNotifyArgs[10] );
}
public function testChangeRemovedNoRev() {
$change = $this->newChange();
$change->triggerCategoryRemovedNotification( Title::makeTitle( NS_CATEGORY, 'CategoryName' ) );
$this->assertSame( 1, self::$notifyCallCounter );
$this->assertSame( 14, strlen( self::$lastNotifyArgs[0] ) );
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
$this->assertEquals( '(autochange-username)', self::$lastNotifyArgs[2]->getName() );
$this->assertEquals( '(recentchanges-page-removed-from-category: ' . self::$pageName . ')',
self::$lastNotifyArgs[3] );
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
$this->assertSame( 0, self::$lastNotifyArgs[5] );
$this->assertSame( 0, self::$lastNotifyArgs[6] );
$this->assertNull( self::$lastNotifyArgs[7] );
$this->assertSame( 1, self::$lastNotifyArgs[8] );
$this->assertSame( '', self::$lastNotifyArgs[9] );
$this->assertSame( 0, self::$lastNotifyArgs[10] );
}
public function testChangeAddedWithRev() {
$revision = $this->getServiceContainer()
->getRevisionLookup()
->getRevisionByTitle( Title::makeTitle( NS_MAIN, self::$pageName ) );
$change = $this->newChange( $revision );
$change->triggerCategoryAddedNotification( Title::makeTitle( NS_CATEGORY, 'CategoryName' ) );
$this->assertSame( 1, self::$notifyCallCounter );
$this->assertSame( 14, strlen( self::$lastNotifyArgs[0] ) );
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
$this->assertEquals( self::$revUser->getName(), self::$lastNotifyArgs[2]->getName() );
$this->assertEquals( '(recentchanges-page-added-to-category: ' . self::$pageName . ')',
self::$lastNotifyArgs[3] );
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
$this->assertEquals( self::$pageRev->getParentId(), self::$lastNotifyArgs[5] );
$this->assertEquals( $revision->getId(), self::$lastNotifyArgs[6] );
$this->assertNull( self::$lastNotifyArgs[7] );
$this->assertSame( 0, self::$lastNotifyArgs[8] );
$this->assertEquals( '127.0.0.1', self::$lastNotifyArgs[9] );
$this->assertSame( 0, self::$lastNotifyArgs[10] );
}
public function testChangeRemovedWithRev() {
$revision = $this->getServiceContainer()
->getRevisionLookup()
->getRevisionByTitle( Title::makeTitle( NS_MAIN, self::$pageName ) );
$change = $this->newChange( $revision );
$change->triggerCategoryRemovedNotification( Title::makeTitle( NS_CATEGORY, 'CategoryName' ) );
$this->assertSame( 1, self::$notifyCallCounter );
$this->assertSame( 14, strlen( self::$lastNotifyArgs[0] ) );
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
$this->assertEquals( self::$revUser->getName(), self::$lastNotifyArgs[2]->getName() );
$this->assertEquals( '(recentchanges-page-removed-from-category: ' . self::$pageName . ')',
self::$lastNotifyArgs[3] );
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
$this->assertEquals( self::$pageRev->getParentId(), self::$lastNotifyArgs[5] );
$this->assertEquals( $revision->getId(), self::$lastNotifyArgs[6] );
$this->assertNull( self::$lastNotifyArgs[7] );
$this->assertSame( 0, self::$lastNotifyArgs[8] );
$this->assertEquals( '127.0.0.1', self::$lastNotifyArgs[9] );
$this->assertSame( 0, self::$lastNotifyArgs[10] );
}
}
|