WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/26/2016 02:14:44 PM (3 years ago)
Author:
ocean90
Message:

I18N: Add $user_id argument to get_user_locale().

This allows to retrieve the locale of any user with the additional fallback to the site locale.

Fixes #38512.
See #29783, #26511.

File:
1 edited

Legend:

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

    r38705 r38955  
    4444    }
    4545
     46    public function test_returns_site_locale_if_no_user() {
     47        wp_set_current_user( 0 );
     48        $this->assertSame( get_locale(), get_user_locale() );
     49    }
     50
    4651    public function test_returns_correct_user_locale() {
    4752        set_current_screen( 'dashboard' );
     
    7681        $this->assertSame( $user_locale, $user_locale_2 );
    7782    }
     83
     84    public function test_user_id_argument_with_id() {
     85        $user_id = $this->factory()->user->create( array(
     86            'locale' => 'es_ES',
     87        ) );
     88
     89        $user_locale1 = get_user_locale( $user_id );
     90
     91        delete_user_meta( $user_id, 'locale' );
     92
     93        $user_locale2 = get_user_locale( $user_id );
     94
     95        $this->assertSame( 'es_ES', $user_locale1 );
     96        $this->assertSame( get_locale(), $user_locale2 );
     97    }
     98
     99    public function test_user_id_argument_with_wp_user_object() {
     100        $user_id = $this->factory()->user->create( array(
     101            'locale' => 'es_ES',
     102        ) );
     103
     104        $user = get_user_by( 'id', $user_id );
     105
     106        $user_locale1 = get_user_locale( $user );
     107
     108        delete_user_meta( $user_id, 'locale' );
     109
     110        $user_locale2 = get_user_locale( $user );
     111
     112        $this->assertSame( 'es_ES', $user_locale1 );
     113        $this->assertSame( get_locale(), $user_locale2 );
     114    }
     115
     116    public function test_user_id_argument_with_nonexistent_user() {
     117        global $wpdb;
     118
     119        $user_id = $wpdb->get_var( "SELECT MAX(ID) FROM $wpdb->users" ) + 1;
     120
     121        $user_locale = get_user_locale( $user_id );
     122
     123        $this->assertSame( get_locale(), $user_locale );
     124    }
     125
     126    public function test_user_id_argument_with_invalid_type() {
     127        $user_locale = get_user_locale( 'string' );
     128        $this->assertSame( get_locale(), $user_locale );
     129    }
    78130}
Note: See TracChangeset for help on using the changeset viewer.