Commit 1f4736c1 authored by catch's avatar catch

Issue #1591928 by aspilicious: Convert language tests to psr-0.

parent 65ed04ff
......@@ -4,4 +4,3 @@ package = Core
version = VERSION
core = 8.x
configure = admin/config/regional/language
files[] = language.test
<?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')));
}
}
}
<?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.'));
}
}
<?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');
}
}
<?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.'));
}
}
<?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)));
}