wiki.techinc.nl/tests/phpunit/includes/user/ExternalUserNamesTest.php
Daimona Eaytoy 485e47ff10 tests: Avoid database usage when possible
We would like to remove DB access in non-database PHPUnit tests. As a
first step, avoid database usage in tested code when possible. In
particular:
- In NameTableStoreFactory, avoid domain ID normalization if the
  provided ID is already false.
- In SpecialDoubleRedirects, do not acquire a DB connection until it's
  needed (which is just one place).
- Use editPage() in TitleDefTest instead of a DIY implementation, and
  add `@group Database` accordingly.
- Avoid parsing titles in ContentHandler tests that don't need to parse
  titles. Among the many dependencies of parsing titles is the interwiki
  lookup, which requires DB access.
- Also remove test cases that used the "Gadget" namespace; it doesn't
  exist in core, so these pages were actually in the mainspace.
- Mock the database in CategoriesRdfTest. The only two methods that use
  the database were already being mocked.
- Add `@group Database` to test classes that are intentionally using the
  Database, mainly via getTestUser().

Bug: T155147
Change-Id: I9385fe14cfeb6b7b7378cc322d510034c4ee0711
2023-07-31 00:46:13 +00:00

183 lines
4.3 KiB
PHP

<?php
use MediaWiki\Tests\Unit\DummyServicesTrait;
use MediaWiki\Title\Title;
/**
* @covers ExternalUserNames
* @group Database
*/
class ExternalUserNamesTest extends MediaWikiIntegrationTestCase {
use DummyServicesTrait;
public static function provideGetUserLinkTitle() {
return [
[
'Valid user name from known import source',
'valid:>User1',
Title::makeTitle( NS_MAIN, ':User:User1', '', 'valid' )
],
[
'Valid user name that looks like an import source, from known import source',
'valid:valid:>User1',
Title::makeTitle( NS_MAIN, 'valid::User:User1', '', 'valid' )
],
[
'Local IP address',
'127.0.0.1',
Title::makeTitle( NS_SPECIAL, 'Contributions/127.0.0.1', '', '' )
],
[
'Valid user name from unknown import source',
'invalid:>User1',
null
],
[
'Corrupt local user name with linebreak',
"Foo\nBar",
null
],
[
'Corrupt local user name with terminal underscore',
'Barf_',
null
],
[
'Corrupt local user name with initial lowercase',
'abcd',
null
],
[
'Corrupt local user name with slash',
'For/Bar',
null
],
[
'Corrupt local user name with octothorpe',
'For#Bar',
null
],
];
}
/**
* @covers ExternalUserNames::getUserLinkTitle
* @dataProvider provideGetUserLinkTitle
*/
public function testGetUserLinkTitle( $caseDescription, $username, $expected ) {
$this->setContentLang( 'en' );
// DummyServicesTrait::getDummyInterwikiLookup
$interwikiLookup = $this->getDummyInterwikiLookup( [ 'valid' ] );
$this->setService( 'InterwikiLookup', $interwikiLookup );
$this->assertEquals(
$expected,
ExternalUserNames::getUserLinkTitle( $username ),
$caseDescription
);
}
public static function provideApplyPrefix() {
return [
[ 'User1', 'prefix', 'prefix>User1' ],
[ 'User1', 'prefix:>', 'prefix>User1' ],
[ 'User1', 'prefix:', 'prefix>User1' ],
[ 'user1', 'prefix', 'prefix>user1' ],
[ '0', 'prefix', 'prefix>0' ],
[ 'Unknown user', 'prefix', 'Unknown user' ],
];
}
/**
* @covers ExternalUserNames::applyPrefix
* @dataProvider provideApplyPrefix
*/
public function testApplyPrefix( $username, $prefix, $expected ) {
$externalUserNames = new ExternalUserNames( $prefix, true );
$this->assertSame(
$expected,
$externalUserNames->applyPrefix( $username )
);
}
/**
* @covers ExternalUserNames::applyPrefix
*/
public function testApplyPrefix_existingUser() {
$testName = $this->getTestUser()->getUser()->getName();
$testName2 = lcfirst( $testName );
$this->assertNotSame( $testName, $testName2 );
$externalUserNames = new ExternalUserNames( 'p', false );
$this->assertSame( "p>$testName", $externalUserNames->applyPrefix( $testName ) );
$this->assertSame( "p>$testName2", $externalUserNames->applyPrefix( $testName2 ) );
$externalUserNames = new ExternalUserNames( 'p', true );
$this->assertSame( $testName, $externalUserNames->applyPrefix( $testName ) );
$this->assertSame( $testName2, $externalUserNames->applyPrefix( $testName2 ) );
}
public static function provideAddPrefix() {
return [
[ 'User1', 'prefix', 'prefix>User1' ],
[ 'User2', 'prefix2', 'prefix2>User2' ],
[ 'User3', 'prefix3', 'prefix3>User3' ],
];
}
/**
* @covers ExternalUserNames::addPrefix
* @dataProvider provideAddPrefix
*/
public function testAddPrefix( $username, $prefix, $expected ) {
$externalUserNames = new ExternalUserNames( $prefix, true );
$this->assertSame(
$expected,
$externalUserNames->addPrefix( $username )
);
}
public static function provideIsExternal() {
return [
[ 'User1', false ],
[ '>User1', true ],
[ 'prefix>User1', true ],
[ 'prefix:>User1', true ],
];
}
/**
* @covers ExternalUserNames::isExternal
* @dataProvider provideIsExternal
*/
public function testIsExternal( $username, $expected ) {
$this->assertSame(
$expected,
ExternalUserNames::isExternal( $username )
);
}
public static function provideGetLocal() {
return [
[ 'User1', 'User1' ],
[ '>User2', 'User2' ],
[ 'prefix>User3', 'User3' ],
[ 'prefix:>User4', 'User4' ],
];
}
/**
* @covers ExternalUserNames::getLocal
* @dataProvider provideGetLocal
*/
public function testGetLocal( $username, $expected ) {
$this->assertSame(
$expected,
ExternalUserNames::getLocal( $username )
);
}
}