1--TEST--
2Test setlocale() function : usage variations - Setting all available locales in the platform
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) == 'WIN') {
6    die('skip Not valid for windows');
7}
8exec("locale -a", $output, $exit_code);
9if ($exit_code !== 0) {
10    die("skip locale -a not available");
11}
12?>
13--FILE--
14<?php
15/* setlocale() to set all available locales in the system and check the success count */
16echo "*** Testing setlocale() : usage variations ***\n";
17
18function good_locale($locale) {
19    /**
20    * Note: no_NO is a bogus locale and should not be used, see https://bugzilla.redhat.com/971416
21    **/
22    return $locale !== 'tt_RU@iqtelif.UTF-8' && $locale !== 'no_NO.ISO-8859-1';
23}
24
25function list_system_locales() {
26  // start the buffering of next command to internal output buffer
27  ob_start();
28
29  // run the command 'locale -a' to fetch all locales available in the system
30  system('locale -a');
31
32  // get the contents from the internal output buffer
33  $all_locales = ob_get_contents();
34
35  // fflush and end the output buffering to internal output buffer
36  ob_end_clean();
37
38  $system_locales = explode("\n", $all_locales);
39
40  // return all the locale found in the system, except for broken one
41  return array_filter($system_locales, 'good_locale');
42}
43
44// gather all the locales installed in the system
45$all_system_locales = list_system_locales();
46
47//try different locale names
48$failure_locale = array();
49$success_count = 0;
50
51echo "-- Test setlocale() with all available locale in the system --\n";
52// gather all locales installed in the system(stored $all_system_locales),
53// try n set each locale using setlocale() and keep track failures, if any
54foreach($all_system_locales as $value){
55  //set locale to $value, if success, count increments
56  if(setlocale(LC_ALL,$value )){
57   $success_count++;
58  }
59  else{
60   //failure values are put in to an array $failure_locale
61   $failure_locale[] = $value;
62  }
63}
64
65echo "No of locales found on the machine = ".count($all_system_locales)."\n";
66echo "No of setlocale() success = ".$success_count."\n";
67echo "Expected no of failures = 0\n";
68echo "Test ";
69// check if there were any failure of setlocale() function earlier, if any
70// failure then dump the list of failing locales
71if($success_count != count($all_system_locales)){
72  echo "FAILED\n";
73  echo "Names of locale() for which setlocale() failed ...\n";
74  var_dump($failure_locale);
75}
76else{
77  echo "PASSED\n";
78}
79
80echo "Done\n";
81?>
82--EXPECTF--
83*** Testing setlocale() : usage variations ***
84-- Test setlocale() with all available locale in the system --
85No of locales found on the machine = %d
86No of setlocale() success = %d
87Expected no of failures = 0
88Test PASSED
89Done
90