setlocale_variation2.phpt 2.6 KB

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