Merge "Add missing return types to User::getOption()"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 23 Mar 2018 01:41:28 +0000 (01:41 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 23 Mar 2018 01:41:28 +0000 (01:41 +0000)
1  2 
includes/user/User.php
tests/phpunit/includes/user/UserTest.php

diff --combined includes/user/User.php
@@@ -1871,9 -1871,7 +1871,9 @@@ class User implements IDBAccessObject, 
                        $this->mHideName = $block->mHideName;
                        $this->mAllowUsertalk = !$block->prevents( 'editownusertalk' );
                } else {
 +                      $this->mBlock = null;
                        $this->mBlockedby = '';
 +                      $this->mBlockreason = '';
                        $this->mHideName = 0;
                        $this->mAllowUsertalk = false;
                }
         * @param string $oname The option to check
         * @param string $defaultOverride A default value returned if the option does not exist
         * @param bool $ignoreHidden Whether to ignore the effects of $wgHiddenPrefs
-        * @return string|null User's current value for the option
+        * @return string|array|int|null User's current value for the option
         * @see getBoolOption()
         * @see getIntOption()
         */
@@@ -350,6 -350,7 +350,7 @@@ class UserTest extends MediaWikiTestCas
  
                $user->setOption( 'userjs-someoption', 'test' );
                $user->setOption( 'rclimit', 200 );
+               $user->setOption( 'wpwatchlistdays', '0' );
                $user->saveSettings();
  
                $user = User::newFromName( $user->getName() );
                MediaWikiServices::getInstance()->getMainWANObjectCache()->clearProcessCache();
                $this->assertEquals( 'test', $user->getOption( 'userjs-someoption' ) );
                $this->assertEquals( 200, $user->getOption( 'rclimit' ) );
+               // Check that an option saved as a string '0' is returned as an integer.
+               $user = User::newFromName( $user->getName() );
+               $user->load( User::READ_LATEST );
+               $this->assertSame( 0, $user->getOption( 'wpwatchlistdays' ) );
        }
  
        /**
                } catch ( InvalidArgumentException $ex ) {
                }
        }
 +
 +      /**
 +       * @covers User::getBlockedStatus
 +       * @covers User::getBlock
 +       * @covers User::blockedBy
 +       * @covers User::blockedFor
 +       * @covers User::isHidden
 +       * @covers User::isBlockedFrom
 +       */
 +      public function testBlockInstanceCache() {
 +              // First, check the user isn't blocked
 +              $user = $this->getMutableTestUser()->getUser();
 +              $ut = Title::makeTitle( NS_USER_TALK, $user->getName() );
 +              $this->assertNull( $user->getBlock( false ), 'sanity check' );
 +              $this->assertSame( '', $user->blockedBy(), 'sanity check' );
 +              $this->assertSame( '', $user->blockedFor(), 'sanity check' );
 +              $this->assertFalse( (bool)$user->isHidden(), 'sanity check' );
 +              $this->assertFalse( $user->isBlockedFrom( $ut ), 'sanity check' );
 +
 +              // Block the user
 +              $blocker = $this->getTestSysop()->getUser();
 +              $block = new Block( [
 +                      'hideName' => true,
 +                      'allowUsertalk' => false,
 +                      'reason' => 'Because',
 +              ] );
 +              $block->setTarget( $user );
 +              $block->setBlocker( $blocker );
 +              $res = $block->insert();
 +              $this->assertTrue( (bool)$res['id'], 'sanity check: Failed to insert block' );
 +
 +              // Clear cache and confirm it loaded the block properly
 +              $user->clearInstanceCache();
 +              $this->assertInstanceOf( Block::class, $user->getBlock( false ) );
 +              $this->assertSame( $blocker->getName(), $user->blockedBy() );
 +              $this->assertSame( 'Because', $user->blockedFor() );
 +              $this->assertTrue( (bool)$user->isHidden() );
 +              $this->assertTrue( $user->isBlockedFrom( $ut ) );
 +
 +              // Unblock
 +              $block->delete();
 +
 +              // Clear cache and confirm it loaded the not-blocked properly
 +              $user->clearInstanceCache();
 +              $this->assertNull( $user->getBlock( false ) );
 +              $this->assertSame( '', $user->blockedBy() );
 +              $this->assertSame( '', $user->blockedFor() );
 +              $this->assertFalse( (bool)$user->isHidden() );
 +              $this->assertFalse( $user->isBlockedFrom( $ut ) );
 +      }
 +
  }