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