|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @group l10n |
| 5 | + * @group i18n |
| 6 | + * |
| 7 | + * @covers ::determine_locale |
| 8 | + */ |
| 9 | +class Tests_L10n_DetermineLocale extends WP_UnitTestCase { |
| 10 | + protected $locale; |
| 11 | + protected static $user_id; |
| 12 | + |
| 13 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 14 | + self::$user_id = $factory->user->create( |
| 15 | + array( |
| 16 | + 'role' => 'administrator', |
| 17 | + 'locale' => 'userLocale', |
| 18 | + ) |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + public function tear_down() { |
| 23 | + unset( $_SERVER['CONTENT_TYPE'], $_GET['_locale'], $_COOKIE['wp_lang'], $GLOBALS['pagenow'] ); |
| 24 | + |
| 25 | + parent::tear_down(); |
| 26 | + } |
| 27 | + |
| 28 | + public function test_short_circuit_empty() { |
| 29 | + add_filter( 'pre_determine_locale', '__return_false' ); |
| 30 | + $this->assertNotFalse( determine_locale() ); |
| 31 | + } |
| 32 | + |
| 33 | + public function test_short_circuit_no_string() { |
| 34 | + add_filter( |
| 35 | + 'pre_determine_locale', |
| 36 | + static function() { |
| 37 | + return 1234; |
| 38 | + } |
| 39 | + ); |
| 40 | + $this->assertNotFalse( determine_locale() ); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_short_circuit_string() { |
| 44 | + add_filter( |
| 45 | + 'pre_determine_locale', |
| 46 | + static function() { |
| 47 | + return 'myNewLocale'; |
| 48 | + } |
| 49 | + ); |
| 50 | + $this->assertSame( 'myNewLocale', determine_locale() ); |
| 51 | + } |
| 52 | + |
| 53 | + public function test_defaults_to_site_locale() { |
| 54 | + add_filter( |
| 55 | + 'locale', |
| 56 | + static function() { |
| 57 | + return 'siteLocale'; |
| 58 | + } |
| 59 | + ); |
| 60 | + |
| 61 | + $this->assertSame( get_locale(), determine_locale() ); |
| 62 | + } |
| 63 | + |
| 64 | + public function test_is_admin_no_user() { |
| 65 | + add_filter( |
| 66 | + 'locale', |
| 67 | + static function() { |
| 68 | + return 'siteLocale'; |
| 69 | + } |
| 70 | + ); |
| 71 | + |
| 72 | + set_current_screen( 'dashboard' ); |
| 73 | + |
| 74 | + $this->assertSame( 'siteLocale', determine_locale() ); |
| 75 | + } |
| 76 | + |
| 77 | + public function test_is_admin_user_locale() { |
| 78 | + add_filter( |
| 79 | + 'locale', |
| 80 | + static function() { |
| 81 | + return 'siteLocale'; |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + set_current_screen( 'dashboard' ); |
| 86 | + wp_set_current_user( self::$user_id ); |
| 87 | + |
| 88 | + $this->assertSame( 'userLocale', determine_locale() ); |
| 89 | + } |
| 90 | + |
| 91 | + public function test_json_request_user_locale() { |
| 92 | + add_filter( |
| 93 | + 'locale', |
| 94 | + static function() { |
| 95 | + return 'siteLocale'; |
| 96 | + } |
| 97 | + ); |
| 98 | + |
| 99 | + wp_set_current_user( self::$user_id ); |
| 100 | + |
| 101 | + $_SERVER['CONTENT_TYPE'] = 'application/json'; |
| 102 | + $_GET['_locale'] = 'user'; |
| 103 | + |
| 104 | + $this->assertSame( 'userLocale', determine_locale() ); |
| 105 | + } |
| 106 | + |
| 107 | + public function test_json_request_user_locale_no_user() { |
| 108 | + add_filter( |
| 109 | + 'locale', |
| 110 | + static function() { |
| 111 | + return 'siteLocale'; |
| 112 | + } |
| 113 | + ); |
| 114 | + |
| 115 | + $_SERVER['CONTENT_TYPE'] = 'application/json'; |
| 116 | + $_GET['_locale'] = 'user'; |
| 117 | + |
| 118 | + $this->assertSame( 'siteLocale', determine_locale() ); |
| 119 | + } |
| 120 | + |
| 121 | + public function test_json_request_missing_get_param() { |
| 122 | + add_filter( |
| 123 | + 'locale', |
| 124 | + static function() { |
| 125 | + return 'siteLocale'; |
| 126 | + } |
| 127 | + ); |
| 128 | + |
| 129 | + wp_set_current_user( self::$user_id ); |
| 130 | + |
| 131 | + $_SERVER['CONTENT_TYPE'] = 'application/json'; |
| 132 | + |
| 133 | + $this->assertSame( 'siteLocale', determine_locale() ); |
| 134 | + } |
| 135 | + |
| 136 | + public function test_json_request_incorrect_get_param() { |
| 137 | + add_filter( |
| 138 | + 'locale', |
| 139 | + static function() { |
| 140 | + return 'siteLocale'; |
| 141 | + } |
| 142 | + ); |
| 143 | + |
| 144 | + wp_set_current_user( self::$user_id ); |
| 145 | + |
| 146 | + $_SERVER['CONTENT_TYPE'] = 'application/json'; |
| 147 | + $_GET['_locale'] = 'foo'; |
| 148 | + |
| 149 | + $this->assertSame( 'siteLocale', determine_locale() ); |
| 150 | + } |
| 151 | + |
| 152 | + public function test_get_param_but_no_json_request() { |
| 153 | + add_filter( |
| 154 | + 'locale', |
| 155 | + static function() { |
| 156 | + return 'siteLocale'; |
| 157 | + } |
| 158 | + ); |
| 159 | + |
| 160 | + wp_set_current_user( self::$user_id ); |
| 161 | + |
| 162 | + $_GET['_locale'] = 'user'; |
| 163 | + |
| 164 | + $this->assertSame( 'siteLocale', determine_locale() ); |
| 165 | + } |
| 166 | + |
| 167 | + public function test_wp_login_get_param_not_on_login_page() { |
| 168 | + add_filter( |
| 169 | + 'locale', |
| 170 | + static function() { |
| 171 | + return 'siteLocale'; |
| 172 | + } |
| 173 | + ); |
| 174 | + |
| 175 | + wp_set_current_user( self::$user_id ); |
| 176 | + |
| 177 | + $_GET['wp_lang'] = 'de_DE'; |
| 178 | + |
| 179 | + $this->assertSame( 'siteLocale', determine_locale() ); |
| 180 | + } |
| 181 | + |
| 182 | + public function test_wp_login_get_param_on_login_page() { |
| 183 | + add_filter( |
| 184 | + 'locale', |
| 185 | + static function() { |
| 186 | + return 'siteLocale'; |
| 187 | + } |
| 188 | + ); |
| 189 | + |
| 190 | + wp_set_current_user( self::$user_id ); |
| 191 | + |
| 192 | + $GLOBALS['pagenow'] = 'wp-login.php'; |
| 193 | + $_GET['wp_lang'] = 'de_DE'; |
| 194 | + |
| 195 | + $this->assertSame( 'de_DE', determine_locale() ); |
| 196 | + } |
| 197 | + |
| 198 | + public function test_wp_login_get_param_on_login_page_empty_string() { |
| 199 | + add_filter( |
| 200 | + 'locale', |
| 201 | + static function() { |
| 202 | + return 'siteLocale'; |
| 203 | + } |
| 204 | + ); |
| 205 | + |
| 206 | + wp_set_current_user( self::$user_id ); |
| 207 | + |
| 208 | + $GLOBALS['pagenow'] = 'wp-login.php'; |
| 209 | + $_GET['wp_lang'] = ''; |
| 210 | + |
| 211 | + $this->assertSame( 'siteLocale', determine_locale() ); |
| 212 | + } |
| 213 | + |
| 214 | + public function test_wp_login_cookie_not_on_login_page() { |
| 215 | + add_filter( |
| 216 | + 'locale', |
| 217 | + static function() { |
| 218 | + return 'siteLocale'; |
| 219 | + } |
| 220 | + ); |
| 221 | + |
| 222 | + wp_set_current_user( self::$user_id ); |
| 223 | + |
| 224 | + $_COOKIE['wp_lang'] = 'de_DE'; |
| 225 | + |
| 226 | + $this->assertSame( 'siteLocale', determine_locale() ); |
| 227 | + } |
| 228 | + |
| 229 | + public function test_wp_login_cookie_on_login_page() { |
| 230 | + add_filter( |
| 231 | + 'locale', |
| 232 | + static function() { |
| 233 | + return 'siteLocale'; |
| 234 | + } |
| 235 | + ); |
| 236 | + |
| 237 | + wp_set_current_user( self::$user_id ); |
| 238 | + |
| 239 | + $GLOBALS['pagenow'] = 'wp-login.php'; |
| 240 | + $_COOKIE['wp_lang'] = 'de_DE'; |
| 241 | + |
| 242 | + $this->assertSame( 'de_DE', determine_locale() ); |
| 243 | + } |
| 244 | + |
| 245 | + public function test_wp_login_cookie_on_login_page_empty_string() { |
| 246 | + add_filter( |
| 247 | + 'locale', |
| 248 | + static function() { |
| 249 | + return 'siteLocale'; |
| 250 | + } |
| 251 | + ); |
| 252 | + |
| 253 | + wp_set_current_user( self::$user_id ); |
| 254 | + |
| 255 | + $GLOBALS['pagenow'] = 'wp-login.php'; |
| 256 | + $_COOKIE['wp_lang'] = ''; |
| 257 | + |
| 258 | + $this->assertSame( 'siteLocale', determine_locale() ); |
| 259 | + } |
| 260 | +} |
0 commit comments