'Language configuration', 'description' => 'Adds a new locale and tests changing its status and the default language.', 'group' => 'Locale', ); } function setUp() { parent::setUp('locale'); } /** * Functional tests for adding, editing and deleting languages. */ function testLanguageConfiguration() { global $base_url; // User to add and remove language. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); $this->drupalLogin($admin_user); // Add predefined language. $edit = array( 'langcode' => 'fr', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); $this->assertText('fr', 'Language added successfully.'); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); // Add custom language. // Code for the language. $langcode = 'xx'; // The English name for the language. $name = $this->randomName(16); // The native name for the language. $native = $this->randomName(16); // The domain prefix. $prefix = $langcode; $edit = array( 'langcode' => $langcode, 'name' => $name, 'native' => $native, 'prefix' => $prefix, 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); $this->assertText($langcode, 'Language code found.'); $this->assertText($name, 'Name found.'); $this->assertText($native, 'Native found.'); $this->assertText($native, 'Test language added.'); // Check if we can change the default language. $path = 'admin/config/regional/language'; $this->drupalGet($path); $this->assertFieldChecked('edit-site-default-en', 'English is the default language.'); // Change the default language. $edit = array( 'site_default' => $langcode, ); $this->drupalPost(NULL, $edit, t('Save configuration')); $this->assertNoFieldChecked('edit-site-default-en', 'Default language updated.'); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); // Check if a valid language prefix is added after changing the default // language. $this->drupalGet('admin/config/regional/language/edit/en'); $this->assertFieldByXPath('//input[@name="prefix"]', 'en', 'A valid path prefix has been added to the previous default language.'); // Ensure we can't delete the default language. $this->drupalGet('admin/config/regional/language/delete/' . $langcode); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); $this->assertText(t('The default language cannot be deleted.'), 'Failed to delete the default language.'); // Check if we can disable a language. $edit = array( 'enabled[en]' => FALSE, ); $this->drupalPost($path, $edit, t('Save configuration')); $this->assertNoFieldChecked('edit-enabled-en', 'Language disabled.'); // Set disabled language to be the default and ensure it is re-enabled. $edit = array( 'site_default' => 'en', ); $this->drupalPost(NULL, $edit, t('Save configuration')); $this->assertFieldChecked('edit-enabled-en', 'Default language re-enabled.'); // Ensure 'edit' link works. $this->clickLink(t('edit')); $this->assertTitle(t('Edit language | Drupal'), 'Page title is "Edit language".'); // Edit a language. $name = $this->randomName(16); $edit = array( 'name' => $name, ); $this->drupalPost('admin/config/regional/language/edit/' . $langcode, $edit, t('Save language')); $this->assertRaw($name, 'The language has been updated.'); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); // Ensure 'delete' link works. $this->drupalGet('admin/config/regional/language'); $this->clickLink(t('delete')); $this->assertText(t('Are you sure you want to delete the language'), '"delete" link is correct.'); // Delete an enabled language. $this->drupalGet('admin/config/regional/language/delete/' . $langcode); // First test the 'cancel' link. $this->clickLink(t('Cancel')); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); $this->assertRaw($name, 'The language was not deleted.'); // Delete the language for real. This a confirm form, we do not need any // fields changed. $this->drupalPost('admin/config/regional/language/delete/' . $langcode, 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.'); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); // Verify that language is no longer found. $this->drupalGet('admin/config/regional/language/delete/' . $langcode); $this->assertResponse(404, 'Language no longer found.'); // Make sure the "language_count" variable has been updated correctly. drupal_static_reset('language_list'); $enabled = language_list('enabled'); $this->assertEqual(variable_get('language_count', 1), count($enabled[1]), 'Language count is correct.'); // Delete a disabled language. // Disable an enabled language. $edit = array( 'enabled[fr]' => FALSE, ); $this->drupalPost($path, $edit, t('Save configuration')); $this->assertNoFieldChecked('edit-enabled-fr', 'French language disabled.'); // Get the count of enabled languages. drupal_static_reset('language_list'); $enabled = language_list('enabled'); // Delete the disabled language. $this->drupalPost('admin/config/regional/language/delete/fr', array(), t('Delete')); // We need raw here because %locale will add HTML. $this->assertRaw(t('The language %locale has been removed.', array('%locale' => 'French')), 'Disabled language has been removed.'); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); // Verify that language is no longer found. $this->drupalGet('admin/config/regional/language/delete/fr'); $this->assertResponse(404, 'Language no longer found.'); // Make sure the "language_count" variable has not changed. $this->assertEqual(variable_get('language_count', 1), count($enabled[1]), 'Language count is correct.'); // Ensure we can't delete the English language. $this->drupalGet('admin/config/regional/language/delete/en'); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); $this->assertText(t('The English language cannot be deleted.'), 'Failed to delete English language.'); } } /** * Tests localization of the JavaScript libraries. * * Currently, only the jQuery datepicker is localized using Drupal translations. */ class LocaleLibraryInfoAlterTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Javascript library localisation', 'description' => 'Tests the localisation of JavaScript libraries.', 'group' => 'Locale', ); } function setUp() { parent::setUp('locale', 'locale_test'); } /** * Verifies that the datepicker can be localized. * * @see locale_library_info_alter() */ public function testLibraryInfoAlter() { drupal_add_library('system', 'ui.datepicker'); $scripts = drupal_get_js(); $this->assertTrue(strpos($scripts, 'locale.datepicker.js'), 'locale.datepicker.js added to scripts.'); } } /** * Functional tests for JavaScript parsing for translatable strings. */ class LocaleJavascriptTranslationTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Javascript translation', 'description' => 'Tests parsing js files for translatable strings', 'group' => 'Locale', ); } function setUp() { parent::setUp('locale', 'locale_test'); } function testFileParsing() { $filename = drupal_get_path('module', 'locale_test') . '/locale_test.js'; // Parse the file to look for source strings. _locale_parse_js_file($filename); // Get all of the source strings that were found. $source_strings = db_select('locales_source', 's') ->fields('s', array('source', 'context')) ->condition('s.location', $filename) ->execute() ->fetchAllKeyed(); // List of all strings that should be in the file. $test_strings = array( "Standard Call t" => '', "Whitespace Call t" => '', "Single Quote t" => '', "Single Quote \\'Escaped\\' t" => '', "Single Quote Concat strings t" => '', "Double Quote t" => '', "Double Quote \\\"Escaped\\\" t" => '', "Double Quote Concat strings t" => '', "Context !key Args t" => "Context string", "Context Unquoted t" => "Context string unquoted", "Context Single Quoted t" => "Context string single quoted", "Context Double Quoted t" => "Context string double quoted", "Standard Call plural" => '', "Standard Call @count plural" => '', "Whitespace Call plural" => '', "Whitespace Call @count plural" => '', "Single Quote plural" => '', "Single Quote @count plural" => '', "Single Quote \\'Escaped\\' plural" => '', "Single Quote \\'Escaped\\' @count plural" => '', "Double Quote plural" => '', "Double Quote @count plural" => '', "Double Quote \\\"Escaped\\\" plural" => '', "Double Quote \\\"Escaped\\\" @count plural" => '', "Context !key Args plural" => "Context string", "Context !key Args @count plural" => "Context string", "Context Unquoted plural" => "Context string unquoted", "Context Unquoted @count plural" => "Context string unquoted", "Context Single Quoted plural" => "Context string single quoted", "Context Single Quoted @count plural" => "Context string single quoted", "Context Double Quoted plural" => "Context string double quoted", "Context Double Quoted @count plural" => "Context string double quoted", ); // Assert that all strings were found properly. foreach ($test_strings as $str => $context) { $args = array('%source' => $str, '%context' => $context); // Make sure that the string was found in the file. $this->assertTrue(isset($source_strings[$str]), format_string('Found source string: %source', $args)); // Make sure that the proper context was matched. $this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, strlen($context) > 0 ? format_string('Context for %source is %context', $args) : format_string('Context for %source is blank', $args)); } $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.'); } /** * Test handling of null values in JS parsing for PHP8.0+ deprecations. */ function testNullValuesLocalesSource() { db_insert('locales_source') ->fields(array( 'location' => NULL, 'source' => 'Standard Call t', 'context' => '', 'textgroup' => 'default', )) ->execute(); $filename = drupal_get_path('module', 'locale_test') . '/locale_test.js'; // Parse the file to look for source strings. _locale_parse_js_file($filename); $num_records = db_select('locales_source')->fields(NULL, array('lid'))->countQuery()->execute()->fetchField(); $this->assertEqual($num_records, 32, 'Correct number of strings parsed from JS file'); } } /** * Functional test for string translation and validation. */ class LocaleTranslationFunctionalTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'String translate, search and validate', 'description' => 'Adds a new locale and translates its name. Checks the validation of translation strings and search results.', 'group' => 'Locale', ); } function setUp() { parent::setUp('locale'); } /** * Adds a language and tests string translation by users with the appropriate permissions. */ function testStringTranslation() { 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 = 'xx'; // 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. $prefix = $langcode; // This is the language indicator on the translation search screen for // untranslated strings. Copied straight from locale.inc. $language_indicator = "$langcode "; // This will be the translation of $name. $translation = $this->randomName(16); // Add custom language. $this->drupalLogin($admin_user); $edit = array( 'langcode' => $langcode, 'name' => $name, 'native' => $native, 'prefix' => $prefix, 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); // Add string. t($name, array(), array('langcode' => $langcode)); // Reset locale cache. locale_reset(); $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->drupalLogout(); // Search for the name and translate it. $this->drupalLogin($translate_user); $search = array( 'string' => $name, 'language' => 'all', 'translation' => 'all', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); // 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.'); // Assume this is the only result, given the random name. $this->clickLink(t('edit')); // We save the lid from the path. $matches = array(); preg_match('!admin/config/regional/translate/edit/(\d+)!', $this->getUrl(), $matches); $lid = $matches[1]; // 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->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.'); $this->assertTrue($name != $translation && t($name, array(), array('langcode' => $langcode)) == $translation, 't() works.'); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); // The indicator should not be here. $this->assertNoRaw($language_indicator, 'String is translated.'); // Verify that a translation set which has an empty target string can be // updated without any database error. db_update('locales_target') ->fields(array('translation' => '')) ->condition('language', $langcode, '=') ->condition('lid', $lid, '=') ->execute(); $this->drupalPost('admin/config/regional/translate/edit/' . $lid, $edit, t('Save translations')); $this->assertText(t('The string has been saved.'), 'The string has been saved.'); // Try to edit a non-existent string and ensure we're redirected correctly. // Assuming we don't have 999,999 strings already. $random_lid = 999999; $this->drupalGet('admin/config/regional/translate/edit/' . $random_lid); $this->assertText(t('String not found'), 'String not found.'); $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.'); $this->drupalLogout(); // Delete the language. $this->drupalLogin($admin_user); $path = 'admin/config/regional/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); // Verify that language is no longer found. $this->assertResponse(404, 'Language no longer found.'); $this->drupalLogout(); // Delete the string. $this->drupalLogin($translate_user); $search = array( 'string' => $name, 'language' => 'all', 'translation' => 'all', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); // Assume this is the only result, given the random name. $this->clickLink(t('delete')); $this->assertText(t('Are you sure you want to delete the string'), '"delete" link is correct.'); // Delete the string. $path = 'admin/config/regional/translate/delete/' . $lid; $this->drupalGet($path); // First test the 'cancel' link. $this->clickLink(t('Cancel')); $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.'); $this->assertRaw($name, 'The string was not deleted.'); // Delete the name string. $this->drupalPost('admin/config/regional/translate/delete/' . $lid, array(), t('Delete')); $this->assertText(t('The string has been removed.'), 'The string has been removed message.'); $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.'); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertNoText($name, 'Search now can not find the name.'); } /* * Adds a language and checks that the JavaScript translation files are * properly created and rebuilt on deletion. */ function testJavaScriptTranslation() { $user = $this->drupalCreateUser(array('translate interface', 'administer languages', 'access administration pages')); $this->drupalLogin($user); $langcode = 'xx'; // 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. $prefix = $langcode; // Add custom language. $edit = array( 'langcode' => $langcode, 'name' => $name, 'native' => $native, 'prefix' => $prefix, 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); drupal_static_reset('language_list'); // Build the JavaScript translation file. $this->drupalGet('admin/config/regional/translate/translate'); // Retrieve the id of the first string available in the {locales_source} // table and translate it. $query = db_select('locales_source', 'l'); $query->addExpression('min(l.lid)', 'lid'); $result = $query->condition('l.location', '%.js%', 'LIKE') ->condition('l.textgroup', 'default') ->execute(); $url = 'admin/config/regional/translate/edit/' . $result->fetchObject()->lid; $edit = array('translations['. $langcode .']' => $this->randomName()); $this->drupalPost($url, $edit, t('Save translations')); // Trigger JavaScript translation parsing and building. require_once DRUPAL_ROOT . '/includes/locale.inc'; _locale_rebuild_js($langcode); // Retrieve the JavaScript translation hash code for the custom language to // check that the translation file has been properly built. $file = db_select('languages', 'l') ->fields('l', array('javascript')) ->condition('language', $langcode) ->execute() ->fetchObject(); $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/' . $langcode . '_' . $file->javascript . '.js'; $this->assertTrue($result = file_exists($js_file), format_string('JavaScript file created: %file', array('%file' => $result ? $js_file : 'not found'))); // Test JavaScript translation rebuilding. file_unmanaged_delete($js_file); $this->assertTrue($result = !file_exists($js_file), format_string('JavaScript file deleted: %file', array('%file' => $result ? $js_file : 'found'))); cache_clear_all(); _locale_rebuild_js($langcode); $this->assertTrue($result = file_exists($js_file), format_string('JavaScript file rebuilt: %file', array('%file' => $result ? $js_file : 'not found'))); // Test if JavaScript translation contains a custom string override. $string_override = $this->randomName(); variable_set('locale_custom_strings_' . $langcode, array('' => array($string_override => $string_override))); _locale_rebuild_js($langcode); $file = db_select('languages', 'l') ->fields('l', array('javascript')) ->condition('language', $langcode) ->execute() ->fetchObject(); $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/' . $langcode . '_' . $file->javascript . '.js'; $content = file_get_contents($js_file); $this->assertTrue(strpos($content, '"' . $string_override . '":"' . $string_override . '"') > 0); } /** * Tests the validation of the translation input. */ function testStringValidation() { global $base_url; // User to add language and strings. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'translate interface')); $this->drupalLogin($admin_user); $langcode = 'xx'; // 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. $prefix = $langcode; // This is the language indicator on the translation search screen for // untranslated strings. Copied straight from locale.inc. $language_indicator = "$langcode "; // These will be the invalid translations of $name. $key = $this->randomName(16); $bad_translations[$key] = "" . $key; $key = $this->randomName(16); $bad_translations[$key] = '' . $key; $key = $this->randomName(16); $bad_translations[$key] = '<' . $key; $key = $this->randomName(16); $bad_translations[$key] ="
" . $key; // Add custom language. $edit = array( 'langcode' => $langcode, 'name' => $name, 'native' => $native, 'prefix' => $prefix, 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); // Add string. t($name, array(), array('langcode' => $langcode)); // Reset locale cache. $search = array( 'string' => $name, 'language' => 'all', 'translation' => 'all', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); // Find the edit path. $content = $this->drupalGetContent(); $this->assertTrue(preg_match('@(admin/config/regional/translate/edit/[0-9]+)@', $content, $matches), 'Found the edit path.'); $path = $matches[0]; foreach ($bad_translations as $key => $translation) { $edit = array( "translations[$langcode]" => $translation, ); $this->drupalPost($path, $edit, t('Save translations')); // Check for a form error on the textarea. $form_class = $this->xpath('//form[@id="locale-translate-edit-form"]//textarea/@class'); $this->assertNotIdentical(FALSE, strpos($form_class[0], 'error'), 'The string was rejected as unsafe.'); $this->assertNoText(t('The string has been saved.'), 'The string was not saved.'); } } /** * Tests translation search form. */ function testStringSearch() { 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 = 'xx'; // 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. $prefix = $langcode; // This is the language indicator on the translation search screen for // untranslated strings. Copied straight from locale.inc. $language_indicator = "$langcode "; // This will be the translation of $name. $translation = $this->randomName(16); // Add custom language. $this->drupalLogin($admin_user); $edit = array( 'langcode' => $langcode, 'name' => $name, 'native' => $native, 'prefix' => $prefix, 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); // Add string. t($name, array(), array('langcode' => $langcode)); // Reset locale cache. locale_reset(); $this->drupalLogout(); // Search for the name. $this->drupalLogin($translate_user); $search = array( 'string' => $name, 'language' => 'all', 'translation' => 'all', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); // 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 string.'); // Ensure untranslated string doesn't appear if searching on 'only // translated strings'. $search = array( 'string' => $name, 'language' => 'all', 'translation' => 'translated', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertText(t('No strings available.'), "Search didn't find the string."); // Ensure untranslated string appears if searching on 'only untranslated // strings' in "all" (hasn't been translated to any language). $search = array( 'string' => $name, 'language' => 'all', 'translation' => 'untranslated', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertNoText(t('No strings available.'), 'Search found the string.'); // Ensure untranslated string appears if searching on 'only untranslated // strings' in the custom language (hasn't been translated to that specific language). $search = array( 'string' => $name, 'language' => $langcode, 'translation' => 'untranslated', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertNoText(t('No strings available.'), 'Search found the string.'); // Add translation. // Assume this is the only result, given the random name. $this->clickLink(t('edit')); // We save the lid from the path. $matches = array(); preg_match('!admin/config/regional/translate/edit/(\d)+!', $this->getUrl(), $matches); $lid = $matches[1]; $edit = array( "translations[$langcode]" => $translation, ); $this->drupalPost(NULL, $edit, t('Save translations')); // Ensure translated string does appear if searching on 'only // translated strings'. $search = array( 'string' => $translation, 'language' => 'all', 'translation' => 'translated', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertNoText(t('No strings available.'), 'Search found the translation.'); // Ensure translated source string doesn't appear if searching on 'only // untranslated strings'. $search = array( 'string' => $name, 'language' => 'all', 'translation' => 'untranslated', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertText(t('No strings available.'), "Search didn't find the source string."); // Ensure translated string doesn't appear if searching on 'only // untranslated strings'. $search = array( 'string' => $translation, 'language' => 'all', 'translation' => 'untranslated', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertText(t('No strings available.'), "Search didn't find the translation."); // Ensure translated string does appear if searching on the custom language. $search = array( 'string' => $translation, 'language' => $langcode, 'translation' => 'all', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertNoText(t('No strings available.'), 'Search found the translation.'); // Ensure translated string doesn't appear if searching on English. $search = array( 'string' => $translation, 'language' => 'en', 'translation' => 'all', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertText(t('No strings available.'), "Search didn't find the translation."); // Search for a string that isn't in the system. $unavailable_string = $this->randomName(16); $search = array( 'string' => $unavailable_string, 'language' => 'all', 'translation' => 'all', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertText(t('No strings available.'), "Search didn't find the invalid string."); } } /** * Tests plural index computation functionality. */ class LocalePluralFormatTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Plural formula evaluation', 'description' => 'Tests plural formula evaluation for various languages.', 'group' => 'Locale', ); } function setUp() { parent::setUp('locale', 'locale_test'); $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages')); $this->drupalLogin($admin_user); // Import some .po files with formulas to set up the environment. // These will also add the languages to the system and enable them. $this->importPoFile($this->getPoFileWithSimplePlural(), array( 'langcode' => 'fr', )); $this->importPoFile($this->getPoFileWithComplexPlural(), array( 'langcode' => 'hr', )); } /** * Tests locale_get_plural() functionality. */ function testGetPluralFormat() { $this->drupalGet('locale_test_plural_format_page'); $tests = _locale_test_plural_format_tests(); $result = array(); foreach ($tests as $test) { $this->assertPluralFormat($test['count'], $test['language'], $test['expected-result']); } } /** * Helper assert to test locale_get_plural page. * * @param $count * Number for testing. * @param $lang * Language for testing * @param $expected_result * Expected result. * @param $message */ function assertPluralFormat($count, $lang, $expected_result) { $message_param = array( '@lang' => $lang, '@count' => $count, '@expected_result' => $expected_result, ); $message = t("Computed plural index for '@lang' with count @count is @expected_result.", $message_param); $message_param = array( '@lang' => $lang, '@expected_result' => $expected_result, ); $this->assertText(format_string('Language: @lang, locale_get_plural: @expected_result.', $message_param, $message)); } /** * Imports a standalone .po file in a given language. * * @param $contents * Contents of the .po file to import. * @param $options * Additional options to pass to the translation import form. */ function importPoFile($contents, array $options = array()) { $name = drupal_tempnam('temporary://', "po_") . '.po'; file_put_contents($name, $contents); $options['files[file]'] = $name; $this->drupalPost('admin/config/regional/translate/import', $options, t('Import')); drupal_unlink($name); } /** * Returns a .po file with a simple plural formula. */ function getPoFileWithSimplePlural() { return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\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 "One sheep" msgid_plural "@count sheep" msgstr[0] "un mouton" msgstr[1] "@count moutons" msgid "Monday" msgstr "lundi" EOF; } /** * Returns a .po file with a complex plural formula. */ function getPoFileWithComplexPlural() { return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\n" "MIME-Version: 1.0\\n" "Content-Type: text/plain; charset=UTF-8\\n" "Content-Transfer-Encoding: 8bit\\n" "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n" msgid "1 hour" msgid_plural "@count hours" msgstr[0] "@count sat" msgstr[1] "@count sata" msgstr[2] "@count sati" msgid "Monday" msgstr "Ponedjeljak" EOF; } } /** * Functional tests for the import of translation files. */ class LocaleImportFunctionalTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Translation import', 'description' => 'Tests the import of locale files.', 'group' => 'Locale', ); } /** * A user able to create languages and import translations. */ protected $admin_user = NULL; function setUp() { parent::setUp('locale', 'locale_test'); $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages')); $this->drupalLogin($this->admin_user); } /** * Test import of standalone .po files. */ function testStandalonePoFile() { // Try importing a .po file. $this->importPoFile($this->getPoFile(), array( 'langcode' => 'fr', )); // The import should automatically create the corresponding language. $this->assertRaw(t('The language %language has been created.', array('%language' => 'French')), 'The language has been automatically created.'); // The import should have created 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' => 9, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.'); // This import should have saved plural forms to have 2 variants. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, 'Plural number initialized.'); // Ensure we were redirected correctly. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate', array('absolute' => TRUE)), 'Correct page redirection.'); // Try importing a .po file with invalid tags in the default text group. $this->importPoFile($this->getBadPoFile(), array( 'langcode' => 'fr', )); // The import should have created 1 string and rejected 2. $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' => 1, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.'); $skip_message = format_plural(2, 'One translation string was skipped because it contains disallowed HTML.', '@count translation strings were skipped because they contain disallowed HTML.'); $this->assertRaw($skip_message, 'Unsafe strings were skipped.'); // Try importing a .po file with invalid tags in a non default text group. $this->importPoFile($this->getBadPoFile(), array( 'langcode' => 'fr', 'group' => 'custom', )); // The import should have created 3 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' => 3, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.'); // Try importing a .po file which doesn't exist. $name = $this->randomName(16); $this->drupalPost('admin/config/regional/translate/import', array( 'langcode' => 'fr', 'files[file]' => $name, 'group' => 'custom', ), t('Import')); $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/import', array('absolute' => TRUE)), 'Correct page redirection.'); $this->assertText(t('File to import not found.'), 'File to import not found message.'); // Try importing a .po file with overriding strings, and ensure existing // strings are kept. $this->importPoFile($this->getOverwritePoFile(), array( 'langcode' => 'fr', 'mode' => 1, // Existing strings are kept, only new strings are added. )); // The import should have created 1 string. $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' => 1, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.'); // Ensure string wasn't overwritten. $search = array( 'string' => 'Montag', 'language' => 'fr', 'translation' => 'translated', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertText(t('No strings available.'), 'String not overwritten by imported string.'); // This import should not have changed number of plural forms. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, 'Plural numbers untouched.'); $this->importPoFile($this->getPoFileWithBrokenPlural(), array( 'langcode' => 'fr', 'mode' => 1, // Existing strings are kept, only new strings are added. )); // Attempt to import broken .po file as well to prove that this // will not overwrite the proper plural formula imported above. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, 'Broken plurals: plural numbers untouched.'); $this->importPoFile($this->getPoFileWithMissingPlural(), array( 'langcode' => 'fr', 'mode' => 1, // Existing strings are kept, only new strings are added. )); // Attempt to import .po file which has no plurals and prove that this // will not overwrite the proper plural formula imported above. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, 'No plurals: plural numbers untouched.'); // Try importing a .po file with overriding strings, and ensure existing // strings are overwritten. $this->importPoFile($this->getOverwritePoFile(), array( 'langcode' => 'fr', 'mode' => 0, // Strings in the uploaded file replace existing ones, new ones are added. )); // The import should have updated 2 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' => 0, '%update' => 2, '%delete' => 0)), 'The translation file was successfully imported.'); // Ensure string was overwritten. $search = array( 'string' => 'Montag', 'language' => 'fr', 'translation' => 'translated', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertNoText(t('No strings available.'), 'String overwritten by imported string.'); // This import should have changed number of plural forms. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 3, 'Plural numbers changed.'); } /** * Test automatic import of a module's translation files when a language is * enabled. */ function testAutomaticModuleTranslationImportLanguageEnable() { // Code for the language - manually set to match the test translation file. $langcode = 'xx'; // The English name for the language. $name = $this->randomName(16); // The native name for the language. $native = $this->randomName(16); // The domain prefix. $prefix = $langcode; // Create a custom language. $edit = array( 'langcode' => $langcode, 'name' => $name, 'native' => $native, 'prefix' => $prefix, 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); // Ensure the translation file was automatically imported when language was // added. $this->assertText(t('One translation file imported for the enabled modules.'), 'Language file automatically imported.'); // Ensure strings were successfully imported. $search = array( 'string' => 'lundi', 'language' => $langcode, 'translation' => 'translated', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); $this->assertNoText(t('No strings available.'), 'String successfully imported.'); } /** * Test msgctxt context support. */ function testLanguageContext() { // Try importing a .po file. $this->importPoFile($this->getPoFileWithContext(), array( 'langcode' => 'hr', )); $this->assertIdentical(t('May', array(), array('langcode' => 'hr', 'context' => 'Long month name')), 'Svibanj', 'Long month name context is working.'); $this->assertIdentical(t('May', array(), array('langcode' => 'hr')), 'Svi.', 'Default context is working.'); } /** * Test empty msgstr at end of .po file see #611786. */ function testEmptyMsgstr() { $langcode = 'hu'; // Try importing a .po file. $this->importPoFile($this->getPoFileWithMsgstr(), array( 'langcode' => $langcode, )); $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' => 1, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.'); $this->assertIdentical(t('Operations', array(), array('langcode' => $langcode)), 'Műveletek', 'String imported and translated.'); // Try importing a .po file. $this->importPoFile($this->getPoFileWithEmptyMsgstr(), array( 'langcode' => $langcode, 'mode' => 0, )); $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' => 0, '%update' => 0, '%delete' => 1)), 'The translation file was successfully imported.'); // This is the language indicator on the translation search screen for // untranslated strings. Copied straight from locale.inc. $language_indicator = "$langcode "; $str = "Operations"; $search = array( 'string' => $str, 'language' => 'all', 'translation' => 'all', 'group' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); // assertText() seems to remove the input field where $str always could be // found, so this is not a false assert. $this->assertText($str, 'Search found the string.'); $this->assertRaw($language_indicator, 'String is untranslated again.'); } /** * Helper function: import a standalone .po file in a given language. * * @param $contents * Contents of the .po file to import. * @param $options * Additional options to pass to the translation import form. */ function importPoFile($contents, array $options = array()) { $name = drupal_tempnam('temporary://', "po_") . '.po'; file_put_contents($name, $contents); $options['files[file]'] = $name; $this->drupalPost('admin/config/regional/translate/import', $options, t('Import')); drupal_unlink($name); } /** * Helper function that returns a proper .po file. */ function getPoFile() { return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\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 "One sheep" msgid_plural "@count sheep" msgstr[0] "un mouton" msgstr[1] "@count moutons" 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; } /** * Helper function that returns a bad .po file. */ function getBadPoFile() { return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\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 "Save configuration" msgstr "Enregistrer la configuration" msgid "edit" msgstr "modifier" msgid "delete" msgstr "supprimer" EOF; } /** * Helper function that returns a proper .po file, for testing overwriting * existing translations. */ function getOverwritePoFile() { return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\n" "MIME-Version: 1.0\\n" "Content-Type: text/plain; charset=UTF-8\\n" "Content-Transfer-Encoding: 8bit\\n" "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n" msgid "Monday" msgstr "Montag" msgid "Day" msgstr "Jour" EOF; } /** * Helper function that returns a .po file with context. */ function getPoFileWithContext() { // Croatian (code hr) is one of the languages that have a different // form for the full name and the abbreviated name for the month May. return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\n" "MIME-Version: 1.0\\n" "Content-Type: text/plain; charset=UTF-8\\n" "Content-Transfer-Encoding: 8bit\\n" "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n" msgctxt "Long month name" msgid "May" msgstr "Svibanj" msgid "May" msgstr "Svi." EOF; } /** * Helper function that returns a .po file with an empty last item. */ function getPoFileWithEmptyMsgstr() { return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\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 "Operations" msgstr "" EOF; } /** * Helper function that returns a .po file with an empty last item. */ function getPoFileWithMsgstr() { return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\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 "Operations" msgstr "Műveletek" msgid "Will not appear in Drupal core, so we can ensure the test passes" msgstr "" EOF; } /** * Returns a .po file with a missing plural formula. */ function getPoFileWithMissingPlural() { return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\n" "MIME-Version: 1.0\\n" "Content-Type: text/plain; charset=UTF-8\\n" "Content-Transfer-Encoding: 8bit\\n" msgid "Monday" msgstr "Ponedjeljak" EOF; } /** * Returns a .po file with a broken plural formula. */ function getPoFileWithBrokenPlural() { return <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 7\\n" "MIME-Version: 1.0\\n" "Content-Type: text/plain; charset=UTF-8\\n" "Content-Transfer-Encoding: 8bit\\n" "Plural-Forms: broken, will not parse\\n" msgid "Monday" msgstr "lundi" EOF; } } /** * Functional tests for the export of translation files. */ class LocaleExportFunctionalTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Translation export', 'description' => 'Tests the exportation of locale files.', 'group' => 'Locale', ); } /** * A user able to create languages and export translations. */ protected $admin_user = NULL; function setUp() { parent::setUp('locale', 'locale_test'); $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages')); $this->drupalLogin($this->admin_user); } /** * Test exportation of translations. */ function testExportTranslation() { // First import some known translations. // This will also automatically enable the 'fr' language. $name = drupal_tempnam('temporary://', "po_") . '.po'; file_put_contents($name, $this->getPoFile()); $this->drupalPost('admin/config/regional/translate/import', array( 'langcode' => 'fr', 'files[file]' => $name, ), t('Import')); drupal_unlink($name); // Get the French translations. $this->drupalPost('admin/config/regional/translate/export', array( 'langcode' => 'fr', ), t('Export')); // Ensure we have a translation file. $this->assertRaw('# French translation of Drupal', 'Exported French translation file.'); // Ensure our imported translations exist in the file. $this->assertRaw('msgstr "lundi"', 'French translations present in exported file.'); } /** * Test exportation of translation template file. */ function testExportTranslationTemplateFile() { // Get the translation template file. // There are two 'Export' buttons on this page, but it somehow works. It'd // be better if we could use the submit button id like documented but that // doesn't work. $this->drupalPost('admin/config/regional/translate/export', array(), t('Export')); // Ensure we have a translation file. $this->assertRaw('# LANGUAGE translation of PROJECT', 'Exported translation template file.'); } /** * 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" EOF; } } /** * Tests for the st() function. */ class LocaleInstallTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'String translation using st()', 'description' => 'Tests that st() works like t().', 'group' => 'Locale', ); } function setUp() { parent::setUp('locale'); // st() lives in install.inc, so ensure that it is loaded for all tests. require_once DRUPAL_ROOT . '/includes/install.inc'; } /** * Verify that function signatures of t() and st() are equal. */ function testFunctionSignatures() { $reflector_t = new ReflectionFunction('t'); $reflector_st = new ReflectionFunction('st'); $this->assertEqual($reflector_t->getParameters(), $reflector_st->getParameters(), 'Function signatures of t() and st() are equal.'); } } /** * Locale uninstall with English UI functional test. */ class LocaleUninstallFunctionalTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Locale uninstall (EN)', 'description' => 'Tests the uninstall process using the built-in UI language.', 'group' => 'Locale', ); } /** * The default language set for the UI before uninstall. */ protected $language; function setUp() { parent::setUp('locale'); $this->language = 'en'; } /** * Check if the values of the Locale variables are correct after uninstall. */ function testUninstallProcess() { $locale_module = array('locale'); // Add a new language and optionally set it as default. require_once DRUPAL_ROOT . '/includes/locale.inc'; locale_add_language('fr', 'French', 'Français', LANGUAGE_LTR, '', '', TRUE, $this->language == 'fr'); // Check the UI language. drupal_language_initialize(); global $language; $this->assertEqual($language->language, $this->language, format_string('Current language: %lang', array('%lang' => $language->language))); // Enable multilingual workflow option for articles. variable_set('language_content_type_article', 1); // Change JavaScript translations directory. variable_set('locale_js_directory', 'js_translations'); // Build the JavaScript translation file for French. $user = $this->drupalCreateUser(array('translate interface', 'access administration pages')); $this->drupalLogin($user); $this->drupalGet('admin/config/regional/translate/translate'); $string = db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE :location AND textgroup = :textgroup', array( ':location' => '%.js%', ':textgroup' => 'default', ))->fetchObject(); $edit = array('translations[fr]' => 'french translation'); $this->drupalPost('admin/config/regional/translate/edit/' . $string->lid, $edit, t('Save translations')); _locale_rebuild_js('fr'); $file = db_query('SELECT javascript FROM {languages} WHERE language = :language', array(':language' => 'fr'))->fetchObject(); $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/fr_' . $file->javascript . '.js'; $this->assertTrue($result = file_exists($js_file), format_string('JavaScript file created: %file', array('%file' => $result ? $js_file : 'none'))); // Disable string caching. variable_set('locale_cache_strings', 0); // Change language negotiation options. drupal_load('module', 'locale'); variable_set('language_types', drupal_language_types() + array('language_custom' => TRUE)); variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, locale_language_negotiation_info()); variable_set('language_negotiation_' . LANGUAGE_TYPE_CONTENT, locale_language_negotiation_info()); variable_set('language_negotiation_' . LANGUAGE_TYPE_URL, locale_language_negotiation_info()); // Change language providers settings. variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX); variable_set('locale_language_negotiation_session_param', TRUE); // Uninstall Locale. module_disable($locale_module); drupal_uninstall_modules($locale_module); // Visit the front page. $this->drupalGet(''); // Check the init language logic. drupal_language_initialize(); $this->assertEqual($language->language, 'en', format_string('Language after uninstall: %lang', array('%lang' => $language->language))); // Check JavaScript files deletion. $this->assertTrue($result = !file_exists($js_file), format_string('JavaScript file deleted: %file', array('%file' => $result ? $js_file : 'found'))); // Check language count. $language_count = variable_get('language_count', 1); $this->assertEqual($language_count, 1, format_string('Language count: %count', array('%count' => $language_count))); // Check language negotiation. require_once DRUPAL_ROOT . '/includes/language.inc'; $this->assertTrue(count(language_types()) == count(drupal_language_types()), 'Language types reset'); $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_INTERFACE) == LANGUAGE_NEGOTIATION_DEFAULT; $this->assertTrue($language_negotiation, format_string('Interface language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set'))); $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_CONTENT) == LANGUAGE_NEGOTIATION_DEFAULT; $this->assertTrue($language_negotiation, format_string('Content language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set'))); $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_URL) == LANGUAGE_NEGOTIATION_DEFAULT; $this->assertTrue($language_negotiation, format_string('URL language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set'))); // Check language providers settings. $this->assertFalse(variable_get('locale_language_negotiation_url_part', FALSE), 'URL language provider indicator settings cleared.'); $this->assertFalse(variable_get('locale_language_negotiation_session_param', FALSE), 'Visit language provider settings cleared.'); // Check JavaScript parsed. $javascript_parsed_count = count(variable_get('javascript_parsed', array())); $this->assertEqual($javascript_parsed_count, 0, format_string('JavaScript parsed count: %count', array('%count' => $javascript_parsed_count))); // Check multilingual workflow option for articles. $multilingual = variable_get('language_content_type_article', 0); $this->assertEqual($multilingual, 0, format_string('Multilingual workflow option: %status', array('%status' => $multilingual ? 'enabled': 'disabled'))); // Check JavaScript translations directory. $locale_js_directory = variable_get('locale_js_directory', 'languages'); $this->assertEqual($locale_js_directory, 'languages', format_string('JavaScript translations directory: %dir', array('%dir' => $locale_js_directory))); // Check string caching. $locale_cache_strings = variable_get('locale_cache_strings', 1); $this->assertEqual($locale_cache_strings, 1, format_string('String caching: %status', array('%status' => $locale_cache_strings ? 'enabled': 'disabled'))); } } /** * Locale uninstall with French UI functional test. * * Because this class extends LocaleUninstallFunctionalTest, it doesn't require a new * test of its own. Rather, it switches the default UI language in setUp and then * runs the testUninstallProcess (which it inherits from LocaleUninstallFunctionalTest) * to test with this new language. */ class LocaleUninstallFrenchFunctionalTest extends LocaleUninstallFunctionalTest { public static function getInfo() { return array( 'name' => 'Locale uninstall (FR)', 'description' => 'Tests the uninstall process using French as interface language.', 'group' => 'Locale', ); } function setUp() { parent::setUp(); $this->language = 'fr'; } } /** * Functional tests for the language switching feature. */ class LocaleLanguageSwitchingFunctionalTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Language switching', 'description' => 'Tests for the language switching feature.', 'group' => 'Locale', ); } function setUp() { parent::setUp('locale'); // Create and login user. $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer languages', 'translate interface', 'access administration pages')); $this->drupalLogin($admin_user); } /** * Functional tests for the language switcher block. */ function testLanguageBlock() { // Enable the language switching block. $language_type = LANGUAGE_TYPE_INTERFACE; $edit = array( "blocks[locale_{$language_type}][region]" => 'sidebar_first', ); $this->drupalPost('admin/structure/block', $edit, t('Save blocks')); // Add language. $edit = array( 'langcode' => 'fr', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); // Enable URL language detection and selection. $edit = array('language[enabled][locale-url]' => '1'); $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings')); // Assert that the language switching block is displayed on the frontpage. $this->drupalGet(''); $this->assertText(t('Languages'), 'Language switcher block found.'); // Assert that only the current language is marked as active. list($language_switcher) = $this->xpath('//div[@id=:id]/div[@class="content"]', array(':id' => 'block-locale-' . $language_type)); $links = array( 'active' => array(), 'inactive' => array(), ); $anchors = array( 'active' => array(), 'inactive' => array(), ); foreach ($language_switcher->ul->li as $link) { $classes = explode(" ", (string) $link['class']); list($language) = array_intersect($classes, array('en', 'fr')); if (in_array('active', $classes)) { $links['active'][] = $language; } else { $links['inactive'][] = $language; } $anchor_classes = explode(" ", (string) $link->a['class']); if (in_array('active', $anchor_classes)) { $anchors['active'][] = $language; } else { $anchors['inactive'][] = $language; } } $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language list item is marked as active on the language switcher block.'); $this->assertIdentical($anchors, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language anchor is marked as active on the language switcher block.'); } /** * Tests that languages can be added as disabled. */ function testNewDisabledLanguage() { // Add new language which will be disabled. locale_add_language('de', 'German', 'Deutsch', LANGUAGE_LTR, '', '', FALSE, FALSE); } } /** * Test browser language detection. */ class LocaleBrowserDetectionTest extends DrupalUnitTestCase { public static function getInfo() { return array( 'name' => 'Browser language detection', 'description' => 'Tests for the browser language detection.', 'group' => 'Locale', ); } /** * Unit tests for the locale_language_from_browser() function. */ function testLanguageFromBrowser() { // Load the required functions. require_once DRUPAL_ROOT . '/includes/locale.inc'; $languages = array( // In our test case, 'en' has priority over 'en-US'. 'en' => (object) array( 'language' => 'en', ), 'en-US' => (object) array( 'language' => 'en-US', ), // But 'fr-CA' has priority over 'fr'. 'fr-CA' => (object) array( 'language' => 'fr-CA', ), 'fr' => (object) array( 'language' => 'fr', ), // 'es-MX' is alone. 'es-MX' => (object) array( 'language' => 'es-MX', ), // 'pt' is alone. 'pt' => (object) array( 'language' => 'pt', ), // Language codes with more then one dash are actually valid. // eh-oh-laa-laa is the official language code of the Teletubbies. 'eh-oh-laa-laa' => (object) array( 'language' => 'eh-oh-laa-laa', ), ); $test_cases = array( // Equal qvalue for each language, choose the site preferred one. 'en,en-US,fr-CA,fr,es-MX' => 'en', 'en-US,en,fr-CA,fr,es-MX' => 'en', 'fr,en' => 'en', 'en,fr' => 'en', 'en-US,fr' => 'en', 'fr,en-US' => 'en', 'fr,fr-CA' => 'fr-CA', 'fr-CA,fr' => 'fr-CA', 'fr' => 'fr-CA', 'fr;q=1' => 'fr-CA', 'fr,es-MX' => 'fr-CA', 'fr,es' => 'fr-CA', 'es,fr' => 'fr-CA', 'es-MX,de' => 'es-MX', 'de,es-MX' => 'es-MX', // Different cases and whitespace. 'en' => 'en', 'En' => 'en', 'EN' => 'en', ' en' => 'en', 'en ' => 'en', 'en, fr' => 'en', // A less specific language from the browser matches a more specific one // from the website, and the other way around for compatibility with // some versions of Internet Explorer. 'es' => 'es-MX', 'es-MX' => 'es-MX', 'pt' => 'pt', 'pt-PT' => 'pt', 'pt-PT;q=0.5,pt-BR;q=1,en;q=0.7' => 'en', 'pt-PT;q=1,pt-BR;q=0.5,en;q=0.7' => 'en', 'pt-PT;q=0.4,pt-BR;q=0.1,en;q=0.7' => 'en', 'pt-PT;q=0.1,pt-BR;q=0.4,en;q=0.7' => 'en', // Language code with several dashes are valid. The less specific language // from the browser matches the more specific one from the website. 'eh-oh-laa-laa' => 'eh-oh-laa-laa', 'eh-oh-laa' => 'eh-oh-laa-laa', 'eh-oh' => 'eh-oh-laa-laa', 'eh' => 'eh-oh-laa-laa', // Different qvalues. 'fr,en;q=0.5' => 'fr-CA', 'fr,en;q=0.5,fr-CA;q=0.25' => 'fr', // Silly wildcards are also valid. '*,fr-CA;q=0.5' => 'en', '*,en;q=0.25' => 'fr-CA', 'en,en-US;q=0.5,fr;q=0.25' => 'en', 'en-US,en;q=0.5,fr;q=0.25' => 'en-US', // Unresolvable cases. '' => FALSE, 'de,pl' => FALSE, 'iecRswK4eh' => FALSE, $this->randomName(10) => FALSE, ); foreach ($test_cases as $accept_language => $expected_result) { $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $accept_language; $result = locale_language_from_browser($languages); $this->assertIdentical($result, $expected_result, format_string("Language selection '@accept-language' selects '@result', result = '@actual'", array('@accept-language' => $accept_language, '@result' => $expected_result, '@actual' => isset($result) ? $result : 'none'))); } } } /** * Functional tests for a user's ability to change their default language. */ class LocaleUserLanguageFunctionalTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'User language settings', 'description' => "Tests user's ability to change their default language.", 'group' => 'Locale', ); } function setUp() { parent::setUp('locale'); } /** * Test if user can change their default language. */ function testUserLanguageConfiguration() { global $base_url; // User to add and remove language. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); // User to change their default language. $web_user = $this->drupalCreateUser(); // Add custom language. $this->drupalLogin($admin_user); // Code for the language. $langcode = 'xx'; // The English name for the language. $name = $this->randomName(16); // The native name for the language. $native = $this->randomName(16); // The domain prefix. $prefix = 'xx'; $edit = array( 'langcode' => $langcode, 'name' => $name, 'native' => $native, 'prefix' => $prefix, 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); // Add custom language and disable it. // Code for the language. $langcode_disabled = 'xx-yy'; // The English name for the language. This will be translated. $name_disabled = $this->randomName(16); // The native name for the language. $native_disabled = $this->randomName(16); // The domain prefix. $prefix_disabled = $langcode_disabled; $edit = array( 'langcode' => $langcode_disabled, 'name' => $name_disabled, 'native' => $native_disabled, 'prefix' => $prefix_disabled, 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); // Disable the language. $edit = array( 'enabled[' . $langcode_disabled . ']' => FALSE, ); $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration')); $this->drupalLogout(); // Login as normal user and edit account settings. $this->drupalLogin($web_user); $path = 'user/' . $web_user->uid . '/edit'; $this->drupalGet($path); // Ensure language settings fieldset is available. $this->assertText(t('Language settings'), 'Language settings available.'); // Ensure custom language is present. $this->assertText($name, 'Language present on form.'); // Ensure disabled language isn't present. $this->assertNoText($name_disabled, 'Disabled language not present on form.'); // Switch to our custom language. $edit = array( 'language' => $langcode, ); $this->drupalPost($path, $edit, t('Save')); // Ensure form was submitted successfully. $this->assertText(t('The changes have been saved.'), 'Changes were saved.'); // Check if language was changed. $elements = $this->xpath('//input[@id=:id]', array(':id' => 'edit-language-' . $langcode)); $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), 'Default language successfully updated.'); $this->drupalLogout(); } } /** * Functional test for language handling during user creation. */ class LocaleUserCreationTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'User creation', 'description' => 'Tests whether proper language is stored for new users and access to language selector.', 'group' => 'Locale', ); } function setUp() { parent::setUp('locale'); variable_set('user_register', USER_REGISTER_VISITORS); } /** * Functional test for language handling during user creation. */ function testLocalUserCreation() { // User to add and remove language and create new users. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer users')); $this->drupalLogin($admin_user); // Add predefined language. $langcode = 'fr'; $edit = array( 'langcode' => 'fr', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); $this->assertText($langcode, 'Language added successfully.'); $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.'); // Set language negotiation. $edit = array( 'language[enabled][locale-url]' => TRUE, ); $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings')); $this->assertText(t('Language negotiation configuration saved.'), 'Set language negotiation.'); // Check if the language selector is available on admin/people/create and // set to the currently active language. $this->drupalGet($langcode . '/admin/people/create'); $this->assertFieldChecked("edit-language-$langcode", 'Global language set in the language selector.'); // Create a user with the admin/people/create form and check if the correct // language is set. $username = $this->randomName(10); $edit = array( 'name' => $username, 'mail' => $this->randomName(4) . '@example.com', 'pass[pass1]' => $username, 'pass[pass2]' => $username, ); $this->drupalPost($langcode . '/admin/people/create', $edit, t('Create new account')); $user = user_load_by_name($username); $this->assertEqual($user->language, $langcode, 'New user has correct language set.'); // Register a new user and check if the language selector is hidden. $this->drupalLogout(); $this->drupalGet($langcode . '/user/register'); $this->assertNoFieldByName('language[fr]', 'Language selector is not accessible.'); $username = $this->randomName(10); $edit = array( 'name' => $username, 'mail' => $this->randomName(4) . '@example.com', ); $this->drupalPost($langcode . '/user/register', $edit, t('Create new account')); $user = user_load_by_name($username); $this->assertEqual($user->language, $langcode, 'New user has correct language set.'); // Test if the admin can use the language selector and if the // correct language is was saved. $user_edit = $langcode . '/user/' . $user->uid . '/edit'; $this->drupalLogin($admin_user); $this->drupalGet($user_edit); $this->assertFieldChecked("edit-language-$langcode", 'Language selector is accessible and correct language is selected.'); // Set pass_raw so we can login the new user. $user->pass_raw = $this->randomName(10); $edit = array( 'pass[pass1]' => $user->pass_raw, 'pass[pass2]' => $user->pass_raw, ); $this->drupalPost($user_edit, $edit, t('Save')); $this->drupalLogin($user); $this->drupalGet($user_edit); $this->assertFieldChecked("edit-language-$langcode", 'Language selector is accessible and correct language is selected.'); } } /** * Functional tests for configuring a different path alias per language. */ class LocalePathFunctionalTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Path language settings', 'description' => 'Checks you can configure a language for individual URL aliases.', 'group' => 'Locale', ); } function setUp() { parent::setUp('locale', 'path'); } /** * Test if a language can be associated with a path alias. */ function testPathLanguageConfiguration() { global $base_url; // User to add and remove language. $admin_user = $this->drupalCreateUser(array('administer languages', 'create page content', 'administer url aliases', 'create url aliases', 'access administration pages')); // Add custom language. $this->drupalLogin($admin_user); // Code for the language. $langcode = 'xx'; // The English name for the language. $name = $this->randomName(16); // The native name for the language. $native = $this->randomName(16); // The domain prefix. $prefix = $langcode; $edit = array( 'langcode' => $langcode, 'name' => $name, 'native' => $native, 'prefix' => $prefix, 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); // Check that the "xx" front page is not available when path prefixes are // not enabled yet. $this->drupalPost('admin/config/regional/language/configure', array(), t('Save settings')); $this->drupalGet($prefix); $this->assertResponse(404, 'The "xx" front page is not available yet.'); // Enable URL language detection and selection. $edit = array('language[enabled][locale-url]' => 1); $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings')); // Create a node. $node = $this->drupalCreateNode(array('type' => 'page')); // Create a path alias in default language (English). $path = 'admin/config/search/path/add'; $english_path = $this->randomName(8); $edit = array( 'source' => 'node/' . $node->nid, 'alias' => $english_path, 'language' => 'en', ); $this->drupalPost($path, $edit, t('Save')); // Create a path alias in new custom language. $custom_language_path = $this->randomName(8); $edit = array( 'source' => 'node/' . $node->nid, 'alias' => $custom_language_path, 'language' => $langcode, ); $this->drupalPost($path, $edit, t('Save')); // Confirm English language path alias works. $this->drupalGet($english_path); $this->assertText($node->title, 'English alias works.'); // Confirm custom language path alias works. $this->drupalGet($prefix . '/' . $custom_language_path); $this->assertText($node->title, 'Custom language alias works.'); // Create a custom path. $custom_path = $this->randomName(8); // Check priority of language for alias by source path. $edit = array( 'source' => 'node/' . $node->nid, 'alias' => $custom_path, 'language' => LANGUAGE_NONE, ); path_save($edit); $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, 'en'); $this->assertEqual($english_path, $lookup_path, 'English language alias has priority.'); // Same check for language 'xx'. $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, $prefix); $this->assertEqual($custom_language_path, $lookup_path, 'Custom language alias has priority.'); path_delete($edit); // Create language nodes to check priority of aliases. $first_node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); $second_node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); // Assign a custom path alias to the first node with the English language. $edit = array( 'source' => 'node/' . $first_node->nid, 'alias' => $custom_path, 'language' => 'en', ); path_save($edit); // Assign a custom path alias to second node with LANGUAGE_NONE. $edit = array( 'source' => 'node/' . $second_node->nid, 'alias' => $custom_path, 'language' => LANGUAGE_NONE, ); path_save($edit); // Test that both node titles link to our path alias. $this->drupalGet('