| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test; |
| 4 |
|
| 5 |
use MediaWiki\Content\ContentHandler; |
| 6 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 7 |
use MediaWiki\Extension\Notifications\UserLocator; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
use MediaWiki\User\User; |
| 10 |
use MediaWikiIntegrationTestCase; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class UserLocatorTest extends MediaWikiIntegrationTestCase { |
| 17 |
|
| 18 |
public function testLocateUsersWatchingTitle() { |
| 19 |
$title = Title::makeTitleSafe( NS_USER_TALK, 'Something_something_something' ); |
| 20 |
$key = $title->getDBkey(); |
| 21 |
|
| 22 |
for ( $i = 1000; $i < 1050; ++$i ) { |
| 23 |
$rows[] = [ |
| 24 |
'wl_user' => $i, |
| 25 |
'wl_namespace' => NS_USER_TALK, |
| 26 |
'wl_title' => $key |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
$this->getDb()->newInsertQueryBuilder() |
| 31 |
->insertInto( 'watchlist' ) |
| 32 |
->rows( $rows ) |
| 33 |
->caller( __METHOD__ ) |
| 34 |
->execute(); |
| 35 |
|
| 36 |
$event = $this->createMock( Event::class ); |
| 37 |
$event->method( 'getTitle' ) |
| 38 |
->willReturn( $title ); |
| 39 |
|
| 40 |
$it = UserLocator::locateUsersWatchingTitle( $event, 10 ); |
| 41 |
$this->assertCount( 50, $it ); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
public static function locateTalkPageOwnerProvider() { |
| 46 |
return [ |
| 47 |
[ |
| 48 |
'Allows null event title', |
| 49 |
|
| 50 |
'empty', |
| 51 |
|
| 52 |
null |
| 53 |
], |
| 54 |
|
| 55 |
[ |
| 56 |
'No users selected for non-user talk namespace', |
| 57 |
|
| 58 |
'empty', |
| 59 |
|
| 60 |
Title::newMainPage(), |
| 61 |
], |
| 62 |
|
| 63 |
[ |
| 64 |
'Selects user from NS_USER_TALK', |
| 65 |
|
| 66 |
'user', |
| 67 |
], |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function testLocateTalkPageOwner( $message, $expectMode, ?Title $title = null ) { |
| 75 |
if ( $expectMode === 'user' ) { |
| 76 |
$user = $this->getTestUser()->getUser(); |
| 77 |
$user->addToDatabase(); |
| 78 |
$expect = [ $user->getId() ]; |
| 79 |
$title = $user->getTalkPage(); |
| 80 |
} else { |
| 81 |
$expect = []; |
| 82 |
} |
| 83 |
$event = $this->createMock( Event::class ); |
| 84 |
$event->method( 'getTitle' ) |
| 85 |
->willReturn( $title ); |
| 86 |
|
| 87 |
$users = UserLocator::locateTalkPageOwner( $event ); |
| 88 |
$this->assertEquals( $expect, array_keys( $users ), $message ); |
| 89 |
} |
| 90 |
|
| 91 |
public static function locateArticleCreatorProvider() { |
| 92 |
return [ |
| 93 |
[ 'Something' ], |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function testLocateArticleCreator( $message ) { |
| 101 |
$user = $this->getTestUser()->getUser(); |
| 102 |
$user->addToDatabase(); |
| 103 |
$title = $user->getTalkPage(); |
| 104 |
$this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title )->doUserEditContent( |
| 105 |
ContentHandler::makeContent( 'content', $title ), |
| 106 |
$user, |
| 107 |
'summary' |
| 108 |
); |
| 109 |
|
| 110 |
$event = $this->createMock( Event::class ); |
| 111 |
$event->method( 'getTitle' ) |
| 112 |
->willReturn( $title ); |
| 113 |
$event->method( 'getAgent' ) |
| 114 |
->willReturn( User::newFromId( 123 ) ); |
| 115 |
|
| 116 |
$users = UserLocator::locateArticleCreator( $event ); |
| 117 |
$this->assertEquals( [ $user->getId() ], array_keys( $users ), $message ); |
| 118 |
} |
| 119 |
|
| 120 |
public function testDontSendPageLinkedNotificationsForPagesCreatedByBotUsers() { |
| 121 |
$botUser = $this->getTestUser( [ 'bot' ] )->getUser(); |
| 122 |
$botUser->addToDatabase(); |
| 123 |
$this->editPage( 'TestBotCreatedPage', 'Test', '', NS_MAIN, $botUser ); |
| 124 |
$this->editPage( 'SomeOtherPage', '[[TestBotCreatedPage]]' ); |
| 125 |
$event = $this->createMock( Event::class ); |
| 126 |
$event->method( 'getTitle' ) |
| 127 |
->willReturn( Title::newFromText( 'TestBotCreatedPage' ) ); |
| 128 |
$event->method( 'getAgent' ) |
| 129 |
->willReturn( User::newFromId( 123 ) ); |
| 130 |
$event->method( 'getType' ) |
| 131 |
->willReturn( 'page-linked' ); |
| 132 |
$this->assertEquals( [], UserLocator::locateArticleCreator( $event ) ); |
| 133 |
|
| 134 |
$normalUser = $this->getTestUser()->getUser(); |
| 135 |
$normalUser->addToDatabase(); |
| 136 |
$this->editPage( 'NormalCreatedPage', 'Test', '', NS_MAIN, $normalUser ); |
| 137 |
$this->editPage( 'AnotherPage', '[[NormalCreatedPage]]' ); |
| 138 |
$event = $this->createMock( Event::class ); |
| 139 |
$event->method( 'getTitle' ) |
| 140 |
->willReturn( Title::newFromText( 'NormalCreatedPage' ) ); |
| 141 |
$event->method( 'getAgent' ) |
| 142 |
->willReturn( User::newFromId( 456 ) ); |
| 143 |
$event->method( 'getType' ) |
| 144 |
->willReturn( 'page-linked' ); |
| 145 |
$this->assertEquals( |
| 146 |
$normalUser->getUser()->getId(), |
| 147 |
array_key_first( UserLocator::locateArticleCreator( $event ) ) |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
public static function locateEventAgentProvider() { |
| 152 |
return [ |
| 153 |
[ |
| 154 |
'Null event agent returns no users', |
| 155 |
|
| 156 |
[], |
| 157 |
|
| 158 |
null, |
| 159 |
], |
| 160 |
|
| 161 |
[ |
| 162 |
'Anonymous event agent returns no users', |
| 163 |
|
| 164 |
[], |
| 165 |
|
| 166 |
User::newFromName( '4.5.6.7', false ), |
| 167 |
], |
| 168 |
|
| 169 |
[ |
| 170 |
'Registed event agent returned as user', |
| 171 |
|
| 172 |
[ 42 ], |
| 173 |
|
| 174 |
User::newFromId( 42 ), |
| 175 |
], |
| 176 |
]; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function testLocateEventAgent( $message, $expect, ?User $agent = null ) { |
| 183 |
$event = $this->createMock( Event::class ); |
| 184 |
$event->method( 'getAgent' ) |
| 185 |
->willReturn( $agent ); |
| 186 |
|
| 187 |
$users = UserLocator::locateEventAgent( $event ); |
| 188 |
$this->assertEquals( $expect, array_keys( $users ), $message ); |
| 189 |
} |
| 190 |
|
| 191 |
public static function locateFromEventExtraProvider() { |
| 192 |
return [ |
| 193 |
[ |
| 194 |
'Event without extra data returns empty result', |
| 195 |
|
| 196 |
[], |
| 197 |
|
| 198 |
[], |
| 199 |
|
| 200 |
[ 'foo' ], |
| 201 |
], |
| 202 |
|
| 203 |
[ |
| 204 |
'Event with specified extra data returns expected result', |
| 205 |
|
| 206 |
[ 123 ], |
| 207 |
|
| 208 |
[ 'foo' => 123 ], |
| 209 |
|
| 210 |
[ 'foo' ], |
| 211 |
], |
| 212 |
|
| 213 |
[ |
| 214 |
'Accepts User objects instead of user ids', |
| 215 |
|
| 216 |
[ 123 ], |
| 217 |
|
| 218 |
[ 'foo' => User::newFromId( 123 ) ], |
| 219 |
|
| 220 |
[ 'foo' ], |
| 221 |
], |
| 222 |
|
| 223 |
[ |
| 224 |
'Allows inner key to be array of ids', |
| 225 |
|
| 226 |
[ 123, 321 ], |
| 227 |
|
| 228 |
[ 'foo' => [ 123, 321 ] ], |
| 229 |
|
| 230 |
[ 'foo' ], |
| 231 |
], |
| 232 |
|
| 233 |
[ |
| 234 |
'Empty inner array causes no error', |
| 235 |
|
| 236 |
[], |
| 237 |
|
| 238 |
[ 'foo' => [] ], |
| 239 |
|
| 240 |
[ 'foo' ], |
| 241 |
], |
| 242 |
|
| 243 |
[ |
| 244 |
'Accepts User object at inner level', |
| 245 |
|
| 246 |
[ 123 ], |
| 247 |
|
| 248 |
[ 'foo' => [ User::newFromId( 123 ) ] ], |
| 249 |
|
| 250 |
[ 'foo' ], |
| 251 |
], |
| 252 |
|
| 253 |
]; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function testLocateFromEventExtra( $message, $expect, array $extra, array $keys ) { |
| 260 |
$event = $this->createMock( Event::class ); |
| 261 |
$event->method( 'getExtra' ) |
| 262 |
->willReturn( $extra ); |
| 263 |
$event->method( 'getExtraParam' ) |
| 264 |
->willReturnMap( self::arrayToValueMap( $extra ) ); |
| 265 |
|
| 266 |
$users = UserLocator::locateFromEventExtra( $event, $keys ); |
| 267 |
$this->assertEquals( $expect, array_keys( $users ), $message ); |
| 268 |
} |
| 269 |
|
| 270 |
protected static function arrayToValueMap( array $array ) { |
| 271 |
$result = []; |
| 272 |
foreach ( $array as $key => $value ) { |
| 273 |
|
| 274 |
$result[] = [ $key, null, $value ]; |
| 275 |
} |
| 276 |
|
| 277 |
return $result; |
| 278 |
} |
| 279 |
|
| 280 |
} |