Projet

Général

Profil

Paste
Télécharger (2,87 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_string / i18n_string.test @ 74f6bef0

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');
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
   * Create strings for all languages
60
   */
61
  public static function stringCreateAll($number = 10, $length = 100) {
62
    foreach (language_list() as $lang => $language) {
63
      $strings[$lang] = self::stringCreateArray($number, $length);
64
    }
65
    return $strings;
66
  }
67
  /**
68
   * Create a bunch of random strings to test the API
69
   */
70
  public static function stringCreateArray($number = 10, $length = 100) {
71
    for ($i=1 ; $i <= $number ; $i++) {
72
      $strings[$i] = self::randomName($length);
73
    }
74
    return $strings;
75
  }
76
  /**
77
   * Create and store one translation into the db
78
   */
79
  public static function stringCreateTranslation($name, $lang, $length = 20) {
80
    $translation = $this->randomName($length);
81
    if (self::stringSaveTranslation($name, $lang, $translation)) {
82
      return $translation;
83
    }
84
  }
85
  /**
86
   * Translate one string into the db
87
   */
88
  public static function stringSaveTranslation($name, $lang, $translation) {
89
    list($textgroup, $context) = i18n_string_context($name);
90
    return i18n_string_textgroup($textgroup)->update_translation($context, $lang, $translation);
91
  }
92
}