2014-07-31 02:31:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Database
|
|
|
|
*/
|
|
|
|
class EchoUserLocatorTest extends MediaWikiTestCase {
|
|
|
|
|
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__ );
|
|
|
|
|
|
|
|
$event = $this->getMockBuilder( 'EchoEvent' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getTitle' )
|
|
|
|
->will( $this->returnValue( $title ) );
|
|
|
|
|
|
|
|
$it = EchoUserLocator::locateUsersWatchingTitle( $event, 10 );
|
|
|
|
$count = 0;
|
|
|
|
foreach ( $it as $user ) {
|
|
|
|
++$count;
|
|
|
|
}
|
|
|
|
$this->assertEquals( 50, $count );
|
|
|
|
// @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() {
|
|
|
|
return $this->getMockBuilder( 'EchoEvent' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
}
|