1--TEST--
2Test setlocale() function : usage variations - passing multiple valid/invalid locales as argument
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) == 'WIN') {
6    die('skip Not valid for windows');
7}
8if (setlocale(LC_ALL, "en_US.utf8", "Ko_KR.utf8", "zh_CN.utf8") === false) {
9    die('skip en_US.utf8/Ko_KR.utf8/zh_CN.utf8 locales not available');
10}
11?>
12--FILE--
13<?php
14/* Test the setlocale() when multiple valid/invalid locales are provided as argument */
15
16function list_system_locales() {
17  // start the buffering of next command to internal output buffer
18  ob_start();
19
20  // run the command 'locale -a' to fetch all locales available in the system
21  system('locale -a');
22
23  // get the contents from the internal output buffer
24  $all_locales = ob_get_contents();
25
26  // fflush and end the output buffering to internal output buffer
27  ob_end_clean();
28
29  $system_locales = explode("\n", $all_locales);
30
31  // return all the locale found in the system
32  return $system_locales;
33}
34
35/* Collect existing system locales and get 2 valid locales that can be use to
36   pass as argument to setlocale(), pass 2 invalid arguments along with two valid arguments*/
37echo "*** Testing setlocale() by passing multiple valid/invalid locales as argument ***\n";
38
39//set of currency symbol according to above list of locales
40$currency_symbol = array(
41  "en_US.utf8" => "USD",
42  "en_AU.utf8" => "AUD",
43  "ko_KR.utf8" => "KRW",
44  "zh_CN.utf8" => "CNY",
45  "de_DE.utf8" => "EUR",
46  "es_EC.utf8" => "USD",
47  "fr_FR.utf8" => "EUR",
48  "ja_JP.utf8" => "JPY",
49  "el_GR.utf8" => "EUR",
50  "nl_NL.utf8" =>"EUR"
51);
52
53// gather all the locales installed in the system
54$all_system_locales = list_system_locales();
55
56// Now check for three locales that is present in the system and use that as argument to setlocale()
57if( in_array("en_US.utf8",$all_system_locales) ||
58    in_array("Ko_KR.utf8",$all_system_locales) ||
59    in_array("zh_CN.utf8",$all_system_locales) ) {
60  echo "-- Testing setlocale() by giving 'category' as LC_ALL & multiple locales(en_US.invalid, en_US.utf8, Ko_KR.utf8, KO_KR.invalid, zh_CN.utf8) --\n";
61
62  // call setlocale()
63  $new_locale = setlocale(LC_ALL, "en_US.invalid", "en_US.utf8", "Ko_KR.utf8", "KO_KR.invalid", "zh_CN.utf8");
64
65  // dump the name of the new locale set by setlocale()
66  var_dump($new_locale);
67
68  // check that new locale setting is effective
69  // use localeconv() to get the details of currently set locale
70  $locale_info = localeconv();
71  $new_currency = trim($locale_info['int_curr_symbol']);
72
73  echo "Checking currency settings in the new locale, expected: ".$currency_symbol[$new_locale].", Found: ".$new_currency."\n";
74  echo "Test ";
75  if( trim($currency_symbol[$new_locale]) == $new_currency) {
76    echo "PASSED.\n";
77  } else {
78    echo "FAILED.\n";
79  }
80}
81
82echo "Done\n";
83?>
84--EXPECTF--
85*** Testing setlocale() by passing multiple valid/invalid locales as argument ***
86-- Testing setlocale() by giving 'category' as LC_ALL & multiple locales(en_US.invalid, en_US.utf8, Ko_KR.utf8, KO_KR.invalid, zh_CN.utf8) --
87string(%d) "%s"
88Checking currency settings in the new locale, expected: %s, Found: %s
89Test PASSED.
90Done
91