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