summaryrefslogtreecommitdiff
path: root/modules/locale/locale.test
blob: 45a014755e53f59ef2f1eaeb42d44a91e4d3a32b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
// $Id$

class LocaleTestCase extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('String translate'),
      'description' => 'Adds a new locale and translates its name',
      'group' => 'Locale',
    );
  }

  function setUp() {
    parent::setUp('locale');
  }

  function testlocaleModuleTest() {
    global $base_url;

    // User to add and remove language.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
    // User to translate and delete string.
    $translate_user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
    // Code for the language.
    $langcode = str_replace('simpletest_', 'si-', $this->randomName(6));
    // The English name for the language. This will be translated.
    $name = $this->randomName(16);
    // The native name for the language.
    $native = $this->randomName(16);
    // The domain prefix. Not tested yet.
    $prefix = strtolower(str_replace('si-', '', $langcode));
    // This is the language indicator on the translation search screen for
    // untranslated strings. Copied straight from locale.inc.
    $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
    // This will be the translation of $name.
    $translation = $this->randomName(16);

    // Add language.
    $this->drupalLogin($admin_user);
    $edit = array (
      'langcode' => $langcode,
      'name' => $name,
      'native' => $native,
      'prefix' => $prefix,
      'direction' => '0',
    );
    $this->drupalPost('admin/settings/language/add', $edit, t('Add custom language'));
    // Add string.
    t($name, array(), $langcode);
    // Reset locale cache.
    locale(NULL, NULL, TRUE);
    $this->assertText($langcode, 'Language code found');
    $this->assertText($name, 'Name found');
    $this->assertText($native, 'Native found');
    // No t() here, we do not want to add this string to the database and it's
    // surely not translated yet.
    $this->assertText($native, 'Test language added');
    $this->drupalGet('logout');

    // Search for the name and translate it.
    $this->drupalLogin($translate_user);
    $search = array (
      'string' => $name,
      'language' => 'all',
      'translation' => 'all',
      'group' => 'all',
    );
    $this->drupalPost('admin/build/translate/search', $search, t('Search'));
    // assertText seems to remove the input field where $name always could be
    // found, so this is not a false assert. See how assertNoText succeeds
    // later.
    $this->assertText($name, 'Search found the name');
    $this->assertRaw($language_indicator, 'Name is untranslated');
    // It's presumed that this is the only result. Given the random name, it's
    // reasonable.
    $this->clickLink(t('edit'));
    // We save the lid from the path.
    $lid = preg_replace('/\D/', '', substr($this->getUrl(), strlen($base_url)));
    // No t() here, it's surely not translated yet.
    $this->assertText($name, 'name found on edit screen');
    $edit = array (
      "translations[$langcode]" => $translation,
    );
    $this->drupalPost(NULL, $edit, t('Save translations'));
    $this->assertText(t('The string has been saved.'), 'The string has been saved.');
    $this->assertTrue($name != $translation && t($name, array(), $langcode) == $translation, 't() works');
    $this->drupalPost('admin/build/translate/search', $search, t('Search'));
    // The indicator should not be here.
    $this->assertNoRaw($language_indicator, 'String is translated');
    $this->drupalGet('logout');

    // Delete the language.
    $this->drupalLogin($admin_user);
    $path = 'admin/settings/language/delete/' . $langcode;
    // This a confirm form, we do not need any fields changed.
    $this->drupalPost($path, array(), t('Delete'));
    // We need raw here because %locale will add HTML.
    $this->assertRaw(t('The language %locale has been removed.', array('%locale' => $name)), 'The test language has been removed.');
    // Reload to remove $name.
    $this->drupalGet($path);
    $this->assertNoText($langcode, 'Language code not found');
    $this->assertNoText($name, 'Name not found');
    $this->assertNoText($native, 'Native not found');
    $this->drupalGet('logout');

    // Delete the name string.
    $this->drupalLogin($translate_user);
    $this->drupalPost('admin/build/translate/delete/' . $lid, array(), t('Delete'));
    $this->assertText(t('The string has been removed.'), 'The string has been removed message.');
    $this->drupalPost('admin/build/translate/search', $search, t('Search'));
    $this->assertNoText($name, 'Search now can not find the name');
  }
}

/**
 * Functional tests for the import of translation files.
 */
class LocaleImportFunctionalTest extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Translation import'),
      'description' => t('Tests the importation of locale files.'),
      'group' => t('Locale'),
    );
  }

  /**
   * A user able to create languages and import translations.
   */
  protected $admin_user = NULL;

  function setUp() {
    parent::setUp('locale');

    $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Test importation of standalone .po files.
   */
  function testStandalonePoFile() {
    // Try importing a .po file.
    $name = tempnam(file_directory_temp(), "po_");
    file_put_contents($name, $this->getPoFile());
    $this->drupalPost('admin/build/translate/import', array(
      'langcode' => 'fr',
      'files[file]' => $name,
    ), t('Import'));
    unlink($name);

    // The importation should automatically create the corresponding language.
    $this->assertRaw(t('The language %language has been created.', array('%language' => 'French')), t('The language has been automatically created'));

    // The importation should have create 7 strings.
    $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 7, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported'));
  }

  /**
   * Helper function that returns a proper .po file.
   */
  function getPoFile() {
    return <<< EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 6\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"

msgid "Monday"
msgstr "lundi"

msgid "Tuesday"
msgstr "mardi"

msgid "Wednesday"
msgstr "mercredi"

msgid "Thursday"
msgstr "jeudi"

msgid "Friday"
msgstr "vendredi"

msgid "Saturday"
msgstr "samedi"

msgid "Sunday"
msgstr "dimanche"
EOF;
  }
}