locale_test.module 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * @file
  4. * Mock module for locale layer tests.
  5. */
  6. /**
  7. * Implements hook_locale().
  8. */
  9. function locale_test_locale($op = 'groups') {
  10. switch ($op) {
  11. case 'groups':
  12. return array('custom' => t('Custom'));
  13. }
  14. }
  15. /**
  16. * Implements hook_boot().
  17. *
  18. * For testing domain language negotiation, we fake it by setting
  19. * the HTTP_HOST here
  20. */
  21. function locale_test_boot() {
  22. if (variable_get('locale_test_domain')) {
  23. $_SERVER['HTTP_HOST'] = variable_get('locale_test_domain');
  24. }
  25. }
  26. /**
  27. * Implements hook_init().
  28. */
  29. function locale_test_init() {
  30. locale_test_store_language_negotiation();
  31. if (isset($GLOBALS['language']) && isset($GLOBALS['language']->provider)) {
  32. drupal_set_message(t('Language negotiation provider: @name', array('@name' => $GLOBALS['language']->provider)));
  33. }
  34. }
  35. /**
  36. * Implements hook_language_types_info().
  37. */
  38. function locale_test_language_types_info() {
  39. if (variable_get('locale_test_language_types', FALSE)) {
  40. return array(
  41. 'test_language_type' => array(
  42. 'name' => t('Test'),
  43. 'description' => t('A test language type.'),
  44. ),
  45. 'fixed_test_language_type' => array(
  46. 'fixed' => array('test_language_provider'),
  47. ),
  48. );
  49. }
  50. }
  51. /**
  52. * Implements hook_menu().
  53. *
  54. * @return array
  55. */
  56. function locale_test_menu() {
  57. $items = array();
  58. $items['locale_test_plural_format_page'] = array(
  59. 'page callback' => 'locale_test_plural_format_page',
  60. 'access callback' => TRUE,
  61. 'type' => MENU_CALLBACK,
  62. );
  63. return $items;
  64. }
  65. /**
  66. * Implements hook_language_types_info_alter().
  67. */
  68. function locale_test_language_types_info_alter(array &$language_types) {
  69. if (variable_get('locale_test_content_language_type', FALSE)) {
  70. unset($language_types[LANGUAGE_TYPE_CONTENT]['fixed']);
  71. }
  72. }
  73. /**
  74. * Implements hook_language_negotiation_info().
  75. */
  76. function locale_test_language_negotiation_info() {
  77. if (variable_get('locale_test_language_negotiation_info', FALSE)) {
  78. $info = array(
  79. 'callbacks' => array(
  80. 'language' => 'locale_test_language_provider',
  81. ),
  82. 'file' => drupal_get_path('module', 'locale_test') .'/locale_test.module',
  83. 'weight' => -10,
  84. 'description' => t('This is a test language provider.'),
  85. );
  86. return array(
  87. 'test_language_provider' => array(
  88. 'name' => t('Test'),
  89. 'types' => array(LANGUAGE_TYPE_CONTENT, 'test_language_type', 'fixed_test_language_type'),
  90. ) + $info,
  91. 'test_language_provider_ts' => array(
  92. 'name' => t('Type-specific test'),
  93. 'types' => array('test_language_type'),
  94. ) + $info,
  95. );
  96. }
  97. }
  98. /**
  99. * Implements hook_language_negotiation_info_alter().
  100. */
  101. function locale_test_language_negotiation_info_alter(array &$language_providers) {
  102. if (variable_get('locale_test_language_negotiation_info_alter', FALSE)) {
  103. unset($language_providers[LOCALE_LANGUAGE_NEGOTIATION_INTERFACE]);
  104. }
  105. }
  106. /**
  107. * Store the last negotiated languages.
  108. */
  109. function locale_test_store_language_negotiation() {
  110. $last = array();
  111. foreach (language_types() as $type) {
  112. $last[$type] = $GLOBALS[$type]->language;
  113. }
  114. variable_set('locale_test_language_negotiation_last', $last);
  115. }
  116. /**
  117. * Test language provider.
  118. */
  119. function locale_test_language_provider($languages) {
  120. return 'it';
  121. }
  122. /**
  123. * Returns markup for locale_get_plural testing.
  124. *
  125. * @return array
  126. */
  127. function locale_test_plural_format_page() {
  128. $tests = _locale_test_plural_format_tests();
  129. $result = array();
  130. foreach ($tests as $test) {
  131. $string_param = array(
  132. '@lang' => $test['language'],
  133. '@locale_get_plural' => locale_get_plural($test['count'], $test['language'])
  134. );
  135. $result[] = array(
  136. '#prefix' => '<br/>',
  137. '#markup' => format_string('Language: @lang, locale_get_plural: @locale_get_plural.', $string_param),
  138. );
  139. }
  140. return $result;
  141. }
  142. /**
  143. * Helper function with list of test cases
  144. *
  145. * @return array
  146. */
  147. function _locale_test_plural_format_tests() {
  148. return array(
  149. // Test data for English (no formula present).
  150. array(
  151. 'count' => 1,
  152. 'language' => 'en',
  153. 'expected-result' => 0,
  154. ),
  155. array(
  156. 'count' => 0,
  157. 'language' => 'en',
  158. 'expected-result' => 1,
  159. ),
  160. array(
  161. 'count' => 5,
  162. 'language' => 'en',
  163. 'expected-result' => 1,
  164. ),
  165. // Test data for French (simpler formula).
  166. array(
  167. 'count' => 1,
  168. 'language' => 'fr',
  169. 'expected-result' => 0,
  170. ),
  171. array(
  172. 'count' => 0,
  173. 'language' => 'fr',
  174. 'expected-result' => 1,
  175. ),
  176. array(
  177. 'count' => 5,
  178. 'language' => 'fr',
  179. 'expected-result' => 1,
  180. ),
  181. // Test data for Croatian (more complex formula).
  182. array(
  183. 'count' => 1,
  184. 'language' => 'hr',
  185. 'expected-result' => 0,
  186. ),
  187. array(
  188. 'count' => 21,
  189. 'language' => 'hr',
  190. 'expected-result' => 0,
  191. ),
  192. array(
  193. 'count' => 0,
  194. 'language' => 'hr',
  195. 'expected-result' => 2,
  196. ),
  197. array(
  198. 'count' => 2,
  199. 'language' => 'hr',
  200. 'expected-result' => 1,
  201. ),
  202. array(
  203. 'count' => 8,
  204. 'language' => 'hr',
  205. 'expected-result' => 2,
  206. ),
  207. // Test data for Hungarian (nonexistent language).
  208. array(
  209. 'count' => 1,
  210. 'language' => 'hu',
  211. 'expected-result' => -1,
  212. ),
  213. array(
  214. 'count' => 21,
  215. 'language' => 'hu',
  216. 'expected-result' => -1,
  217. ),
  218. array(
  219. 'count' => 0,
  220. 'language' => 'hu',
  221. 'expected-result' => -1,
  222. ),
  223. );
  224. }