Commit f6cb088e authored by catch's avatar catch
Browse files

Issue #1527720 by Gábor Hojtsy: Fixed OpenID language matching finds least...

Issue #1527720 by Gábor Hojtsy: Fixed OpenID language matching finds least specific instead of most specific match.
parent b5973cd9
......@@ -255,21 +255,24 @@ function openid_form_user_register_form_alter(&$form, &$form_state) {
// preferred langugage is "en-GB", we look for "en-gb" (Drupal's language
// codes are lower-case) or fall back to "en".
$parts = preg_split('/[_-]/', strtolower($language));
$candidate_languages[] = $parts[0];
$candidate_langcodes[] = $parts[0];
if (count($parts) > 1) {
$candidate_languages[] = $parts[0] . '-' . $parts[1];
$candidate_langcodes[] = $parts[0] . '-' . $parts[1];
}
if (count($parts) > 2) {
$candidate_languages[] = $parts[0] . '-' . $parts[2];
$candidate_languages[] = $parts[0] . '-' . $parts[1] . '-' . $parts[2];
$candidate_langcodes[] = $parts[0] . '-' . $parts[2];
$candidate_langcodes[] = $parts[0] . '-' . $parts[1] . '-' . $parts[2];
}
$enabled_languages = language_list(TRUE);
// Iterate over the generated permutations starting with the longest (most
// specific) strings.
foreach (array_reverse($candidate_languages) as $candidate_language) {
if (isset($enabled_languages[$candidate_language])) {
foreach (array_reverse($candidate_langcodes) as $candidate_langcode) {
if (isset($enabled_languages[$candidate_langcode])) {
$form['locale']['preferred_langcode']['#type'] = 'hidden';
$form['locale']['preferred_langcode']['#value'] = $candidate_language;
$form['locale']['preferred_langcode']['#value'] = $candidate_langcode;
// Skip the rest of the foreach to not overwrite the specific
// language we found.
break;
}
}
}
......
......@@ -453,7 +453,8 @@ class OpenIDRegistrationTestCase extends OpenIDWebTestCase {
}
function setUp() {
parent::setUp('openid', 'openid_test');
// Add language module too to test with some non-built-in languages.
parent::setUp('openid', 'openid_test', 'language');
variable_set('user_register', USER_REGISTER_VISITORS);
}
......@@ -469,10 +470,23 @@ class OpenIDRegistrationTestCase extends OpenIDWebTestCase {
variable_set('openid_test_response', array(
'openid.sreg.nickname' => 'john',
'openid.sreg.email' => 'john@example.com',
'openid.sreg.language' => 'en-GB',
'openid.sreg.language' => 'pt-BR',
'openid.sreg.timezone' => 'Europe/London',
));
// Save Portuguese and Portuguese, Portugal as optional languages. The
// process should pick 'pt' based on the sreg.language being 'pt-BR'
// (and falling back on least specific language given no pt-br available
// locally).
$language = (object) array(
'langcode' => 'pt',
);
language_save($language);
$language = (object) array(
'langcode' => 'pt-pt',
);
language_save($language);
// Use a User-supplied Identity that is the URL of an XRDS document.
$identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
$this->submitLoginForm($identity);
......@@ -484,7 +498,7 @@ class OpenIDRegistrationTestCase extends OpenIDWebTestCase {
$this->assertTrue($user, t('User was registered with right username.'));
$this->assertEqual($user->mail, 'john@example.com', t('User was registered with right email address.'));
$this->assertEqual($user->timezone, 'Europe/London', t('User was registered with right timezone.'));
$this->assertEqual($user->preferred_langcode, 'en', t('User was registered with right language.'));
$this->assertEqual($user->preferred_langcode, 'pt', t('User was registered with right language.'));
$this->assertFalse($user->data, t('No additional user info was saved.'));
$this->submitLoginForm($identity);
......@@ -513,10 +527,17 @@ class OpenIDRegistrationTestCase extends OpenIDWebTestCase {
variable_set('openid_test_response', array(
'openid.sreg.nickname' => 'john',
'openid.sreg.email' => 'john@example.com',
'openid.sreg.language' => 'en-GB',
'openid.sreg.language' => 'pt-BR',
'openid.sreg.timezone' => 'Europe/London',
));
// Save Portuguese, Brazil as an optional language. The process should pick
// 'pt-br' based on the sreg.language later.
$language = (object) array(
'langcode' => 'pt-br',
);
language_save($language);
// Use a User-supplied Identity that is the URL of an XRDS document.
$identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
$this->submitLoginForm($identity);
......@@ -526,7 +547,7 @@ class OpenIDRegistrationTestCase extends OpenIDWebTestCase {
$this->assertTrue($user, t('User was registered with right username.'));
$this->assertEqual($user->mail, 'john@example.com', t('User was registered with right email address.'));
$this->assertEqual($user->timezone, 'Europe/London', t('User was registered with right timezone.'));
$this->assertEqual($user->preferred_langcode, 'en', t('User was registered with right language.'));
$this->assertEqual($user->preferred_langcode, 'pt-br', t('User was registered with right language.'));
$this->assertFalse($user->data, t('No additional user info was saved.'));
$this->drupalLogout();
......@@ -644,9 +665,20 @@ class OpenIDRegistrationTestCase extends OpenIDWebTestCase {
'openid.ext123.type.timezone' => 'http://axschema.org/pref/timezone',
'openid.ext123.value.timezone' => 'Europe/London',
'openid.ext123.type.language' => 'http://axschema.org/pref/language',
'openid.ext123.value.language' => 'en-GB',
'openid.ext123.value.language' => 'pt-PT',
));
// Save Portuguese and Portuguese, Portugal as optional languages. The
// process should pick 'pt-pt' as the more specific language.
$language = (object) array(
'langcode' => 'pt',
);
language_save($language);
$language = (object) array(
'langcode' => 'pt-pt',
);
language_save($language);
// Use a User-supplied Identity that is the URL of an XRDS document.
$identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
$this->submitLoginForm($identity);
......@@ -656,7 +688,7 @@ class OpenIDRegistrationTestCase extends OpenIDWebTestCase {
$this->assertTrue($user, t('User was registered with right username.'));
$this->assertEqual($user->mail, 'john@example.com', t('User was registered with right email address.'));
$this->assertEqual($user->timezone, 'Europe/London', t('User was registered with right timezone.'));
$this->assertEqual($user->preferred_langcode, 'en', t('User was registered with right language.'));
$this->assertEqual($user->preferred_langcode, 'pt-pt', t('User was registered with right language.'));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment