2018-01-19 21:26:49 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-22 15:31:00 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Test;
|
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
use MediaWiki\Extension\Notifications\SummaryParser;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2024-03-15 08:27:36 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2024-02-22 15:31:00 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2022-11-13 07:48:43 +00:00
|
|
|
|
2018-01-19 21:26:49 +00:00
|
|
|
/**
|
|
|
|
* @group Echo
|
|
|
|
*/
|
2022-11-13 07:48:43 +00:00
|
|
|
class SummaryParserTest extends MediaWikiIntegrationTestCase {
|
2021-01-23 11:54:27 +00:00
|
|
|
/** @var string[] */
|
2018-01-19 21:26:49 +00:00
|
|
|
private $existingUsers = [
|
|
|
|
'Werdna',
|
|
|
|
'Jorm',
|
|
|
|
'Jim Carter',
|
|
|
|
];
|
2019-07-14 10:57:45 +00:00
|
|
|
|
2018-01-19 21:26:49 +00:00
|
|
|
/**
|
2022-11-13 07:48:43 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\SummaryParser::parse
|
2018-01-19 21:26:49 +00:00
|
|
|
* @dataProvider provideParse
|
|
|
|
*
|
|
|
|
* @param string $summary
|
|
|
|
* @param string[] $expectedUsers
|
|
|
|
*/
|
|
|
|
public function testParse( $summary, array $expectedUsers ) {
|
2024-03-15 08:27:36 +00:00
|
|
|
$parser = new SummaryParser( function ( UserIdentity $user ) {
|
2018-01-19 21:26:49 +00:00
|
|
|
if ( in_array( $user->getName(), $this->existingUsers ) ) {
|
|
|
|
return crc32( $user->getName() );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} );
|
|
|
|
|
|
|
|
$users = $parser->parse( $summary );
|
|
|
|
foreach ( $users as $name => $user ) {
|
2020-01-14 15:56:49 +00:00
|
|
|
$this->assertInstanceof( User::class, $user );
|
2018-01-19 21:26:49 +00:00
|
|
|
$this->assertEquals( $name, $user->getName() );
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = array_keys( $users );
|
|
|
|
|
|
|
|
$this->assertArrayEquals( $expectedUsers, $users );
|
|
|
|
}
|
|
|
|
|
2023-05-20 18:35:53 +00:00
|
|
|
public static function provideParse() {
|
2018-01-19 21:26:49 +00:00
|
|
|
return [
|
|
|
|
[ '', [] ],
|
|
|
|
[ " \t\r\n ", [] ],
|
|
|
|
[ 'foo bar', [] ],
|
|
|
|
[ 'Werdna', [] ],
|
|
|
|
[ 'User:Werdna', [] ],
|
|
|
|
[ '[User:Werdna]', [] ],
|
2018-03-07 20:50:48 +00:00
|
|
|
[ '[[]]', [] ],
|
|
|
|
[ '[[:]]', [] ],
|
|
|
|
[ '[[|]]', [] ],
|
|
|
|
[ '[[:|]]', [] ],
|
|
|
|
[ '[[:|test]]', [] ],
|
2018-01-19 21:26:49 +00:00
|
|
|
[ '[[User:Nonexistent]]', [] ],
|
|
|
|
[ '/* [[User:Werdna */', [] ],
|
2018-03-08 22:36:58 +00:00
|
|
|
[ '[[User talk:Werdna]]', [] ],
|
2018-01-19 21:26:49 +00:00
|
|
|
[ '[[User:Werdna]]', [ 'Werdna' ] ],
|
|
|
|
[ 'this is [[ [[User:Werdna]] ]]', [ 'Werdna' ] ],
|
|
|
|
[ '[[User:Werdna|]]', [ 'Werdna' ] ],
|
|
|
|
[ '[[User:Werdna| ]]', [ 'Werdna' ] ],
|
|
|
|
[ '[[User:Werdna|Wer | d[n]a]]', [ 'Werdna' ] ],
|
|
|
|
[ '[[User:Werdna]][[User:Werdna]][[User:Werdna]]', [ 'Werdna' ] ],
|
|
|
|
[ '/**/[[User:Werdna]][[user:jorm]]', [ 'Werdna', 'Jorm' ] ],
|
|
|
|
[ '/* [[User:Werdna]] */ [[ user : jim_ Carter_]]', [ 'Jim Carter' ] ],
|
|
|
|
[ '[[User:/* Jorm */]][[User:/* remove me */Werdna]]', [] ],
|
2018-03-07 20:50:48 +00:00
|
|
|
[ '[[:User:Werdna]]', [] ],
|
|
|
|
[ '[[:User:Werdna|]]', [] ],
|
|
|
|
[ '[[:User:Werdna|foo]]', [] ],
|
2018-01-19 21:26:49 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|