2014-07-31 02:31:47 +00:00
|
|
|
<?php
|
|
|
|
|
2023-04-28 09:58:56 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Test;
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use ContentHandler;
|
|
|
|
use EchoUserLocator;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2023-04-28 09:58:56 +00:00
|
|
|
use MediaWiki\Title\Title;
|
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
use User;
|
2022-11-02 21:34:17 +00:00
|
|
|
|
2014-07-31 02:31:47 +00:00
|
|
|
/**
|
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
|
|
|
*/
|
2023-04-28 09:58:56 +00:00
|
|
|
class UserLocatorTest extends MediaWikiIntegrationTestCase {
|
2014-07-31 02:31:47 +00:00
|
|
|
|
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
|
|
|
}
|
2021-05-03 07:28:02 +00:00
|
|
|
wfGetDB( DB_PRIMARY )->insert( 'watchlist', $rows, __METHOD__ );
|
2014-07-30 03:18:48 +00:00
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getTitle' )
|
|
|
|
->willReturn( $title );
|
2014-07-30 03:18:48 +00:00
|
|
|
|
|
|
|
$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 () {
|
2021-01-29 12:54:21 +00:00
|
|
|
$user = $this->getTestUser()->getUser();
|
2014-07-31 02:31:47 +00:00
|
|
|
$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 ) {
|
2022-11-08 16:41:24 +00:00
|
|
|
[ $expect, $title ] = $expect();
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getTitle' )
|
|
|
|
->willReturn( $title );
|
2014-07-31 02:31:47 +00:00
|
|
|
|
|
|
|
$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 () {
|
2021-01-29 12:54:21 +00:00
|
|
|
$user = $this->getTestUser()->getUser();
|
2014-07-31 02:31:47 +00:00
|
|
|
$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 ) {
|
2022-11-08 16:41:24 +00:00
|
|
|
[ $expect, $title, $user ] = $initialize();
|
2022-06-03 21:00:26 +00:00
|
|
|
$this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title )->doUserEditContent(
|
2014-07-31 02:31:47 +00:00
|
|
|
/* $content = */ ContentHandler::makeContent( 'content', $title ),
|
2021-06-24 04:54:45 +00:00
|
|
|
/* $user = */ $user,
|
|
|
|
/* $summary = */ 'summary'
|
2014-07-31 02:31:47 +00:00
|
|
|
);
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getTitle' )
|
|
|
|
->willReturn( $title );
|
|
|
|
$event->method( 'getAgent' )
|
|
|
|
->willReturn( User::newFromId( 123 ) );
|
2014-07-31 02:31:47 +00:00
|
|
|
|
|
|
|
$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
|
2022-11-12 06:37:37 +00:00
|
|
|
User::newFromName( '4.5.6.7', 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 ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getAgent' )
|
|
|
|
->willReturn( $agent );
|
2014-07-31 02:31:47 +00:00
|
|
|
|
|
|
|
$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 ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getExtra' )
|
|
|
|
->willReturn( $extra );
|
|
|
|
$event->method( 'getExtraParam' )
|
2022-11-12 06:37:37 +00:00
|
|
|
->willReturnMap( self::arrayToValueMap( $extra ) );
|
2014-07-31 02:31:47 +00:00
|
|
|
|
|
|
|
$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 ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
// Event::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|