2014-07-31 02:31:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2018-01-24 00:31:53 +00:00
|
|
|
* @group Database
|
2019-10-23 10:23:09 +00:00
|
|
|
* @covers \EchoUserLocator
|
2014-07-31 02:31:47 +00:00
|
|
|
*/
|
|
|
|
class EchoUserLocatorTest extends MediaWikiTestCase {
|
|
|
|
|
2021-01-23 11:54:27 +00:00
|
|
|
/** @inheritDoc */
|
2016-12-05 18:51:07 +00:00
|
|
|
protected $tablesUsed = [ 'user', 'watchlist' ];
|
2014-07-30 03:18:48 +00:00
|
|
|
|
|
|
|
public function testLocateUsersWatchingTitle() {
|
|
|
|
$title = Title::makeTitleSafe( NS_USER_TALK, 'Something_something_something' );
|
|
|
|
$key = $title->getDBkey();
|
|
|
|
|
|
|
|
for ( $i = 1000; $i < 1050; ++$i ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$rows[] = [
|
2014-07-30 03:18:48 +00:00
|
|
|
'wl_user' => $i,
|
|
|
|
'wl_namespace' => NS_USER_TALK,
|
|
|
|
'wl_title' => $key
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-30 03:18:48 +00:00
|
|
|
}
|
|
|
|
wfGetDB( DB_MASTER )->insert( 'watchlist', $rows, __METHOD__ );
|
|
|
|
|
2019-02-19 10:35:54 +00:00
|
|
|
$event = $this->getMockBuilder( EchoEvent::class )
|
2014-07-30 03:18:48 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getTitle' )
|
|
|
|
->will( $this->returnValue( $title ) );
|
|
|
|
|
|
|
|
$it = EchoUserLocator::locateUsersWatchingTitle( $event, 10 );
|
2019-10-23 10:28:30 +00:00
|
|
|
$this->assertCount( 50, $it );
|
2014-07-30 03:18:48 +00:00
|
|
|
// @todo assert more than one query was issued
|
|
|
|
}
|
2014-07-31 02:31:47 +00:00
|
|
|
|
|
|
|
public function locateTalkPageOwnerProvider() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Allows null event title',
|
|
|
|
// expected user id's
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event title
|
|
|
|
null
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'No users selected for non-user talk namespace',
|
|
|
|
// expected user id's
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event title
|
|
|
|
Title::newMainPage(),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Selects user from NS_USER_TALK',
|
|
|
|
// callback returning expected user ids and event title.
|
|
|
|
// required because database insert must be inside test.
|
2015-10-01 13:48:52 +00:00
|
|
|
function () {
|
2014-07-31 02:31:47 +00:00
|
|
|
$user = User::newFromName( 'UTUser' );
|
|
|
|
$user->addToDatabase();
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[ $user->getId() ],
|
2014-07-31 02:31:47 +00:00
|
|
|
$user->getTalkPage(),
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider locateTalkPageOwnerProvider
|
|
|
|
*/
|
|
|
|
public function testLocateTalkPageOwner( $message, $expect, Title $title = null ) {
|
|
|
|
if ( $expect instanceof Closure ) {
|
|
|
|
list( $expect, $title ) = $expect();
|
|
|
|
}
|
|
|
|
$event = $this->mockEchoEvent();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getTitle' )
|
|
|
|
->will( $this->returnValue( $title ) );
|
|
|
|
|
|
|
|
$users = EchoUserLocator::locateTalkPageOwner( $event );
|
|
|
|
$this->assertEquals( $expect, array_keys( $users ), $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function locateArticleCreatorProvider() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Something',
|
2015-10-01 13:48:52 +00:00
|
|
|
function () {
|
2014-07-31 02:31:47 +00:00
|
|
|
$user = User::newFromName( 'UTUser' );
|
|
|
|
$user->addToDatabase();
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[ $user->getId() ],
|
2014-07-31 02:31:47 +00:00
|
|
|
$user->getTalkPage(),
|
|
|
|
$user
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider locateArticleCreatorProvider
|
|
|
|
*/
|
|
|
|
public function testLocateArticleCreator( $message, $initialize ) {
|
|
|
|
list( $expect, $title, $user ) = $initialize();
|
|
|
|
WikiPage::factory( $title )->doEditContent(
|
|
|
|
/* $content = */ ContentHandler::makeContent( 'content', $title ),
|
|
|
|
/* $summary = */ 'summary',
|
|
|
|
/* $flags = */ 0,
|
|
|
|
/* $baseRevId = */ false,
|
|
|
|
/* $user = */ $user
|
|
|
|
);
|
|
|
|
|
|
|
|
$event = $this->mockEchoEvent();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getTitle' )
|
|
|
|
->will( $this->returnValue( $title ) );
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getAgent' )
|
|
|
|
->will( $this->returnValue( User::newFromId( 123 ) ) );
|
|
|
|
|
|
|
|
$users = EchoUserLocator::locateArticleCreator( $event );
|
|
|
|
$this->assertEquals( $expect, array_keys( $users ), $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function locateEventAgentProvider() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Null event agent returns no users',
|
|
|
|
// expected result user id's
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event agent
|
|
|
|
null,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Anonymous event agent returns no users',
|
|
|
|
// expected result user id's
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event agent
|
|
|
|
User::newFromName( '4.5.6.7', /* $validate = */ false ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Registed event agent returned as user',
|
|
|
|
// expected result user id's
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 42 ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event agent
|
|
|
|
User::newFromId( 42 ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider locateEventAgentProvider
|
|
|
|
*/
|
|
|
|
public function testLocateEventAgent( $message, $expect, User $agent = null ) {
|
|
|
|
$event = $this->mockEchoEvent();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getAgent' )
|
|
|
|
->will( $this->returnValue( $agent ) );
|
|
|
|
|
|
|
|
$users = EchoUserLocator::locateEventAgent( $event );
|
|
|
|
$this->assertEquals( $expect, array_keys( $users ), $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function locateFromEventExtraProvider() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Event without extra data returns empty result',
|
|
|
|
// expected user list
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event extra data
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-31 02:31:47 +00:00
|
|
|
// extra keys to get ids from
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' ],
|
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Event with specified extra data returns expected result',
|
|
|
|
// expected user list
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 123 ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event extra data
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' => 123 ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// extra keys to get ids from
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' ],
|
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Accepts User objects instead of user ids',
|
|
|
|
// expected user list
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 123 ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event extra data
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' => User::newFromId( 123 ) ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// extra keys to get ids from
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' ],
|
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Allows inner key to be array of ids',
|
|
|
|
// expected user list
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 123, 321 ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event extra data
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' => [ 123, 321 ] ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// extra keys to get ids from
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' ],
|
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Empty inner array causes no error',
|
|
|
|
// expected user list
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event extra data
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' => [] ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// extra keys to get ids from
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' ],
|
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-31 02:31:47 +00:00
|
|
|
'Accepts User object at inner level',
|
|
|
|
// expected user list
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 123 ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// event extra data
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' => [ User::newFromId( 123 ) ] ],
|
2014-07-31 02:31:47 +00:00
|
|
|
// extra keys to get ids from
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'foo' ],
|
|
|
|
],
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider locateFromEventExtraProvider
|
|
|
|
*/
|
|
|
|
public function testLocateFromEventExtra( $message, $expect, array $extra, array $keys ) {
|
|
|
|
$event = $this->mockEchoEvent();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getExtra' )
|
|
|
|
->will( $this->returnValue( $extra ) );
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getExtraParam' )
|
|
|
|
->will( $this->returnValueMap( self::arrayToValueMap( $extra ) ) );
|
|
|
|
|
|
|
|
$users = EchoUserLocator::locateFromEventExtra( $event, $keys );
|
|
|
|
$this->assertEquals( $expect, array_keys( $users ), $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function arrayToValueMap( array $array ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$result = [];
|
2014-07-31 02:31:47 +00:00
|
|
|
foreach ( $array as $key => $value ) {
|
|
|
|
// EchoEvent::getExtraParam second argument defaults to null
|
2016-12-05 18:51:07 +00:00
|
|
|
$result[] = [ $key, null, $value ];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-31 02:31:47 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockEchoEvent() {
|
2019-02-19 10:35:54 +00:00
|
|
|
return $this->getMockBuilder( EchoEvent::class )
|
2014-07-31 02:31:47 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
}
|