Projet

Général

Profil

Paste
Télécharger (7,49 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_string / i18n_string.test @ e4c061ad

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

    
58
  /**
59
   * Test base i18n_string caching.
60
   */
61
  function testCaching() {
62
    // Create a bunch of strings for all languages.
63
    $textgroup = 'test_cached';
64
    $strings = $this->stringCreateArray(2);
65
    $translations = array();
66
    $textgroup_object = i18n_string_textgroup($textgroup);
67
    // Save source strings and store translations.
68
    foreach ($strings as $key => $string) {
69
      $name = "$textgroup:item:$key:title";
70
      i18n_string_update($name, $string);
71
      $translations[$key] = $this->createStringTranslation($textgroup, $string);
72
    }
73

    
74
    // Now fetch the strings to fill the cache.
75
    foreach ($textgroup_object->strings as $context => $string_object) {
76
      $this->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
77
    }
78
    foreach ($strings as $key => $string) {
79
      $this->drupalGet('tests/i18n/i18n_string_translation_search/' . $textgroup . ':item:' . $key . ':*/es');
80
    }
81

    
82
    // Check the persistent cache for contents.
83
    $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':strings');
84
    if ($this->assertNotEqual($cache, FALSE, 'Textgroup strings cache found')) {
85
      foreach ($textgroup_object->strings as $context => $string_object) {
86
        if ($this->assertTrue(isset($cache->data[$context]), format_string('Cached string %context found', array('%context' => $context)))) {
87
          $this->assertEqual($cache->data[$context], $context, 'Cached string is a string and not an object');
88
        }
89
        // Check if the string object cache is also available.
90
        $string_cache = cache_get($string_object->get_cid());
91
        if ($this->assertNotEqual($string_cache, FALSE, format_string('Cached string object %cid found', array('%cid' => $string_object->get_cid())))) {
92
          $this->assertTrue(is_array($string_cache->data), 'Cached string object is an array.');
93
        }
94
      }
95
    }
96
    $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':cache_multiple');
97
    if ($this->assertNotEqual($cache, FALSE, 'Textgroup cache_multiple cache found')) {
98
      foreach ($strings as $key => $string) {
99
        $pattern = 'item:' . $key . ':*:es';
100
        if ($this->assertTrue(isset($cache->data[$pattern]), format_string('Cached multiple cache for pattern %pattern found', array('%pattern_key' => $pattern)))) {
101
          $property_pattern = 'item:' . $key . ':title';
102
          if ($this->assertTrue(isset($cache->data[$pattern][$property_pattern]), format_string('Cached multiple property title found', array('%pattern_key' => $pattern)))) {
103
            $this->assertEqual($cache->data[$pattern][$property_pattern], $property_pattern);
104
          }
105
        }
106
      }
107
    }
108

    
109
    // Test cache injection.
110
    foreach ($textgroup_object->strings as $context => $string_object) {
111
      // Check if the string object cache is also available.
112
      $string_cache = cache_get($string_object->get_cid());
113
      if (isset($string_cache->data)) {
114
        // Modify cache.
115
        $string_cache->data['string'] = "Injected value.";
116
        cache_set($string_object->get_cid(), $string_cache->data, 'cache', CACHE_TEMPORARY);
117

    
118
        // Check if modification is reflected on the next page call.
119
        $this->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
120
        $this->assertText($string_cache->data['string']);
121
      }
122
    }
123

    
124
    // Test that un-translated strings are cached correctly.
125
    $textgroup = 'test_cached';
126
    $key = 3;
127
    $string = self::randomName(100);
128
    $name = "$textgroup:item:$key:title";
129
    i18n_string_update($name, $string);
130

    
131
    // Generate the cache entry.
132
    $string_object = i18n_string_build($name, $string);
133
    $langcode = i18n_langcode();
134
    $string_object->get_translation($langcode);
135

    
136
    // Destroy the textgroup object to write the cache entry.
137
    $textgroup_object = i18n_string_textgroup($textgroup);
138
    $textgroup_object->__destruct();
139
    $this->assertTrue(cache_get($string_object->get_cid()) !== FALSE, "Cache entry created.");
140
    drupal_static_reset('i18n_string_textgroup');
141

    
142
    // Reset the loaded translation variable.
143
    variable_del('i18n_loaded_translations');
144
    $loaded_translations = variable_get('i18n_loaded_translations', array());
145
    $this->verbose(var_export($loaded_translations, TRUE));
146

    
147
    // Rebuild the string.
148
    $string_object = i18n_string_build($name, $string);
149
    $string_object->get_translation($langcode);
150

    
151
    // Check that the string hasn't been loaded.
152
    $loaded_translations = variable_get('i18n_loaded_translations', array());
153
    $this->verbose(var_export($loaded_translations, TRUE));
154
    $this->assertFalse(isset($loaded_translations['test_cached:item:3:title']), "The untranslated string was correctly cached.");
155
  }
156

    
157

    
158
  /**
159
   * Create strings for all languages
160
   */
161
  public static function stringCreateAll($number = 10, $length = 100) {
162
    foreach (language_list() as $lang => $language) {
163
      $strings[$lang] = self::stringCreateArray($number, $length);
164
    }
165
    return $strings;
166
  }
167
  /**
168
   * Create a bunch of random strings to test the API
169
   */
170
  public static function stringCreateArray($number = 10, $length = 100) {
171
    for ($i=1 ; $i <= $number ; $i++) {
172
      $strings[$i] = self::randomName($length);
173
    }
174
    return $strings;
175
  }
176
  /**
177
   * Create and store one translation into the db
178
   */
179
  public function stringCreateTranslation($name, $lang, $length = 20) {
180
    $translation = $this->randomName($length);
181
    if (self::stringSaveTranslation($name, $lang, $translation)) {
182
      return $translation;
183
    }
184
  }
185
  /**
186
   * Translate one string into the db
187
   */
188
  public static function stringSaveTranslation($name, $lang, $translation) {
189
    list($textgroup, $context) = i18n_string_context($name);
190
    return i18n_string_textgroup($textgroup)->update_translation($context, $lang, $translation);
191
  }
192
}