WordPress.org

Make WordPress Core

Changeset 37746


Ignore:
Timestamp:
06/18/2016 10:09:26 AM (4 years ago)
Author:
swissspidy
Message:

L10n: Add unit tests for the override_load_textdomain filter.

Fixes #36398.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/l10n.php

    r36766 r37746  
    178178    }
    179179
     180    function test_override_load_textdomain_noop() {
     181        add_filter( 'override_load_textdomain', '__return_true' );
     182        $load_textdomain = load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/non-existent-file' );
     183        remove_filter( 'override_load_textdomain', '__return_true' );
     184
     185        $this->assertTrue( $load_textdomain );
     186        $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) );
     187    }
     188
     189    function test_override_load_textdomain_non_existent_mofile() {
     190        add_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ), 10, 3 );
     191        $load_textdomain = load_textdomain( 'wp-tests-domain', WP_LANG_DIR . '/non-existent-file.mo' );
     192        remove_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ) );
     193
     194        $is_textdomain_loaded = is_textdomain_loaded( 'wp-tests-domain' );
     195        unload_textdomain( 'wp-tests-domain' );
     196        $is_textdomain_loaded_after = is_textdomain_loaded( 'wp-tests-domain' );
     197
     198        $this->assertFalse( $load_textdomain );
     199        $this->assertFalse( $is_textdomain_loaded );
     200        $this->assertFalse( $is_textdomain_loaded_after );
     201    }
     202
     203    function test_override_load_textdomain_custom_mofile() {
     204        add_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ), 10, 3 );
     205        $load_textdomain = load_textdomain( 'wp-tests-domain', WP_LANG_DIR . '/plugins/internationalized-plugin-de_DE.mo' );
     206        remove_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ) );
     207
     208        $is_textdomain_loaded = is_textdomain_loaded( 'wp-tests-domain' );
     209        unload_textdomain( 'wp-tests-domain' );
     210        $is_textdomain_loaded_after = is_textdomain_loaded( 'wp-tests-domain' );
     211
     212        $this->assertTrue( $load_textdomain );
     213        $this->assertTrue( $is_textdomain_loaded );
     214        $this->assertFalse( $is_textdomain_loaded_after );
     215    }
     216
     217    /**
     218     * @param bool   $override Whether to override the .mo file loading. Default false.
     219     * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
     220     * @param string $mofile   Path to the MO file.
     221     * @return bool
     222     */
     223    function _override_load_textdomain_filter( $override, $domain, $file ) {
     224        global $l10n;
     225
     226        if ( ! is_readable( $file ) ) {
     227            return false;
     228        }
     229
     230        $mo = new MO();
     231
     232        if ( ! $mo->import_from_file( $file ) ) {
     233            return false;
     234        }
     235
     236        if ( isset( $l10n[ $domain ] ) ) {
     237            $mo->merge_with( $l10n[ $domain ] );
     238        }
     239
     240        $l10n[ $domain ] = &$mo;
     241
     242        return true;
     243    }
    180244}
Note: See TracChangeset for help on using the changeset viewer.