Let User::idFromName always return int or null
[lhc/web/wiklou.git] / tests / phpunit / includes / user / UserTest.php
index 164b466..d41a1f8 100644 (file)
@@ -757,7 +757,7 @@ class UserTest extends MediaWikiTestCase {
 
                // 3. Change the block's expiry (to 2 hours), and the cookie's should be changed also.
                $newExpiry = wfTimestamp() + 2 * 60 * 60;
-               $block->mExpiry = wfTimestamp( TS_MW, $newExpiry );
+               $block->setExpiry( wfTimestamp( TS_MW, $newExpiry ) );
                $block->update();
                $user2tmp = $this->getTestUser()->getUser();
                $request2 = new FauxRequest();
@@ -777,30 +777,36 @@ class UserTest extends MediaWikiTestCase {
         * @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() );
        }
@@ -1500,4 +1506,38 @@ class UserTest extends MediaWikiTestCase {
                $updater->setContent( 'main', $content );
                return $updater->saveRevision( CommentStoreComment::newUnsavedComment( $comment ) );
        }
+
+       /**
+        * @covers User::idFromName
+        */
+       public function testExistingIdFromName() {
+               $this->assertTrue(
+                       array_key_exists( $this->user->getName(), User::$idCacheByName ),
+                       'Test user should already be in the id cache.'
+               );
+               $this->assertSame(
+                       $this->user->getId(), User::idFromName( $this->user->getName() ),
+                       'Id is correctly retreived from the cache.'
+               );
+               $this->assertSame(
+                       $this->user->getId(), User::idFromName( $this->user->getName(), User::READ_LATEST ),
+                       'Id is correctly retreived from the database.'
+               );
+       }
+
+       /**
+        * @covers User::idFromName
+        */
+       public function testNonExistingIdFromName() {
+               $this->assertFalse(
+                       array_key_exists( 'NotExisitngUser', User::$idCacheByName ),
+                       'Non exisitng user should not be in the id cache.'
+               );
+               $this->assertSame( null, User::idFromName( 'NotExisitngUser' ) );
+               $this->assertTrue(
+                       array_key_exists( 'NotExisitngUser', User::$idCacheByName ),
+                       'Username will be cached when requested once.'
+               );
+               $this->assertSame( null, User::idFromName( 'NotExisitngUser' ) );
+       }
 }