X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fuser%2FUserTest.php;h=642e8b402573a880311dd61c1f907a24174f6a51;hb=3b0effb20334f19f662aed6760ddfe7d4b43c6d6;hp=b9289dba52fd11c6e82dcaa28a679acfda1e920b;hpb=fe2ca43a0c376e58ca72cc8da800efe00ec392c4;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/user/UserTest.php b/tests/phpunit/includes/user/UserTest.php index b9289dba52..642e8b4025 100644 --- a/tests/phpunit/includes/user/UserTest.php +++ b/tests/phpunit/includes/user/UserTest.php @@ -4,6 +4,7 @@ define( 'NS_UNITTEST', 5600 ); define( 'NS_UNITTEST_TALK', 5601 ); use MediaWiki\Block\Restriction\PageRestriction; +use MediaWiki\Block\Restriction\NamespaceRestriction; use MediaWiki\MediaWikiServices; use MediaWiki\User\UserIdentityValue; use Wikimedia\TestingAccessWrapper; @@ -392,6 +393,7 @@ class UserTest extends MediaWikiTestCase { ], ], ] ); + $this->hideDeprecated( 'User::getPasswordValidity' ); $user = static::getTestUser()->getUser(); @@ -581,6 +583,7 @@ class UserTest extends MediaWikiTestCase { * When a user is autoblocked a cookie is set with which to track them * in case they log out and change IP addresses. * @link https://phabricator.wikimedia.org/T5233 + * @covers User::trackBlockWithCookie */ public function testAutoblockCookies() { // Set up the bits of global configuration that we use. @@ -596,7 +599,6 @@ class UserTest extends MediaWikiTestCase { ] ); // 1. Log in a test user, and block them. - $userBlocker = $this->getTestSysop()->getUser(); $user1tmp = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $user1tmp ); @@ -607,7 +609,6 @@ class UserTest extends MediaWikiTestCase { ] ); $block->setBlocker( $this->getTestSysop()->getUser() ); $block->setTarget( $user1tmp ); - $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user1 = User::newFromSession( $request1 ); @@ -663,6 +664,7 @@ class UserTest extends MediaWikiTestCase { /** * Make sure that no cookie is set to track autoblocked users * when $wgCookieSetOnAutoblock is false. + * @covers User::trackBlockWithCookie */ public function testAutoblockCookiesDisabled() { // Set up the bits of global configuration that we use. @@ -678,14 +680,12 @@ class UserTest extends MediaWikiTestCase { ] ); // 1. Log in a test user, and block them. - $userBlocker = $this->getTestSysop()->getUser(); $testUser = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $testUser ); $block = new Block( [ 'enableAutoblock' => true ] ); $block->setBlocker( $this->getTestSysop()->getUser() ); $block->setTarget( $testUser ); - $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user = User::newFromSession( $request1 ); @@ -710,6 +710,7 @@ class UserTest extends MediaWikiTestCase { * When a user is autoblocked and a cookie is set to track them, the expiry time of the cookie * should match the block's expiry, to a maximum of 24 hours. If the expiry time is changed, * the cookie's should change with it. + * @covers User::trackBlockWithCookie */ public function testAutoblockCookieInfiniteExpiry() { $this->setMwGlobals( [ @@ -724,14 +725,12 @@ class UserTest extends MediaWikiTestCase { ] ); // 1. Log in a test user, and block them indefinitely. - $userBlocker = $this->getTestSysop()->getUser(); $user1Tmp = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $user1Tmp ); $block = new Block( [ 'enableAutoblock' => true, 'expiry' => 'infinity' ] ); $block->setBlocker( $this->getTestSysop()->getUser() ); $block->setTarget( $user1Tmp ); - $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user1 = User::newFromSession( $request1 ); @@ -774,37 +773,47 @@ class UserTest extends MediaWikiTestCase { $block->delete(); } + /** + * @covers User::getBlockedStatus + */ public function testSoftBlockRanges() { - global $wgUser; - - $this->setMwGlobals( [ - 'wgSoftBlockRanges' => [ '10.0.0.0/8' ], - 'wgUser' => null, - ] ); + $setSessionUser = function ( User $user, WebRequest $request ) { + $this->setMwGlobals( 'wgUser', $user ); + RequestContext::getMain()->setUser( $user ); + RequestContext::getMain()->setRequest( $request ); + TestingAccessWrapper::newFromObject( $user )->mRequest = $request; + $request->getSession()->setUser( $user ); + }; + $this->setMwGlobals( 'wgSoftBlockRanges', [ '10.0.0.0/8' ] ); // IP isn't in $wgSoftBlockRanges + $wgUser = new User(); $request = new FauxRequest(); $request->setIP( '192.168.0.1' ); - $wgUser = User::newFromSession( $request ); + $setSessionUser( $wgUser, $request ); $this->assertNull( $wgUser->getBlock() ); // IP is in $wgSoftBlockRanges + $wgUser = new User(); $request = new FauxRequest(); $request->setIP( '10.20.30.40' ); - $wgUser = User::newFromSession( $request ); + $setSessionUser( $wgUser, $request ); $block = $wgUser->getBlock(); $this->assertInstanceOf( Block::class, $block ); $this->assertSame( 'wgSoftBlockRanges', $block->getSystemBlockType() ); // Make sure the block is really soft - $request->getSession()->setUser( $this->getTestUser()->getUser() ); - $wgUser = User::newFromSession( $request ); + $wgUser = $this->getTestUser()->getUser(); + $request = new FauxRequest(); + $request->setIP( '10.20.30.40' ); + $setSessionUser( $wgUser, $request ); $this->assertFalse( $wgUser->isAnon(), 'sanity check' ); $this->assertNull( $wgUser->getBlock() ); } /** * Test that a modified BlockID cookie doesn't actually load the relevant block (T152951). + * @covers User::trackBlockWithCookie */ public function testAutoblockCookieInauthentic() { // Set up the bits of global configuration that we use. @@ -820,14 +829,12 @@ class UserTest extends MediaWikiTestCase { ] ); // 1. Log in a blocked test user. - $userBlocker = $this->getTestSysop()->getUser(); $user1tmp = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $user1tmp ); $block = new Block( [ 'enableAutoblock' => true ] ); $block->setBlocker( $this->getTestSysop()->getUser() ); $block->setTarget( $user1tmp ); - $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user1 = User::newFromSession( $request1 ); @@ -851,6 +858,7 @@ class UserTest extends MediaWikiTestCase { /** * The BlockID cookie is normally verified with a HMAC, but not if wgSecretKey is not set. * This checks that a non-authenticated cookie still works. + * @covers User::trackBlockWithCookie */ public function testAutoblockCookieNoSecretKey() { // Set up the bits of global configuration that we use. @@ -866,14 +874,12 @@ class UserTest extends MediaWikiTestCase { ] ); // 1. Log in a blocked test user. - $userBlocker = $this->getTestSysop()->getUser(); $user1tmp = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $user1tmp ); $block = new Block( [ 'enableAutoblock' => true ] ); $block->setBlocker( $this->getTestSysop()->getUser() ); $block->setTarget( $user1tmp ); - $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user1 = User::newFromSession( $request1 ); @@ -1020,6 +1026,9 @@ class UserTest extends MediaWikiTestCase { $this->assertTrue( User::isLocallyBlockedProxy( $ip ) ); } + /** + * @covers User::newFromActorId + */ public function testActorId() { $domain = MediaWikiServices::getInstance()->getDBLoadBalancer()->getLocalDomainID(); $this->hideDeprecated( 'User::selectFields' ); @@ -1083,6 +1092,9 @@ class UserTest extends MediaWikiTestCase { 'User::newFromActorId works for an anonymous user' ); } + /** + * @covers User::newFromAnyId + */ public function testNewFromAnyId() { // Registered user $user = $this->getTestUser()->getUser(); @@ -1244,6 +1256,9 @@ class UserTest extends MediaWikiTestCase { ); $restrictions[] = new PageRestriction( 0, $page->getId() ); } + foreach ( $options['namespaceRestrictions'] ?? [] as $ns ) { + $restrictions[] = new NamespaceRestriction( 0, $ns ); + } $block = new Block( [ 'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ), @@ -1266,42 +1281,85 @@ class UserTest extends MediaWikiTestCase { public static function provideIsBlockedFrom() { return [ - 'Basic operation' => [ 'Test page', true ], - 'User talk page, not allowed' => [ self::USER_TALK_PAGE, true, [ - 'allowUsertalk' => false, - ] ], - 'User talk page, allowed' => [ self::USER_TALK_PAGE, false, [ - 'allowUsertalk' => true, - ] ], - 'User talk page, allowed but $wgBlockAllowsUTEdit is false' => [ self::USER_TALK_PAGE, true, [ - 'allowUsertalk' => true, - 'blockAllowsUTEdit' => false, - ] ], - - 'Partial block, blocking the page' => [ 'Test page', true, [ - 'pageRestrictions' => [ 'Test page' ], - ] ], - 'Partial block, not blocking the page' => [ 'Test page 2', false, [ - 'pageRestrictions' => [ 'Test page' ], - ] ], - 'Partial block, overriding allowUsertalk' => [ self::USER_TALK_PAGE, true, [ - 'allowUsertalk' => false, - 'pageRestrictions' => [ self::USER_TALK_PAGE ], - ] ], - 'Partial block, allowing user talk' => [ self::USER_TALK_PAGE, false, [ - 'allowUsertalk' => true, - 'pageRestrictions' => [ 'Test page' ], - ] ], - 'Partial block, not allowing user talk' => [ self::USER_TALK_PAGE, true, [ - 'allowUsertalk' => false, - 'pageRestrictions' => [ 'Test page' ], - ] ], + 'Sitewide block, basic operation' => [ 'Test page', true ], + 'Sitewide block, not allowing user talk' => [ + self::USER_TALK_PAGE, true, [ + 'allowUsertalk' => false, + ] + ], + 'Sitewide block, allowing user talk' => [ + self::USER_TALK_PAGE, false, [ + 'allowUsertalk' => true, + ] + ], + 'Sitewide block, allowing user talk but $wgBlockAllowsUTEdit is false' => [ + self::USER_TALK_PAGE, true, [ + 'allowUsertalk' => true, + 'blockAllowsUTEdit' => false, + ] + ], + 'Partial block, blocking the page' => [ + 'Test page', true, [ + 'pageRestrictions' => [ 'Test page' ], + ] + ], + 'Partial block, not blocking the page' => [ + 'Test page 2', false, [ + 'pageRestrictions' => [ 'Test page' ], + ] + ], + 'Partial block, not allowing user talk but user talk page is not blocked' => [ + self::USER_TALK_PAGE, false, [ + 'allowUsertalk' => false, + 'pageRestrictions' => [ 'Test page' ], + ] + ], + 'Partial block, allowing user talk but user talk page is blocked' => [ + self::USER_TALK_PAGE, true, [ + 'allowUsertalk' => true, + 'pageRestrictions' => [ self::USER_TALK_PAGE ], + ] + ], + 'Partial block, user talk page is not blocked but $wgBlockAllowsUTEdit is false' => [ + self::USER_TALK_PAGE, false, [ + 'allowUsertalk' => false, + 'pageRestrictions' => [ 'Test page' ], + 'blockAllowsUTEdit' => false, + ] + ], + 'Partial block, user talk page is blocked and $wgBlockAllowsUTEdit is false' => [ + self::USER_TALK_PAGE, true, [ + 'allowUsertalk' => true, + 'pageRestrictions' => [ self::USER_TALK_PAGE ], + 'blockAllowsUTEdit' => false, + ] + ], + 'Partial user talk namespace block, not allowing user talk' => [ + self::USER_TALK_PAGE, true, [ + 'allowUsertalk' => false, + 'namespaceRestrictions' => [ NS_USER_TALK ], + ] + ], + 'Partial user talk namespace block, allowing user talk' => [ + self::USER_TALK_PAGE, false, [ + 'allowUsertalk' => true, + 'namespaceRestrictions' => [ NS_USER_TALK ], + ] + ], + 'Partial user talk namespace block, where $wgBlockAllowsUTEdit is false' => [ + self::USER_TALK_PAGE, true, [ + 'allowUsertalk' => true, + 'namespaceRestrictions' => [ NS_USER_TALK ], + 'blockAllowsUTEdit' => false, + ] + ], ]; } /** * Block cookie should be set for IP Blocks if * wgCookieSetOnIpBlock is set to true + * @covers User::trackBlockWithCookie */ public function testIpBlockCookieSet() { $this->setMwGlobals( [ @@ -1337,6 +1395,7 @@ class UserTest extends MediaWikiTestCase { /** * Block cookie should NOT be set when wgCookieSetOnIpBlock * is disabled + * @covers User::trackBlockWithCookie */ public function testIpBlockCookieNotSet() { $this->setMwGlobals( [ @@ -1372,6 +1431,7 @@ class UserTest extends MediaWikiTestCase { /** * When an ip user is blocked and then they log in, cookie block * should be invalid and the cookie removed. + * @covers User::trackBlockWithCookie */ public function testIpBlockCookieIgnoredWhenUserLoggedIn() { $this->setMwGlobals( [ @@ -1408,4 +1468,42 @@ class UserTest extends MediaWikiTestCase { // clean up $block->delete(); } + + /** + * @covers User::getFirstEditTimestamp + * @covers User::getLatestEditTimestamp + */ + public function testGetFirstLatestEditTimestamp() { + $clock = MWTimestamp::convert( TS_UNIX, '20100101000000' ); + MWTimestamp::setFakeTime( function () use ( &$clock ) { + return $clock += 1000; + } ); + try { + $user = $this->getTestUser()->getUser(); + $firstRevision = self::makeEdit( $user, 'Help:UserTest_GetEditTimestamp', 'one', 'test' ); + $secondRevision = self::makeEdit( $user, 'Help:UserTest_GetEditTimestamp', 'two', 'test' ); + // Sanity check: revisions timestamp are different + $this->assertNotEquals( $firstRevision->getTimestamp(), $secondRevision->getTimestamp() ); + + $this->assertEquals( $firstRevision->getTimestamp(), $user->getFirstEditTimestamp() ); + $this->assertEquals( $secondRevision->getTimestamp(), $user->getLatestEditTimestamp() ); + } finally { + MWTimestamp::setFakeTime( false ); + } + } + + /** + * @param User $user + * @param string $title + * @param string $content + * @param string $comment + * @return \MediaWiki\Revision\RevisionRecord|null + */ + private static function makeEdit( User $user, $title, $content, $comment ) { + $page = WikiPage::factory( Title::newFromText( $title ) ); + $content = ContentHandler::makeContent( $content, $page->getTitle() ); + $updater = $page->newPageUpdater( $user ); + $updater->setContent( 'main', $content ); + return $updater->saveRevision( CommentStoreComment::newUnsavedComment( $comment ) ); + } }