Project

General

Profile

Paste
Download (8.4 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / i18n / i18n_string / i18n_string.test @ 01f36513

1
<?php
2

    
3
/**
4
 * @file
5
 * Test case for multilingual string
6
 */
7

    
8
/**
9
 * Class for testing i18n_string and modules using these features
10
 *
11
 * Tests basic API functions
12
 */
13

    
14

    
15
class i18nStringTestCase extends Drupali18nTestCase {
16

    
17
  public static function getInfo() {
18
    return array(
19
      'name' => 'String translation API',
20
      'group' => 'Internationalization',
21
      'description' => 'User defined strings translation functions'
22
    );
23
  }
24

    
25
  function setUp() {
26
    // We can use any of the modules that define a text group, to use it for testing
27
    parent::setUp('i18n_string', 'i18n_menu', 'i18n_test');
28
    parent::setUpLanguages();
29
    $this->translator = $this->drupalCreateUser(array('translate interface', 'translate user-defined strings'));
30
  }
31

    
32
  /**
33
   * Test base i18n_string API
34
   */
35
  function testStringsAPI() {
36
    // Create a bunch of strings for all languages
37
    $textgroup = 'menu';
38
    $strings = $this->stringCreateArray(2);
39
    $translations = array();
40
    // Save source strings and store translations
41
    foreach ($strings as $key => $string) {
42
      $name = "$textgroup:item:$key:title";
43
      i18n_string_update($name, $string);
44
      $translations[$key] = $this->createStringTranslation($textgroup, $string);
45
    }
46
    // Reset cache for text group
47
    i18n_string_textgroup($textgroup)->cache_reset();
48
    // Check translations using the API
49
    foreach ($this->getOtherLanguages() as $language) {
50
      foreach ($strings as $key => $value) {
51
        $name = "$textgroup:item:$key:title";
52
        $translation = i18n_string_translate($name, 'NOT FOUND', array('langcode' => $language->language));
53
        $this->assertEqual($translation, $translations[$key][$language->language], "The right $language->name ($language->language) translation has been retrieved for $name, $translation");
54
      }
55
    }
56

    
57
    // Test that regular strings can be translated. Use 'Built-in interface' as
58
    // filter, and translate first one.
59
    $search = array(
60
      'language' => 'all',
61
      'translation' => 'all',
62
      'group' => 'default',
63
      'string' => '',
64
    );
65
    $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
66
    $this->clickLink(t('edit'));
67
    // Just add a random translation.
68
    $translation = $this->randomName();
69
    $edit = array();
70
    foreach ($this->getOtherLanguages() as $language) {
71
      $langcode = $language->language;
72
      $edit["translations[$langcode]"] = $translation;
73
    }
74
    $this->drupalPost(NULL, $edit, t('Save translations'));
75
    $this->assertText(t('The string has been saved.'), t('The string has been saved.'));
76
    $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
77

    
78
  }
79

    
80
  /**
81
   * Test base i18n_string caching.
82
   */
83
  function testCaching() {
84
    // Create a bunch of strings for all languages.
85
    $textgroup = 'test_cached';
86
    $strings = $this->stringCreateArray(2);
87
    $translations = array();
88
    $textgroup_object = i18n_string_textgroup($textgroup);
89
    // Save source strings and store translations.
90
    foreach ($strings as $key => $string) {
91
      $name = "$textgroup:item:$key:title";
92
      i18n_string_update($name, $string);
93
      $translations[$key] = $this->createStringTranslation($textgroup, $string);
94
    }
95

    
96
    // Now fetch the strings to fill the cache.
97
    foreach ($textgroup_object->strings as $context => $string_object) {
98
      $this->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
99
    }
100
    foreach ($strings as $key => $string) {
101
      $this->drupalGet('tests/i18n/i18n_string_translation_search/' . $textgroup . ':item:' . $key . ':*/es');
102
    }
103

    
104
    // Check the persistent cache for contents.
105
    $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':strings');
106
    if ($this->assertNotEqual($cache, FALSE, 'Textgroup strings cache found')) {
107
      foreach ($textgroup_object->strings as $context => $string_object) {
108
        if ($this->assertTrue(isset($cache->data[$context]), format_string('Cached string %context found', array('%context' => $context)))) {
109
          $this->assertEqual($cache->data[$context], $context, 'Cached string is a string and not an object');
110
        }
111
        // Check if the string object cache is also available.
112
        $string_cache = cache_get($string_object->get_cid());
113
        if ($this->assertNotEqual($string_cache, FALSE, format_string('Cached string object %cid found', array('%cid' => $string_object->get_cid())))) {
114
          $this->assertTrue(is_array($string_cache->data), 'Cached string object is an array.');
115
        }
116
      }
117
    }
118
    $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':cache_multiple');
119
    if ($this->assertNotEqual($cache, FALSE, 'Textgroup cache_multiple cache found')) {
120
      foreach ($strings as $key => $string) {
121
        $pattern = 'item:' . $key . ':*:es';
122
        if ($this->assertTrue(isset($cache->data[$pattern]), format_string('Cached multiple cache for pattern %pattern found', array('%pattern_key' => $pattern)))) {
123
          $property_pattern = 'item:' . $key . ':title';
124
          if ($this->assertTrue(isset($cache->data[$pattern][$property_pattern]), format_string('Cached multiple property title found', array('%pattern_key' => $pattern)))) {
125
            $this->assertEqual($cache->data[$pattern][$property_pattern], $property_pattern);
126
          }
127
        }
128
      }
129
    }
130

    
131
    // Test cache injection.
132
    foreach ($textgroup_object->strings as $context => $string_object) {
133
      // Check if the string object cache is also available.
134
      $string_cache = cache_get($string_object->get_cid());
135
      if (isset($string_cache->data)) {
136
        // Modify cache.
137
        $string_cache->data['string'] = "Injected value.";
138
        cache_set($string_object->get_cid(), $string_cache->data, 'cache', CACHE_TEMPORARY);
139

    
140
        // Check if modification is reflected on the next page call.
141
        $this->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
142
        $this->assertText($string_cache->data['string']);
143
      }
144
    }
145

    
146
    // Test that un-translated strings are cached correctly.
147
    $textgroup = 'test_cached';
148
    $key = 3;
149
    $string = self::randomName(100);
150
    $name = "$textgroup:item:$key:title";
151
    i18n_string_update($name, $string);
152

    
153
    // Generate the cache entry.
154
    $string_object = i18n_string_build($name, $string);
155
    $langcode = i18n_langcode();
156
    $string_object->get_translation($langcode);
157

    
158
    // Destroy the textgroup object to write the cache entry.
159
    $textgroup_object = i18n_string_textgroup($textgroup);
160
    $textgroup_object->__destruct();
161
    $this->assertTrue(cache_get($string_object->get_cid()) !== FALSE, "Cache entry created.");
162
    drupal_static_reset('i18n_string_textgroup');
163

    
164
    // Reset the loaded translation variable.
165
    variable_del('i18n_loaded_translations');
166
    $loaded_translations = variable_get('i18n_loaded_translations', array());
167
    $this->verbose(var_export($loaded_translations, TRUE));
168

    
169
    // Rebuild the string.
170
    $string_object = i18n_string_build($name, $string);
171
    $string_object->get_translation($langcode);
172

    
173
    // Check that the string hasn't been loaded.
174
    $loaded_translations = variable_get('i18n_loaded_translations', array());
175
    $this->verbose(var_export($loaded_translations, TRUE));
176
    $this->assertFalse(isset($loaded_translations['test_cached:item:3:title']), "The untranslated string was correctly cached.");
177
  }
178

    
179

    
180
  /**
181
   * Create strings for all languages
182
   */
183
  public static function stringCreateAll($number = 10, $length = 100) {
184
    foreach (language_list() as $lang => $language) {
185
      $strings[$lang] = self::stringCreateArray($number, $length);
186
    }
187
    return $strings;
188
  }
189
  /**
190
   * Create a bunch of random strings to test the API
191
   */
192
  public static function stringCreateArray($number = 10, $length = 100) {
193
    for ($i=1 ; $i <= $number ; $i++) {
194
      $strings[$i] = self::randomName($length);
195
    }
196
    return $strings;
197
  }
198
  /**
199
   * Create and store one translation into the db
200
   */
201
  public function stringCreateTranslation($name, $lang, $length = 20) {
202
    $translation = $this->randomName($length);
203
    if (self::stringSaveTranslation($name, $lang, $translation)) {
204
      return $translation;
205
    }
206
  }
207
  /**
208
   * Translate one string into the db
209
   */
210
  public static function stringSaveTranslation($name, $lang, $translation) {
211
    list($textgroup, $context) = i18n_string_context($name);
212
    return i18n_string_textgroup($textgroup)->update_translation($context, $lang, $translation);
213
  }
214
}