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