L10n: Add unit tests for the override_load_textdomain
filter.
Fixes #36398. git-svn-id: https://develop.svn.wordpress.org/trunk@37746 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
c3394e2c93
commit
db843d80ab
@ -177,4 +177,68 @@ class Tests_L10n extends WP_UnitTestCase {
|
||||
$this->assertNotEmpty( $array['X-Generator'] );
|
||||
}
|
||||
|
||||
function test_override_load_textdomain_noop() {
|
||||
add_filter( 'override_load_textdomain', '__return_true' );
|
||||
$load_textdomain = load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/non-existent-file' );
|
||||
remove_filter( 'override_load_textdomain', '__return_true' );
|
||||
|
||||
$this->assertTrue( $load_textdomain );
|
||||
$this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) );
|
||||
}
|
||||
|
||||
function test_override_load_textdomain_non_existent_mofile() {
|
||||
add_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ), 10, 3 );
|
||||
$load_textdomain = load_textdomain( 'wp-tests-domain', WP_LANG_DIR . '/non-existent-file.mo' );
|
||||
remove_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ) );
|
||||
|
||||
$is_textdomain_loaded = is_textdomain_loaded( 'wp-tests-domain' );
|
||||
unload_textdomain( 'wp-tests-domain' );
|
||||
$is_textdomain_loaded_after = is_textdomain_loaded( 'wp-tests-domain' );
|
||||
|
||||
$this->assertFalse( $load_textdomain );
|
||||
$this->assertFalse( $is_textdomain_loaded );
|
||||
$this->assertFalse( $is_textdomain_loaded_after );
|
||||
}
|
||||
|
||||
function test_override_load_textdomain_custom_mofile() {
|
||||
add_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ), 10, 3 );
|
||||
$load_textdomain = load_textdomain( 'wp-tests-domain', WP_LANG_DIR . '/plugins/internationalized-plugin-de_DE.mo' );
|
||||
remove_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ) );
|
||||
|
||||
$is_textdomain_loaded = is_textdomain_loaded( 'wp-tests-domain' );
|
||||
unload_textdomain( 'wp-tests-domain' );
|
||||
$is_textdomain_loaded_after = is_textdomain_loaded( 'wp-tests-domain' );
|
||||
|
||||
$this->assertTrue( $load_textdomain );
|
||||
$this->assertTrue( $is_textdomain_loaded );
|
||||
$this->assertFalse( $is_textdomain_loaded_after );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $override Whether to override the .mo file loading. Default false.
|
||||
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
|
||||
* @param string $mofile Path to the MO file.
|
||||
* @return bool
|
||||
*/
|
||||
function _override_load_textdomain_filter( $override, $domain, $file ) {
|
||||
global $l10n;
|
||||
|
||||
if ( ! is_readable( $file ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$mo = new MO();
|
||||
|
||||
if ( ! $mo->import_from_file( $file ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( isset( $l10n[ $domain ] ) ) {
|
||||
$mo->merge_with( $l10n[ $domain ] );
|
||||
}
|
||||
|
||||
$l10n[ $domain ] = &$mo;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user