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
|
|
125
|
|
126
|
/**
|
127
|
* Create strings for all languages
|
128
|
*/
|
129
|
public static function stringCreateAll($number = 10, $length = 100) {
|
130
|
foreach (language_list() as $lang => $language) {
|
131
|
$strings[$lang] = self::stringCreateArray($number, $length);
|
132
|
}
|
133
|
return $strings;
|
134
|
}
|
135
|
/**
|
136
|
* Create a bunch of random strings to test the API
|
137
|
*/
|
138
|
public static function stringCreateArray($number = 10, $length = 100) {
|
139
|
for ($i=1 ; $i <= $number ; $i++) {
|
140
|
$strings[$i] = self::randomName($length);
|
141
|
}
|
142
|
return $strings;
|
143
|
}
|
144
|
/**
|
145
|
* Create and store one translation into the db
|
146
|
*/
|
147
|
public function stringCreateTranslation($name, $lang, $length = 20) {
|
148
|
$translation = $this->randomName($length);
|
149
|
if (self::stringSaveTranslation($name, $lang, $translation)) {
|
150
|
return $translation;
|
151
|
}
|
152
|
}
|
153
|
/**
|
154
|
* Translate one string into the db
|
155
|
*/
|
156
|
public static function stringSaveTranslation($name, $lang, $translation) {
|
157
|
list($textgroup, $context) = i18n_string_context($name);
|
158
|
return i18n_string_textgroup($textgroup)->update_translation($context, $lang, $translation);
|
159
|
}
|
160
|
}
|