LocaleFileImportStatus.php 6.29 KB
Newer Older
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
<?php

/**
 * @file
 * Definition of Drupal\locale\Tests\LocaleFileImportStatus.
 */

namespace Drupal\locale\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Functional tests for the import of translation files.
 */
class LocaleFileImportStatus extends WebTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Translation file import status',
      'description' => 'Tests the status of imported translation files.',
      'group' => 'Locale',
    );
  }

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

    // Create and login user.
    $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages'));
    $this->drupalLogin($admin_user);

    // Set the translation file directory.
    variable_set('locale_translate_file_directory', drupal_get_path('module', 'locale') . '/tests');
  }

  /**
   * Add a language.
   *
   * @param $langcode
   *   The language of the langcode to add.
   */
  function addLanguage($langcode) {
    $edit = array('predefined_langcode' => $langcode);
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
    drupal_static_reset('language_list');
    $this->assertTrue(language_load($langcode), t('Language %langcode added.', array('%langcode' => $langcode)));
  }

  /**
49
   * Get translations for an array of strings.
50
51
   *
   * @param $strings
52
   *   An array of strings to translate.
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
   * @param $langcode
   *   The language code of the language to translate to.
   */
  function checkTranslations($strings, $langcode) {
    foreach ($strings as $source => $translation) {
      $db_translation = db_query('SELECT translation FROM {locales_target} lt INNER JOIN {locales_source} ls ON ls.lid = lt.lid WHERE ls.source = :source AND lt.language = :langcode', array(':source' => $source, ':langcode' => $langcode))->fetchField();
      $this->assertEqual((string) $translation, (string) $db_translation);
    }
  }

  /**
   * Import a single interface translation file.
   *
   * @param $langcode
   *   Langcode of the po file and language to import.
   * @param int $timestamp_difference
   *  (optional) Timestamp offset, used to mock older or newer files.
   *
   * @return stdClass
   *   A file object of type stdClass.
   */
  function mockImportedPoFile($langcode, $timestamp_difference = 0) {
    $dir = variable_get('locale_translate_file_directory', drupal_get_path('module', 'locale') . '/tests');
    $testfile_uri = $dir . '/test.' . $langcode . '.po';

    $file = locale_translate_file_create($testfile_uri);
    $file->original_timestamp = $file->timestamp;
    $file->timestamp = $file->timestamp + $timestamp_difference;
    $file->langcode = $langcode;

    // Fill the {locale_file} with a custom timestamp.
    if ($timestamp_difference != 0) {
      locale_translate_update_file_history($file);
    }

    $count = db_query('SELECT COUNT(*) FROM {locale_file} WHERE langcode = :langcode', array(':langcode' => $langcode))->fetchField();
    $this->assertEqual(1, $count, format_plural($count, '@count file registered in {locale_file}.', '@count files registered in {locale_file}.'));

    $result = db_query('SELECT langcode, uri FROM {locale_file}')->fetchAssoc();
    $this->assertEqual($result['uri'], $testfile_uri, t('%uri is in {locale_file}.', array('%uri' => $result['uri'])));
    $this->assertEqual($result['langcode'], $langcode, t('Langcode is %langcode.', array('%langcode' => $langcode)));
    return $file;
  }

  /**
   * Test the basic bulk import functionality.
   */
  function testBulkImport() {
    $langcode = 'de';

    // Translations should not exist.
    $strings = array(
      'Monday' => '',
      'Tuesday' => '',
    );
    $this->checkTranslations($strings, $langcode);

    // Add language.
    $this->addLanguage($langcode);

    // The file was imported, translations should exist.
    $strings = array(
      'Monday' => 'Montag',
      'Tuesday' => 'Dienstag',
    );
    $this->checkTranslations($strings, $langcode);
  }

  /**
   * Update a pre-existing file.
   */
  function testBulkImportUpdateExisting() {
    $langcode = 'de';

    // Translations should not exist.
    $strings = array(
      'Monday' => '',
      'Tuesday' => '',
    );
    $this->checkTranslations($strings, $langcode);

134
    // Fill the {locale_file} table with an older file.
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
    $file = $this->mockImportedPoFile($langcode, -1);

    // Add language.
    $this->addLanguage($langcode);

    // The file was imported, translations should exist.
    $strings = array(
      'Monday' => 'Montag',
      'Tuesday' => 'Dienstag',
    );
    $this->checkTranslations($strings, $langcode);

    $timestamp = db_query('SELECT timestamp FROM {locale_file} WHERE uri = :uri', array(':uri' => $file->uri))->fetchField();
    $this->assertEqual($timestamp, $file->original_timestamp, t('File is updated.'));
  }

  /**
   * Don't update a pre-existing file.
   */
  function testBulkImportNotUpdateExisting() {
    $langcode = 'de';

    // Translations should not exist.
    $strings = array(
      'Monday' => '',
      'Tuesday' => '',
    );
    $this->checkTranslations($strings, $langcode);

    // Fill the {locale_file} table with a newer file.
    $file = $this->mockImportedPoFile($langcode, 1);

    // Add language.
    $this->addLanguage($langcode);

    // The file was not imported, the translation should not exist.
    $strings = array(
      'Monday' => '',
      'Tuesday' => '',
    );
    $this->checkTranslations($strings, $langcode);

    $timestamp = db_query('SELECT timestamp FROM {locale_file} WHERE uri = :uri', array(':uri' => $file->uri))->fetchField();
    $this->assertEqual($timestamp, $file->timestamp);
  }
180
181
182
183
184
185
186
187
188
189

  /**
   * Delete translation files after deleting a language.
   */
  function testDeleteLanguage() {
    $dir = conf_path() . '/files/translations';
    file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
    variable_set('locale_translate_file_directory', $dir);
    $langcode = 'de';
    $this->addLanguage($langcode);
190
191
    $file_uri = $dir . '/po_' . $this->randomName() . '.' . $langcode . '.po';
    file_put_contents($file_uri, $this->randomName());
192
193
194
195
196
    $this->assertTrue(is_file($file_uri), 'Translation file is created.');
    language_delete($langcode);
    $this->assertTrue($file_uri);
    $this->assertFalse(is_file($file_uri), 'Translation file deleted after deleting language');
  }
197
}