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