diff --git a/core/CHANGELOG.txt b/core/CHANGELOG.txt index 88ea5168e3d40238eec00943cb2e05500a527d00..85f9bc67aa8f609d8c17433d02334cff7ebd0943 100644 --- a/core/CHANGELOG.txt +++ b/core/CHANGELOG.txt @@ -16,6 +16,8 @@ Drupal 8.0, xxxx-xx-xx (development version) * Profile * Trigger - Removed the Garland theme from core. +- Removed backwards-compatibility with 'magic_quotes_gpc'/'magic_quotes_runtime' + PHP configuration settings. Both are required to be disabled. - Universally Unique IDentifier (UUID): * Support for generating and validating UUIDs. - JavaScript changes: diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index aa20e90ff8b9a471e44a489f79c0d85ab0262f6f..1eaaac7f4e15022dc9382e2c24d07e786d9f5c44 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -232,6 +232,22 @@ */ const LANGUAGE_RTL = 1; +/** + * Indicates an error during check for PHP unicode support. + */ +const UNICODE_ERROR = -1; + +/** + * Indicates that standard PHP (emulated) unicode support is being used. + */ +const UNICODE_SINGLEBYTE = 0; + +/** + * Indicates that full unicode support with the PHP mbstring extension is being + * used. + */ +const UNICODE_MULTIBYTE = 1; + /** * Time of the current request in seconds elapsed since the Unix Epoch. * @@ -562,8 +578,13 @@ function drupal_environment_initialize() { // sites/default/default.settings.php contains more runtime settings. // The .htaccess file contains settings that cannot be changed at runtime. - // Don't escape quotes when reading files from the database, disk, etc. - ini_set('magic_quotes_runtime', '0'); + // Deny execution with enabled "magic quotes" (both GPC and runtime). + if (get_magic_quotes_gpc() || get_magic_quotes_runtime()) { + header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); + print "PHP's 'magic_quotes_gpc' and 'magic_quotes_runtime' settings are not supported and must be disabled."; + exit; + } + // Use session cookies, not transparent sessions that puts the session id in // the query string. ini_set('session.use_cookies', '1'); @@ -578,6 +599,9 @@ function drupal_environment_initialize() { // Set sane locale settings, to ensure consistent string, dates, times and // numbers handling. setlocale(LC_ALL, 'C'); + + // Detect string handling method. + unicode_check(); } /** @@ -590,6 +614,51 @@ function drupal_valid_http_host($host) { return preg_match('/^\[?(?:[a-zA-Z0-9-:\]_]+\.?)+$/', $host); } +/** + * Checks for Unicode support in PHP and sets the proper settings if possible. + * + * Because Drupal needs to be able to handle text in various encodings, we do + * not support mbstring function overloading. HTTP input/output conversion must + * be disabled for similar reasons. + * + * @return string + * A string identifier of a failed multibyte extension check, if any. + * Otherwise, an empty string. + */ +function unicode_check() { + global $multibyte; + + // Check for mbstring extension. + if (!function_exists('mb_strlen')) { + $multibyte = UNICODE_SINGLEBYTE; + return 'mb_strlen'; + } + + // Check mbstring configuration. + if (ini_get('mbstring.func_overload') != 0) { + $multibyte = UNICODE_ERROR; + return 'mbstring.func_overload'; + } + if (ini_get('mbstring.encoding_translation') != 0) { + $multibyte = UNICODE_ERROR; + return 'mbstring.encoding_translation'; + } + if (ini_get('mbstring.http_input') != 'pass') { + $multibyte = UNICODE_ERROR; + return 'mbstring.http_input'; + } + if (ini_get('mbstring.http_output') != 'pass') { + $multibyte = UNICODE_ERROR; + return 'mbstring.http_output'; + } + + // Set appropriate configuration. + mb_internal_encoding('utf-8'); + mb_language('uni'); + $multibyte = UNICODE_MULTIBYTE; + return ''; +} + /** * Sets the base URL, cookie domain, and session name from configuration. */ diff --git a/core/includes/common.inc b/core/includes/common.inc index be2278fda637262495ca189a62f5f51ae933850e..c9e06a5b0f91fbb33a427d6b8c3413913d79504b 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1025,67 +1025,6 @@ function drupal_http_request($url, array $options = array()) { * @} End of "defgroup http_handling". */ -/** - * Strips slashes from a string or array of strings. - * - * Callback for array_walk() within fix_gpx_magic(). - * - * @param $item - * An individual string or array of strings from superglobals. - */ -function _fix_gpc_magic(&$item) { - if (is_array($item)) { - array_walk($item, '_fix_gpc_magic'); - } - else { - $item = stripslashes($item); - } -} - -/** - * Strips slashes from $_FILES items. - * - * Callback for array_walk() within fix_gpc_magic(). - * - * The tmp_name key is skipped keys since PHP generates single backslashes for - * file paths on Windows systems. - * - * @param $item - * An item from $_FILES. - * @param $key - * The key for the item within $_FILES. - * - * @see http://php.net/manual/features.file-upload.php#42280 - */ -function _fix_gpc_magic_files(&$item, $key) { - if ($key != 'tmp_name') { - if (is_array($item)) { - array_walk($item, '_fix_gpc_magic_files'); - } - else { - $item = stripslashes($item); - } - } -} - -/** - * Fixes double-escaping caused by "magic quotes" in some PHP installations. - * - * @see _fix_gpc_magic() - * @see _fix_gpc_magic_files() - */ -function fix_gpc_magic() { - static $fixed = FALSE; - if (!$fixed && ini_get('magic_quotes_gpc')) { - array_walk($_GET, '_fix_gpc_magic'); - array_walk($_POST, '_fix_gpc_magic'); - array_walk($_COOKIE, '_fix_gpc_magic'); - array_walk($_REQUEST, '_fix_gpc_magic'); - array_walk($_FILES, '_fix_gpc_magic_files'); - } - $fixed = TRUE; -} - /** * @defgroup validation Input validation * @{ @@ -5180,13 +5119,7 @@ function _drupal_bootstrap_code() { require_once DRUPAL_ROOT . '/core/includes/errors.inc'; require_once DRUPAL_ROOT . '/core/includes/schema.inc'; - // @todo Move this to earlier in bootstrap: http://drupal.org/node/1569456. - unicode_check(); - - // @todo Remove this: http://drupal.org/node/1569456. - fix_gpc_magic(); - - // Load all enabled modules. + // Load all enabled modules module_load_all(); // Make sure all stream wrappers are registered. diff --git a/core/includes/registry.inc b/core/includes/registry.inc index 2316444d6e0f2cd2486122adf02524ecf57c6112..0fc2c57ead8b904d10ef8c8f15b667f7599e2f64 100644 --- a/core/includes/registry.inc +++ b/core/includes/registry.inc @@ -29,6 +29,11 @@ function _registry_update() { $connection_info = Database::getConnectionInfo(); $driver = $connection_info['default']['driver']; + // During the first registry rebuild in a request, we check all the files. + // During subsequent rebuilds, we only add new files. It makes the rebuilding + // process faster during installation of modules. + static $check_existing_files = TRUE; + // Get current list of modules and their files. $modules = db_query("SELECT * FROM {system} WHERE type = 'module'")->fetchAll(); // Get the list of files we are going to parse. @@ -59,7 +64,14 @@ function _registry_update() { foreach (registry_get_parsed_files() as $filename => $file) { // Add the hash for those files we have already parsed. if (isset($files[$filename])) { - $files[$filename]['hash'] = $file['hash']; + if ($check_existing_files) { + $files[$filename]['hash'] = $file['hash']; + } + else { + // Ignore that file for this request, it has been parsed previously + // and it is unlikely it has changed. + unset($files[$filename]); + } } else { // Flush the registry of resources in files that are no longer on disc @@ -95,6 +107,10 @@ function _registry_update() { throw $e; } + // During the next run in this request, don't bother re-checking existing + // files. + $check_existing_files = FALSE; + // We have some unchanged resources, warm up the cache - no need to pay // for looking them up again. if (count($unchanged_resources) > 0) { diff --git a/core/includes/unicode.inc b/core/includes/unicode.inc index 1450b435fa1e6cc4ba2596de071bda48d1ca5dd8..2a9b31e8e22da92a48b5ee093aa030d66556df7f 100644 --- a/core/includes/unicode.inc +++ b/core/includes/unicode.inc @@ -1,21 +1,5 @@ <?php -/** - * Indicates an error during check for PHP unicode support. - */ -const UNICODE_ERROR = -1; - -/** - * Indicates that standard PHP (emulated) unicode support is being used. - */ -const UNICODE_SINGLEBYTE = 0; - -/** - * Indicates that full unicode support with the PHP mbstring extension is being - * used. - */ -const UNICODE_MULTIBYTE = 1; - /** * Matches Unicode characters that are word boundaries. * @@ -77,53 +61,6 @@ '\x{FE10}-\x{FE19}\x{FE30}-\x{FE6B}\x{FEFF}-\x{FF0F}\x{FF1A}-\x{FF20}' . '\x{FF3B}-\x{FF40}\x{FF5B}-\x{FF65}\x{FFE0}-\x{FFFD}'); -/** - * Wrapper around _unicode_check(). - */ -function unicode_check() { - list($GLOBALS['multibyte']) = _unicode_check(); -} - -/** - * Perform checks about Unicode support in PHP, and set the right settings if - * needed. - * - * Because Drupal needs to be able to handle text in various encodings, we do - * not support mbstring function overloading. HTTP input/output conversion must - * be disabled for similar reasons. - * - * @param $errors - * Whether to report any fatal errors with form_set_error(). - */ -function _unicode_check() { - // Ensure translations don't break at install time - $t = get_t(); - - // Check for mbstring extension - if (!function_exists('mb_strlen')) { - return array(UNICODE_SINGLEBYTE, $t('Operations on Unicode strings are emulated on a best-effort basis. Install the <a href="@url">PHP mbstring extension</a> for improved Unicode support.', array('@url' => 'http://www.php.net/mbstring'))); - } - - // Check mbstring configuration - if (ini_get('mbstring.func_overload') != 0) { - return array(UNICODE_ERROR, $t('Multibyte string function overloading in PHP is active and must be disabled. Check the php.ini <em>mbstring.func_overload</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', array('@url' => 'http://www.php.net/mbstring'))); - } - if (ini_get('mbstring.encoding_translation') != 0) { - return array(UNICODE_ERROR, $t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.encoding_translation</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', array('@url' => 'http://www.php.net/mbstring'))); - } - if (ini_get('mbstring.http_input') != 'pass') { - return array(UNICODE_ERROR, $t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_input</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', array('@url' => 'http://www.php.net/mbstring'))); - } - if (ini_get('mbstring.http_output') != 'pass') { - return array(UNICODE_ERROR, $t('Multibyte string output conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_output</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', array('@url' => 'http://www.php.net/mbstring'))); - } - - // Set appropriate configuration - mb_internal_encoding('utf-8'); - mb_language('uni'); - return array(UNICODE_MULTIBYTE, ''); -} - /** * Return Unicode library status and errors. */ @@ -138,21 +75,40 @@ function unicode_requirements() { ); $severities = array( UNICODE_SINGLEBYTE => REQUIREMENT_WARNING, - UNICODE_MULTIBYTE => REQUIREMENT_OK, + UNICODE_MULTIBYTE => NULL, UNICODE_ERROR => REQUIREMENT_ERROR, ); - list($library, $description) = _unicode_check(); + $failed_check = unicode_check(); + $library = $GLOBALS['multibyte']; $requirements['unicode'] = array( 'title' => $t('Unicode library'), 'value' => $libraries[$library], + 'severity' => $severities[$library], ); - if ($description) { - $requirements['unicode']['description'] = $description; + $t_args = array('@url' => 'http://www.php.net/mbstring'); + switch ($failed_check) { + case 'mb_strlen': + $requirements['unicode']['description'] = $t('Operations on Unicode strings are emulated on a best-effort basis. Install the <a href="@url">PHP mbstring extension</a> for improved Unicode support.', $t_args); + break; + + case 'mbstring.func_overload': + $requirements['unicode']['description'] = $t('Multibyte string function overloading in PHP is active and must be disabled. Check the php.ini <em>mbstring.func_overload</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', $t_args); + break; + + case 'mbstring.encoding_translation': + $requirements['unicode']['description'] = $t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.encoding_translation</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', $t_args); + break; + + case 'mbstring.http_input': + $requirements['unicode']['description'] = $t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_input</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', $t_args); + break; + + case 'mbstring.http_output': + $requirements['unicode']['description'] = $t('Multibyte string output conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_output</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', $t_args); + break; } - $requirements['unicode']['severity'] = $severities[$library]; - return $requirements; } diff --git a/core/includes/update.inc b/core/includes/update.inc index 18ce17196d394ce36e4bc71a9c74db8bf735c2a8..b0d09a710a021916fd295bd3339c02b63b9d8548 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -84,7 +84,6 @@ function update_prepare_d8_bootstrap() { // Allow the database system to work even if the registry has not been // created yet. include_once DRUPAL_ROOT . '/core/includes/install.inc'; - include_once DRUPAL_ROOT . '/core/modules/entity/entity.controller.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE); // If the site has not updated to Drupal 8 yet, check to make sure that it is diff --git a/core/modules/color/color.install b/core/modules/color/color.install index a1879f9b55945eda15bd24bf74d491d46dd5d6fb..c3f9a74ea9a17e7b0e87bff69053aafc2bd9b95c 100644 --- a/core/modules/color/color.install +++ b/core/modules/color/color.install @@ -20,10 +20,7 @@ function color_requirements($phase) { ); // Check for PNG support. - if (function_exists('imagecreatefrompng')) { - $requirements['color_gd']['severity'] = REQUIREMENT_OK; - } - else { + if (!function_exists('imagecreatefrompng')) { $requirements['color_gd']['severity'] = REQUIREMENT_WARNING; $requirements['color_gd']['description'] = t('The GD library for PHP is enabled, but was compiled without PNG support. Check the <a href="@url">PHP image documentation</a> for information on how to correct this.', array('@url' => 'http://www.php.net/manual/ref.image.php')); } diff --git a/core/modules/comment/comment.pages.inc b/core/modules/comment/comment.pages.inc index 1f00353c69cc05252ff395e583450564b8a28453..2986d135d9ac0b995f15eeeaed4eec1425271c99 100644 --- a/core/modules/comment/comment.pages.inc +++ b/core/modules/comment/comment.pages.inc @@ -51,7 +51,7 @@ function comment_reply(Node $node, $pid = NULL) { if (user_access('access comments')) { // Load the parent comment. $comment = comment_load($pid); - if ($comment->status = COMMENT_PUBLISHED) { + if ($comment->status == COMMENT_PUBLISHED) { // If that comment exists, make sure that the current comment and the // parent comment both belong to the same parent node. if ($comment->nid != $node->nid) { diff --git a/core/modules/comment/comment.test b/core/modules/comment/comment.test index a791b2d599dce5d9e1cbc3bfeedb1cb0e6f1513c..1ede06328f65a1654e1cdc9db3e9b7c5e550f78b 100644 --- a/core/modules/comment/comment.test +++ b/core/modules/comment/comment.test @@ -123,6 +123,12 @@ class CommentInterfaceTest extends CommentTestBase { $this->assertTrue($this->commentExists($reply, TRUE), t('Page two exists. %s')); $this->setCommentsPerPage(50); + // Attempt to reply to an unpublished comment. + $reply_loaded->status = COMMENT_NOT_PUBLISHED; + $reply_loaded->save(); + $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $reply_loaded->cid); + $this->assertText(t('The comment you are replying to does not exist.'), 'Replying to an unpublished comment'); + // Attempt to post to node with comments disabled. $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_HIDDEN)); $this->assertTrue($this->node, t('Article node created.')); diff --git a/core/modules/contact/contact.admin.inc b/core/modules/contact/contact.admin.inc index 7b1c90b1a525b35023eb860554b74fd50306b100..f6835f9c3ce7136d4c9e0220158c9644fc156a4f 100644 --- a/core/modules/contact/contact.admin.inc +++ b/core/modules/contact/contact.admin.inc @@ -112,14 +112,9 @@ function contact_category_edit_form($form, &$form_state, array $category = array '#description' => t('When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'), ); $form['selected'] = array( - '#type' => 'select', - '#title' => t('Selected'), - '#options' => array( - 0 => t('No'), - 1 => t('Yes'), - ), + '#type' => 'checkbox', + '#title' => t('Make this the default category.'), '#default_value' => $category['selected'], - '#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'), ); $form['cid'] = array( '#type' => 'value', diff --git a/core/modules/contact/contact.test b/core/modules/contact/contact.test index c80f21dadb1da836bf43aae4d9babd006b33f5c9..3c7f61af257eca64568978999ad3dd30e29c4167 100644 --- a/core/modules/contact/contact.test +++ b/core/modules/contact/contact.test @@ -221,7 +221,7 @@ class ContactSitewideTestCase extends WebTestBase { $edit['category'] = $category; $edit['recipients'] = $recipients; $edit['reply'] = $reply; - $edit['selected'] = ($selected ? '1' : '0'); + $edit['selected'] = ($selected ? TRUE : FALSE); $this->drupalPost('admin/structure/contact/add', $edit, t('Save')); } @@ -244,7 +244,7 @@ class ContactSitewideTestCase extends WebTestBase { $edit['category'] = $category; $edit['recipients'] = $recipients; $edit['reply'] = $reply; - $edit['selected'] = ($selected ? '1' : '0'); + $edit['selected'] = ($selected ? TRUE : FALSE); $this->drupalPost('admin/structure/contact/edit/' . $category_id, $edit, t('Save')); return ($category_id); } diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 5bd36d1e34140c6963d43af98a0f6617554ba96b..e0229a63b3606a9f9785c1fd5aaf23760c13a6a6 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -536,51 +536,33 @@ function _field_sort_items_value_helper($a, $b) { /** * Gets or sets administratively defined bundle settings. * - * For each bundle, settings are provided as a nested array with the following - * structure: - * @code - * array( - * 'view_modes' => array( - * // One sub-array per view mode for the entity type: - * 'full' => array( - * 'custom_display' => Whether the view mode uses custom display - * settings or settings of the 'default' mode, - * ), - * 'teaser' => ... - * ), - * 'extra_fields' => array( - * 'form' => array( - * // One sub-array per pseudo-field in displayed entities: - * 'extra_field_1' => array( - * 'weight' => The weight of the pseudo-field, - * ), - * 'extra_field_2' => ... - * ), - * 'display' => array( - * // One sub-array per pseudo-field in displayed entities: - * 'extra_field_1' => array( - * // One sub-array per view mode for the entity type, including - * // the 'default' mode: - * 'default' => array( - * 'weight' => The weight of the pseudo-field, - * 'visible' => TRUE if the pseudo-field is visible, FALSE if hidden, - * ), - * 'full' => ... - * ), - * 'extra_field_2' => ... - * ), - * ), - * ); - * @endcode - * - * @param $entity_type + * @param string $entity_type * The type of $entity; e.g., 'node' or 'user'. - * @param $bundle + * @param string $bundle * The bundle name. - * @param $settings - * (optional) The settings to store. + * @param array|null $settings + * (optional) The settings to store, an associative array with the following + * elements: + * - view_modes: An associative array keyed by view mode, with the following + * key/value pairs: + * - custom_settings: Boolean specifying whether the view mode uses a + * dedicated set of display options (TRUE), or the 'default' options + * (FALSE). Defaults to FALSE. + * - extra_fields: An associative array containing the form and display + * settings for extra fields (also known as pseudo-fields): + * - form: An associative array whose keys are the names of extra fields, + * and whose values are associative arrays with the following elements: + * - weight: The weight of the extra field, determining its position on an + * entity form. + * - display: An associative array whose keys are the names of extra fields, + * and whose values are associative arrays keyed by the name of view + * modes. This array must include an item for the 'default' view mode. + * Each view mode sub-array contains the following elements: + * - weight: The weight of the extra field, determining its position when + * an entity is viewed. + * - visible: TRUE if the extra field is visible, FALSE otherwise. * - * @return + * @return array|null * If no $settings are passed, the current settings are returned. */ function field_bundle_settings($entity_type, $bundle, $settings = NULL) { diff --git a/core/modules/file/file.install b/core/modules/file/file.install index dff930b5f1d108abc1fe31c6a34551b2e663ca21..49fc458b428b7619ce1ff469d5638f02d22c972c 100644 --- a/core/modules/file/file.install +++ b/core/modules/file/file.install @@ -60,36 +60,29 @@ function file_requirements($phase) { if (!$apache) { $value = t('Not enabled'); $description = t('Your server is not capable of displaying file upload progress. File upload progress requires an Apache server running PHP with mod_php.'); - $severity = REQUIREMENT_INFO; } elseif ($fastcgi) { $value = t('Not enabled'); $description = t('Your server is not capable of displaying file upload progress. File upload progress requires PHP be run with mod_php and not as FastCGI.'); - $severity = REQUIREMENT_INFO; } elseif (!$implementation && extension_loaded('apc')) { $value = t('Not enabled'); $description = t('Your server is capable of displaying file upload progress through APC, but it is not enabled. Add <code>apc.rfc1867 = 1</code> to your php.ini configuration. Alternatively, it is recommended to use <a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress</a>, which supports more than one simultaneous upload.'); - $severity = REQUIREMENT_INFO; } elseif (!$implementation) { $value = t('Not enabled'); $description = t('Your server is capable of displaying file upload progress, but does not have the required libraries. It is recommended to install the <a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a> (preferred) or to install <a href="http://php.net/apc">APC</a>.'); - $severity = REQUIREMENT_INFO; } elseif ($implementation == 'apc') { $value = t('Enabled (<a href="http://php.net/manual/apc.configuration.php#ini.apc.rfc1867">APC RFC1867</a>)'); $description = t('Your server is capable of displaying file upload progress using APC RFC1867. Note that only one upload at a time is supported. It is recommended to use the <a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a> if possible.'); - $severity = REQUIREMENT_OK; } elseif ($implementation == 'uploadprogress') { $value = t('Enabled (<a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress</a>)'); - $severity = REQUIREMENT_OK; } $requirements['file_progress'] = array( 'title' => t('Upload progress'), 'value' => $value, - 'severity' => $severity, 'description' => $description, ); } diff --git a/core/modules/image/image.install b/core/modules/image/image.install index 91349a92a29213f2636d45e1b693e2da1a6eea9c..735e0073bf0ddb475e34b53ab1955b45edac5af1 100644 --- a/core/modules/image/image.install +++ b/core/modules/image/image.install @@ -86,10 +86,7 @@ function image_requirements($phase) { ); // Check for filter and rotate support. - if (function_exists('imagefilter') && function_exists('imagerotate')) { - $requirements['image_gd']['severity'] = REQUIREMENT_OK; - } - else { + if (!function_exists('imagefilter') || !function_exists('imagerotate')) { $requirements['image_gd']['severity'] = REQUIREMENT_WARNING; $requirements['image_gd']['description'] = t('The GD Library for PHP is enabled, but was compiled without support for functions used by the rotate and desaturate effects. It was probably compiled using the official GD libraries from http://www.libgd.org instead of the GD library bundled with PHP. You should recompile PHP --with-gd using the bundled GD library. See <a href="http://www.php.net/manual/book.image.php">the PHP manual</a>.'); } diff --git a/core/modules/language/language.info b/core/modules/language/language.info index b4b307a82130b4fee5a69c6db43ccae621fde92d..309704f7783a6cd6eaf95145dd4a728af439a1d6 100644 --- a/core/modules/language/language.info +++ b/core/modules/language/language.info @@ -4,4 +4,3 @@ package = Core version = VERSION core = 8.x configure = admin/config/regional/language -files[] = language.test diff --git a/core/modules/language/language.negotiation.inc b/core/modules/language/language.negotiation.inc index 4d560e7d361d1240662e774032a91ac8aea6a8fe..a16dbe82ce5a2e932ec10fbc1c7566d780091657 100644 --- a/core/modules/language/language.negotiation.inc +++ b/core/modules/language/language.negotiation.inc @@ -220,6 +220,11 @@ function language_from_url($languages) { break; case LANGUAGE_NEGOTIATION_URL_DOMAIN: + // Get only the host, not the port. + $http_host= $_SERVER['HTTP_HOST']; + if (strpos($http_host, ':') !== FALSE) { + $http_host = current(explode(':', $http_host)); + } $domains = language_negotiation_url_domains(); foreach ($languages as $language) { // Skip the check if the language doesn't have a domain. @@ -228,7 +233,7 @@ function language_from_url($languages) { // the hostname. $host = 'http://' . str_replace(array('http://', 'https://'), '', $domains[$language->langcode]); $host = parse_url($host, PHP_URL_HOST); - if ($_SERVER['HTTP_HOST'] == $host) { + if ($http_host == $host) { $language_url = $language->langcode; break; } diff --git a/core/modules/language/language.test b/core/modules/language/language.test deleted file mode 100644 index d48f2a9e82066d59ce9acc85eb9d856dee0b129f..0000000000000000000000000000000000000000 --- a/core/modules/language/language.test +++ /dev/null @@ -1,1127 +0,0 @@ -<?php -use Drupal\Core\DependencyInjection\ContainerBuilder; - -/** - * @file - * Tests for language.module. - * - * The test file includes: - * - a functional test for the language configuration forms; - * - comparison of $GLOBALS default language against dependency injection; - */ - -use Drupal\simpletest\WebTestBase; -use Drupal\simpletest\UnitTestBase; - -/** - * Functional tests for the language list configuration forms. - */ -class LanguageListTest extends WebTestBase { - public static function getInfo() { - return array( - 'name' => 'Language list configuration', - 'description' => 'Adds a new language and tests changing its status and the default language.', - 'group' => 'Language', - ); - } - - function setUp() { - parent::setUp('language'); - } - - /** - * Functional tests for adding, editing and deleting languages. - */ - function testLanguageList() { - 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( - 'predefined_langcode' => 'fr', - ); - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - $this->assertText('French', t('Language added successfully.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); - - // Add custom language. - $langcode = 'xx'; - $name = $this->randomName(16); - $edit = array( - 'predefined_langcode' => 'custom', - 'langcode' => $langcode, - 'name' => $name, - '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)), t('Correct page redirection.')); - $this->assertRaw('"edit-site-default-' . $langcode .'"', t('Language code found.')); - $this->assertText(t($name), t('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', t('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', t('Default language updated.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); - - // 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)), t('Correct page redirection.')); - $this->assertText(t('The default language cannot be deleted.'), t('Failed to delete the default language.')); - - // Ensure 'edit' link works. - $this->clickLink(t('edit')); - $this->assertTitle(t('Edit language | Drupal'), t('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, t('The language has been updated.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); - - // Change back the default language. - $edit = array( - 'site_default' => 'en', - ); - $this->drupalPost(NULL, $edit, t('Save configuration')); - // 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'), t('"delete" link is correct.')); - // Delete a 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)), t('Correct page redirection.')); - $this->assertRaw($name, t('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 %language and %langcode will add HTML. - $t_args = array('%language' => $name, '%langcode' => $langcode); - $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The test language has been removed.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); - // Verify that language is no longer found. - $this->drupalGet('admin/config/regional/language/delete/' . $langcode); - $this->assertResponse(404, t('Language no longer found.')); - // Make sure the "language_count" variable has been updated correctly. - drupal_static_reset('language_list'); - $languages = language_list(); - $this->assertEqual(variable_get('language_count', 1), count($languages), t('Language count is correct.')); - // Delete French. - $this->drupalPost('admin/config/regional/language/delete/fr', array(), t('Delete')); - // Get the count of languages. - drupal_static_reset('language_list'); - $languages = language_list(); - // We need raw here because %language and %langcode will add HTML. - $t_args = array('%language' => 'French', '%langcode' => 'fr'); - $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('Disabled language has been removed.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); - // Verify that language is no longer found. - $this->drupalGet('admin/config/regional/language/delete/fr'); - $this->assertResponse(404, t('Language no longer found.')); - // Make sure the "language_count" variable has not changed. - $this->assertEqual(variable_get('language_count', 1), count($languages), t('Language count is correct.')); - - // Ensure we can delete the English language. Right now English is the only - // language so we must add a new language and make it the default before - // deleting English. - $langcode = 'xx'; - $name = $this->randomName(16); - $edit = array( - 'predefined_langcode' => 'custom', - 'langcode' => $langcode, - 'name' => $name, - '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)), t('Correct page redirection.')); - $this->assertText($name, t('Name found.')); - - // Check if we can change the default language. - $path = 'admin/config/regional/language'; - $this->drupalGet($path); - $this->assertFieldChecked('edit-site-default-en', t('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', t('Default language updated.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); - - $this->drupalPost('admin/config/regional/language/delete/en', array(), t('Delete')); - // We need raw here because %language and %langcode will add HTML. - $t_args = array('%language' => 'English', '%langcode' => 'en'); - $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The English language has been removed.')); - } -} - -/** - * Test for dependency injected language object. - */ -class LanguageDependencyInjectionTest extends WebTestBase { - public static function getInfo() { - return array( - 'name' => 'Language dependency injection', - 'description' => 'Compares the default language from $GLOBALS against the dependency injected language object.', - 'group' => 'Language', - ); - } - - function setUp() { - parent::setUp('language'); - - // Set up a new container to ensure we are building a new Language object - // for each test. - drupal_container(new ContainerBuilder()); - } - - /** - * Test dependency injected Language against the GLOBAL language object. - * - * @todo Once the PHP global is gone, we won't need this test as the same - * test is done without the PHP global in the following test. - */ - function testDependencyInjectedLanguage() { - // Initialize the language system. - drupal_language_initialize(); - - $expected = $GLOBALS[LANGUAGE_TYPE_INTERFACE]; - $result = drupal_container()->get(LANGUAGE_TYPE_INTERFACE); - foreach ($expected as $property => $value) { - $this->assertEqual($expected->$property, $result->$property, t('The dependency injected language object %prop property equals the $GLOBAL language object %prop property.', array('%prop' => $property))); - } - } - - /** - * Test dependency injected languages against a new Language object. - * - * @see Drupal\Core\Language\Language - */ - function testDependencyInjectedNewLanguage() { - // Initialize the language system. - drupal_language_initialize(); - - $expected = new Drupal\Core\Language\Language((array) language_default()); - $result = drupal_container()->get(LANGUAGE_TYPE_INTERFACE); - foreach ($expected as $property => $value) { - $this->assertEqual($expected->$property, $result->$property, t('The dependency injected language object %prop property equals the new Language object %prop property.', array('%prop' => $property))); - } - } - - /** - * Test dependency injected Language object against a new default language - * object. - * - * @see Drupal\Core\Language\Language - */ - function testDependencyInjectedNewDefaultLanguage() { - // Change the language default object to different values. - $new_language_default = (object) array( - 'langcode' => 'fr', - 'name' => 'French', - 'direction' => 0, - 'weight' => 0, - 'default' => TRUE, - ); - variable_set('language_default', $new_language_default); - - // Initialize the language system. - drupal_language_initialize(); - - // The langauge system creates a Language object which contains the - // same properties as the new default language object. - $expected = $new_language_default; - $result = drupal_container()->get(LANGUAGE_TYPE_INTERFACE); - foreach ($expected as $property => $value) { - $this->assertEqual($expected->$property, $result->$property, t('The dependency injected language object %prop property equals the default language object %prop property.', array('%prop' => $property))); - } - - // Delete the language_default variable we previously set. - variable_del('language_default'); - } -} - -/** - * Functional tests for language configuration's effect on negotiation setup. - */ -class LanguageConfigurationTestCase extends WebTestBase { - public static function getInfo() { - return array( - 'name' => 'Language negotiation autoconfiguration', - 'description' => 'Adds and configures languages to check negotiation changes.', - 'group' => 'Language', - ); - } - - function setUp() { - parent::setUp('language'); - } - - /** - * 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); - - // Check if the Default English language has no path prefix. - $this->drupalGet('admin/config/regional/language/detection/url'); - $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', t('Default English has no path prefix.')); - - // Add predefined language. - $edit = array( - 'predefined_langcode' => 'fr', - ); - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - $this->assertText('French'); - $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); - - // Check if the Default English language has no path prefix. - $this->drupalGet('admin/config/regional/language/detection/url'); - $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', t('Default English has no path prefix.')); - // Check if French has a path prefix. - $this->drupalGet('admin/config/regional/language/detection/url'); - $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', t('French has a path prefix.')); - - // Check if we can change the default language. - $this->drupalGet('admin/config/regional/language'); - $this->assertFieldChecked('edit-site-default-en', t('English is the default language.')); - // Change the default language. - $edit = array( - 'site_default' => 'fr', - ); - $this->drupalPost(NULL, $edit, t('Save configuration')); - $this->assertNoFieldChecked('edit-site-default-en', t('Default language updated.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); - - // Check if a valid language prefix is added afrer changing the default - // language. - $this->drupalGet('admin/config/regional/language/detection/url'); - $this->assertFieldByXPath('//input[@name="prefix[en]"]', 'en', t('A valid path prefix has been added to the previous default language.')); - // Check if French still has a path prefix. - $this->drupalGet('admin/config/regional/language/detection/url'); - $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', t('French still has a path prefix.')); - } -} - -/** - * Functional tests for the language switching feature. - */ -class LanguageSwitchingFunctionalTestCase extends WebTestBase { - - public static function getInfo() { - return array( - 'name' => 'Language switching', - 'description' => 'Tests for the language switching feature.', - 'group' => 'Language', - ); - } - - function setUp() { - parent::setUp(array('language', 'block')); - - // Create and login user. - $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer languages', '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[language_{$language_type}][region]" => 'sidebar_first', - ); - $this->drupalPost('admin/structure/block', $edit, t('Save blocks')); - - // Add language. - $edit = array( - 'predefined_langcode' => 'fr', - ); - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Enable URL language detection and selection. - $edit = array('language_interface[enabled][language-url]' => '1'); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Assert that the language switching block is displayed on the frontpage. - $this->drupalGet(''); - $this->assertText(t('Languages'), t('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-language-' . str_replace('_', '-', $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($langcode) = array_intersect($classes, array('en', 'fr')); - if (in_array('active', $classes)) { - $links['active'][] = $langcode; - } - else { - $links['inactive'][] = $langcode; - } - $anchor_classes = explode(" ", (string) $link->a['class']); - if (in_array('active', $anchor_classes)) { - $anchors['active'][] = $langcode; - } - else { - $anchors['inactive'][] = $langcode; - } - } - $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), t('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')), t('Only the current language anchor is marked as active on the language switcher block.')); - } -} - -/** - * Test browser language detection. - */ -class LanguageBrowserDetectionTestCase extends UnitTestBase { - - public static function getInfo() { - return array( - 'name' => 'Browser language detection', - 'description' => 'Tests for the browser language detection.', - 'group' => 'Language', - ); - } - - /** - * Unit tests for the language_from_browser() function. - */ - function testLanguageFromBrowser() { - // Load the required functions. - require_once DRUPAL_ROOT . '/core/modules/language/language.negotiation.inc'; - - $languages = array( - // In our test case, 'en' has priority over 'en-US'. - 'en' => (object) array( - 'langcode' => 'en', - ), - 'en-US' => (object) array( - 'langcode' => 'en-US', - ), - // But 'fr-CA' has priority over 'fr'. - 'fr-CA' => (object) array( - 'langcode' => 'fr-CA', - ), - 'fr' => (object) array( - 'langcode' => 'fr', - ), - // 'es-MX' is alone. - 'es-MX' => (object) array( - 'langcode' => 'es-MX', - ), - // 'pt' is alone. - 'pt' => (object) array( - 'langcode' => '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( - 'langcode' => 'eh-oh-laa-laa', - ), - ); - - $test_cases = array( - // Equal qvalue for each language, choose the site prefered 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. - 'en-US,en;q=0.5,fr;q=0.25' => 'en-US', - '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 = language_from_browser($languages); - $this->assertIdentical($result, $expected_result, t("Language selection '@accept-language' selects '@result', result = '@actual'", array('@accept-language' => $accept_language, '@result' => $expected_result, '@actual' => isset($result) ? $result : 'none'))); - } - } -} - -/** - * Test UI language negotiation - * - * 1. URL (PATH) > DEFAULT - * UI Language base on URL prefix, browser language preference has no - * influence: - * admin/config - * UI in site default language - * zh-hans/admin/config - * UI in Chinese - * blah-blah/admin/config - * 404 - * 2. URL (PATH) > BROWSER > DEFAULT - * admin/config - * UI in user's browser language preference if the site has that - * language enabled, if not, the default language - * zh-hans/admin/config - * UI in Chinese - * blah-blah/admin/config - * 404 - * 3. URL (DOMAIN) > DEFAULT - * http://example.com/admin/config - * UI language in site default - * http://example.cn/admin/config - * UI language in Chinese - */ -class LanguageUILanguageNegotiationTestCase extends WebTestBase { - public static function getInfo() { - return array( - 'name' => 'UI language negotiation', - 'description' => 'Test UI language switching by url path prefix and domain.', - 'group' => 'Language', - ); - } - - function setUp() { - // We marginally use interface translation functionality here, so need to - // use the locale module instead of language only, but the 90% of the test - // is about the negotiation process which is solely in language module. - parent::setUp(array('locale', 'language_test', 'block')); - require_once DRUPAL_ROOT . '/core/includes/language.inc'; - drupal_load('module', 'locale'); - $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages', 'administer blocks')); - $this->drupalLogin($admin_user); - } - - /** - * Tests for language switching by URL path. - */ - function testUILanguageNegotiation() { - // A few languages to switch to. - // This one is unknown, should get the default lang version. - $langcode_unknown = 'blah-blah'; - // For testing browser lang preference. - $langcode_browser_fallback = 'vi'; - // For testing path prefix. - $langcode = 'zh-hans'; - // For setting browser language preference to 'vi'. - $http_header_browser_fallback = array("Accept-Language: $langcode_browser_fallback;q=1"); - // For setting browser language preference to some unknown. - $http_header_blah = array("Accept-Language: blah;q=1"); - - // This domain should switch the UI to Chinese. - $language_domain = 'example.cn'; - - // Setup the site languages by installing two languages. - $language = (object) array( - 'langcode' => $langcode_browser_fallback, - ); - language_save($language); - $language = (object) array( - 'langcode' => $langcode, - ); - language_save($language); - - // We will look for this string in the admin/config screen to see if the - // corresponding translated string is shown. - $default_string = 'Configure languages for content and the user interface'; - - // Set the default language in order for the translated string to be registered - // into database when seen by t(). Without doing this, our target string - // is for some reason not found when doing translate search. This might - // be some bug. - drupal_static_reset('language_list'); - $languages = language_list(); - variable_set('language_default', $languages['vi']); - // First visit this page to make sure our target string is searchable. - $this->drupalGet('admin/config'); - // Now the t()'ed string is in db so switch the language back to default. - variable_del('language_default'); - - // Translate the string. - $language_browser_fallback_string = "In $langcode_browser_fallback In $langcode_browser_fallback In $langcode_browser_fallback"; - $language_string = "In $langcode In $langcode In $langcode"; - // Do a translate search of our target string. - $edit = array( 'string' => $default_string); - $this->drupalPost('admin/config/regional/translate/translate', $edit, t('Filter')); - // Should find the string and now click edit to post translated string. - $this->clickLink('edit'); - $edit = array( - "translations[$langcode_browser_fallback][0]" => $language_browser_fallback_string, - "translations[$langcode][0]" => $language_string, - ); - $this->drupalPost(NULL, $edit, t('Save translations')); - - // Configure URL language rewrite. - variable_set('language_negotiation_url_type', LANGUAGE_TYPE_INTERFACE); - - $tests = array( - // Default, browser preference should have no influence. - array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT), - 'path' => 'admin/config', - 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_DEFAULT, - 'http_header' => $http_header_browser_fallback, - 'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.', - ), - // Language prefix. - array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT), - 'path' => "$langcode/admin/config", - 'expect' => $language_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_URL, - 'http_header' => $http_header_browser_fallback, - 'message' => 'URL (PATH) > DEFAULT: with language prefix, UI language is switched based on path prefix', - ), - // Default, go by browser preference. - array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER), - 'path' => 'admin/config', - 'expect' => $language_browser_fallback_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_BROWSER, - 'http_header' => $http_header_browser_fallback, - 'message' => 'URL (PATH) > BROWSER: no language prefix, UI language is determined by browser language preference', - ), - // Prefix, switch to the language. - array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER), - 'path' => "$langcode/admin/config", - 'expect' => $language_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_URL, - 'http_header' => $http_header_browser_fallback, - 'message' => 'URL (PATH) > BROWSER: with langage prefix, UI language is based on path prefix', - ), - // Default, browser language preference is not one of site's lang. - array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER, LANGUAGE_NEGOTIATION_DEFAULT), - 'path' => 'admin/config', - 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_DEFAULT, - 'http_header' => $http_header_blah, - 'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language', - ), - ); - - foreach ($tests as $test) { - $this->runTest($test); - } - - // Unknown language prefix should return 404. - variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, language_language_negotiation_info()); - $this->drupalGet("$langcode_unknown/admin/config", array(), $http_header_browser_fallback); - $this->assertResponse(404, "Unknown language path prefix should return 404"); - - // Setup for domain negotiation, first configure the language to have domain - // URL. - $edit = array("domain[$langcode]" => $language_domain); - $this->drupalPost("admin/config/regional/language/detection/url", $edit, t('Save configuration')); - // Set the site to use domain language negotiation. - - $tests = array( - // Default domain, browser preference should have no influence. - array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT), - 'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN, - 'path' => 'admin/config', - 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_DEFAULT, - 'http_header' => $http_header_browser_fallback, - 'message' => 'URL (DOMAIN) > DEFAULT: default domain should get default language', - ), - // Language domain specific URL, we set the $_SERVER['HTTP_HOST'] in - // language_test.module hook_boot() to simulate this. - array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT), - 'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN, - 'language_test_domain' => $language_domain, - 'path' => 'admin/config', - 'expect' => $language_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_URL, - 'http_header' => $http_header_browser_fallback, - 'message' => 'URL (DOMAIN) > DEFAULT: domain example.cn should switch to Chinese', - ), - ); - - foreach ($tests as $test) { - $this->runTest($test); - } - } - - protected function runTest($test) { - if (!empty($test['language_negotiation'])) { - $method_weights = array_flip($test['language_negotiation']); - language_negotiation_set(LANGUAGE_TYPE_INTERFACE, $method_weights); - } - if (!empty($test['language_negotiation_url_part'])) { - variable_set('language_negotiation_url_part', $test['language_negotiation_url_part']); - } - if (!empty($test['language_test_domain'])) { - variable_set('language_test_domain', $test['language_test_domain']); - } - $this->drupalGet($test['path'], array(), $test['http_header']); - $this->assertText($test['expect'], $test['message']); - $this->assertText(t('Language negotiation method: @name', array('@name' => $test['expected_method_id']))); - } - - /** - * Test URL language detection when the requested URL has no language. - */ - function testUrlLanguageFallback() { - // Add the Italian language. - $langcode_browser_fallback = 'it'; - $language = (object) array( - 'langcode' => $langcode_browser_fallback, - ); - language_save($language); - $languages = language_list(); - - // Enable the path prefix for the default language: this way any unprefixed - // URL must have a valid fallback value. - $edit = array('prefix[en]' => 'en'); - $this->drupalPost('admin/config/regional/language/detection/url', $edit, t('Save configuration')); - - // Enable browser and URL language detection. - $edit = array( - 'language_interface[enabled][language-browser]' => TRUE, - 'language_interface[enabled][language-url]' => TRUE, - 'language_interface[weight][language-browser]' => -8, - 'language_interface[weight][language-url]' => -10, - ); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - $this->drupalGet('admin/config/regional/language/detection'); - - // Enable the language switcher block. - $edit = array('blocks[language_language_interface][region]' => 'sidebar_first'); - $this->drupalPost('admin/structure/block', $edit, t('Save blocks')); - - // Access the front page without specifying any valid URL language prefix - // and having as browser language preference a non-default language. - $http_header = array("Accept-Language: $langcode_browser_fallback;q=1"); - $language = (object) array('langcode' => ''); - $this->drupalGet('', array('language' => $language), $http_header); - - // Check that the language switcher active link matches the given browser - // language. - $args = array(':url' => base_path() . $GLOBALS['script_path'] . $langcode_browser_fallback); - $fields = $this->xpath('//div[@id="block-language-language-interface"]//a[@class="language-link active" and starts-with(@href, :url)]', $args); - $this->assertTrue($fields[0] == $languages[$langcode_browser_fallback]->name, t('The browser language is the URL active language')); - - // Check that URLs are rewritten using the given browser language. - $fields = $this->xpath('//p[@id="site-name"]/strong/a[@rel="home" and @href=:url]', $args); - $this->assertTrue($fields[0] == 'Drupal', t('URLs are rewritten using the browser language.')); - } - - /** - * Tests url() when separate domains are used for multiple languages. - */ - function testLanguageDomain() { - // Add the Italian language. - $langcode = 'it'; - $language = (object) array( - 'langcode' => $langcode, - ); - language_save($language); - $languages = language_list(); - - // Enable browser and URL language detection. - $edit = array( - 'language_interface[enabled][language-url]' => TRUE, - 'language_interface[weight][language-url]' => -10, - ); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Change the domain for the Italian language. - $edit = array( - 'language_negotiation_url_part' => 1, - 'domain[it]' => 'it.example.com', - ); - $this->drupalPost('admin/config/regional/language/detection/url', $edit, t('Save configuration')); - - // Build the link we're going to test. - $link = 'it.example.com/admin'; - - global $is_https; - // Test URL in another language: http://it.example.com/admin. - // Base path gives problems on the testbot, so $correct_link is hard-coded. - // @see UrlAlterFunctionalTest::assertUrlOutboundAlter (path.test). - $italian_url = url('admin', array('language' => $languages['it'], 'script' => '')); - $url_scheme = ($is_https) ? 'https://' : 'http://'; - $correct_link = $url_scheme . $link; - $this->assertTrue($italian_url == $correct_link, t('The url() function returns the right url (@url) in accordance with the chosen language', array('@url' => $italian_url))); - - // Test https via options. - variable_set('https', TRUE); - $italian_url = url('admin', array('https' => TRUE, 'language' => $languages['it'], 'script' => '')); - $correct_link = 'https://' . $link; - $this->assertTrue($italian_url == $correct_link, t('The url() function returns the right https url (via options) (@url) in accordance with the chosen language', array('@url' => $italian_url))); - variable_set('https', FALSE); - - // Test https via current url scheme. - $temp_https = $is_https; - $is_https = TRUE; - $italian_url = url('admin', array('language' => $languages['it'], 'script' => '')); - $correct_link = 'https://' . $link; - $this->assertTrue($italian_url == $correct_link, t('The url() function returns the right url (via current url scheme) (@url) in accordance with the chosen language', array('@url' => $italian_url))); - $is_https = $temp_https; - } -} - -/** - * Test that URL rewriting works as expected. - */ -class LanguageUrlRewritingTestCase extends WebTestBase { - public static function getInfo() { - return array( - 'name' => 'URL rewriting', - 'description' => 'Test that URL rewriting works as expected.', - 'group' => 'Language', - ); - } - - function setUp() { - parent::setUp('language'); - - // Create and login user. - $this->web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); - $this->drupalLogin($this->web_user); - - // Install French language. - $edit = array(); - $edit['predefined_langcode'] = 'fr'; - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Enable URL language detection and selection. - $edit = array('language_interface[enabled][language-url]' => 1); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Reset static caching. - drupal_static_reset('language_list'); - drupal_static_reset('language_url_outbound_alter'); - drupal_static_reset('language_url_rewrite_url'); - } - - /** - * Check that non-installed languages are not considered. - */ - function testUrlRewritingEdgeCases() { - // Check URL rewriting with a non-installed language. - $non_existing = language_default(); - $non_existing->langcode = $this->randomName(); - $this->checkUrl($non_existing, t('Path language is ignored if language is not installed.'), t('URL language negotiation does not work with non-installed languages')); - } - - /** - * Check URL rewriting for the given language. - * - * The test is performed with a fixed URL (the default front page) to simply - * check that language prefixes are not added to it and that the prefixed URL - * is actually not working. - */ - private function checkUrl($language, $message1, $message2) { - $options = array('language' => $language, 'script' => ''); - $base_path = trim(base_path(), '/'); - $rewritten_path = trim(str_replace($base_path, '', url('node', $options)), '/'); - $segments = explode('/', $rewritten_path, 2); - $prefix = $segments[0]; - $path = isset($segments[1]) ? $segments[1] : $prefix; - - // If the rewritten URL has not a language prefix we pick a random prefix so - // we can always check the prefixed URL. - $prefixes = language_negotiation_url_prefixes(); - $stored_prefix = isset($prefixes[$language->langcode]) ? $prefixes[$language->langcode] : $this->randomName(); - if ($this->assertNotEqual($stored_prefix, $prefix, $message1)) { - $prefix = $stored_prefix; - } - - $this->drupalGet("$prefix/$path"); - $this->assertResponse(404, $message2); - } -} - -/** - * Functional test for language types/negotiation info. - */ -class LanguageNegotiationInfoTestCase extends WebTestBase { - - public static function getInfo() { - return array( - 'name' => 'Language negotiation info', - 'description' => 'Tests alterations to language types/negotiation info.', - 'group' => 'Language', - ); - } - - function setUp() { - parent::setUp('language'); - require_once DRUPAL_ROOT .'/core/includes/language.inc'; - $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'view the administration theme')); - $this->drupalLogin($admin_user); - $this->drupalPost('admin/config/regional/language/add', array('predefined_langcode' => 'it'), t('Add language')); - } - - /** - * Tests alterations to language types/negotiation info. - */ - function testInfoAlterations() { - // Enable language type/negotiation info alterations. - variable_set('language_test_language_types', TRUE); - variable_set('language_test_language_negotiation_info', TRUE); - $this->languageNegotiationUpdate(); - - // Check that fixed language types are properly configured without the need - // of saving the language negotiation settings. - $this->checkFixedLanguageTypes(); - - // Make the content language type configurable by updating the language - // negotiation settings with the proper flag enabled. - variable_set('language_test_content_language_type', TRUE); - $this->languageNegotiationUpdate(); - $type = LANGUAGE_TYPE_CONTENT; - $language_types = variable_get('language_types', language_types_get_default()); - $this->assertTrue($language_types[$type], t('Content language type is configurable.')); - - // Enable some core and custom language negotiation methods. The test - // language type is supposed to be configurable. - $test_type = 'test_language_type'; - $interface_method_id = LANGUAGE_NEGOTIATION_INTERFACE; - $test_method_id = 'test_language_negotiation_method'; - $form_field = $type . '[enabled]['. $interface_method_id .']'; - $edit = array( - $form_field => TRUE, - $type . '[enabled][' . $test_method_id . ']' => TRUE, - $test_type . '[enabled][' . $test_method_id . ']' => TRUE, - ); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Remove the interface language negotiation method by updating the language - // negotiation settings with the proper flag enabled. - variable_set('language_test_language_negotiation_info_alter', TRUE); - $this->languageNegotiationUpdate(); - $negotiation = variable_get("language_negotiation_$type", array()); - $this->assertFalse(isset($negotiation[$interface_method_id]), t('Interface language negotiation method removed from the stored settings.')); - $this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, t('Interface language negotiation method unavailable.')); - - // Check that type-specific language negotiation methods can be assigned - // only to the corresponding language types. - foreach (language_types_get_configurable() as $type) { - $form_field = $type . '[enabled][test_language_negotiation_method_ts]'; - if ($type == $test_type) { - $this->assertFieldByXPath("//input[@name=\"$form_field\"]", NULL, t('Type-specific test language negotiation method available for %type.', array('%type' => $type))); - } - else { - $this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, t('Type-specific test language negotiation method unavailable for %type.', array('%type' => $type))); - } - } - - // Check language negotiation results. - $this->drupalGet(''); - $last = variable_get('language_test_language_negotiation_last', array()); - foreach (language_types_get_all() as $type) { - $langcode = $last[$type]; - $value = $type == LANGUAGE_TYPE_CONTENT || strpos($type, 'test') !== FALSE ? 'it' : 'en'; - $this->assertEqual($langcode, $value, t('The negotiated language for %type is %language', array('%type' => $type, '%language' => $langcode))); - } - - // Disable language_test and check that everything is set back to the - // original status. - $this->languageNegotiationUpdate('disable'); - - // Check that only the core language types are available. - foreach (language_types_get_all() as $type) { - $this->assertTrue(strpos($type, 'test') === FALSE, t('The %type language is still available', array('%type' => $type))); - } - - // Check that fixed language types are properly configured, even those - // previously set to configurable. - $this->checkFixedLanguageTypes(); - - // Check that unavailable language negotiation methods are not present in - // the negotiation settings. - $negotiation = variable_get("language_negotiation_$type", array()); - $this->assertFalse(isset($negotiation[$test_method_id]), t('The disabled test language negotiation method is not part of the content language negotiation settings.')); - - // Check that configuration page presents the correct options and settings. - $this->assertNoRaw(t('Test language detection'), t('No test language type configuration available.')); - $this->assertNoRaw(t('This is a test language negotiation method'), t('No test language negotiation method available.')); - } - - /** - * Update language types/negotiation information. - * - * Manually invoke language_modules_enabled()/language_modules_disabled() - * since they would not be invoked after enabling/disabling language_test the - * first time. - */ - protected function languageNegotiationUpdate($op = 'enable') { - static $last_op = NULL; - $modules = array('language_test'); - - // Enable/disable language_test only if we did not already before. - if ($last_op != $op) { - $function = "module_{$op}"; - $function($modules); - // Reset hook implementation cache. - module_implements_reset(); - } - - drupal_static_reset('language_types_info'); - drupal_static_reset('language_negotiation_info'); - $function = "language_modules_{$op}d"; - if (function_exists($function)) { - $function($modules); - } - - $this->drupalGet('admin/config/regional/language/detection'); - } - - /** - * Check that language negotiation for fixed types matches the stored one. - */ - protected function checkFixedLanguageTypes() { - drupal_static_reset('language_types_info'); - foreach (language_types_info() as $type => $info) { - if (isset($info['fixed'])) { - $negotiation = variable_get("language_negotiation_$type", array()); - $equal = count($info['fixed']) == count($negotiation); - while ($equal && list($id) = each($negotiation)) { - list(, $info_id) = each($info['fixed']); - $equal = $info_id == $id; - } - $this->assertTrue($equal, t('language negotiation for %type is properly set up', array('%type' => $type))); - } - } - } -} - -/** - * Tests that paths are not prefixed on a monolingual site. - */ -class LanguagePathMonolingualTestCase extends WebTestBase { - public static function getInfo() { - return array( - 'name' => 'Paths on non-English monolingual sites', - 'description' => 'Confirm that paths are not changed on monolingual non-English sites', - 'group' => 'Language', - ); - } - - function setUp() { - parent::setUp('path', 'language'); - - // Create and login user. - $web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); - $this->drupalLogin($web_user); - - // Enable French language. - $edit = array(); - $edit['predefined_langcode'] = 'fr'; - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Make French the default language. - $edit = array('site_default' => 'fr'); - $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration')); - - // Delete English. - $this->drupalPost('admin/config/regional/language/delete/en', array(), t('Delete')); - - // Verify that French is the only language. - $this->assertFalse(language_multilingual(), t('Site is mono-lingual')); - $this->assertEqual(language_default()->langcode, 'fr', t('French is the default language')); - - // Set language detection to URL. - $edit = array('language_interface[enabled][language-url]' => TRUE); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Force languages to be initialized. - drupal_language_initialize(); - } - - /** - * Verifies that links do not have language prefixes in them. - */ - function testPageLinks() { - // Navigate to 'admin/config' path. - $this->drupalGet('admin/config'); - - // Verify that links in this page do not have a 'fr/' prefix. - $this->assertNoLinkByHref('/fr/', 'Links do not contain language prefix'); - - // Verify that links in this page can be followed and work. - $this->clickLink(t('Languages')); - $this->assertResponse(200, 'Clicked link results in a valid page'); - $this->assertText(t('Add language'), 'Page contains the add language text'); - } -} diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageBrowserDetectionUnitTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageBrowserDetectionUnitTest.php new file mode 100644 index 0000000000000000000000000000000000000000..715da9c0cd86fa7db892fb337c2165db4da31db2 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageBrowserDetectionUnitTest.php @@ -0,0 +1,131 @@ +<?php + +/** + * @file + * Definition of Drupal\language\Tests\LanguageBrowserDetectionUnitTest. + */ + +namespace Drupal\language\Tests; + +use Drupal\simpletest\UnitTestBase; + +/** + * Test browser language detection. + */ +class LanguageBrowserDetectionUnitTest extends UnitTestBase { + + public static function getInfo() { + return array( + 'name' => 'Browser language detection', + 'description' => 'Tests for the browser language detection.', + 'group' => 'Language', + ); + } + + /** + * Unit tests for the language_from_browser() function. + */ + function testLanguageFromBrowser() { + // Load the required functions. + require_once DRUPAL_ROOT . '/core/modules/language/language.negotiation.inc'; + + $languages = array( + // In our test case, 'en' has priority over 'en-US'. + 'en' => (object) array( + 'langcode' => 'en', + ), + 'en-US' => (object) array( + 'langcode' => 'en-US', + ), + // But 'fr-CA' has priority over 'fr'. + 'fr-CA' => (object) array( + 'langcode' => 'fr-CA', + ), + 'fr' => (object) array( + 'langcode' => 'fr', + ), + // 'es-MX' is alone. + 'es-MX' => (object) array( + 'langcode' => 'es-MX', + ), + // 'pt' is alone. + 'pt' => (object) array( + 'langcode' => '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( + 'langcode' => 'eh-oh-laa-laa', + ), + ); + + $test_cases = array( + // Equal qvalue for each language, choose the site prefered 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. + 'en-US,en;q=0.5,fr;q=0.25' => 'en-US', + '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 = language_from_browser($languages); + $this->assertIdentical($result, $expected_result, t("Language selection '@accept-language' selects '@result', result = '@actual'", array('@accept-language' => $accept_language, '@result' => $expected_result, '@actual' => isset($result) ? $result : 'none'))); + } + } +} diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageConfigurationTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageConfigurationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..1e5f430a621b5f9eaf5c05fb69af2b20e720c642 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageConfigurationTest.php @@ -0,0 +1,76 @@ +<?php + +/** + * @file + * Definition of Drupal\language\Tests\LanguageConfigurationTest. + */ + +namespace Drupal\language\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional tests for language configuration's effect on negotiation setup. + */ +class LanguageConfigurationTest extends WebTestBase { + public static function getInfo() { + return array( + 'name' => 'Language negotiation autoconfiguration', + 'description' => 'Adds and configures languages to check negotiation changes.', + 'group' => 'Language', + ); + } + + function setUp() { + parent::setUp('language'); + } + + /** + * 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); + + // Check if the Default English language has no path prefix. + $this->drupalGet('admin/config/regional/language/detection/url'); + $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', t('Default English has no path prefix.')); + + // Add predefined language. + $edit = array( + 'predefined_langcode' => 'fr', + ); + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + $this->assertText('French'); + $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); + + // Check if the Default English language has no path prefix. + $this->drupalGet('admin/config/regional/language/detection/url'); + $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', t('Default English has no path prefix.')); + // Check if French has a path prefix. + $this->drupalGet('admin/config/regional/language/detection/url'); + $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', t('French has a path prefix.')); + + // Check if we can change the default language. + $this->drupalGet('admin/config/regional/language'); + $this->assertFieldChecked('edit-site-default-en', t('English is the default language.')); + // Change the default language. + $edit = array( + 'site_default' => 'fr', + ); + $this->drupalPost(NULL, $edit, t('Save configuration')); + $this->assertNoFieldChecked('edit-site-default-en', t('Default language updated.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); + + // Check if a valid language prefix is added afrer changing the default + // language. + $this->drupalGet('admin/config/regional/language/detection/url'); + $this->assertFieldByXPath('//input[@name="prefix[en]"]', 'en', t('A valid path prefix has been added to the previous default language.')); + // Check if French still has a path prefix. + $this->drupalGet('admin/config/regional/language/detection/url'); + $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', t('French still has a path prefix.')); + } +} diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageDependencyInjectionTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageDependencyInjectionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e00053197e24128e90fe55ad3a80ad9bf83b1c26 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageDependencyInjectionTest.php @@ -0,0 +1,98 @@ +<?php + +/** + * @file + * Definition of Drupal\language\Tests\LanguageDependencyInjectionTest. + */ + +namespace Drupal\language\Tests; + +use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Language\Language; +use Drupal\simpletest\WebTestBase; + +/** + * Test for dependency injected language object. + */ +class LanguageDependencyInjectionTest extends WebTestBase { + public static function getInfo() { + return array( + 'name' => 'Language dependency injection', + 'description' => 'Compares the default language from $GLOBALS against the dependency injected language object.', + 'group' => 'Language', + ); + } + + function setUp() { + parent::setUp('language'); + + // Set up a new container to ensure we are building a new Language object + // for each test. + drupal_container(new ContainerBuilder()); + } + + /** + * Test dependency injected Language against the GLOBAL language object. + * + * @todo Once the PHP global is gone, we won't need this test as the same + * test is done without the PHP global in the following test. + */ + function testDependencyInjectedLanguage() { + // Initialize the language system. + drupal_language_initialize(); + + $expected = $GLOBALS[LANGUAGE_TYPE_INTERFACE]; + $result = drupal_container()->get(LANGUAGE_TYPE_INTERFACE); + foreach ($expected as $property => $value) { + $this->assertEqual($expected->$property, $result->$property, t('The dependency injected language object %prop property equals the $GLOBAL language object %prop property.', array('%prop' => $property))); + } + } + + /** + * Test dependency injected languages against a new Language object. + * + * @see Drupal\Core\Language\Language + */ + function testDependencyInjectedNewLanguage() { + // Initialize the language system. + drupal_language_initialize(); + + $expected = new Language((array) language_default()); + $result = drupal_container()->get(LANGUAGE_TYPE_INTERFACE); + foreach ($expected as $property => $value) { + $this->assertEqual($expected->$property, $result->$property, t('The dependency injected language object %prop property equals the new Language object %prop property.', array('%prop' => $property))); + } + } + + /** + * Test dependency injected Language object against a new default language + * object. + * + * @see Drupal\Core\Language\Language + */ + function testDependencyInjectedNewDefaultLanguage() { + // Change the language default object to different values. + $new_language_default = (object) array( + 'langcode' => 'fr', + 'name' => 'French', + 'direction' => 0, + 'weight' => 0, + 'default' => TRUE, + ); + variable_set('language_default', $new_language_default); + + // Initialize the language system. + drupal_language_initialize(); + + // The langauge system creates a Language object which contains the + // same properties as the new default language object. + $expected = $new_language_default; + $result = drupal_container()->get(LANGUAGE_TYPE_INTERFACE); + foreach ($expected as $property => $value) { + $this->assertEqual($expected->$property, $result->$property, t('The dependency injected language object %prop property equals the default language object %prop property.', array('%prop' => $property))); + } + + // Delete the language_default variable we previously set. + variable_del('language_default'); + } +} diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php new file mode 100644 index 0000000000000000000000000000000000000000..7346bfc0c907a99f38515cef01c1a700c14618c1 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php @@ -0,0 +1,165 @@ +<?php + +/** + * @file + * Definition of Drupal\language\Tests\LanguageListTest. + */ + +namespace Drupal\language\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional tests for the language list configuration forms. + */ +class LanguageListTest extends WebTestBase { + public static function getInfo() { + return array( + 'name' => 'Language list configuration', + 'description' => 'Adds a new language and tests changing its status and the default language.', + 'group' => 'Language', + ); + } + + function setUp() { + parent::setUp('language'); + } + + /** + * Functional tests for adding, editing and deleting languages. + */ + function testLanguageList() { + 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( + 'predefined_langcode' => 'fr', + ); + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + $this->assertText('French', t('Language added successfully.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); + + // Add custom language. + $langcode = 'xx'; + $name = $this->randomName(16); + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'name' => $name, + '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)), t('Correct page redirection.')); + $this->assertRaw('"edit-site-default-' . $langcode .'"', t('Language code found.')); + $this->assertText(t($name), t('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', t('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', t('Default language updated.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); + + // 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)), t('Correct page redirection.')); + $this->assertText(t('The default language cannot be deleted.'), t('Failed to delete the default language.')); + + // Ensure 'edit' link works. + $this->clickLink(t('edit')); + $this->assertTitle(t('Edit language | Drupal'), t('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, t('The language has been updated.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); + + // Change back the default language. + $edit = array( + 'site_default' => 'en', + ); + $this->drupalPost(NULL, $edit, t('Save configuration')); + // 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'), t('"delete" link is correct.')); + // Delete a 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)), t('Correct page redirection.')); + $this->assertRaw($name, t('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 %language and %langcode will add HTML. + $t_args = array('%language' => $name, '%langcode' => $langcode); + $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The test language has been removed.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); + // Verify that language is no longer found. + $this->drupalGet('admin/config/regional/language/delete/' . $langcode); + $this->assertResponse(404, t('Language no longer found.')); + // Make sure the "language_count" variable has been updated correctly. + drupal_static_reset('language_list'); + $languages = language_list(); + $this->assertEqual(variable_get('language_count', 1), count($languages), t('Language count is correct.')); + // Delete French. + $this->drupalPost('admin/config/regional/language/delete/fr', array(), t('Delete')); + // Get the count of languages. + drupal_static_reset('language_list'); + $languages = language_list(); + // We need raw here because %language and %langcode will add HTML. + $t_args = array('%language' => 'French', '%langcode' => 'fr'); + $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('Disabled language has been removed.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); + // Verify that language is no longer found. + $this->drupalGet('admin/config/regional/language/delete/fr'); + $this->assertResponse(404, t('Language no longer found.')); + // Make sure the "language_count" variable has not changed. + $this->assertEqual(variable_get('language_count', 1), count($languages), t('Language count is correct.')); + + // Ensure we can delete the English language. Right now English is the only + // language so we must add a new language and make it the default before + // deleting English. + $langcode = 'xx'; + $name = $this->randomName(16); + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'name' => $name, + '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)), t('Correct page redirection.')); + $this->assertText($name, t('Name found.')); + + // Check if we can change the default language. + $path = 'admin/config/regional/language'; + $this->drupalGet($path); + $this->assertFieldChecked('edit-site-default-en', t('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', t('Default language updated.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.')); + + $this->drupalPost('admin/config/regional/language/delete/en', array(), t('Delete')); + // We need raw here because %language and %langcode will add HTML. + $t_args = array('%language' => 'English', '%langcode' => 'en'); + $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The English language has been removed.')); + } +} diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d7d68e45fa38239c481c44599d4cd048a5795549 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php @@ -0,0 +1,165 @@ +<?php + +/** + * @file + * Definition of Drupal\language\Tests\LanguageNegotiationInfoTest. + */ + +namespace Drupal\language\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional test for language types/negotiation info. + */ +class LanguageNegotiationInfoTest extends WebTestBase { + + public static function getInfo() { + return array( + 'name' => 'Language negotiation info', + 'description' => 'Tests alterations to language types/negotiation info.', + 'group' => 'Language', + ); + } + + function setUp() { + parent::setUp('language'); + require_once DRUPAL_ROOT .'/core/includes/language.inc'; + $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'view the administration theme')); + $this->drupalLogin($admin_user); + $this->drupalPost('admin/config/regional/language/add', array('predefined_langcode' => 'it'), t('Add language')); + } + + /** + * Tests alterations to language types/negotiation info. + */ + function testInfoAlterations() { + // Enable language type/negotiation info alterations. + variable_set('language_test_language_types', TRUE); + variable_set('language_test_language_negotiation_info', TRUE); + $this->languageNegotiationUpdate(); + + // Check that fixed language types are properly configured without the need + // of saving the language negotiation settings. + $this->checkFixedLanguageTypes(); + + // Make the content language type configurable by updating the language + // negotiation settings with the proper flag enabled. + variable_set('language_test_content_language_type', TRUE); + $this->languageNegotiationUpdate(); + $type = LANGUAGE_TYPE_CONTENT; + $language_types = variable_get('language_types', language_types_get_default()); + $this->assertTrue($language_types[$type], t('Content language type is configurable.')); + + // Enable some core and custom language negotiation methods. The test + // language type is supposed to be configurable. + $test_type = 'test_language_type'; + $interface_method_id = LANGUAGE_NEGOTIATION_INTERFACE; + $test_method_id = 'test_language_negotiation_method'; + $form_field = $type . '[enabled]['. $interface_method_id .']'; + $edit = array( + $form_field => TRUE, + $type . '[enabled][' . $test_method_id . ']' => TRUE, + $test_type . '[enabled][' . $test_method_id . ']' => TRUE, + ); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Remove the interface language negotiation method by updating the language + // negotiation settings with the proper flag enabled. + variable_set('language_test_language_negotiation_info_alter', TRUE); + $this->languageNegotiationUpdate(); + $negotiation = variable_get("language_negotiation_$type", array()); + $this->assertFalse(isset($negotiation[$interface_method_id]), t('Interface language negotiation method removed from the stored settings.')); + $this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, t('Interface language negotiation method unavailable.')); + + // Check that type-specific language negotiation methods can be assigned + // only to the corresponding language types. + foreach (language_types_get_configurable() as $type) { + $form_field = $type . '[enabled][test_language_negotiation_method_ts]'; + if ($type == $test_type) { + $this->assertFieldByXPath("//input[@name=\"$form_field\"]", NULL, t('Type-specific test language negotiation method available for %type.', array('%type' => $type))); + } + else { + $this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, t('Type-specific test language negotiation method unavailable for %type.', array('%type' => $type))); + } + } + + // Check language negotiation results. + $this->drupalGet(''); + $last = variable_get('language_test_language_negotiation_last', array()); + foreach (language_types_get_all() as $type) { + $langcode = $last[$type]; + $value = $type == LANGUAGE_TYPE_CONTENT || strpos($type, 'test') !== FALSE ? 'it' : 'en'; + $this->assertEqual($langcode, $value, t('The negotiated language for %type is %language', array('%type' => $type, '%language' => $langcode))); + } + + // Disable language_test and check that everything is set back to the + // original status. + $this->languageNegotiationUpdate('disable'); + + // Check that only the core language types are available. + foreach (language_types_get_all() as $type) { + $this->assertTrue(strpos($type, 'test') === FALSE, t('The %type language is still available', array('%type' => $type))); + } + + // Check that fixed language types are properly configured, even those + // previously set to configurable. + $this->checkFixedLanguageTypes(); + + // Check that unavailable language negotiation methods are not present in + // the negotiation settings. + $negotiation = variable_get("language_negotiation_$type", array()); + $this->assertFalse(isset($negotiation[$test_method_id]), t('The disabled test language negotiation method is not part of the content language negotiation settings.')); + + // Check that configuration page presents the correct options and settings. + $this->assertNoRaw(t('Test language detection'), t('No test language type configuration available.')); + $this->assertNoRaw(t('This is a test language negotiation method'), t('No test language negotiation method available.')); + } + + /** + * Update language types/negotiation information. + * + * Manually invoke language_modules_enabled()/language_modules_disabled() + * since they would not be invoked after enabling/disabling language_test the + * first time. + */ + protected function languageNegotiationUpdate($op = 'enable') { + static $last_op = NULL; + $modules = array('language_test'); + + // Enable/disable language_test only if we did not already before. + if ($last_op != $op) { + $function = "module_{$op}"; + $function($modules); + // Reset hook implementation cache. + module_implements_reset(); + } + + drupal_static_reset('language_types_info'); + drupal_static_reset('language_negotiation_info'); + $function = "language_modules_{$op}d"; + if (function_exists($function)) { + $function($modules); + } + + $this->drupalGet('admin/config/regional/language/detection'); + } + + /** + * Check that language negotiation for fixed types matches the stored one. + */ + protected function checkFixedLanguageTypes() { + drupal_static_reset('language_types_info'); + foreach (language_types_info() as $type => $info) { + if (isset($info['fixed'])) { + $negotiation = variable_get("language_negotiation_$type", array()); + $equal = count($info['fixed']) == count($negotiation); + while ($equal && list($id) = each($negotiation)) { + list(, $info_id) = each($info['fixed']); + $equal = $info_id == $id; + } + $this->assertTrue($equal, t('language negotiation for %type is properly set up', array('%type' => $type))); + } + } + } +} diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguagePathMonolingualTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguagePathMonolingualTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4f80277e7df585db406838ab6041624f4628b513 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/LanguagePathMonolingualTest.php @@ -0,0 +1,70 @@ +<?php + +/** + * @file + * Definition of Drupal\language\Tests\LanguagePathMonolingualTest. + */ + +namespace Drupal\language\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Tests that paths are not prefixed on a monolingual site. + */ +class LanguagePathMonolingualTest extends WebTestBase { + public static function getInfo() { + return array( + 'name' => 'Paths on non-English monolingual sites', + 'description' => 'Confirm that paths are not changed on monolingual non-English sites', + 'group' => 'Language', + ); + } + + function setUp() { + parent::setUp('path', 'language'); + + // Create and login user. + $web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); + $this->drupalLogin($web_user); + + // Enable French language. + $edit = array(); + $edit['predefined_langcode'] = 'fr'; + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Make French the default language. + $edit = array('site_default' => 'fr'); + $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration')); + + // Delete English. + $this->drupalPost('admin/config/regional/language/delete/en', array(), t('Delete')); + + // Verify that French is the only language. + $this->assertFalse(language_multilingual(), t('Site is mono-lingual')); + $this->assertEqual(language_default()->langcode, 'fr', t('French is the default language')); + + // Set language detection to URL. + $edit = array('language_interface[enabled][language-url]' => TRUE); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Force languages to be initialized. + drupal_language_initialize(); + } + + /** + * Verifies that links do not have language prefixes in them. + */ + function testPageLinks() { + // Navigate to 'admin/config' path. + $this->drupalGet('admin/config'); + + // Verify that links in this page do not have a 'fr/' prefix. + $this->assertNoLinkByHref('/fr/', 'Links do not contain language prefix'); + + // Verify that links in this page can be followed and work. + $this->clickLink(t('Languages')); + $this->assertResponse(200, 'Clicked link results in a valid page'); + $this->assertText(t('Add language'), 'Page contains the add language text'); + } +} diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageSwitchingTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageSwitchingTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c8c67327aa51b07c1c6796edb4817edcfaa2d23d --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageSwitchingTest.php @@ -0,0 +1,88 @@ +<?php + +/** + * @file + * Definition of Drupal\language\Tests\LanguageSwitchingTest. + */ + +namespace Drupal\language\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional tests for the language switching feature. + */ +class LanguageSwitchingTest extends WebTestBase { + + public static function getInfo() { + return array( + 'name' => 'Language switching', + 'description' => 'Tests for the language switching feature.', + 'group' => 'Language', + ); + } + + function setUp() { + parent::setUp(array('language', 'block')); + + // Create and login user. + $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer languages', '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[language_{$language_type}][region]" => 'sidebar_first', + ); + $this->drupalPost('admin/structure/block', $edit, t('Save blocks')); + + // Add language. + $edit = array( + 'predefined_langcode' => 'fr', + ); + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Enable URL language detection and selection. + $edit = array('language_interface[enabled][language-url]' => '1'); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Assert that the language switching block is displayed on the frontpage. + $this->drupalGet(''); + $this->assertText(t('Languages'), t('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-language-' . str_replace('_', '-', $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($langcode) = array_intersect($classes, array('en', 'fr')); + if (in_array('active', $classes)) { + $links['active'][] = $langcode; + } + else { + $links['inactive'][] = $langcode; + } + $anchor_classes = explode(" ", (string) $link->a['class']); + if (in_array('active', $anchor_classes)) { + $anchors['active'][] = $langcode; + } + else { + $anchors['inactive'][] = $langcode; + } + } + $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), t('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')), t('Only the current language anchor is marked as active on the language switcher block.')); + } +} diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5958ac84d7f76ccc948257ff65e14f2aec74168f --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php @@ -0,0 +1,330 @@ +<?php + +/** + * @file + * Definition of Drupal\language\Tests\LanguageUILanguageNegotiationTest. + */ + +namespace Drupal\language\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Test UI language negotiation + * + * 1. URL (PATH) > DEFAULT + * UI Language base on URL prefix, browser language preference has no + * influence: + * admin/config + * UI in site default language + * zh-hans/admin/config + * UI in Chinese + * blah-blah/admin/config + * 404 + * 2. URL (PATH) > BROWSER > DEFAULT + * admin/config + * UI in user's browser language preference if the site has that + * language enabled, if not, the default language + * zh-hans/admin/config + * UI in Chinese + * blah-blah/admin/config + * 404 + * 3. URL (DOMAIN) > DEFAULT + * http://example.com/admin/config + * UI language in site default + * http://example.cn/admin/config + * UI language in Chinese + */ +class LanguageUILanguageNegotiationTest extends WebTestBase { + public static function getInfo() { + return array( + 'name' => 'UI language negotiation', + 'description' => 'Test UI language switching by url path prefix and domain.', + 'group' => 'Language', + ); + } + + function setUp() { + // We marginally use interface translation functionality here, so need to + // use the locale module instead of language only, but the 90% of the test + // is about the negotiation process which is solely in language module. + parent::setUp(array('locale', 'language_test', 'block')); + require_once DRUPAL_ROOT . '/core/includes/language.inc'; + drupal_load('module', 'locale'); + $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages', 'administer blocks')); + $this->drupalLogin($admin_user); + } + + /** + * Tests for language switching by URL path. + */ + function testUILanguageNegotiation() { + // A few languages to switch to. + // This one is unknown, should get the default lang version. + $langcode_unknown = 'blah-blah'; + // For testing browser lang preference. + $langcode_browser_fallback = 'vi'; + // For testing path prefix. + $langcode = 'zh-hans'; + // For setting browser language preference to 'vi'. + $http_header_browser_fallback = array("Accept-Language: $langcode_browser_fallback;q=1"); + // For setting browser language preference to some unknown. + $http_header_blah = array("Accept-Language: blah;q=1"); + + // This domain should switch the UI to Chinese. + $language_domain = 'example.cn'; + + // Setup the site languages by installing two languages. + $language = (object) array( + 'langcode' => $langcode_browser_fallback, + ); + language_save($language); + $language = (object) array( + 'langcode' => $langcode, + ); + language_save($language); + + // We will look for this string in the admin/config screen to see if the + // corresponding translated string is shown. + $default_string = 'Configure languages for content and the user interface'; + + // Set the default language in order for the translated string to be registered + // into database when seen by t(). Without doing this, our target string + // is for some reason not found when doing translate search. This might + // be some bug. + drupal_static_reset('language_list'); + $languages = language_list(); + variable_set('language_default', $languages['vi']); + // First visit this page to make sure our target string is searchable. + $this->drupalGet('admin/config'); + // Now the t()'ed string is in db so switch the language back to default. + variable_del('language_default'); + + // Translate the string. + $language_browser_fallback_string = "In $langcode_browser_fallback In $langcode_browser_fallback In $langcode_browser_fallback"; + $language_string = "In $langcode In $langcode In $langcode"; + // Do a translate search of our target string. + $edit = array( 'string' => $default_string); + $this->drupalPost('admin/config/regional/translate/translate', $edit, t('Filter')); + // Should find the string and now click edit to post translated string. + $this->clickLink('edit'); + $edit = array( + "translations[$langcode_browser_fallback][0]" => $language_browser_fallback_string, + "translations[$langcode][0]" => $language_string, + ); + $this->drupalPost(NULL, $edit, t('Save translations')); + + // Configure URL language rewrite. + variable_set('language_negotiation_url_type', LANGUAGE_TYPE_INTERFACE); + + $tests = array( + // Default, browser preference should have no influence. + array( + 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT), + 'path' => 'admin/config', + 'expect' => $default_string, + 'expected_method_id' => LANGUAGE_NEGOTIATION_DEFAULT, + 'http_header' => $http_header_browser_fallback, + 'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.', + ), + // Language prefix. + array( + 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT), + 'path' => "$langcode/admin/config", + 'expect' => $language_string, + 'expected_method_id' => LANGUAGE_NEGOTIATION_URL, + 'http_header' => $http_header_browser_fallback, + 'message' => 'URL (PATH) > DEFAULT: with language prefix, UI language is switched based on path prefix', + ), + // Default, go by browser preference. + array( + 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER), + 'path' => 'admin/config', + 'expect' => $language_browser_fallback_string, + 'expected_method_id' => LANGUAGE_NEGOTIATION_BROWSER, + 'http_header' => $http_header_browser_fallback, + 'message' => 'URL (PATH) > BROWSER: no language prefix, UI language is determined by browser language preference', + ), + // Prefix, switch to the language. + array( + 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER), + 'path' => "$langcode/admin/config", + 'expect' => $language_string, + 'expected_method_id' => LANGUAGE_NEGOTIATION_URL, + 'http_header' => $http_header_browser_fallback, + 'message' => 'URL (PATH) > BROWSER: with langage prefix, UI language is based on path prefix', + ), + // Default, browser language preference is not one of site's lang. + array( + 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER, LANGUAGE_NEGOTIATION_DEFAULT), + 'path' => 'admin/config', + 'expect' => $default_string, + 'expected_method_id' => LANGUAGE_NEGOTIATION_DEFAULT, + 'http_header' => $http_header_blah, + 'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language', + ), + ); + + foreach ($tests as $test) { + $this->runTest($test); + } + + // Unknown language prefix should return 404. + variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, language_language_negotiation_info()); + $this->drupalGet("$langcode_unknown/admin/config", array(), $http_header_browser_fallback); + $this->assertResponse(404, "Unknown language path prefix should return 404"); + + // Setup for domain negotiation, first configure the language to have domain + // URL. + $edit = array("domain[$langcode]" => $language_domain); + $this->drupalPost("admin/config/regional/language/detection/url", $edit, t('Save configuration')); + // Set the site to use domain language negotiation. + + $tests = array( + // Default domain, browser preference should have no influence. + array( + 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT), + 'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN, + 'path' => 'admin/config', + 'expect' => $default_string, + 'expected_method_id' => LANGUAGE_NEGOTIATION_DEFAULT, + 'http_header' => $http_header_browser_fallback, + 'message' => 'URL (DOMAIN) > DEFAULT: default domain should get default language', + ), + // Language domain specific URL, we set the $_SERVER['HTTP_HOST'] in + // language_test.module hook_boot() to simulate this. + array( + 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT), + 'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN, + 'language_test_domain' => $language_domain . ':88', + 'path' => 'admin/config', + 'expect' => $language_string, + 'expected_method_id' => LANGUAGE_NEGOTIATION_URL, + 'http_header' => $http_header_browser_fallback, + 'message' => 'URL (DOMAIN) > DEFAULT: domain example.cn should switch to Chinese', + ), + ); + + foreach ($tests as $test) { + $this->runTest($test); + } + } + + protected function runTest($test) { + if (!empty($test['language_negotiation'])) { + $method_weights = array_flip($test['language_negotiation']); + language_negotiation_set(LANGUAGE_TYPE_INTERFACE, $method_weights); + } + if (!empty($test['language_negotiation_url_part'])) { + variable_set('language_negotiation_url_part', $test['language_negotiation_url_part']); + } + if (!empty($test['language_test_domain'])) { + variable_set('language_test_domain', $test['language_test_domain']); + } + $this->drupalGet($test['path'], array(), $test['http_header']); + $this->assertText($test['expect'], $test['message']); + $this->assertText(t('Language negotiation method: @name', array('@name' => $test['expected_method_id']))); + } + + /** + * Test URL language detection when the requested URL has no language. + */ + function testUrlLanguageFallback() { + // Add the Italian language. + $langcode_browser_fallback = 'it'; + $language = (object) array( + 'langcode' => $langcode_browser_fallback, + ); + language_save($language); + $languages = language_list(); + + // Enable the path prefix for the default language: this way any unprefixed + // URL must have a valid fallback value. + $edit = array('prefix[en]' => 'en'); + $this->drupalPost('admin/config/regional/language/detection/url', $edit, t('Save configuration')); + + // Enable browser and URL language detection. + $edit = array( + 'language_interface[enabled][language-browser]' => TRUE, + 'language_interface[enabled][language-url]' => TRUE, + 'language_interface[weight][language-browser]' => -8, + 'language_interface[weight][language-url]' => -10, + ); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + $this->drupalGet('admin/config/regional/language/detection'); + + // Enable the language switcher block. + $edit = array('blocks[language_language_interface][region]' => 'sidebar_first'); + $this->drupalPost('admin/structure/block', $edit, t('Save blocks')); + + // Access the front page without specifying any valid URL language prefix + // and having as browser language preference a non-default language. + $http_header = array("Accept-Language: $langcode_browser_fallback;q=1"); + $language = (object) array('langcode' => ''); + $this->drupalGet('', array('language' => $language), $http_header); + + // Check that the language switcher active link matches the given browser + // language. + $args = array(':url' => base_path() . $GLOBALS['script_path'] . $langcode_browser_fallback); + $fields = $this->xpath('//div[@id="block-language-language-interface"]//a[@class="language-link active" and starts-with(@href, :url)]', $args); + $this->assertTrue($fields[0] == $languages[$langcode_browser_fallback]->name, t('The browser language is the URL active language')); + + // Check that URLs are rewritten using the given browser language. + $fields = $this->xpath('//p[@id="site-name"]/strong/a[@rel="home" and @href=:url]', $args); + $this->assertTrue($fields[0] == 'Drupal', t('URLs are rewritten using the browser language.')); + } + + /** + * Tests url() when separate domains are used for multiple languages. + */ + function testLanguageDomain() { + // Add the Italian language. + $langcode = 'it'; + $language = (object) array( + 'langcode' => $langcode, + ); + language_save($language); + $languages = language_list(); + + // Enable browser and URL language detection. + $edit = array( + 'language_interface[enabled][language-url]' => TRUE, + 'language_interface[weight][language-url]' => -10, + ); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Change the domain for the Italian language. + $edit = array( + 'language_negotiation_url_part' => 1, + 'domain[it]' => 'it.example.com', + ); + $this->drupalPost('admin/config/regional/language/detection/url', $edit, t('Save configuration')); + + // Build the link we're going to test. + $link = 'it.example.com/admin'; + + global $is_https; + // Test URL in another language: http://it.example.com/admin. + // Base path gives problems on the testbot, so $correct_link is hard-coded. + // @see UrlAlterFunctionalTest::assertUrlOutboundAlter (path.test). + $italian_url = url('admin', array('language' => $languages['it'], 'script' => '')); + $url_scheme = ($is_https) ? 'https://' : 'http://'; + $correct_link = $url_scheme . $link; + $this->assertTrue($italian_url == $correct_link, t('The url() function returns the right url (@url) in accordance with the chosen language', array('@url' => $italian_url))); + + // Test https via options. + variable_set('https', TRUE); + $italian_url = url('admin', array('https' => TRUE, 'language' => $languages['it'], 'script' => '')); + $correct_link = 'https://' . $link; + $this->assertTrue($italian_url == $correct_link, t('The url() function returns the right https url (via options) (@url) in accordance with the chosen language', array('@url' => $italian_url))); + variable_set('https', FALSE); + + // Test https via current url scheme. + $temp_https = $is_https; + $is_https = TRUE; + $italian_url = url('admin', array('language' => $languages['it'], 'script' => '')); + $correct_link = 'https://' . $link; + $this->assertTrue($italian_url == $correct_link, t('The url() function returns the right url (via current url scheme) (@url) in accordance with the chosen language', array('@url' => $italian_url))); + $is_https = $temp_https; + } +} diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageUrlRewritingTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageUrlRewritingTest.php new file mode 100644 index 0000000000000000000000000000000000000000..137bba971645a1bbbb5b673c26a201e512370c83 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageUrlRewritingTest.php @@ -0,0 +1,82 @@ +<?php + +/** + * @file + * Definition of Drupal\language\Tests\LanguageUrlRewritingTest. + */ + +namespace Drupal\language\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Test that URL rewriting works as expected. + */ +class LanguageUrlRewritingTest extends WebTestBase { + public static function getInfo() { + return array( + 'name' => 'URL rewriting', + 'description' => 'Test that URL rewriting works as expected.', + 'group' => 'Language', + ); + } + + function setUp() { + parent::setUp('language'); + + // Create and login user. + $this->web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); + $this->drupalLogin($this->web_user); + + // Install French language. + $edit = array(); + $edit['predefined_langcode'] = 'fr'; + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Enable URL language detection and selection. + $edit = array('language_interface[enabled][language-url]' => 1); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Reset static caching. + drupal_static_reset('language_list'); + drupal_static_reset('language_url_outbound_alter'); + drupal_static_reset('language_url_rewrite_url'); + } + + /** + * Check that non-installed languages are not considered. + */ + function testUrlRewritingEdgeCases() { + // Check URL rewriting with a non-installed language. + $non_existing = language_default(); + $non_existing->langcode = $this->randomName(); + $this->checkUrl($non_existing, t('Path language is ignored if language is not installed.'), t('URL language negotiation does not work with non-installed languages')); + } + + /** + * Check URL rewriting for the given language. + * + * The test is performed with a fixed URL (the default front page) to simply + * check that language prefixes are not added to it and that the prefixed URL + * is actually not working. + */ + private function checkUrl($language, $message1, $message2) { + $options = array('language' => $language, 'script' => ''); + $base_path = trim(base_path(), '/'); + $rewritten_path = trim(str_replace($base_path, '', url('node', $options)), '/'); + $segments = explode('/', $rewritten_path, 2); + $prefix = $segments[0]; + $path = isset($segments[1]) ? $segments[1] : $prefix; + + // If the rewritten URL has not a language prefix we pick a random prefix so + // we can always check the prefixed URL. + $prefixes = language_negotiation_url_prefixes(); + $stored_prefix = isset($prefixes[$language->langcode]) ? $prefixes[$language->langcode] : $this->randomName(); + if ($this->assertNotEqual($stored_prefix, $prefix, $message1)) { + $prefix = $stored_prefix; + } + + $this->drupalGet("$prefix/$path"); + $this->assertResponse(404, $message2); + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleCommentLanguageTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleCommentLanguageTest.php new file mode 100644 index 0000000000000000000000000000000000000000..16fc83ff778fba7395802351503240c870dd5c31 --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleCommentLanguageTest.php @@ -0,0 +1,108 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleCommentLanguageTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional tests for comment language. + */ +class LocaleCommentLanguageTest extends WebTestBase { + protected $profile = 'standard'; + + public static function getInfo() { + return array( + 'name' => 'Comment language', + 'description' => 'Tests for comment language.', + 'group' => 'Locale', + ); + } + + function setUp() { + // We also use language_test module here to be able to turn on content + // language negotiation. Drupal core does not provide a way in itself + // to do that. + parent::setUp('locale', 'language_test'); + + // Create and login user. + $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'create article content')); + $this->drupalLogin($admin_user); + + // Add language. + $edit = array('predefined_langcode' => 'fr'); + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Set "Article" content type to use multilingual support. + $edit = array('node_type_language' => 1); + $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type')); + + // Enable content language negotiation UI. + variable_set('language_test_content_language_type', TRUE); + + // Set interface language detection to user and content language detection + // to URL. Disable inheritance from interface language to ensure content + // language will fall back to the default language if no URL language can be + // detected. + $edit = array( + 'language_interface[enabled][language-user]' => TRUE, + 'language_content[enabled][language-url]' => TRUE, + 'language_content[enabled][language-interface]' => FALSE, + ); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Change user language preference, this way interface language is always + // French no matter what path prefix the URLs have. + $edit = array('preferred_langcode' => 'fr'); + $this->drupalPost("user/{$admin_user->uid}/edit", $edit, t('Save')); + } + + /** + * Test that comment language is properly set. + */ + function testCommentLanguage() { + drupal_static_reset('language_list'); + + // Create two nodes, one for english and one for french, and comment each + // node using both english and french as content language by changing URL + // language prefixes. Meanwhile interface language is always French, which + // is the user language preference. This way we can ensure that node + // language and interface language do not influence comment language, as + // only content language has to. + foreach (language_list() as $node_langcode => $node_language) { + $langcode_not_specified = LANGUAGE_NOT_SPECIFIED; + + // Create "Article" content. + $title = $this->randomName(); + $edit = array( + "title" => $title, + "body[$langcode_not_specified][0][value]" => $this->randomName(), + "langcode" => $node_langcode, + ); + $this->drupalPost("node/add/article", $edit, t('Save')); + $node = $this->drupalGetNodeByTitle($title); + + $prefixes = language_negotiation_url_prefixes(); + foreach (language_list() as $langcode => $language) { + // Post a comment with content language $langcode. + $prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/'; + $edit = array("comment_body[$langcode_not_specified][0][value]" => $this->randomName()); + $this->drupalPost("{$prefix}node/{$node->nid}", $edit, t('Save')); + + // Check that comment language matches the current content language. + $comment = db_select('comment', 'c') + ->fields('c') + ->condition('nid', $node->nid) + ->orderBy('cid', 'DESC') + ->execute() + ->fetchObject(); + $args = array('%node_language' => $node_langcode, '%comment_language' => $comment->langcode, '%langcode' => $langcode); + $this->assertEqual($comment->langcode, $langcode, t('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args)); + } + } + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleContentTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleContentTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0c5cea3689dc1b865bd435d9adb8c94c0017958c --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleContentTest.php @@ -0,0 +1,210 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleContentTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional tests for multilingual support on nodes. + */ +class LocaleContentTest extends WebTestBase { + protected $profile = 'standard'; + + public static function getInfo() { + return array( + 'name' => 'Content language settings', + 'description' => 'Checks you can enable multilingual support on content types and configure a language for a node.', + 'group' => 'Locale', + ); + } + + function setUp() { + parent::setUp('locale'); + } + + /** + * Verifies that machine name fields are always LTR. + */ + function testMachineNameLTR() { + // User to add and remove language. + $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages')); + + // Log in as admin. + $this->drupalLogin($admin_user); + + // Verify that the machine name field is LTR for a new content type. + $this->drupalGet('admin/structure/types/add'); + $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when no additional language is configured.'); + + // Install the Arabic language (which is RTL) and configure as the default. + $edit = array(); + $edit['predefined_langcode'] = 'ar'; + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + $edit = array(); + $edit['site_default'] = 'ar'; + $this->drupalPost(NULL, $edit, t('Save configuration')); + + // Verify that the machine name field is still LTR for a new content type. + $this->drupalGet('admin/structure/types/add'); + $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when the default language is RTL.'); + } + + /** + * Test if a content type can be set to multilingual and language is present. + */ + function testContentTypeLanguageConfiguration() { + global $base_url; + + // User to add and remove language. + $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages')); + // User to create a node. + $web_user = $this->drupalCreateUser(array('create article content', 'create page content', 'edit any page content')); + + // Add custom language. + $this->drupalLogin($admin_user); + // Code for the language. + $langcode = 'xx'; + // The English name for the language. + $name = $this->randomName(16); + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'name' => $name, + 'direction' => '0', + ); + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); + + // Set "Basic page" content type to use multilingual support. + $this->drupalGet('admin/structure/types/manage/page'); + $this->assertText(t('Multilingual support'), t('Multilingual support fieldset present on content type configuration form.')); + $edit = array( + 'node_type_language' => 1, + ); + $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type')); + $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.')); + $this->drupalLogout(); + + // Verify language selection is not present on add article form. + $this->drupalLogin($web_user); + $this->drupalGet('node/add/article'); + // Verify language select list is not present. + $this->assertNoFieldByName('language', NULL, t('Language select not present on add article form.')); + + // Verify language selection appears on add "Basic page" form. + $this->drupalGet('node/add/page'); + // Verify language select list is present. + $this->assertFieldByName('langcode', NULL, t('Language select present on add Basic page form.')); + // Ensure language appears. + $this->assertText($name, t('Language present.')); + + // Create "Basic page" content. + $node_title = $this->randomName(); + $node_body = $this->randomName(); + $edit = array( + 'type' => 'page', + 'title' => $node_title, + 'body' => array($langcode => array(array('value' => $node_body))), + 'langcode' => $langcode, + ); + $node = $this->drupalCreateNode($edit); + // Edit the content and ensure correct language is selected. + $path = 'node/' . $node->nid . '/edit'; + $this->drupalGet($path); + $this->assertRaw('<option value="' . $langcode . '" selected="selected">' . $name . '</option>', t('Correct language selected.')); + // Ensure we can change the node language. + $edit = array( + 'langcode' => 'en', + ); + $this->drupalPost($path, $edit, t('Save')); + $this->assertRaw(t('%title has been updated.', array('%title' => $node_title)), t('Basic page content updated.')); + + $this->drupalLogout(); + } + + /** + * Test if a dir and lang tags exist in node's attributes. + */ + function testContentTypeDirLang() { + // User to add and remove language. + $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages')); + // User to create a node. + $web_user = $this->drupalCreateUser(array('create article content', 'edit own article content')); + + // Login as admin. + $this->drupalLogin($admin_user); + + // Install Arabic language. + $edit = array(); + $edit['predefined_langcode'] = 'ar'; + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Install Spanish language. + $edit = array(); + $edit['predefined_langcode'] = 'es'; + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Set "Article" content type to use multilingual support. + $this->drupalGet('admin/structure/types/manage/article'); + $edit = array( + 'node_type_language' => 1, + ); + $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type')); + $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Article')), t('Article content type has been updated.')); + $this->drupalLogout(); + + // Login as web user to add new article. + $this->drupalLogin($web_user); + + // Create three nodes: English, Arabic and Spanish. + $node_en = $this->createNodeArticle('en'); + $node_ar = $this->createNodeArticle('ar'); + $node_es = $this->createNodeArticle('es'); + + $this->drupalGet('node'); + + // Check if English node does not have lang tag. + $pattern = '|id="node-' . $node_en->nid . '"[^<>]*lang="en"|'; + $this->assertNoPattern($pattern, t('The lang tag has not been assigned to the English node.')); + + // Check if English node does not have dir tag. + $pattern = '|id="node-' . $node_en->nid . '"[^<>]*dir="ltr"|'; + $this->assertNoPattern($pattern, t('The dir tag has not been assigned to the English node.')); + + // Check if Arabic node has lang="ar" & dir="rtl" tags. + $pattern = '|id="node-' . $node_ar->nid . '"[^<>]*lang="ar" dir="rtl"|'; + $this->assertPattern($pattern, t('The lang and dir tags have been assigned correctly to the Arabic node.')); + + // Check if Spanish node has lang="es" tag. + $pattern = '|id="node-' . $node_es->nid . '"[^<>]*lang="es"|'; + $this->assertPattern($pattern, t('The lang tag has been assigned correctly to the Spanish node.')); + + // Check if Spanish node does not have dir="ltr" tag. + $pattern = '|id="node-' . $node_es->nid . '"[^<>]*lang="es" dir="ltr"|'; + $this->assertNoPattern($pattern, t('The dir tag has not been assigned to the Spanish node.')); + + $this->drupalLogout(); + } + + /** + * Create node in a specific language. + */ + protected function createNodeArticle($langcode) { + $this->drupalGet('node/add/article'); + $node_title = $this->randomName(); + $node_body = $this->randomName(); + $edit = array( + 'type' => 'article', + 'title' => $node_title, + 'body' => array($langcode => array(array('value' => $node_body))), + 'langcode' => $langcode, + 'promote' => 1, + ); + return $this->drupalCreateNode($edit); + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleDateFormatsTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleDateFormatsTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e5514b8b1a9d3ca76f489f3cf6fcab3636b36b84 --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleDateFormatsTest.php @@ -0,0 +1,82 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleDateFormatsTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional tests for localizing date formats. + */ +class LocaleDateFormatsTest extends WebTestBase { + + public static function getInfo() { + return array( + 'name' => 'Localize date formats', + 'description' => 'Tests for the localization of date formats.', + 'group' => 'Locale', + ); + } + + function setUp() { + parent::setUp(array('node', 'locale')); + + // Create Article node type. + $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); + + // Create and login user. + $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'create article content')); + $this->drupalLogin($admin_user); + } + + /** + * Functional tests for localizing date formats. + */ + function testLocalizeDateFormats() { + // Add language. + $edit = array( + 'predefined_langcode' => 'fr', + ); + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Set language negotiation. + $language_type = LANGUAGE_TYPE_INTERFACE; + $edit = array( + "{$language_type}[enabled][language-url]" => TRUE, + ); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Configure date formats. + $this->drupalGet('admin/config/regional/date-time/locale'); + $this->assertText('French', 'Configured languages appear.'); + $edit = array( + 'date_format_long' => 'd.m.Y - H:i', + 'date_format_medium' => 'd.m.Y - H:i', + 'date_format_short' => 'd.m.Y - H:i', + ); + $this->drupalPost('admin/config/regional/date-time/locale/fr/edit', $edit, t('Save configuration')); + $this->assertText(t('Configuration saved.'), 'French date formats updated.'); + $edit = array( + 'date_format_long' => 'j M Y - g:ia', + 'date_format_medium' => 'j M Y - g:ia', + 'date_format_short' => 'j M Y - g:ia', + ); + $this->drupalPost('admin/config/regional/date-time/locale/en/edit', $edit, t('Save configuration')); + $this->assertText(t('Configuration saved.'), 'English date formats updated.'); + + // Create node content. + $node = $this->drupalCreateNode(array('type' => 'article')); + + // Configure format for the node posted date changes with the language. + $this->drupalGet('node/' . $node->nid); + $english_date = format_date($node->created, 'custom', 'j M Y'); + $this->assertText($english_date, t('English date format appears')); + $this->drupalGet('fr/node/' . $node->nid); + $french_date = format_date($node->created, 'custom', 'd.m.Y'); + $this->assertText($french_date, t('French date format appears')); + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleExportTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleExportTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4ad935669e473bdb0338635857aa9f18df9f576d --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleExportTest.php @@ -0,0 +1,152 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleExportTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Tests for the export of translation files. + */ +class LocaleExportTest extends WebTestBase { + 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'); + + $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 = 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', t('Exported French translation file.')); + // Ensure our imported translations exist in the file. + $this->assertRaw('msgstr "lundi"', t('French translations present in exported file.')); + + // Import some more French translations which will be marked as customized. + $name = tempnam('temporary://', "po2_") . '.po'; + file_put_contents($name, $this->getCustomPoFile()); + $this->drupalPost('admin/config/regional/translate/import', array( + 'langcode' => 'fr', + 'files[file]' => $name, + 'customized' => 1, + ), t('Import')); + drupal_unlink($name); + + // We can't import a string with an empty translation, but calling + // locale() for an new string creates an entry in the locales_source table. + locale('February', NULL, 'fr'); + + // Export only customized French translations. + $this->drupalPost('admin/config/regional/translate/export', array( + 'langcode' => 'fr', + 'content_options[not_customized]' => FALSE, + 'content_options[customized]' => TRUE, + 'content_options[not_translated]' => FALSE, + ), t('Export')); + + // Ensure we have a translation file. + $this->assertRaw('# French translation of Drupal', t('Exported French translation file with only customized strings.')); + // Ensure the customized translations exist in the file. + $this->assertRaw('msgstr "janvier"', t('French custom translation present in exported file.')); + // Ensure no untranslated strings exist in the file. + $this->assertNoRaw('msgid "February"', t('Untranslated string not present in exported file.')); + + // Export only untranslated French translations. + $this->drupalPost('admin/config/regional/translate/export', array( + 'langcode' => 'fr', + 'content_options[not_customized]' => FALSE, + 'content_options[customized]' => FALSE, + 'content_options[not_translated]' => TRUE, + ), t('Export')); + + // Ensure we have a translation file. + $this->assertRaw('# French translation of Drupal', t('Exported French translation file with only untranslated strings.')); + // Ensure no customized translations exist in the file. + $this->assertNoRaw('msgstr "janvier"', t('French custom translation not present in exported file.')); + // Ensure the untranslated strings exist in the file. + $this->assertRaw('msgid "February"', t('Untranslated string present in exported file.')); + } + + /** + * Test exportation of translation template file. + */ + function testExportTranslationTemplateFile() { + // Get the translation template file. + $this->drupalPost('admin/config/regional/translate/export', array(), t('Export')); + // Ensure we have a translation file. + $this->assertRaw('# LANGUAGE translation of PROJECT', t('Exported translation template file.')); + } + + /** + * Helper function that returns a proper .po file. + */ + function getPoFile() { + return <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\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; + } + + /** + * Helper function that returns a .po file which strings will be marked + * as customized. + */ + function getCustomPoFile() { + return <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\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 "January" +msgstr "janvier" +EOF; + } + +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php new file mode 100644 index 0000000000000000000000000000000000000000..3beee64b24a9d1afe028ec2d96ed918f5b2c9a41 --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php @@ -0,0 +1,487 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleImportFunctionalTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional tests for the import of translation files. + */ +class LocaleImportFunctionalTest extends WebTestBase { + 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(array('locale', 'dblog')); + + // Set the translation file directory. + variable_set('locale_translate_file_directory', drupal_get_path('module', 'locale') . '/tests'); + + $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')), t('The language has been automatically created.')); + + // The import should have created 8 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' => 8, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.')); + + // This import should have saved plural forms to have 2 variants. + $locale_plurals = variable_get('locale_translation_plurals', array()); + $this->assert($locale_plurals['fr']['plurals'] == 2, t('Plural number initialized.')); + + // Ensure we were redirected correctly. + $this->assertEqual($this->getUrl(), url('admin/config/regional/translate', array('absolute' => TRUE)), t('Correct page redirection.')); + + + // Try importing a .po file with invalid tags. + $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)), t('The translation file was successfully imported.')); + $skip_message = format_plural(2, 'A translation string was skipped because of disallowed or malformed HTML. <a href="@url">See the log</a> for details.', '@count translation strings were skipped because of disallowed or malformed HTML. <a href="@url">See the log</a> for details.', array('@url' => url('admin/reports/dblog'))); + $this->assertRaw($skip_message, t('Unsafe strings were skipped.')); + + + // 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, + ), t('Import')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/import', array('absolute' => TRUE)), t('Correct page redirection.')); + $this->assertText(t('File to import not found.'), t('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', + )); + + // 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)), t('The translation file was successfully imported.')); + // Ensure string wasn't overwritten. + $search = array( + 'string' => 'Montag', + 'language' => 'fr', + 'translation' => 'translated', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertText(t('No strings available.'), t('String not overwritten by imported string.')); + + // This import should not have changed number of plural forms. + $locale_plurals = variable_get('locale_translation_plurals', array()); + $this->assert($locale_plurals['fr']['plurals'] == 2, t('Plural numbers untouched.')); + + // Try importing a .po file with overriding strings, and ensure existing + // strings are overwritten. + $this->importPoFile($this->getOverwritePoFile(), array( + 'langcode' => 'fr', + 'overwrite_options[not_customized]' => TRUE, + )); + + // 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)), t('The translation file was successfully imported.')); + // Ensure string was overwritten. + $search = array( + 'string' => 'Montag', + 'language' => 'fr', + 'translation' => 'translated', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertNoText(t('No strings available.'), t('String overwritten by imported string.')); + // This import should have changed number of plural forms. + $locale_plurals = variable_get('locale_translation_plurals', array()); + $this->assert($locale_plurals['fr']['plurals'] == 3, t('Plural numbers changed.')); + + // Importing a .po file and mark its strings as customized strings. + $this->importPoFile($this->getCustomPoFile(), array( + 'langcode' => 'fr', + 'customized' => TRUE, + )); + + // The import should have created 6 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' => 6, '%update' => 0, '%delete' => 0)), t('The customized translation file was successfully imported.')); + + // The database should now contain 6 customized strings (two imported + // strings are not translated). + $count = db_query('SELECT lid FROM {locales_target} WHERE customized = :custom', array(':custom' => 1))->rowCount(); + $this->assertEqual($count, 6, t('Customized translations succesfully imported.')); + + // Try importing a .po file with overriding strings, and ensure existing + // customized strings are kept. + $this->importPoFile($this->getCustomOverwritePoFile(), array( + 'langcode' => 'fr', + 'overwrite_options[not_customized]' => TRUE, + 'overwrite_options[customized]' => FALSE, + )); + + // 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)), t('The customized translation file was successfully imported.')); + // Ensure string wasn't overwritten. + $search = array( + 'string' => 'januari', + 'language' => 'fr', + 'translation' => 'translated', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertText(t('No strings available.'), t('Customized string not overwritten by imported string.')); + + // Try importing a .po file with overriding strings, and ensure existing + // customized strings are overwritten. + $this->importPoFile($this->getCustomOverwritePoFile(), array( + 'langcode' => 'fr', + 'overwrite_options[not_customized]' => FALSE, + 'overwrite_options[customized]' => TRUE, + )); + + // 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)), t('The customized translation file was successfully imported.')); + // Ensure string was overwritten. + $search = array( + 'string' => 'januari', + 'language' => 'fr', + 'translation' => 'translated', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertNoText(t('No strings available.'), t('Customized string overwritten by imported string.')); + + } + + /** + * Test automatic import of a module's translation files. + */ + 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); + + // Create a custom language. + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'name' => $name, + '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.'), t('Language file automatically imported.')); + + // Ensure strings were successfully imported. + $search = array( + 'string' => 'lundi', + 'language' => $langcode, + 'translation' => 'translated', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertNoText(t('No strings available.'), t('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', t('Long month name context is working.')); + $this->assertIdentical(t('May', array(), array('langcode' => 'hr')), 'Svi.', t('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)), t('The translation file was successfully imported.')); + $this->assertIdentical(t('Operations', array(), array('langcode' => $langcode)), 'Műveletek', t('String imported and translated.')); + + // Try importing a .po file. + $this->importPoFile($this->getPoFileWithEmptyMsgstr(), array( + 'langcode' => $langcode, + 'overwrite_options[not_customized]' => TRUE, + )); + $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)), t('The translation file was successfully imported.')); + // This is the language indicator on the translation search screen for + // untranslated strings. + $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> "; + $str = "Operations"; + $search = array( + 'string' => $str, + 'language' => 'all', + 'translation' => '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, t('Search found the string.')); + $this->assertRaw($language_indicator, t('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 = 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 8\\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 8\\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<img SRC="javascript:alert(\'xss\');">" + +msgid "delete" +msgstr "supprimer<script>alert('xss');</script>" + +EOF; + } + + /** + * Helper function that returns a proper .po file for testing. + */ + function getOverwritePoFile() { + return <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\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 which strings will be marked + * as customized. + */ + function getCustomPoFile() { + return <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\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 dog" +msgid_plural "@count dogs" +msgstr[0] "un chien" +msgstr[1] "@count chiens" + +msgid "January" +msgstr "janvier" + +msgid "February" +msgstr "février" + +msgid "March" +msgstr "mars" + +msgid "April" +msgstr "avril" + +msgid "June" +msgstr "juin" +EOF; + } + + /** + * Helper function that returns a .po file for testing customized strings. + */ + function getCustomOverwritePoFile() { + return <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\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 "January" +msgstr "januari" + +msgid "February" +msgstr "februari" + +msgid "July" +msgstr "juillet" +EOF; + } + + /** + * Helper function that returns a .po file with context. + */ + function getPoFileWithContext() { + // Croatian (code hr) is one the 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 8\\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 8\\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 8\\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; + } + +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleInstallTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleInstallTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f1677eb21e4292d5a1d7949e4514cd9f9074405c --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleInstallTest.php @@ -0,0 +1,40 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleInstallTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; +use ReflectionFunction; + +/** + * Tests for the st() function. + */ +class LocaleInstallTest extends WebTestBase { + 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 . '/core/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(), t('Function signatures of t() and st() are equal.')); + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleJavascriptTranslation.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleJavascriptTranslation.php new file mode 100644 index 0000000000000000000000000000000000000000..97395379f1815ec1cc613f88e16b4c9c6cdaabc8 --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleJavascriptTranslation.php @@ -0,0 +1,100 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleJavascriptTranslation. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional tests for JavaScript parsing for translatable strings. + */ +class LocaleJavascriptTranslation extends WebTestBase { + public static function getInfo() { + return array( + 'name' => 'Javascript translation', + 'description' => 'Tests parsing js files for translatable strings', + 'group' => 'Locale', + ); + } + + function setUp() { + parent::setUp('locale'); + } + + function testFileParsing() { + + $filename = drupal_get_path('module', 'locale') . '/tests/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]), t("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 ? t("Context for %source is %context", $args) : t("Context for %source is blank", $args)); + } + + $this->assertEqual(count($source_strings), count($test_strings), t("Found correct number of source strings.")); + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleMultilingualFieldsTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleMultilingualFieldsTest.php new file mode 100644 index 0000000000000000000000000000000000000000..2a8299ade56810fc3439dbf451bc3fc86f37438d --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleMultilingualFieldsTest.php @@ -0,0 +1,137 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleMultilingualFieldsTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional test for multilingual fields. + */ +class LocaleMultilingualFieldsTest extends WebTestBase { + public static function getInfo() { + return array( + 'name' => 'Multilingual fields', + 'description' => 'Test multilingual support for fields.', + 'group' => 'Locale', + ); + } + + function setUp() { + parent::setUp(array('node', 'locale')); + + // Create Basic page node type. + $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); + + // Setup users. + $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages', 'create page content', 'edit own page content')); + $this->drupalLogin($admin_user); + + // Add a new language. + $language = (object) array( + 'langcode' => 'it', + 'name' => 'Italian', + ); + language_save($language); + + // Enable URL language detection and selection. + $edit = array('language_interface[enabled][language-url]' => '1'); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Set "Basic page" content type to use multilingual support. + $edit = array( + 'node_type_language' => 1, + ); + $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type')); + $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.')); + + // Make node body translatable. + $field = field_info_field('body'); + $field['translatable'] = TRUE; + field_update_field($field); + } + + /** + * Test if field languages are correctly set through the node form. + */ + function testMultilingualNodeForm() { + // Create "Basic page" content. + $langcode = LANGUAGE_NOT_SPECIFIED; + $title_key = "title"; + $title_value = $this->randomName(8); + $body_key = "body[$langcode][0][value]"; + $body_value = $this->randomName(16); + + // Create node to edit. + $edit = array(); + $edit[$title_key] = $title_value; + $edit[$body_key] = $body_value; + $edit['langcode'] = 'en'; + $this->drupalPost('node/add/page', $edit, t('Save')); + + // Check that the node exists in the database. + $node = $this->drupalGetNodeByTitle($edit[$title_key]); + $this->assertTrue($node, t('Node found in database.')); + + $assert = isset($node->body['en']) && !isset($node->body[LANGUAGE_NOT_SPECIFIED]) && $node->body['en'][0]['value'] == $body_value; + $this->assertTrue($assert, t('Field language correctly set.')); + + // Change node language. + $this->drupalGet("node/$node->nid/edit"); + $edit = array( + $title_key => $this->randomName(8), + 'langcode' => 'it' + ); + $this->drupalPost(NULL, $edit, t('Save')); + $node = $this->drupalGetNodeByTitle($edit[$title_key]); + $this->assertTrue($node, t('Node found in database.')); + + $assert = isset($node->body['it']) && !isset($node->body['en']) && $node->body['it'][0]['value'] == $body_value; + $this->assertTrue($assert, t('Field language correctly changed.')); + + // Enable content language URL detection. + language_negotiation_set(LANGUAGE_TYPE_CONTENT, array(LANGUAGE_NEGOTIATION_URL => 0)); + + // Test multilingual field language fallback logic. + $this->drupalGet("it/node/$node->nid"); + $this->assertRaw($body_value, t('Body correctly displayed using Italian as requested language')); + + $this->drupalGet("node/$node->nid"); + $this->assertRaw($body_value, t('Body correctly displayed using English as requested language')); + } + + /* + * Test multilingual field display settings. + */ + function testMultilingualDisplaySettings() { + // Create "Basic page" content. + $langcode = LANGUAGE_NOT_SPECIFIED; + $title_key = "title"; + $title_value = $this->randomName(8); + $body_key = "body[$langcode][0][value]"; + $body_value = $this->randomName(16); + + // Create node to edit. + $edit = array(); + $edit[$title_key] = $title_value; + $edit[$body_key] = $body_value; + $edit['langcode'] = 'en'; + $this->drupalPost('node/add/page', $edit, t('Save')); + + // Check that the node exists in the database. + $node = $this->drupalGetNodeByTitle($edit[$title_key]); + $this->assertTrue($node, t('Node found in database.')); + + // Check if node body is showed. + $this->drupalGet("node/$node->nid"); + $body = $this->xpath('//article[@id=:id]//div[@class=:class]/descendant::p', array( + ':id' => 'node-' . $node->nid, + ':class' => 'content', + )); + $this->assertEqual(current($body), $node->body['en'][0]['value'], 'Node body found.'); + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0d33663e7784b72bd73ffa6645dffbe8907df593 --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php @@ -0,0 +1,149 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocalePathTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional tests for configuring a different path alias per language. + */ +class LocalePathTest extends WebTestBase { + 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(array('node', 'locale', 'path')); + + $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); + variable_set('site_frontpage', 'node'); + } + + /** + * 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 domain prefix. + $prefix = $langcode; + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'name' => $name, + 'direction' => '0', + ); + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); + + // Set path prefix. + $edit = array( "prefix[$langcode]" => $prefix ); + $this->drupalPost('admin/config/regional/language/detection/url', $edit, t('Save configuration')); + + // Check that the "xx" front page is readily available because path prefix + // negotiation is pre-configured. + $this->drupalGet($prefix); + $this->assertText(t('Welcome to Drupal'), t('The "xx" front page is readily available.')); + + // 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, + 'langcode' => '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, + 'langcode' => $langcode, + ); + $this->drupalPost($path, $edit, t('Save')); + + // Confirm English language path alias works. + $this->drupalGet($english_path); + $this->assertText($node->title, t('English alias works.')); + + // Confirm custom language path alias works. + $this->drupalGet($prefix . '/' . $custom_language_path); + $this->assertText($node->title, t('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, + 'langcode' => LANGUAGE_NOT_SPECIFIED, + ); + path_save($edit); + $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, 'en'); + $this->assertEqual($english_path, $lookup_path, t('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, t('Custom language alias has priority.')); + path_delete($edit); + + // Create language nodes to check priority of aliases. + $first_node = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1)); + $second_node = $this->drupalCreateNode(array('type' => 'page', '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, + 'langcode' => 'en', + ); + path_save($edit); + + // Assign a custom path alias to second node with LANGUAGE_NOT_SPECIFIED. + $edit = array( + 'source' => 'node/' . $second_node->nid, + 'alias' => $custom_path, + 'langcode' => LANGUAGE_NOT_SPECIFIED, + ); + path_save($edit); + + // Test that both node titles link to our path alias. + $this->drupalGet('<front>'); + $custom_path_url = base_path() . $GLOBALS['script_path'] . $custom_path; + $elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $first_node->title)); + $this->assertTrue(!empty($elements), t('First node links to the path alias.')); + $elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $second_node->title)); + $this->assertTrue(!empty($elements), t('Second node links to the path alias.')); + + // Confirm that the custom path leads to the first node. + $this->drupalGet($custom_path); + $this->assertText($first_node->title, t('Custom alias returns first node.')); + + // Confirm that the custom path with prefix leads to the second node. + $this->drupalGet($prefix . '/' . $custom_path); + $this->assertText($second_node->title, t('Custom alias with prefix returns second node.')); + + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocalePluralFormatTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocalePluralFormatTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6d45d2152258e87d52f1e101fa55cd16c3d6c93f --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocalePluralFormatTest.php @@ -0,0 +1,323 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocalePluralFormatTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Tests plural format handling functionality. + */ +class LocalePluralFormatTest extends WebTestBase { + public static function getInfo() { + return array( + 'name' => 'Plural handling', + 'description' => 'Tests plural handling for various languages.', + 'group' => 'Locale', + ); + } + + function setUp() { + parent::setUp('locale'); + + $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages')); + $this->drupalLogin($admin_user); + } + + /** + * Tests locale_get_plural() and format_plural() functionality. + */ + function testGetPluralFormat() { + // 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', + )); + + // Attempt to import some broken .po files as well to prove that these + // will not overwrite the proper plural formula imported above. + $this->importPoFile($this->getPoFileWithMissingPlural(), array( + 'langcode' => 'fr', + 'overwrite_options[not_customized]' => TRUE, + )); + $this->importPoFile($this->getPoFileWithBrokenPlural(), array( + 'langcode' => 'hr', + 'overwrite_options[not_customized]' => TRUE, + )); + + // Reset static caches from locale_get_plural() to ensure we get fresh data. + drupal_static_reset('locale_get_plural'); + drupal_static_reset('locale_get_plural:plurals'); + drupal_static_reset('locale'); + + // Expected plural translation strings for each plural index. + $plural_strings = array( + // English is not imported in this case, so we assume built-in text + // and formulas. + 'en' => array( + 0 => '1 hour', + 1 => '@count hours', + ), + 'fr' => array( + 0 => '1 heure', + 1 => '@count heures', + ), + 'hr' => array( + 0 => '@count sat', + 1 => '@count sata', + 2 => '@count sati', + ), + // Hungarian is not imported, so it should assume the same text as + // English, but it will always pick the plural form as per the built-in + // logic, so only index -1 is relevant with the plural value. + 'hu' => array( + 0 => '1 hour', + -1 => '@count hours', + ), + ); + + // Expected plural indexes precomputed base on the plural formulas with + // given $count value. + $plural_tests = array( + 'en' => array( + 1 => 0, + 0 => 1, + 5 => 1, + ), + 'fr' => array( + 1 => 0, + 0 => 0, + 5 => 1, + ), + 'hr' => array( + 1 => 0, + 21 => 0, + 0 => 2, + 2 => 1, + 8 => 2, + ), + 'hu' => array( + 1 => -1, + 21 => -1, + 0 => -1, + ), + ); + + foreach ($plural_tests as $langcode => $tests) { + foreach ($tests as $count => $expected_plural_index) { + // Assert that the we get the right plural index. + $this->assertIdentical(locale_get_plural($count, $langcode), $expected_plural_index, 'Computed plural index for ' . $langcode . ' for count ' . $count . ' is ' . $expected_plural_index); + // Assert that the we get the right translation for that. Change the + // expected index as per the logic for translation lookups. + $expected_plural_index = ($count == 1) ? 0 : $expected_plural_index; + $expected_plural_string = str_replace('@count', $count, $plural_strings[$langcode][$expected_plural_index]); + $this->assertIdentical(format_plural($count, '1 hour', '@count hours', array(), array('langcode' => $langcode)), $expected_plural_string, 'Plural translation of 1 hours / @count hours for count ' . $count . ' in ' . $langcode . ' is ' . $expected_plural_string); + } + } + } + + /** + * Tests plural editing and export functionality. + */ + function testPluralEditExport() { + // 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', + )); + + // 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', t('Exported French translation file.')); + // Ensure our imported translations exist in the file. + $this->assertRaw("msgid \"Monday\"\nmsgstr \"lundi\"", t('French translations present in exported file.')); + // Check for plural export specifically. + $this->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"1 heure\"\nmsgstr[1] \"@count heures\"", t('Plural translations exported properly.')); + + // Get the Croatian translations. + $this->drupalPost('admin/config/regional/translate/export', array( + 'langcode' => 'hr', + ), t('Export')); + // Ensure we have a translation file. + $this->assertRaw('# Croatian translation of Drupal', t('Exported Croatian translation file.')); + // Ensure our imported translations exist in the file. + $this->assertRaw("msgid \"Monday\"\nmsgstr \"Ponedjeljak\"", t('Croatian translations present in exported file.')); + // Check for plural export specifically. + $this->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata\"\nmsgstr[2] \"@count sati\"", t('Plural translations exported properly.')); + + // Check if the source appears on the translation page. + $this->drupalGet('admin/config/regional/translate'); + $this->assertText("1 hour, @count hours"); + + // Look up editing page for this plural string and check fields. + $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", array(':source' => "1 hour" . LOCALE_PLURAL_DELIMITER . "@count hours"))->fetchField(); + $path = 'admin/config/regional/translate/edit/' . $lid; + $this->drupalGet($path); + // Labels for plural editing elements. + $this->assertText('Singular form'); + $this->assertText('First plural form'); + $this->assertText('2. plural form'); + // Plural values for both languages. + $this->assertFieldById('edit-translations-hr-0', '@count sat'); + $this->assertFieldById('edit-translations-hr-1', '@count sata'); + $this->assertFieldById('edit-translations-hr-2', '@count sati'); + $this->assertNoFieldById('edit-translations-hr-3'); + $this->assertFieldById('edit-translations-fr-0', '1 heure'); + $this->assertFieldById('edit-translations-fr-1', '@count heures'); + $this->assertNoFieldById('edit-translations-fr-2'); + + // Edit some translations and see if that took effect. + $edit = array( + 'translations[fr][0]' => '1 heure edited', + 'translations[hr][1]' => '@count sata edited', + ); + $this->drupalPost($path, $edit, t('Save translations')); + + // Inject a plural source string to the database. We need to use a specific + // langcode here because the language will be English by default and will + // not save our source string for performance optimization if we do not ask + // specifically for a language. + format_plural(1, '1 day', '@count days', array(), array('langcode' => 'fr')); + // Look up editing page for this plural string and check fields. + $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", array(':source' => "1 day" . LOCALE_PLURAL_DELIMITER . "@count days"))->fetchField(); + $path = 'admin/config/regional/translate/edit/' . $lid; + + // Save complete translations for the string in both languages. + $edit = array( + 'translations[fr][0]' => '1 jour', + 'translations[fr][1]' => '@count jours', + 'translations[hr][0]' => '@count dan', + 'translations[hr][1]' => '@count dana', + 'translations[hr][2]' => '@count dana', + ); + $this->drupalPost($path, $edit, t('Save translations')); + + // Get the French translations. + $this->drupalPost('admin/config/regional/translate/export', array( + 'langcode' => 'fr', + ), t('Export')); + // Check for plural export specifically. + $this->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"1 heure edited\"\nmsgstr[1] \"@count heures\"", t('Edited French plural translations for hours exported properly.')); + $this->assertRaw("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"1 jour\"\nmsgstr[1] \"@count jours\"", t('Added French plural translations for days exported properly.')); + + // Get the Croatian translations. + $this->drupalPost('admin/config/regional/translate/export', array( + 'langcode' => 'hr', + ), t('Export')); + // Check for plural export specifically. + $this->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata edited\"\nmsgstr[2] \"@count sati\"", t('Edited Croatian plural translations exported properly.')); + $this->assertRaw("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"@count dan\"\nmsgstr[1] \"@count dana\"\nmsgstr[2] \"@count dana\"", t('Added Croatian plural translations exported properly.')); + } + + + /** + * 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 = 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 8\\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 "1 hour" +msgid_plural "@count hours" +msgstr[0] "1 heure" +msgstr[1] "@count heures" + +msgid "Monday" +msgstr "lundi" +EOF; + } + + /** + * Returns a .po file with a complex plural formula. + */ + function getPoFileWithComplexPlural() { + return <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\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; + } + + /** + * Returns a .po file with a missing plural formula. + */ + function getPoFileWithMissingPlural() { + return <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\n" +"MIME-Version: 1.0\\n" +"Content-Type: text/plain; charset=UTF-8\\n" +"Content-Transfer-Encoding: 8bit\\n" + +msgid "Monday" +msgstr "lundi" +EOF; + } + + /** + * Returns a .po file with a broken plural formula. + */ + function getPoFileWithBrokenPlural() { + return <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\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 "Ponedjeljak" +EOF; + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleTranslationTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleTranslationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..28e0834ba48d3f0b307ca0f4753382d2a3b3aaed --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleTranslationTest.php @@ -0,0 +1,418 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleTranslationTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Functional test for string translation and validation. + */ +class LocaleTranslationTest extends WebTestBase { + 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); + // This is the language indicator on the translation search screen for + // untranslated strings. + $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> "; + // This will be the translation of $name. + $translation = $this->randomName(16); + $translation_to_en = $this->randomName(16); + + // Add custom language. + $this->drupalLogin($admin_user); + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'name' => $name, + '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->assertRaw('"edit-site-default-' . $langcode .'"', t('Language code found.')); + $this->assertText(t($name), t('Test language added.')); + $this->drupalLogout(); + + // Search for the name and translate it. + $this->drupalLogin($translate_user); + $search = array( + 'string' => $name, + 'language' => 'all', + 'translation' => '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, t('Search found the name.')); + $this->assertRaw($language_indicator, t('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, t('name found on edit screen.')); + $this->assertNoText('English', t('No way to translate the string to English.')); + $this->drupalLogout(); + $this->drupalLogin($admin_user); + $this->drupalPost('admin/config/regional/language/edit/en', array('locale_translate_english' => TRUE), t('Save language')); + $this->drupalLogout(); + $this->drupalLogin($translate_user); + $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, t('Search found the name.')); + $this->assertRaw($language_indicator, t('Name is untranslated.')); + // Assume this is the only result, given the random name. + $this->clickLink(t('edit')); + $string_edit_url = $this->getUrl(); + $edit = array( + "translations[$langcode][0]" => $translation, + 'translations[en][0]' => $translation_to_en, + ); + $this->drupalPost(NULL, $edit, t('Save translations')); + $this->assertText(t('The string has been saved.'), t('The string has been saved.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.')); + $this->drupalGet($string_edit_url); + $this->assertRaw($translation, t('Non-English translation properly saved.')); + $this->assertRaw($translation_to_en, t('English translation properly saved.')); + $this->assertTrue($name != $translation && t($name, array(), array('langcode' => $langcode)) == $translation, t('t() works for non-English.')); + // Refresh the locale() cache to get fresh data from t() below. We are in + // the same HTTP request and therefore t() is not refreshed by saving the + // translation above. + locale_reset(); + // Now we should get the proper fresh translation from t(). + $this->assertTrue($name != $translation_to_en && t($name, array(), array('langcode' => 'en')) == $translation_to_en, t('t() works for English.')); + $this->assertTrue(t($name, array(), array('langcode' => LANGUAGE_SYSTEM)) == $name, t('t() works for LANGUAGE_SYSTEM.')); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + // The indicator should not be here. + $this->assertNoRaw($language_indicator, t('String is translated.')); + + // 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'), t('String not found.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('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 %language and %langcode will add HTML. + $t_args = array('%language' => $name, '%langcode' => $langcode); + $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The test language has been removed.')); + // Reload to remove $name. + $this->drupalGet($path); + // Verify that language is no longer found. + $this->assertResponse(404, t('Language no longer found.')); + $this->drupalLogout(); + + // Delete the string. + $this->drupalLogin($translate_user); + $search = array( + 'string' => $name, + 'language' => 'all', + 'translation' => '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'), t('"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)), t('Correct page redirection.')); + $this->assertRaw($name, t('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.'), t('The string has been removed message.')); + $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.')); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertNoText($name, t('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); + + // Add custom language. + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'name' => $name, + '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')->execute(); + $url = 'admin/config/regional/translate/edit/' . $result->fetchObject()->lid; + $edit = array('translations['. $langcode .'][0]' => $this->randomName()); + $this->drupalPost($url, $edit, t('Save translations')); + + // Trigger JavaScript translation parsing and building. + _locale_rebuild_js($langcode); + + $locale_javascripts = variable_get('locale_translation_javascript', array()); + $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js'; + $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('not found')))); + + // Test JavaScript translation rebuilding. + file_unmanaged_delete($js_file); + $this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found')))); + cache_clear_all(); + _locale_rebuild_js($langcode); + $this->assertTrue($result = file_exists($js_file), t('JavaScript file rebuilt: %file', array('%file' => $result ? $js_file : t('not found')))); + } + + /** + * 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); + // This is the language indicator on the translation search screen for + // untranslated strings. + $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> "; + // These will be the invalid translations of $name. + $key = $this->randomName(16); + $bad_translations[$key] = "<script>alert('xss');</script>" . $key; + $key = $this->randomName(16); + $bad_translations[$key] = '<img SRC="javascript:alert(\'xss\');">' . $key; + $key = $this->randomName(16); + $bad_translations[$key] = '<<SCRIPT>alert("xss");//<</SCRIPT>' . $key; + $key = $this->randomName(16); + $bad_translations[$key] ="<BODY ONLOAD=alert('xss')>" . $key; + + // Add custom language. + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'name' => $name, + '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', + ); + $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), t('Found the edit path.')); + $path = $matches[0]; + foreach ($bad_translations as $key => $translation) { + $edit = array( + "translations[$langcode][0]" => $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'), t('The string was rejected as unsafe.')); + $this->assertNoText(t('The string has been saved.'), t('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); + // This is the language indicator on the translation search screen for + // untranslated strings. + $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> "; + // This will be the translation of $name. + $translation = $this->randomName(16); + + // Add custom language. + $this->drupalLogin($admin_user); + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'name' => $name, + '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', + ); + $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, t('Search found the string.')); + + // Ensure untranslated string doesn't appear if searching on 'only + // translated strings'. + $search = array( + 'string' => $name, + 'language' => 'all', + 'translation' => 'translated', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertText(t('No strings available.'), t("Search didn't find the string.")); + + // Ensure untranslated string appears if searching on 'only untranslated + // strings'. + $search = array( + 'string' => $name, + 'language' => 'all', + 'translation' => 'untranslated', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertNoText(t('No strings available.'), t('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][0]" => $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', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertNoText(t('No strings available.'), t('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', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertText(t('No strings available.'), t("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', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertText(t('No strings available.'), t("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', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertNoText(t('No strings available.'), t('Search found the translation.')); + + // Ensure translated string doesn't appear if searching in System (English). + $search = array( + 'string' => $translation, + 'language' => LANGUAGE_SYSTEM, + 'translation' => 'all', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertText(t('No strings available.'), t("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', + ); + $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); + $this->assertText(t('No strings available.'), t("Search didn't find the invalid string.")); + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallFrenchTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallFrenchTest.php new file mode 100644 index 0000000000000000000000000000000000000000..047ff6306707c626bb5929b718d62c76c4ddb89b --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallFrenchTest.php @@ -0,0 +1,31 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleUninstallFrenchTest. + */ + +namespace Drupal\locale\Tests; + +/** + * 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 LocaleUninstallFrenchTest extends LocaleUninstallTest { + 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->langcode = 'fr'; + } +} diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php new file mode 100644 index 0000000000000000000000000000000000000000..72868e9e1bc3b03eb1d8963257d58f897e6c12be --- /dev/null +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php @@ -0,0 +1,132 @@ +<?php + +/** + * @file + * Definition of Drupal\locale\Tests\LocaleUninstallTest. + */ + +namespace Drupal\locale\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Locale uninstall with English UI functional test. + */ +class LocaleUninstallTest extends WebTestBase { + 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(array('node', 'locale')); + $this->langcode = 'en'; + + // Create Article node type. + $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); + } + + /** + * Check if the values of the Locale variables are correct after uninstall. + */ + function testUninstallProcess() { + $locale_module = array('locale', 'language'); + + $language = (object) array( + 'langcode' => 'fr', + 'name' => 'French', + 'default' => $this->langcode == 'fr', + ); + language_save($language); + + // Check the UI language. + drupal_language_initialize(); + $this->assertEqual(drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode, $this->langcode, t('Current language: %lang', array('%lang' => drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode))); + + // Enable multilingual workflow option for articles. + variable_set('node_type_language_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', array( + ':location' => '%.js%', + ))->fetchObject(); + $edit = array('translations[fr][0]' => 'french translation'); + $this->drupalPost('admin/config/regional/translate/edit/' . $string->lid, $edit, t('Save translations')); + _locale_rebuild_js('fr'); + $locale_javascripts = variable_get('locale_translation_javascript', array()); + $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/fr_' . $locale_javascripts['fr'] . '.js'; + $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('none')))); + + // Disable string caching. + variable_set('locale_cache_strings', 0); + + // Change language negotiation options. + drupal_load('module', 'locale'); + variable_set('language_types', language_types_get_default() + array('language_custom' => TRUE)); + variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, language_language_negotiation_info()); + variable_set('language_negotiation_' . LANGUAGE_TYPE_CONTENT, language_language_negotiation_info()); + variable_set('language_negotiation_' . LANGUAGE_TYPE_URL, language_language_negotiation_info()); + + // Change language negotiation settings. + variable_set('language_negotiation_url_part', LANGUAGE_NEGOTIATION_URL_PREFIX); + variable_set('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(drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode, 'en', t('Language after uninstall: %lang', array('%lang' => drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode))); + + // Check JavaScript files deletion. + $this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found')))); + + // Check language count. + $language_count = variable_get('language_count', 1); + $this->assertEqual($language_count, 1, t('Language count: %count', array('%count' => $language_count))); + + // Check language negotiation. + require_once DRUPAL_ROOT . '/core/includes/language.inc'; + $this->assertTrue(count(language_types_get_all()) == count(language_types_get_default()), t('Language types reset')); + $language_negotiation = language_negotiation_method_get_first(LANGUAGE_TYPE_INTERFACE) == LANGUAGE_NEGOTIATION_DEFAULT; + $this->assertTrue($language_negotiation, t('Interface language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set')))); + $language_negotiation = language_negotiation_method_get_first(LANGUAGE_TYPE_CONTENT) == LANGUAGE_NEGOTIATION_DEFAULT; + $this->assertTrue($language_negotiation, t('Content language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set')))); + $language_negotiation = language_negotiation_method_get_first(LANGUAGE_TYPE_URL) == LANGUAGE_NEGOTIATION_DEFAULT; + $this->assertTrue($language_negotiation, t('URL language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set')))); + + // Check language negotiation method settings. + $this->assertFalse(variable_get('language_negotiation_url_part', FALSE), t('URL language negotiation method indicator settings cleared.')); + $this->assertFalse(variable_get('language_negotiation_session_param', FALSE), t('Visit language negotiation method settings cleared.')); + + // Check JavaScript parsed. + $javascript_parsed_count = count(variable_get('javascript_parsed', array())); + $this->assertEqual($javascript_parsed_count, 0, t('JavaScript parsed count: %count', array('%count' => $javascript_parsed_count))); + + // Check JavaScript translations directory. + $locale_js_directory = variable_get('locale_js_directory', 'languages'); + $this->assertEqual($locale_js_directory, 'languages', t('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, t('String caching: %status', array('%status' => t($locale_cache_strings ? 'enabled': 'disabled')))); + } +} diff --git a/core/modules/locale/locale.info b/core/modules/locale/locale.info index 8b3e5f74aca478ec1dc9964ce6bf1e3b7b2bd631..e0749dba9d664990342f8f579e372143984929cb 100644 --- a/core/modules/locale/locale.info +++ b/core/modules/locale/locale.info @@ -4,4 +4,3 @@ package = Core version = VERSION core = 8.x dependencies[] = language -files[] = locale.test diff --git a/core/modules/locale/locale.test b/core/modules/locale/locale.test deleted file mode 100644 index dce77b22e1d7724b60c372da2b4fb3d0861fe8c1..0000000000000000000000000000000000000000 --- a/core/modules/locale/locale.test +++ /dev/null @@ -1,2263 +0,0 @@ -<?php - -/** - * @file - * Tests for locale.module. - * - * The test file includes: - * - a functional test for the language configuration forms; - * - functional tests for the translation functionalities, including searching; - * - a functional test for the PO files import feature, including validation; - * - functional tests for translations and templates export feature; - * - functional tests for the uninstall process; - * - a functional test for the language switching feature; - * - a functional test for a user's ability to change their default language; - * - a functional test for configuring a different path alias per language; - * - a functional test for configuring a different path alias per language; - * - a functional test for multilingual support by content type and on nodes. - * - a functional test for multilingual fields. - * - a functional test for comment language. - * - a functional test fot language types/negotiation info. - */ - -use Drupal\simpletest\WebTestBase; - -/** - * Functional tests for JavaScript parsing for translatable strings. - */ -class LocaleJavascriptTranslationTest extends WebTestBase { - public static function getInfo() { - return array( - 'name' => 'Javascript translation', - 'description' => 'Tests parsing js files for translatable strings', - 'group' => 'Locale', - ); - } - - function setUp() { - parent::setUp('locale'); - } - - function testFileParsing() { - - $filename = drupal_get_path('module', 'locale') . '/tests/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]), t("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 ? t("Context for %source is %context", $args) : t("Context for %source is blank", $args)); - } - - $this->assertEqual(count($source_strings), count($test_strings), t("Found correct number of source strings.")); - } -} - -/** - * Functional test for string translation and validation. - */ -class LocaleTranslationFunctionalTest extends WebTestBase { - 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); - // This is the language indicator on the translation search screen for - // untranslated strings. - $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> "; - // This will be the translation of $name. - $translation = $this->randomName(16); - $translation_to_en = $this->randomName(16); - - // Add custom language. - $this->drupalLogin($admin_user); - $edit = array( - 'predefined_langcode' => 'custom', - 'langcode' => $langcode, - 'name' => $name, - '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->assertRaw('"edit-site-default-' . $langcode .'"', t('Language code found.')); - $this->assertText(t($name), t('Test language added.')); - $this->drupalLogout(); - - // Search for the name and translate it. - $this->drupalLogin($translate_user); - $search = array( - 'string' => $name, - 'language' => 'all', - 'translation' => '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, t('Search found the name.')); - $this->assertRaw($language_indicator, t('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, t('name found on edit screen.')); - $this->assertNoText('English', t('No way to translate the string to English.')); - $this->drupalLogout(); - $this->drupalLogin($admin_user); - $this->drupalPost('admin/config/regional/language/edit/en', array('locale_translate_english' => TRUE), t('Save language')); - $this->drupalLogout(); - $this->drupalLogin($translate_user); - $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, t('Search found the name.')); - $this->assertRaw($language_indicator, t('Name is untranslated.')); - // Assume this is the only result, given the random name. - $this->clickLink(t('edit')); - $string_edit_url = $this->getUrl(); - $edit = array( - "translations[$langcode][0]" => $translation, - 'translations[en][0]' => $translation_to_en, - ); - $this->drupalPost(NULL, $edit, t('Save translations')); - $this->assertText(t('The string has been saved.'), t('The string has been saved.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.')); - $this->drupalGet($string_edit_url); - $this->assertRaw($translation, t('Non-English translation properly saved.')); - $this->assertRaw($translation_to_en, t('English translation properly saved.')); - $this->assertTrue($name != $translation && t($name, array(), array('langcode' => $langcode)) == $translation, t('t() works for non-English.')); - // Refresh the locale() cache to get fresh data from t() below. We are in - // the same HTTP request and therefore t() is not refreshed by saving the - // translation above. - locale_reset(); - // Now we should get the proper fresh translation from t(). - $this->assertTrue($name != $translation_to_en && t($name, array(), array('langcode' => 'en')) == $translation_to_en, t('t() works for English.')); - $this->assertTrue(t($name, array(), array('langcode' => LANGUAGE_SYSTEM)) == $name, t('t() works for LANGUAGE_SYSTEM.')); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - // The indicator should not be here. - $this->assertNoRaw($language_indicator, t('String is translated.')); - - // 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'), t('String not found.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('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 %language and %langcode will add HTML. - $t_args = array('%language' => $name, '%langcode' => $langcode); - $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The test language has been removed.')); - // Reload to remove $name. - $this->drupalGet($path); - // Verify that language is no longer found. - $this->assertResponse(404, t('Language no longer found.')); - $this->drupalLogout(); - - // Delete the string. - $this->drupalLogin($translate_user); - $search = array( - 'string' => $name, - 'language' => 'all', - 'translation' => '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'), t('"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)), t('Correct page redirection.')); - $this->assertRaw($name, t('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.'), t('The string has been removed message.')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.')); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertNoText($name, t('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); - - // Add custom language. - $edit = array( - 'predefined_langcode' => 'custom', - 'langcode' => $langcode, - 'name' => $name, - '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')->execute(); - $url = 'admin/config/regional/translate/edit/' . $result->fetchObject()->lid; - $edit = array('translations['. $langcode .'][0]' => $this->randomName()); - $this->drupalPost($url, $edit, t('Save translations')); - - // Trigger JavaScript translation parsing and building. - _locale_rebuild_js($langcode); - - $locale_javascripts = variable_get('locale_translation_javascript', array()); - $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js'; - $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('not found')))); - - // Test JavaScript translation rebuilding. - file_unmanaged_delete($js_file); - $this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found')))); - cache_clear_all(); - _locale_rebuild_js($langcode); - $this->assertTrue($result = file_exists($js_file), t('JavaScript file rebuilt: %file', array('%file' => $result ? $js_file : t('not found')))); - } - - /** - * 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); - // This is the language indicator on the translation search screen for - // untranslated strings. - $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> "; - // These will be the invalid translations of $name. - $key = $this->randomName(16); - $bad_translations[$key] = "<script>alert('xss');</script>" . $key; - $key = $this->randomName(16); - $bad_translations[$key] = '<img SRC="javascript:alert(\'xss\');">' . $key; - $key = $this->randomName(16); - $bad_translations[$key] = '<<SCRIPT>alert("xss");//<</SCRIPT>' . $key; - $key = $this->randomName(16); - $bad_translations[$key] ="<BODY ONLOAD=alert('xss')>" . $key; - - // Add custom language. - $edit = array( - 'predefined_langcode' => 'custom', - 'langcode' => $langcode, - 'name' => $name, - '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', - ); - $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), t('Found the edit path.')); - $path = $matches[0]; - foreach ($bad_translations as $key => $translation) { - $edit = array( - "translations[$langcode][0]" => $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'), t('The string was rejected as unsafe.')); - $this->assertNoText(t('The string has been saved.'), t('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); - // This is the language indicator on the translation search screen for - // untranslated strings. - $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> "; - // This will be the translation of $name. - $translation = $this->randomName(16); - - // Add custom language. - $this->drupalLogin($admin_user); - $edit = array( - 'predefined_langcode' => 'custom', - 'langcode' => $langcode, - 'name' => $name, - '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', - ); - $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, t('Search found the string.')); - - // Ensure untranslated string doesn't appear if searching on 'only - // translated strings'. - $search = array( - 'string' => $name, - 'language' => 'all', - 'translation' => 'translated', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertText(t('No strings available.'), t("Search didn't find the string.")); - - // Ensure untranslated string appears if searching on 'only untranslated - // strings'. - $search = array( - 'string' => $name, - 'language' => 'all', - 'translation' => 'untranslated', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertNoText(t('No strings available.'), t('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][0]" => $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', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertNoText(t('No strings available.'), t('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', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertText(t('No strings available.'), t("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', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertText(t('No strings available.'), t("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', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertNoText(t('No strings available.'), t('Search found the translation.')); - - // Ensure translated string doesn't appear if searching in System (English). - $search = array( - 'string' => $translation, - 'language' => LANGUAGE_SYSTEM, - 'translation' => 'all', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertText(t('No strings available.'), t("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', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertText(t('No strings available.'), t("Search didn't find the invalid string.")); - } -} - -/** - * Tests plural format handling functionality. - */ -class LocalePluralFormatTest extends WebTestBase { - public static function getInfo() { - return array( - 'name' => 'Plural handling', - 'description' => 'Tests plural handling for various languages.', - 'group' => 'Locale', - ); - } - - function setUp() { - parent::setUp('locale'); - - $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages')); - $this->drupalLogin($admin_user); - } - - /** - * Tests locale_get_plural() and format_plural() functionality. - */ - function testGetPluralFormat() { - // 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', - )); - - // Attempt to import some broken .po files as well to prove that these - // will not overwrite the proper plural formula imported above. - $this->importPoFile($this->getPoFileWithMissingPlural(), array( - 'langcode' => 'fr', - 'overwrite_options[not_customized]' => TRUE, - )); - $this->importPoFile($this->getPoFileWithBrokenPlural(), array( - 'langcode' => 'hr', - 'overwrite_options[not_customized]' => TRUE, - )); - - // Reset static caches from locale_get_plural() to ensure we get fresh data. - drupal_static_reset('locale_get_plural'); - drupal_static_reset('locale_get_plural:plurals'); - drupal_static_reset('locale'); - - // Expected plural translation strings for each plural index. - $plural_strings = array( - // English is not imported in this case, so we assume built-in text - // and formulas. - 'en' => array( - 0 => '1 hour', - 1 => '@count hours', - ), - 'fr' => array( - 0 => '1 heure', - 1 => '@count heures', - ), - 'hr' => array( - 0 => '@count sat', - 1 => '@count sata', - 2 => '@count sati', - ), - // Hungarian is not imported, so it should assume the same text as - // English, but it will always pick the plural form as per the built-in - // logic, so only index -1 is relevant with the plural value. - 'hu' => array( - 0 => '1 hour', - -1 => '@count hours', - ), - ); - - // Expected plural indexes precomputed base on the plural formulas with - // given $count value. - $plural_tests = array( - 'en' => array( - 1 => 0, - 0 => 1, - 5 => 1, - ), - 'fr' => array( - 1 => 0, - 0 => 0, - 5 => 1, - ), - 'hr' => array( - 1 => 0, - 21 => 0, - 0 => 2, - 2 => 1, - 8 => 2, - ), - 'hu' => array( - 1 => -1, - 21 => -1, - 0 => -1, - ), - ); - - foreach ($plural_tests as $langcode => $tests) { - foreach ($tests as $count => $expected_plural_index) { - // Assert that the we get the right plural index. - $this->assertIdentical(locale_get_plural($count, $langcode), $expected_plural_index, 'Computed plural index for ' . $langcode . ' for count ' . $count . ' is ' . $expected_plural_index); - // Assert that the we get the right translation for that. Change the - // expected index as per the logic for translation lookups. - $expected_plural_index = ($count == 1) ? 0 : $expected_plural_index; - $expected_plural_string = str_replace('@count', $count, $plural_strings[$langcode][$expected_plural_index]); - $this->assertIdentical(format_plural($count, '1 hour', '@count hours', array(), array('langcode' => $langcode)), $expected_plural_string, 'Plural translation of 1 hours / @count hours for count ' . $count . ' in ' . $langcode . ' is ' . $expected_plural_string); - } - } - } - - /** - * Tests plural editing and export functionality. - */ - function testPluralEditExport() { - // 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', - )); - - // 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', t('Exported French translation file.')); - // Ensure our imported translations exist in the file. - $this->assertRaw("msgid \"Monday\"\nmsgstr \"lundi\"", t('French translations present in exported file.')); - // Check for plural export specifically. - $this->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"1 heure\"\nmsgstr[1] \"@count heures\"", t('Plural translations exported properly.')); - - // Get the Croatian translations. - $this->drupalPost('admin/config/regional/translate/export', array( - 'langcode' => 'hr', - ), t('Export')); - // Ensure we have a translation file. - $this->assertRaw('# Croatian translation of Drupal', t('Exported Croatian translation file.')); - // Ensure our imported translations exist in the file. - $this->assertRaw("msgid \"Monday\"\nmsgstr \"Ponedjeljak\"", t('Croatian translations present in exported file.')); - // Check for plural export specifically. - $this->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata\"\nmsgstr[2] \"@count sati\"", t('Plural translations exported properly.')); - - // Check if the source appears on the translation page. - $this->drupalGet('admin/config/regional/translate'); - $this->assertText("1 hour, @count hours"); - - // Look up editing page for this plural string and check fields. - $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", array(':source' => "1 hour" . LOCALE_PLURAL_DELIMITER . "@count hours"))->fetchField(); - $path = 'admin/config/regional/translate/edit/' . $lid; - $this->drupalGet($path); - // Labels for plural editing elements. - $this->assertText('Singular form'); - $this->assertText('First plural form'); - $this->assertText('2. plural form'); - // Plural values for both languages. - $this->assertFieldById('edit-translations-hr-0', '@count sat'); - $this->assertFieldById('edit-translations-hr-1', '@count sata'); - $this->assertFieldById('edit-translations-hr-2', '@count sati'); - $this->assertNoFieldById('edit-translations-hr-3'); - $this->assertFieldById('edit-translations-fr-0', '1 heure'); - $this->assertFieldById('edit-translations-fr-1', '@count heures'); - $this->assertNoFieldById('edit-translations-fr-2'); - - // Edit some translations and see if that took effect. - $edit = array( - 'translations[fr][0]' => '1 heure edited', - 'translations[hr][1]' => '@count sata edited', - ); - $this->drupalPost($path, $edit, t('Save translations')); - - // Inject a plural source string to the database. We need to use a specific - // langcode here because the language will be English by default and will - // not save our source string for performance optimization if we do not ask - // specifically for a language. - format_plural(1, '1 day', '@count days', array(), array('langcode' => 'fr')); - // Look up editing page for this plural string and check fields. - $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", array(':source' => "1 day" . LOCALE_PLURAL_DELIMITER . "@count days"))->fetchField(); - $path = 'admin/config/regional/translate/edit/' . $lid; - - // Save complete translations for the string in both languages. - $edit = array( - 'translations[fr][0]' => '1 jour', - 'translations[fr][1]' => '@count jours', - 'translations[hr][0]' => '@count dan', - 'translations[hr][1]' => '@count dana', - 'translations[hr][2]' => '@count dana', - ); - $this->drupalPost($path, $edit, t('Save translations')); - - // Get the French translations. - $this->drupalPost('admin/config/regional/translate/export', array( - 'langcode' => 'fr', - ), t('Export')); - // Check for plural export specifically. - $this->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"1 heure edited\"\nmsgstr[1] \"@count heures\"", t('Edited French plural translations for hours exported properly.')); - $this->assertRaw("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"1 jour\"\nmsgstr[1] \"@count jours\"", t('Added French plural translations for days exported properly.')); - - // Get the Croatian translations. - $this->drupalPost('admin/config/regional/translate/export', array( - 'langcode' => 'hr', - ), t('Export')); - // Check for plural export specifically. - $this->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata edited\"\nmsgstr[2] \"@count sati\"", t('Edited Croatian plural translations exported properly.')); - $this->assertRaw("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"@count dan\"\nmsgstr[1] \"@count dana\"\nmsgstr[2] \"@count dana\"", t('Added Croatian plural translations exported properly.')); - } - - - /** - * 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 = 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 8\\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 "1 hour" -msgid_plural "@count hours" -msgstr[0] "1 heure" -msgstr[1] "@count heures" - -msgid "Monday" -msgstr "lundi" -EOF; - } - - /** - * Returns a .po file with a complex plural formula. - */ - function getPoFileWithComplexPlural() { - return <<< EOF -msgid "" -msgstr "" -"Project-Id-Version: Drupal 8\\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; - } - - /** - * Returns a .po file with a missing plural formula. - */ - function getPoFileWithMissingPlural() { - return <<< EOF -msgid "" -msgstr "" -"Project-Id-Version: Drupal 8\\n" -"MIME-Version: 1.0\\n" -"Content-Type: text/plain; charset=UTF-8\\n" -"Content-Transfer-Encoding: 8bit\\n" - -msgid "Monday" -msgstr "lundi" -EOF; - } - - /** - * Returns a .po file with a broken plural formula. - */ - function getPoFileWithBrokenPlural() { - return <<< EOF -msgid "" -msgstr "" -"Project-Id-Version: Drupal 8\\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 "Ponedjeljak" -EOF; - } -} - -/** - * Functional tests for the import of translation files. - */ -class LocaleImportFunctionalTest extends WebTestBase { - 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(array('locale', 'dblog')); - - // Set the translation file directory. - variable_set('locale_translate_file_directory', drupal_get_path('module', 'locale') . '/tests'); - - $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')), t('The language has been automatically created.')); - - // The import should have created 8 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' => 8, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.')); - - // This import should have saved plural forms to have 2 variants. - $locale_plurals = variable_get('locale_translation_plurals', array()); - $this->assert($locale_plurals['fr']['plurals'] == 2, t('Plural number initialized.')); - - // Ensure we were redirected correctly. - $this->assertEqual($this->getUrl(), url('admin/config/regional/translate', array('absolute' => TRUE)), t('Correct page redirection.')); - - - // Try importing a .po file with invalid tags. - $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)), t('The translation file was successfully imported.')); - $skip_message = format_plural(2, 'A translation string was skipped because of disallowed or malformed HTML. <a href="@url">See the log</a> for details.', '@count translation strings were skipped because of disallowed or malformed HTML. <a href="@url">See the log</a> for details.', array('@url' => url('admin/reports/dblog'))); - $this->assertRaw($skip_message, t('Unsafe strings were skipped.')); - - - // 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, - ), t('Import')); - $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/import', array('absolute' => TRUE)), t('Correct page redirection.')); - $this->assertText(t('File to import not found.'), t('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', - )); - - // 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)), t('The translation file was successfully imported.')); - // Ensure string wasn't overwritten. - $search = array( - 'string' => 'Montag', - 'language' => 'fr', - 'translation' => 'translated', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertText(t('No strings available.'), t('String not overwritten by imported string.')); - - // This import should not have changed number of plural forms. - $locale_plurals = variable_get('locale_translation_plurals', array()); - $this->assert($locale_plurals['fr']['plurals'] == 2, t('Plural numbers untouched.')); - - // Try importing a .po file with overriding strings, and ensure existing - // strings are overwritten. - $this->importPoFile($this->getOverwritePoFile(), array( - 'langcode' => 'fr', - 'overwrite_options[not_customized]' => TRUE, - )); - - // 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)), t('The translation file was successfully imported.')); - // Ensure string was overwritten. - $search = array( - 'string' => 'Montag', - 'language' => 'fr', - 'translation' => 'translated', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertNoText(t('No strings available.'), t('String overwritten by imported string.')); - // This import should have changed number of plural forms. - $locale_plurals = variable_get('locale_translation_plurals', array()); - $this->assert($locale_plurals['fr']['plurals'] == 3, t('Plural numbers changed.')); - - // Importing a .po file and mark its strings as customized strings. - $this->importPoFile($this->getCustomPoFile(), array( - 'langcode' => 'fr', - 'customized' => TRUE, - )); - - // The import should have created 6 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' => 6, '%update' => 0, '%delete' => 0)), t('The customized translation file was successfully imported.')); - - // The database should now contain 6 customized strings (two imported - // strings are not translated). - $count = db_query('SELECT lid FROM {locales_target} WHERE customized = :custom', array(':custom' => 1))->rowCount(); - $this->assertEqual($count, 6, t('Customized translations succesfully imported.')); - - // Try importing a .po file with overriding strings, and ensure existing - // customized strings are kept. - $this->importPoFile($this->getCustomOverwritePoFile(), array( - 'langcode' => 'fr', - 'overwrite_options[not_customized]' => TRUE, - 'overwrite_options[customized]' => FALSE, - )); - - // 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)), t('The customized translation file was successfully imported.')); - // Ensure string wasn't overwritten. - $search = array( - 'string' => 'januari', - 'language' => 'fr', - 'translation' => 'translated', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertText(t('No strings available.'), t('Customized string not overwritten by imported string.')); - - // Try importing a .po file with overriding strings, and ensure existing - // customized strings are overwritten. - $this->importPoFile($this->getCustomOverwritePoFile(), array( - 'langcode' => 'fr', - 'overwrite_options[not_customized]' => FALSE, - 'overwrite_options[customized]' => TRUE, - )); - - // 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)), t('The customized translation file was successfully imported.')); - // Ensure string was overwritten. - $search = array( - 'string' => 'januari', - 'language' => 'fr', - 'translation' => 'translated', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertNoText(t('No strings available.'), t('Customized string overwritten by imported string.')); - - } - - /** - * Test automatic import of a module's translation files. - */ - 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); - - // Create a custom language. - $edit = array( - 'predefined_langcode' => 'custom', - 'langcode' => $langcode, - 'name' => $name, - '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.'), t('Language file automatically imported.')); - - // Ensure strings were successfully imported. - $search = array( - 'string' => 'lundi', - 'language' => $langcode, - 'translation' => 'translated', - ); - $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); - $this->assertNoText(t('No strings available.'), t('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', t('Long month name context is working.')); - $this->assertIdentical(t('May', array(), array('langcode' => 'hr')), 'Svi.', t('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)), t('The translation file was successfully imported.')); - $this->assertIdentical(t('Operations', array(), array('langcode' => $langcode)), 'Műveletek', t('String imported and translated.')); - - // Try importing a .po file. - $this->importPoFile($this->getPoFileWithEmptyMsgstr(), array( - 'langcode' => $langcode, - 'overwrite_options[not_customized]' => TRUE, - )); - $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)), t('The translation file was successfully imported.')); - // This is the language indicator on the translation search screen for - // untranslated strings. - $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> "; - $str = "Operations"; - $search = array( - 'string' => $str, - 'language' => 'all', - 'translation' => '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, t('Search found the string.')); - $this->assertRaw($language_indicator, t('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 = 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 8\\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 8\\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<img SRC="javascript:alert(\'xss\');">" - -msgid "delete" -msgstr "supprimer<script>alert('xss');</script>" - -EOF; - } - - /** - * Helper function that returns a proper .po file for testing. - */ - function getOverwritePoFile() { - return <<< EOF -msgid "" -msgstr "" -"Project-Id-Version: Drupal 8\\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 which strings will be marked - * as customized. - */ - function getCustomPoFile() { - return <<< EOF -msgid "" -msgstr "" -"Project-Id-Version: Drupal 8\\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 dog" -msgid_plural "@count dogs" -msgstr[0] "un chien" -msgstr[1] "@count chiens" - -msgid "January" -msgstr "janvier" - -msgid "February" -msgstr "février" - -msgid "March" -msgstr "mars" - -msgid "April" -msgstr "avril" - -msgid "June" -msgstr "juin" -EOF; - } - - /** - * Helper function that returns a .po file for testing customized strings. - */ - function getCustomOverwritePoFile() { - return <<< EOF -msgid "" -msgstr "" -"Project-Id-Version: Drupal 8\\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 "January" -msgstr "januari" - -msgid "February" -msgstr "februari" - -msgid "July" -msgstr "juillet" -EOF; - } - - /** - * Helper function that returns a .po file with context. - */ - function getPoFileWithContext() { - // Croatian (code hr) is one the 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 8\\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 8\\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 8\\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; - } - -} - -/** - * Functional tests for the export of translation files. - */ -class LocaleExportFunctionalTest extends WebTestBase { - 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'); - - $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 = 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', t('Exported French translation file.')); - // Ensure our imported translations exist in the file. - $this->assertRaw('msgstr "lundi"', t('French translations present in exported file.')); - - // Import some more French translations which will be marked as customized. - $name = tempnam('temporary://', "po2_") . '.po'; - file_put_contents($name, $this->getCustomPoFile()); - $this->drupalPost('admin/config/regional/translate/import', array( - 'langcode' => 'fr', - 'files[file]' => $name, - 'customized' => 1, - ), t('Import')); - drupal_unlink($name); - - // We can't import a string with an empty translation, but calling - // locale() for an new string creates an entry in the locales_source table. - locale('February', NULL, 'fr'); - - // Export only customized French translations. - $this->drupalPost('admin/config/regional/translate/export', array( - 'langcode' => 'fr', - 'content_options[not_customized]' => FALSE, - 'content_options[customized]' => TRUE, - 'content_options[not_translated]' => FALSE, - ), t('Export')); - - // Ensure we have a translation file. - $this->assertRaw('# French translation of Drupal', t('Exported French translation file with only customized strings.')); - // Ensure the customized translations exist in the file. - $this->assertRaw('msgstr "janvier"', t('French custom translation present in exported file.')); - // Ensure no untranslated strings exist in the file. - $this->assertNoRaw('msgid "February"', t('Untranslated string not present in exported file.')); - - // Export only untranslated French translations. - $this->drupalPost('admin/config/regional/translate/export', array( - 'langcode' => 'fr', - 'content_options[not_customized]' => FALSE, - 'content_options[customized]' => FALSE, - 'content_options[not_translated]' => TRUE, - ), t('Export')); - - // Ensure we have a translation file. - $this->assertRaw('# French translation of Drupal', t('Exported French translation file with only untranslated strings.')); - // Ensure no customized translations exist in the file. - $this->assertNoRaw('msgstr "janvier"', t('French custom translation not present in exported file.')); - // Ensure the untranslated strings exist in the file. - $this->assertRaw('msgid "February"', t('Untranslated string present in exported file.')); - } - - /** - * Test exportation of translation template file. - */ - function testExportTranslationTemplateFile() { - // Get the translation template file. - $this->drupalPost('admin/config/regional/translate/export', array(), t('Export')); - // Ensure we have a translation file. - $this->assertRaw('# LANGUAGE translation of PROJECT', t('Exported translation template file.')); - } - - /** - * Helper function that returns a proper .po file. - */ - function getPoFile() { - return <<< EOF -msgid "" -msgstr "" -"Project-Id-Version: Drupal 8\\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; - } - - /** - * Helper function that returns a .po file which strings will be marked - * as customized. - */ - function getCustomPoFile() { - return <<< EOF -msgid "" -msgstr "" -"Project-Id-Version: Drupal 8\\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 "January" -msgstr "janvier" -EOF; - } - -} - -/** - * Tests for the st() function. - */ -class LocaleInstallTest extends WebTestBase { - 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 . '/core/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(), t('Function signatures of t() and st() are equal.')); - } -} - -/** - * Locale uninstall with English UI functional test. - */ -class LocaleUninstallFunctionalTest extends WebTestBase { - 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(array('node', 'locale')); - $this->langcode = 'en'; - - // Create Article node type. - $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); - } - - /** - * Check if the values of the Locale variables are correct after uninstall. - */ - function testUninstallProcess() { - $locale_module = array('locale', 'language'); - - $language = (object) array( - 'langcode' => 'fr', - 'name' => 'French', - 'default' => $this->langcode == 'fr', - ); - language_save($language); - - // Check the UI language. - drupal_language_initialize(); - $this->assertEqual(drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode, $this->langcode, t('Current language: %lang', array('%lang' => drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode))); - - // Enable multilingual workflow option for articles. - variable_set('node_type_language_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', array( - ':location' => '%.js%', - ))->fetchObject(); - $edit = array('translations[fr][0]' => 'french translation'); - $this->drupalPost('admin/config/regional/translate/edit/' . $string->lid, $edit, t('Save translations')); - _locale_rebuild_js('fr'); - $locale_javascripts = variable_get('locale_translation_javascript', array()); - $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/fr_' . $locale_javascripts['fr'] . '.js'; - $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('none')))); - - // Disable string caching. - variable_set('locale_cache_strings', 0); - - // Change language negotiation options. - drupal_load('module', 'locale'); - variable_set('language_types', language_types_get_default() + array('language_custom' => TRUE)); - variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, language_language_negotiation_info()); - variable_set('language_negotiation_' . LANGUAGE_TYPE_CONTENT, language_language_negotiation_info()); - variable_set('language_negotiation_' . LANGUAGE_TYPE_URL, language_language_negotiation_info()); - - // Change language negotiation settings. - variable_set('language_negotiation_url_part', LANGUAGE_NEGOTIATION_URL_PREFIX); - variable_set('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(drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode, 'en', t('Language after uninstall: %lang', array('%lang' => drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode))); - - // Check JavaScript files deletion. - $this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found')))); - - // Check language count. - $language_count = variable_get('language_count', 1); - $this->assertEqual($language_count, 1, t('Language count: %count', array('%count' => $language_count))); - - // Check language negotiation. - require_once DRUPAL_ROOT . '/core/includes/language.inc'; - $this->assertTrue(count(language_types_get_all()) == count(language_types_get_default()), t('Language types reset')); - $language_negotiation = language_negotiation_method_get_first(LANGUAGE_TYPE_INTERFACE) == LANGUAGE_NEGOTIATION_DEFAULT; - $this->assertTrue($language_negotiation, t('Interface language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set')))); - $language_negotiation = language_negotiation_method_get_first(LANGUAGE_TYPE_CONTENT) == LANGUAGE_NEGOTIATION_DEFAULT; - $this->assertTrue($language_negotiation, t('Content language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set')))); - $language_negotiation = language_negotiation_method_get_first(LANGUAGE_TYPE_URL) == LANGUAGE_NEGOTIATION_DEFAULT; - $this->assertTrue($language_negotiation, t('URL language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set')))); - - // Check language negotiation method settings. - $this->assertFalse(variable_get('language_negotiation_url_part', FALSE), t('URL language negotiation method indicator settings cleared.')); - $this->assertFalse(variable_get('language_negotiation_session_param', FALSE), t('Visit language negotiation method settings cleared.')); - - // Check JavaScript parsed. - $javascript_parsed_count = count(variable_get('javascript_parsed', array())); - $this->assertEqual($javascript_parsed_count, 0, t('JavaScript parsed count: %count', array('%count' => $javascript_parsed_count))); - - // Check JavaScript translations directory. - $locale_js_directory = variable_get('locale_js_directory', 'languages'); - $this->assertEqual($locale_js_directory, 'languages', t('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, t('String caching: %status', array('%status' => t($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->langcode = 'fr'; - } -} - -/** - * Functional tests for configuring a different path alias per language. - */ -class LocalePathFunctionalTest extends WebTestBase { - 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(array('node', 'locale', 'path')); - - $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); - variable_set('site_frontpage', 'node'); - } - - /** - * 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 domain prefix. - $prefix = $langcode; - $edit = array( - 'predefined_langcode' => 'custom', - 'langcode' => $langcode, - 'name' => $name, - 'direction' => '0', - ); - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); - - // Set path prefix. - $edit = array( "prefix[$langcode]" => $prefix ); - $this->drupalPost('admin/config/regional/language/detection/url', $edit, t('Save configuration')); - - // Check that the "xx" front page is readily available because path prefix - // negotiation is pre-configured. - $this->drupalGet($prefix); - $this->assertText(t('Welcome to Drupal'), t('The "xx" front page is readily available.')); - - // 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, - 'langcode' => '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, - 'langcode' => $langcode, - ); - $this->drupalPost($path, $edit, t('Save')); - - // Confirm English language path alias works. - $this->drupalGet($english_path); - $this->assertText($node->title, t('English alias works.')); - - // Confirm custom language path alias works. - $this->drupalGet($prefix . '/' . $custom_language_path); - $this->assertText($node->title, t('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, - 'langcode' => LANGUAGE_NOT_SPECIFIED, - ); - path_save($edit); - $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, 'en'); - $this->assertEqual($english_path, $lookup_path, t('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, t('Custom language alias has priority.')); - path_delete($edit); - - // Create language nodes to check priority of aliases. - $first_node = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1)); - $second_node = $this->drupalCreateNode(array('type' => 'page', '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, - 'langcode' => 'en', - ); - path_save($edit); - - // Assign a custom path alias to second node with LANGUAGE_NOT_SPECIFIED. - $edit = array( - 'source' => 'node/' . $second_node->nid, - 'alias' => $custom_path, - 'langcode' => LANGUAGE_NOT_SPECIFIED, - ); - path_save($edit); - - // Test that both node titles link to our path alias. - $this->drupalGet('<front>'); - $custom_path_url = base_path() . $GLOBALS['script_path'] . $custom_path; - $elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $first_node->title)); - $this->assertTrue(!empty($elements), t('First node links to the path alias.')); - $elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $second_node->title)); - $this->assertTrue(!empty($elements), t('Second node links to the path alias.')); - - // Confirm that the custom path leads to the first node. - $this->drupalGet($custom_path); - $this->assertText($first_node->title, t('Custom alias returns first node.')); - - // Confirm that the custom path with prefix leads to the second node. - $this->drupalGet($prefix . '/' . $custom_path); - $this->assertText($second_node->title, t('Custom alias with prefix returns second node.')); - - } -} - -/** - * Functional tests for multilingual support on nodes. - */ -class LocaleContentFunctionalTest extends WebTestBase { - protected $profile = 'standard'; - - public static function getInfo() { - return array( - 'name' => 'Content language settings', - 'description' => 'Checks you can enable multilingual support on content types and configure a language for a node.', - 'group' => 'Locale', - ); - } - - function setUp() { - parent::setUp('locale'); - } - - /** - * Verifies that machine name fields are always LTR. - */ - function testMachineNameLTR() { - // User to add and remove language. - $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages')); - - // Log in as admin. - $this->drupalLogin($admin_user); - - // Verify that the machine name field is LTR for a new content type. - $this->drupalGet('admin/structure/types/add'); - $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when no additional language is configured.'); - - // Install the Arabic language (which is RTL) and configure as the default. - $edit = array(); - $edit['predefined_langcode'] = 'ar'; - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - $edit = array(); - $edit['site_default'] = 'ar'; - $this->drupalPost(NULL, $edit, t('Save configuration')); - - // Verify that the machine name field is still LTR for a new content type. - $this->drupalGet('admin/structure/types/add'); - $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when the default language is RTL.'); - } - - /** - * Test if a content type can be set to multilingual and language is present. - */ - function testContentTypeLanguageConfiguration() { - global $base_url; - - // User to add and remove language. - $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages')); - // User to create a node. - $web_user = $this->drupalCreateUser(array('create article content', 'create page content', 'edit any page content')); - - // Add custom language. - $this->drupalLogin($admin_user); - // Code for the language. - $langcode = 'xx'; - // The English name for the language. - $name = $this->randomName(16); - $edit = array( - 'predefined_langcode' => 'custom', - 'langcode' => $langcode, - 'name' => $name, - 'direction' => '0', - ); - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); - - // Set "Basic page" content type to use multilingual support. - $this->drupalGet('admin/structure/types/manage/page'); - $this->assertText(t('Multilingual support'), t('Multilingual support fieldset present on content type configuration form.')); - $edit = array( - 'node_type_language' => 1, - ); - $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type')); - $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.')); - $this->drupalLogout(); - - // Verify language selection is not present on add article form. - $this->drupalLogin($web_user); - $this->drupalGet('node/add/article'); - // Verify language select list is not present. - $this->assertNoFieldByName('language', NULL, t('Language select not present on add article form.')); - - // Verify language selection appears on add "Basic page" form. - $this->drupalGet('node/add/page'); - // Verify language select list is present. - $this->assertFieldByName('langcode', NULL, t('Language select present on add Basic page form.')); - // Ensure language appears. - $this->assertText($name, t('Language present.')); - - // Create "Basic page" content. - $node_title = $this->randomName(); - $node_body = $this->randomName(); - $edit = array( - 'type' => 'page', - 'title' => $node_title, - 'body' => array($langcode => array(array('value' => $node_body))), - 'langcode' => $langcode, - ); - $node = $this->drupalCreateNode($edit); - // Edit the content and ensure correct language is selected. - $path = 'node/' . $node->nid . '/edit'; - $this->drupalGet($path); - $this->assertRaw('<option value="' . $langcode . '" selected="selected">' . $name . '</option>', t('Correct language selected.')); - // Ensure we can change the node language. - $edit = array( - 'langcode' => 'en', - ); - $this->drupalPost($path, $edit, t('Save')); - $this->assertRaw(t('%title has been updated.', array('%title' => $node_title)), t('Basic page content updated.')); - - $this->drupalLogout(); - } - - /** - * Test if a dir and lang tags exist in node's attributes. - */ - function testContentTypeDirLang() { - // User to add and remove language. - $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages')); - // User to create a node. - $web_user = $this->drupalCreateUser(array('create article content', 'edit own article content')); - - // Login as admin. - $this->drupalLogin($admin_user); - - // Install Arabic language. - $edit = array(); - $edit['predefined_langcode'] = 'ar'; - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Install Spanish language. - $edit = array(); - $edit['predefined_langcode'] = 'es'; - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Set "Article" content type to use multilingual support. - $this->drupalGet('admin/structure/types/manage/article'); - $edit = array( - 'node_type_language' => 1, - ); - $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type')); - $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Article')), t('Article content type has been updated.')); - $this->drupalLogout(); - - // Login as web user to add new article. - $this->drupalLogin($web_user); - - // Create three nodes: English, Arabic and Spanish. - $node_en = $this->createNodeArticle('en'); - $node_ar = $this->createNodeArticle('ar'); - $node_es = $this->createNodeArticle('es'); - - $this->drupalGet('node'); - - // Check if English node does not have lang tag. - $pattern = '|id="node-' . $node_en->nid . '"[^<>]*lang="en"|'; - $this->assertNoPattern($pattern, t('The lang tag has not been assigned to the English node.')); - - // Check if English node does not have dir tag. - $pattern = '|id="node-' . $node_en->nid . '"[^<>]*dir="ltr"|'; - $this->assertNoPattern($pattern, t('The dir tag has not been assigned to the English node.')); - - // Check if Arabic node has lang="ar" & dir="rtl" tags. - $pattern = '|id="node-' . $node_ar->nid . '"[^<>]*lang="ar" dir="rtl"|'; - $this->assertPattern($pattern, t('The lang and dir tags have been assigned correctly to the Arabic node.')); - - // Check if Spanish node has lang="es" tag. - $pattern = '|id="node-' . $node_es->nid . '"[^<>]*lang="es"|'; - $this->assertPattern($pattern, t('The lang tag has been assigned correctly to the Spanish node.')); - - // Check if Spanish node does not have dir="ltr" tag. - $pattern = '|id="node-' . $node_es->nid . '"[^<>]*lang="es" dir="ltr"|'; - $this->assertNoPattern($pattern, t('The dir tag has not been assigned to the Spanish node.')); - - $this->drupalLogout(); - } - - /** - * Create node in a specific language. - */ - protected function createNodeArticle($langcode) { - $this->drupalGet('node/add/article'); - $node_title = $this->randomName(); - $node_body = $this->randomName(); - $edit = array( - 'type' => 'article', - 'title' => $node_title, - 'body' => array($langcode => array(array('value' => $node_body))), - 'langcode' => $langcode, - 'promote' => 1, - ); - return $this->drupalCreateNode($edit); - } -} - -/** - * Functional test for multilingual fields. - */ -class LocaleMultilingualFieldsFunctionalTest extends WebTestBase { - public static function getInfo() { - return array( - 'name' => 'Multilingual fields', - 'description' => 'Test multilingual support for fields.', - 'group' => 'Locale', - ); - } - - function setUp() { - parent::setUp(array('node', 'locale')); - - // Create Basic page node type. - $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); - - // Setup users. - $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages', 'create page content', 'edit own page content')); - $this->drupalLogin($admin_user); - - // Add a new language. - $language = (object) array( - 'langcode' => 'it', - 'name' => 'Italian', - ); - language_save($language); - - // Enable URL language detection and selection. - $edit = array('language_interface[enabled][language-url]' => '1'); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Set "Basic page" content type to use multilingual support. - $edit = array( - 'node_type_language' => 1, - ); - $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type')); - $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.')); - - // Make node body translatable. - $field = field_info_field('body'); - $field['translatable'] = TRUE; - field_update_field($field); - } - - /** - * Test if field languages are correctly set through the node form. - */ - function testMultilingualNodeForm() { - // Create "Basic page" content. - $langcode = LANGUAGE_NOT_SPECIFIED; - $title_key = "title"; - $title_value = $this->randomName(8); - $body_key = "body[$langcode][0][value]"; - $body_value = $this->randomName(16); - - // Create node to edit. - $edit = array(); - $edit[$title_key] = $title_value; - $edit[$body_key] = $body_value; - $edit['langcode'] = 'en'; - $this->drupalPost('node/add/page', $edit, t('Save')); - - // Check that the node exists in the database. - $node = $this->drupalGetNodeByTitle($edit[$title_key]); - $this->assertTrue($node, t('Node found in database.')); - - $assert = isset($node->body['en']) && !isset($node->body[LANGUAGE_NOT_SPECIFIED]) && $node->body['en'][0]['value'] == $body_value; - $this->assertTrue($assert, t('Field language correctly set.')); - - // Change node language. - $this->drupalGet("node/$node->nid/edit"); - $edit = array( - $title_key => $this->randomName(8), - 'langcode' => 'it' - ); - $this->drupalPost(NULL, $edit, t('Save')); - $node = $this->drupalGetNodeByTitle($edit[$title_key]); - $this->assertTrue($node, t('Node found in database.')); - - $assert = isset($node->body['it']) && !isset($node->body['en']) && $node->body['it'][0]['value'] == $body_value; - $this->assertTrue($assert, t('Field language correctly changed.')); - - // Enable content language URL detection. - language_negotiation_set(LANGUAGE_TYPE_CONTENT, array(LANGUAGE_NEGOTIATION_URL => 0)); - - // Test multilingual field language fallback logic. - $this->drupalGet("it/node/$node->nid"); - $this->assertRaw($body_value, t('Body correctly displayed using Italian as requested language')); - - $this->drupalGet("node/$node->nid"); - $this->assertRaw($body_value, t('Body correctly displayed using English as requested language')); - } - - /* - * Test multilingual field display settings. - */ - function testMultilingualDisplaySettings() { - // Create "Basic page" content. - $langcode = LANGUAGE_NOT_SPECIFIED; - $title_key = "title"; - $title_value = $this->randomName(8); - $body_key = "body[$langcode][0][value]"; - $body_value = $this->randomName(16); - - // Create node to edit. - $edit = array(); - $edit[$title_key] = $title_value; - $edit[$body_key] = $body_value; - $edit['langcode'] = 'en'; - $this->drupalPost('node/add/page', $edit, t('Save')); - - // Check that the node exists in the database. - $node = $this->drupalGetNodeByTitle($edit[$title_key]); - $this->assertTrue($node, t('Node found in database.')); - - // Check if node body is showed. - $this->drupalGet("node/$node->nid"); - $body = $this->xpath('//article[@id=:id]//div[@class=:class]/descendant::p', array( - ':id' => 'node-' . $node->nid, - ':class' => 'content', - )); - $this->assertEqual(current($body), $node->body['en'][0]['value'], 'Node body found.'); - } -} - -/** - * Functional tests for comment language. - */ -class LocaleCommentLanguageFunctionalTest extends WebTestBase { - protected $profile = 'standard'; - - public static function getInfo() { - return array( - 'name' => 'Comment language', - 'description' => 'Tests for comment language.', - 'group' => 'Locale', - ); - } - - function setUp() { - // We also use language_test module here to be able to turn on content - // language negotiation. Drupal core does not provide a way in itself - // to do that. - parent::setUp('locale', 'language_test'); - - // Create and login user. - $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'create article content')); - $this->drupalLogin($admin_user); - - // Add language. - $edit = array('predefined_langcode' => 'fr'); - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Set "Article" content type to use multilingual support. - $edit = array('node_type_language' => 1); - $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type')); - - // Enable content language negotiation UI. - variable_set('language_test_content_language_type', TRUE); - - // Set interface language detection to user and content language detection - // to URL. Disable inheritance from interface language to ensure content - // language will fall back to the default language if no URL language can be - // detected. - $edit = array( - 'language_interface[enabled][language-user]' => TRUE, - 'language_content[enabled][language-url]' => TRUE, - 'language_content[enabled][language-interface]' => FALSE, - ); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Change user language preference, this way interface language is always - // French no matter what path prefix the URLs have. - $edit = array('preferred_langcode' => 'fr'); - $this->drupalPost("user/{$admin_user->uid}/edit", $edit, t('Save')); - } - - /** - * Test that comment language is properly set. - */ - function testCommentLanguage() { - drupal_static_reset('language_list'); - - // Create two nodes, one for english and one for french, and comment each - // node using both english and french as content language by changing URL - // language prefixes. Meanwhile interface language is always French, which - // is the user language preference. This way we can ensure that node - // language and interface language do not influence comment language, as - // only content language has to. - foreach (language_list() as $node_langcode => $node_language) { - $langcode_not_specified = LANGUAGE_NOT_SPECIFIED; - - // Create "Article" content. - $title = $this->randomName(); - $edit = array( - "title" => $title, - "body[$langcode_not_specified][0][value]" => $this->randomName(), - "langcode" => $node_langcode, - ); - $this->drupalPost("node/add/article", $edit, t('Save')); - $node = $this->drupalGetNodeByTitle($title); - - $prefixes = language_negotiation_url_prefixes(); - foreach (language_list() as $langcode => $language) { - // Post a comment with content language $langcode. - $prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/'; - $edit = array("comment_body[$langcode_not_specified][0][value]" => $this->randomName()); - $this->drupalPost("{$prefix}node/{$node->nid}", $edit, t('Save')); - - // Check that comment language matches the current content language. - $comment = db_select('comment', 'c') - ->fields('c') - ->condition('nid', $node->nid) - ->orderBy('cid', 'DESC') - ->execute() - ->fetchObject(); - $args = array('%node_language' => $node_langcode, '%comment_language' => $comment->langcode, '%langcode' => $langcode); - $this->assertEqual($comment->langcode, $langcode, t('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args)); - } - } - } -} - -/** - * Functional tests for localizing date formats. - */ -class LocaleDateFormatsFunctionalTest extends WebTestBase { - - public static function getInfo() { - return array( - 'name' => 'Localize date formats', - 'description' => 'Tests for the localization of date formats.', - 'group' => 'Locale', - ); - } - - function setUp() { - parent::setUp(array('node', 'locale')); - - // Create Article node type. - $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); - - // Create and login user. - $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'create article content')); - $this->drupalLogin($admin_user); - } - - /** - * Functional tests for localizing date formats. - */ - function testLocalizeDateFormats() { - // Add language. - $edit = array( - 'predefined_langcode' => 'fr', - ); - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Set language negotiation. - $language_type = LANGUAGE_TYPE_INTERFACE; - $edit = array( - "{$language_type}[enabled][language-url]" => TRUE, - ); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Configure date formats. - $this->drupalGet('admin/config/regional/date-time/locale'); - $this->assertText('French', 'Configured languages appear.'); - $edit = array( - 'date_format_long' => 'd.m.Y - H:i', - 'date_format_medium' => 'd.m.Y - H:i', - 'date_format_short' => 'd.m.Y - H:i', - ); - $this->drupalPost('admin/config/regional/date-time/locale/fr/edit', $edit, t('Save configuration')); - $this->assertText(t('Configuration saved.'), 'French date formats updated.'); - $edit = array( - 'date_format_long' => 'j M Y - g:ia', - 'date_format_medium' => 'j M Y - g:ia', - 'date_format_short' => 'j M Y - g:ia', - ); - $this->drupalPost('admin/config/regional/date-time/locale/en/edit', $edit, t('Save configuration')); - $this->assertText(t('Configuration saved.'), 'English date formats updated.'); - - // Create node content. - $node = $this->drupalCreateNode(array('type' => 'article')); - - // Configure format for the node posted date changes with the language. - $this->drupalGet('node/' . $node->nid); - $english_date = format_date($node->created, 'custom', 'j M Y'); - $this->assertText($english_date, t('English date format appears')); - $this->drupalGet('fr/node/' . $node->nid); - $french_date = format_date($node->created, 'custom', 'd.m.Y'); - $this->assertText($french_date, t('French date format appears')); - } -} diff --git a/core/modules/openid/openid.install b/core/modules/openid/openid.install index d348043f7a3cb5f2807551921c7c393e9649820b..3753f17166a903e52e0f987694b6c0c4e3fd624d 100644 --- a/core/modules/openid/openid.install +++ b/core/modules/openid/openid.install @@ -108,7 +108,6 @@ function openid_requirements($phase) { else { $requirements['openid_math'] = array( 'value' => t('Installed'), - 'severity' => REQUIREMENT_OK, ); } $requirements['openid_math']['title'] = t('OpenID Math library'); diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 0c19eec667a8ae81084e38c4f456e5fe2bd69bd7..536bb96d4887c560f5305e671a43642401113ee3 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -1178,9 +1178,9 @@ protected function drupalGetAJAX($path, array $options = array(), array $headers * @param $edit * Field data in an associative array. Changes the current input fields * (where possible) to the values indicated. A checkbox can be set to - * TRUE to be checked and FALSE to be unchecked. Note that when a form - * contains file upload fields, other fields cannot start with the '@' - * character. + * TRUE to be checked and should be set to FALSE to be unchecked. Note that + * when a form contains file upload fields, other fields cannot start with + * the '@' character. * * Multiple select fields can be set using name[] and setting each of the * possible values. Example: diff --git a/core/modules/system/system.admin-rtl.css b/core/modules/system/system.admin-rtl.css index e597e633f19e5f180c4d01728ba893340d9c47b6..d7553b552b0ed90eef9dd4dc59572614f6303ee7 100644 --- a/core/modules/system/system.admin-rtl.css +++ b/core/modules/system/system.admin-rtl.css @@ -36,9 +36,6 @@ table.system-status-report td.status-icon { padding-left: 0; padding-right: 6px; } -table.system-status-report tr.merge-up td { - padding: 0 28px 8px 6px; -} /** * Appearance page. diff --git a/core/modules/system/system.admin.css b/core/modules/system/system.admin.css index 0f3c0e53a9589808f8722ba6d6c3ee5194be68bd..957d3e08d981f894e9524ddd632f232d80d72021 100644 --- a/core/modules/system/system.admin.css +++ b/core/modules/system/system.admin.css @@ -99,10 +99,10 @@ a.module-link-configure { */ table.system-status-report td { padding: 6px; - vertical-align: middle; + vertical-align: top; } -table.system-status-report tr.merge-up td { - padding: 0 6px 8px 28px; /* LTR */ +table.system-status-report td:nth-child(-n+2) { + background-color: rgba(0, 0, 0, 0.04); } table.system-status-report td.status-icon { width: 16px; @@ -119,13 +119,8 @@ table.system-status-report tr.error td.status-icon div { table.system-status-report tr.warning td.status-icon div { background-image: url(../../misc/message-16-warning.png); } -tr.merge-down, -tr.merge-down td { - border-bottom-width: 0 !important; -} -tr.merge-up, -tr.merge-up td { - border-top-width: 0 !important; +table.system-status-report .status-title { + width: 25%; } /** diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index 22d8c1f7174b3eb8d2ae0a13d686f3b211110fff..a31e278483c4076df5ce38bb91bda2c2887a8407 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -2513,25 +2513,39 @@ function theme_status_report($variables) { 'class' => 'error', ), ); - $output = '<table class="system-status-report">'; + $output = '<table class="system-status-report"><tbody>'; foreach ($requirements as $requirement) { if (empty($requirement['#type'])) { - $severity = $severities[isset($requirement['severity']) ? (int) $requirement['severity'] : 0]; + // Always use the explicit requirement severity, if defined. Otherwise, + // default to REQUIREMENT_OK in the installer to visually confirm that + // installation requirements are met. And default to REQUIREMENT_INFO to + // denote neutral information without special visualization. + if (isset($requirement['severity'])) { + $severity = $severities[(int) $requirement['severity']]; + } + elseif (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'install') { + $severity = $severities[REQUIREMENT_OK]; + } + else { + $severity = $severities[REQUIREMENT_INFO]; + } + $severity['icon'] = '<div title="' . $severity['title'] . '"><span class="element-invisible">' . $severity['title'] . '</span></div>'; - // Output table row(s) + // Output table rows. + $output .= '<tr class="' . $severity['class'] . '">'; + $output .= '<td class="status-icon">' . $severity['icon'] . '</td>'; + $output .= '<td class="status-title">' . $requirement['title'] . '</td>'; + $output .= '<td class="status-value">' . $requirement['value']; if (!empty($requirement['description'])) { - $output .= '<tr class="' . $severity['class'] . ' merge-down"><td class="status-icon">' . $severity['icon'] . '</td><td class="status-title">' . $requirement['title'] . '</td><td class="status-value">' . $requirement['value'] . '</td></tr>'; - $output .= '<tr class="' . $severity['class'] . ' merge-up"><td colspan="3" class="status-description">' . $requirement['description'] . '</td></tr>'; - } - else { - $output .= '<tr class="' . $severity['class'] . '"><td class="status-icon">' . $severity['icon'] . '</td><td class="status-title">' . $requirement['title'] . '</td><td class="status-value">' . $requirement['value'] . '</td></tr>'; + $output .= '<div class="description">' . $requirement['description'] . '</div>'; } + $output .= '</td></tr>'; } } - $output .= '</table>'; + $output .= '</tbody></table>'; return $output; } diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 199f8c0616d5559de0ca6292fd5465bfa214b69a..f967bb677e3ab00ab158f97498eba9a8a565ee6c 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -268,7 +268,7 @@ function system_requirements($phase) { } // Determine severity based on time since cron last ran. - $severity = REQUIREMENT_OK; + $severity = REQUIREMENT_INFO; if (REQUEST_TIME - $cron_last > $threshold_error) { $severity = REQUIREMENT_ERROR; } @@ -279,7 +279,7 @@ function system_requirements($phase) { // Set summary and description based on values determined above. $summary = $t('Last run !time ago', array('!time' => format_interval(REQUEST_TIME - $cron_last))); $description = ''; - if ($severity != REQUIREMENT_OK) { + if ($severity != REQUIREMENT_INFO) { $description = $t('Cron has not run recently.') . ' ' . $help; } @@ -385,7 +385,6 @@ function system_requirements($phase) { if ($phase == 'runtime') { $requirements['update'] = array( 'title' => $t('Database updates'), - 'severity' => REQUIREMENT_OK, 'value' => $t('Up to date'), ); diff --git a/core/modules/system/tests/xmlrpc.test b/core/modules/system/tests/xmlrpc.test index a77c38e265dc9bd46d807f4d9eb174762e3f4d5f..b5c07ca87d341eb0917aa432f6383dd76fe9aeab 100644 --- a/core/modules/system/tests/xmlrpc.test +++ b/core/modules/system/tests/xmlrpc.test @@ -19,6 +19,8 @@ class XMLRPCBasicTestCase extends WebTestBase { * Ensure that a basic XML-RPC call with no parameters works. */ protected function testListMethods() { + global $base_url; + // Minimum list of methods that should be included. $minimum = array( 'system.multicall', @@ -29,7 +31,7 @@ class XMLRPCBasicTestCase extends WebTestBase { ); // Invoke XML-RPC call to get list of methods. - $url = url(NULL, array('absolute' => TRUE)) . 'core/xmlrpc.php'; + $url = $base_url . '/core/xmlrpc.php'; $methods = xmlrpc($url, array('system.listMethods' => array())); // Ensure that the minimum methods were found. @@ -47,7 +49,9 @@ class XMLRPCBasicTestCase extends WebTestBase { * Ensure that system.methodSignature returns an array of signatures. */ protected function testMethodSignature() { - $url = url(NULL, array('absolute' => TRUE)) . 'core/xmlrpc.php'; + global $base_url; + + $url = $base_url . '/core/xmlrpc.php'; $signature = xmlrpc($url, array('system.methodSignature' => array('system.listMethods'))); $this->assert(is_array($signature) && !empty($signature) && is_array($signature[0]), t('system.methodSignature returns an array of signature arrays.')); @@ -99,7 +103,8 @@ class XMLRPCValidator1IncTestCase extends WebTestBase { * Run validator1 tests. */ function testValidator1() { - $xml_url = url(NULL, array('absolute' => TRUE)) . 'core/xmlrpc.php'; + global $base_url; + $xml_url = $base_url . '/core/xmlrpc.php'; srand(); mt_srand(); @@ -213,7 +218,9 @@ class XMLRPCMessagesTestCase extends WebTestBase { * Make sure that XML-RPC can transfer large messages. */ function testSizedMessages() { - $xml_url = url(NULL, array('absolute' => TRUE)) . 'core/xmlrpc.php'; + global $base_url; + + $xml_url = $base_url . '/core/xmlrpc.php'; $sizes = array(8, 80, 160); foreach ($sizes as $size) { $xml_message_l = xmlrpc_test_message_sized_in_kb($size); @@ -227,10 +234,11 @@ class XMLRPCMessagesTestCase extends WebTestBase { * Ensure that hook_xmlrpc_alter() can hide even builtin methods. */ protected function testAlterListMethods() { + global $base_url; // Ensure xmlrpc_test_xmlrpc_alter() is disabled and retrieve regular list of methods. variable_set('xmlrpc_test_xmlrpc_alter', FALSE); - $url = url(NULL, array('absolute' => TRUE)) . 'core/xmlrpc.php'; + $url = $base_url . '/core/xmlrpc.php'; $methods1 = xmlrpc($url, array('system.listMethods' => array())); // Enable the alter hook and retrieve the list of methods again. diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 7fdb8a2cb10ea917b4313fec258f3e24c08f31ea..b536b0803842ee2fed0a94666ed9a191f8f2d5ae 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -1661,3 +1661,35 @@ function taxonomy_taxonomy_term_delete(Term $term) { /** * @} End of "defgroup taxonomy_index". */ + +/** + * Implements hook_entity_query_alter(). + * + * Converts EntityFieldQuery instances on taxonomy terms that have an entity + * condition on term bundles (vocabulary machine names). Since the vocabulary + * machine name is not present in the {taxonomy_term_data} table itself, we have + * to convert the bundle condition into a proprety condition of vocabulary IDs + * to match against {taxonomy_term_data}.vid. + */ +function taxonomy_entity_query_alter($query) { + $conditions = &$query->entityConditions; + + // Alter only taxonomy term queries with bundle conditions. + if (isset($conditions['entity_type']) && $conditions['entity_type']['value'] == 'taxonomy_term' && isset($conditions['bundle'])) { + // Convert vocabulary machine names to vocabulary IDs. + $vocabulary_data = taxonomy_vocabulary_get_names(); + $vids = array(); + if (is_array($conditions['bundle']['value'])) { + foreach ($conditions['bundle']['value'] as $vocabulary_machine_name) { + $vids[] = $vocabulary_data[$vocabulary_machine_name]->vid; + } + } + else { + $vocabulary_machine_name = $conditions['bundle']['value']; + $vids = $vocabulary_data[$vocabulary_machine_name]->vid; + } + + $query->propertyCondition('vid', $vids, $conditions['bundle']['operator']); + unset($conditions['bundle']); + } +} diff --git a/core/modules/taxonomy/taxonomy.test b/core/modules/taxonomy/taxonomy.test index bf22852b6ce01aa168345c17c108664fd75a82f8..c7e87e8ced510e5e21f6485b06866ec7e3e5401b 100644 --- a/core/modules/taxonomy/taxonomy.test +++ b/core/modules/taxonomy/taxonomy.test @@ -1951,5 +1951,22 @@ class TaxonomyEFQTestCase extends TaxonomyWebTestCase { $result = $result['taxonomy_term']; asort($result); $this->assertEqual(array_keys($terms), array_keys($result), 'Taxonomy terms were retrieved by EntityFieldQuery.'); + + // Create a second vocabulary and five more terms. + $vocabulary2 = $this->createVocabulary(); + $terms2 = array(); + for ($i = 0; $i < 5; $i++) { + $term = $this->createTerm($vocabulary2); + $terms2[$term->tid] = $term; + } + + $query = new EntityFieldQuery(); + $query->entityCondition('entity_type', 'taxonomy_term'); + $query->entityCondition('bundle', $vocabulary2->machine_name); + $result = $query->execute(); + $result = $result['taxonomy_term']; + asort($result); + $this->assertEqual(array_keys($terms2), array_keys($result), format_string('Taxonomy terms from the %name vocabulary were retrieved by EntityFieldQuery.', array('%name' => $vocabulary2->name))); } + } diff --git a/core/modules/translation/translation.test b/core/modules/translation/lib/Drupal/translation/Tests/TranslationTest.php similarity index 99% rename from core/modules/translation/translation.test rename to core/modules/translation/lib/Drupal/translation/Tests/TranslationTest.php index 1a0f0a017ddb349bbffcfb2dbf3bd585e972982c..19e7a38173dde64e578fb6377df0734d53328363 100644 --- a/core/modules/translation/translation.test +++ b/core/modules/translation/lib/Drupal/translation/Tests/TranslationTest.php @@ -2,16 +2,18 @@ /** * @file - * Tests for the Translation module. + * Definition of Drupal\translation\Tests\TranslationTest. */ +namespace Drupal\translation\Tests; + use Drupal\node\Node; use Drupal\simpletest\WebTestBase; /** * Functional tests for the Translation module. */ -class TranslationTestCase extends WebTestBase { +class TranslationTest extends WebTestBase { protected $profile = 'standard'; protected $book; diff --git a/core/modules/translation/translation.info b/core/modules/translation/translation.info index 7d574abeb4981fd0c055ff0dfe06236a4533ddaf..df1b63e4213f7e3b49ea3c9f75c2b08285e598de 100644 --- a/core/modules/translation/translation.info +++ b/core/modules/translation/translation.info @@ -4,4 +4,3 @@ dependencies[] = locale package = Core version = VERSION core = 8.x -files[] = translation.test diff --git a/core/themes/seven/style.css b/core/themes/seven/style.css index a3d6773a62d59e72a33c335ca1a5257aec6ed387..380aec821faf0f06ee9888bf816a39015fb3c744 100644 --- a/core/themes/seven/style.css +++ b/core/themes/seven/style.css @@ -499,6 +499,7 @@ table tr.selected td { background: #ffc; border-color: #eeb; } + table.system-status-report tr { border-bottom: 1px solid #ccc; } @@ -506,10 +507,6 @@ table.system-status-report tr.ok { color: #255b1e; background-color: #e5ffe2; } -table.system-status-report tr.info { - color: #040f37; - background-color: #bdf; -} table.system-status-report tr.warning { color: #840; background-color: #fffce5; @@ -518,6 +515,7 @@ table.system-status-report tr.error { color: #8c2e0b; background-color: #fef5f1; } + /** * Exception for webkit bug with the right border of the last cell * in some tables, since it's webkit only, we can use :last-child