Merge "Prevent fatal PHP errors when PageRestriction::getTitle() returns null."
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 12 Feb 2019 16:27:04 +0000 (16:27 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 12 Feb 2019 16:27:04 +0000 (16:27 +0000)
1  2 
includes/specials/SpecialBlock.php
tests/phpunit/includes/block/Restriction/PageRestrictionTest.php
tests/phpunit/includes/specials/SpecialBlockTest.php
tests/phpunit/includes/specials/pagers/BlockListPagerTest.php

@@@ -148,6 -148,7 +148,6 @@@ class SpecialBlock extends FormSpecialP
                $suggestedDurations = self::getSuggestedDurations();
  
                $conf = $this->getConfig();
 -              $oldCommentSchema = $conf->get( 'CommentTableSchemaMigrationStage' ) === MIGRATION_OLD;
                $enablePartialBlocks = $conf->get( 'EnablePartialBlocks' );
  
                $a = [];
                        'type' => 'selectandother',
                        // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
                        // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
 -                      // Unicode codepoints (or 255 UTF-8 bytes for old schema).
 -                      'maxlength' => $oldCommentSchema ? 255 : CommentStore::COMMENT_CHARACTER_LIMIT,
 +                      // Unicode codepoints.
 +                      'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT,
                        'maxlength-unit' => 'codepoints',
                        'options-message' => 'ipbreason-dropdown',
                        'section' => 'reason',
                                foreach ( $block->getRestrictions() as $restriction ) {
                                        switch ( $restriction->getType() ) {
                                                case PageRestriction::TYPE:
-                                                       $pageRestrictions[] = $restriction->getTitle()->getPrefixedText();
+                                                       if ( $restriction->getTitle() ) {
+                                                               $pageRestrictions[] = $restriction->getTitle()->getPrefixedText();
+                                                       }
                                                        break;
                                                case NamespaceRestriction::TYPE:
                                                        $namespaceRestrictions[] = $restriction->getValue();
  
                $performer = $context->getUser();
                $enablePartialBlocks = $context->getConfig()->get( 'EnablePartialBlocks' );
 +              $isPartialBlock = $enablePartialBlocks &&
 +                      isset( $data['EditingRestriction'] ) &&
 +                      $data['EditingRestriction'] === 'partial';
  
                // Handled by field validator callback
                // self::validateTargetField( $data['Target'] );
                                return [ 'badaccess-group0' ];
                        }
  
 +                      if ( $isPartialBlock ) {
 +                              return [ 'ipb_hide_partial' ];
 +                      }
 +
                        # Recheck params here...
                        if ( $type != Block::TYPE_USER ) {
                                $data['HideUser'] = false; # IP users should not be hidden
                $block->isAutoblocking( $data['AutoBlock'] );
                $block->mHideName = $data['HideUser'];
  
 -              if (
 -                      $enablePartialBlocks &&
 -                      isset( $data['EditingRestriction'] ) &&
 -                      $data['EditingRestriction'] === 'partial'
 -               ) {
 -                       $block->isSitewide( false );
 +              if ( $isPartialBlock ) {
 +                      $block->isSitewide( false );
                }
  
                $reason = [ 'hookaborted' ];
                                        $title = Title::newFromText( $text );
                                        // Use the link cache since the title has already been loaded when
                                        // the field was validated.
 -                                      $restriction = new PageRestriction( 0, $title->getArticleId() );
 +                                      $restriction = new PageRestriction( 0, $title->getArticleID() );
                                        $restriction->setTitle( $title );
                                        return $restriction;
                                }, explode( "\n", $data['PageRestrictions'] ) );
@@@ -20,6 -20,11 +20,11 @@@ class PageRestrictionTest extends Restr
  
                $page = $this->getExistingTestPage( 'Mars' );
                $this->assertFalse( $restriction->matches( $page->getTitle() ) );
+               // Deleted page.
+               $restriction = new $class( 2, 99999 );
+               $page = $this->getExistingTestPage( 'Saturn' );
+               $this->assertFalse( $restriction->matches( $page->getTitle() ) );
        }
  
        public function testGetType() {
@@@ -38,7 -43,7 +43,7 @@@
  
                $restriction = new $class( 1, 1 );
                $title = \Title::newFromId( 1 );
 -              $this->assertEquals( $title->getArticleId(), $restriction->getTitle()->getArticleId() );
 +              $this->assertEquals( $title->getArticleID(), $restriction->getTitle()->getArticleID() );
        }
  
        public function testNewFromRow() {
@@@ -29,7 -29,6 +29,7 @@@ class SpecialBlockTest extends SpecialP
        public function testGetFormFields() {
                $this->setMwGlobals( [
                        'wgEnablePartialBlocks' => false,
 +                      'wgBlockAllowsUTEdit' => true,
                ] );
                $page = $this->newSpecialPage();
                $wrappedPage = TestingAccessWrapper::newFromObject( $page );
@@@ -72,7 -71,6 +72,7 @@@
        public function testMaybeAlterFormDefaults() {
                $this->setMwGlobals( [
                        'wgEnablePartialBlocks' => false,
 +                      'wgBlockAllowsUTEdit' => true,
                ] );
  
                $block = $this->insertBlock();
                        new PageRestriction( 0, $pageSaturn->getId() ),
                        new PageRestriction( 0, $pageMars->getId() ),
                        new NamespaceRestriction( 0, NS_TALK ),
+                       // Deleted page.
+                       new PageRestriction( 0, 999999 ),
                ] );
  
                $block->insert();
@@@ -140,7 -140,9 +140,9 @@@ class BlockListPagerTest extends MediaW
  
                $restrictions = [
                        ( new PageRestriction( 0, $pageId ) )->setTitle( $title ),
-                       new NamespaceRestriction( 0, NS_MAIN )
+                       new NamespaceRestriction( 0, NS_MAIN ),
+                       // Deleted page.
+                       new PageRestriction( 0, 999999 ),
                ];
  
                $wrappedPager = TestingAccessWrapper::newFromObject( $pager );
  
                $restriction = $restrictions[0];
                $this->assertEquals( $page->getId(), $restriction->getValue() );
 -              $this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleId() );
 +              $this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleID() );
                $this->assertEquals( $title->getDBKey(), $restriction->getTitle()->getDBKey() );
                $this->assertEquals( $title->getNamespace(), $restriction->getTitle()->getNamespace() );