summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--og.module46
-rw-r--r--tests/og.language.test140
-rw-r--r--tests/og_testcase.php7
3 files changed, 188 insertions, 5 deletions
diff --git a/og.module b/og.module
index d62857e..05f2bf7 100644
--- a/og.module
+++ b/og.module
@@ -496,14 +496,52 @@ function og_exit() {
/**
* Set the language for the page based on group's language. Will have no effect
- * if user has set a personal language.
+ * if user has set a personal language or a URL language domain/prefix.
* @param string $node
* A group node object.
+ * @see language_initialize().
*/
function og_set_language($node) {
+ // If group specifies language, and the URL does not:
if ($node->og_language) {
- $map = language_list();
- $og_language = $map[$node->og_language];
+ // Configured presentation language mode.
+ $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);
+ // Get a list of enabled languages.
+ $languages = language_list('enabled');
+ $languages = $languages[1];
+
+ // Set to true if the url holds a language designator.
+ $url_set = FALSE;
+ // Check whether the URL holds a language domain/prefix.
+ switch ($mode) {
+ case LANGUAGE_NEGOTIATION_DOMAIN:
+ foreach ($languages as $language) {
+ $parts = parse_url($language->domain);
+ if (!empty($parts['host']) && ($_SERVER['HTTP_HOST'] == $parts['host'])) {
+ $url_set = TRUE;
+ break;
+ }
+ }
+ break;
+ case LANGUAGE_NEGOTIATION_PATH:
+ // Uses $_REQUEST as language_initialize removes prefix from $_GET
+ $args = isset($_REQUEST['q']) ? explode('/', $_REQUEST['q']) : array();
+ $prefix = array_shift($args);
+ // Search prefix within enabled languages.
+ foreach ($languages as $language) {
+ if (!empty($language->prefix) && $language->prefix == $prefix) {
+ $url_set = TRUE;
+ break;
+ }
+ }
+ break;
+ }
+
+ if ($url_set) {
+ return;
+ }
+
+ $og_language = $languages[$node->og_language];
global $user;
$user_language = user_preferred_language($user, $og_language);
if ($og_language == $user_language) {
@@ -2449,4 +2487,4 @@ function og_check_token($token, $seed) {
*/
function og_broadcast_access($node) {
return og_is_group_admin($node) && module_exists('og_notifications');
-} \ No newline at end of file
+}
diff --git a/tests/og.language.test b/tests/og.language.test
new file mode 100644
index 0000000..c6fd6b5
--- /dev/null
+++ b/tests/og.language.test
@@ -0,0 +1,140 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Language tests for the organic groups module.
+ */
+
+ require_once drupal_get_path('module', 'og') . '/tests/og_testcase.php';
+ require_once './includes/locale.inc';
+
+class OgLanguage extends OgTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => t('Organic groups language tests'),
+ 'description' => t('Tests the language in which the page is rendered.'),
+ 'group' => t('Organic groups'),
+ );
+ }
+
+ function setUp() {
+ parent::setUp('og', 'locale');
+ // Create a user with admin permissions.
+ $web_admin = $this->drupalCreateUser(array(
+ 'administer nodes',
+ 'administer content types',
+ 'access administration pages',
+ 'administer site configuration',
+ 'administer organic groups',
+ ));
+
+ $this->drupalLogin($web_admin);
+
+ // Create a group node content type.
+ $og_group_type = $this->drupalCreateContentType();
+ variable_set('og_content_type_usage_'. $og_group_type->name, 'group');
+ $this->group_type = $og_group_type->name;
+
+ // Setup languages
+ variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH);
+ locale_add_language('es');
+ locale_add_language('de');
+
+ // Rebuild the menu so the new content types will appear in the menu.
+ menu_rebuild();
+
+ $this->group_nid = array();
+ // Create a group node (default language).
+ $this->group_nid['default'] = $this->addOgGroup($og_group_type->name);
+ // Create a group node (German language).
+ $this->group_nid['de'] = $this->addOgGroup($og_group_type->name, OG_OPEN, array('og_language' => 'de'));
+ // Create a group node (Spanish language).
+ $this->group_nid['es'] = $this->addOgGroup($og_group_type->name, OG_OPEN, array('og_language' => 'es'));
+
+ // Create viewing users.
+ $this->language_user = $this->drupalCreateUser(array(
+ 'access content',
+ 'edit any ' . $og_group_type->type . ' content',
+ ));
+ }
+
+ /**
+ * Test the page language in different group/path/user combinations.
+ */
+ function testOgLanguage() {
+ // Neutral Language User
+ $this->drupalLogin($this->language_user);
+
+ // Language neutral group, no path prefix. Should be site default.
+ $this->ogLanguageTestCase(language_default('language'), NULL, 'default');
+ // German group, no path prefix. Should be German.
+ $this->ogLanguageTestCase('de', NULL, 'de');
+ // Language neutral group, German prefix. Should be German.
+ $this->ogLanguageTestCase('de', 'de', 'default');
+ // Language German group, Spanish prefix. Should be Spanish.
+ $this->ogLanguageTestCase('es', 'es', 'de');
+
+ // German Language User
+ $this->drupalPost('user/' . $this->language_user->uid . '/edit',
+ array('language' => 'de'), t('Save'));
+ $this->language_user->language = 'de';
+
+ // Language neutral group, no path prefix. Should be German.
+ $this->ogLanguageTestCase('de', NULL, 'default');
+ // Language neutral group, Spanish path prefix. Should be Spanish.
+ $this->ogLanguageTestCase('es', 'es', 'default');
+ // Language Spanish group, no path prefix. Should be German.
+ $this->ogLanguageTestCase('de', NULL, 'es');
+ }
+
+ /**
+ * Test language negotiation.
+ *
+ * @param $langcode
+ * Two-letter language code.
+ * @param $prefix
+ * URL Language Prefix. Not necessarily the same as $langcode.
+ * @param $group
+ * Language of the group to be tested.
+ */
+ function ogLanguageTestCase($langcode, $prefix = NULL, $group = 'default') {
+ $url_prefix = $prefix . '/';
+ $output = $this->drupalGet($url_prefix . 'node/' . $this->group_nid[$group]);
+ $this->assertLanguage($langcode, $prefix, $group);
+
+ // test language persistence to outgoing links
+ $this->clickLink(t('Edit'));
+ $this->assertLanguage($langcode, $prefix, $group);
+ }
+
+ /**
+ * Check the language of the rendered page.
+ */
+ function assertLanguage($langcode, $prefix, $group) {
+ // Add sensible defaults for test results.
+ if (empty($prefix)) {
+ $prefix = 'none';
+ }
+ if (empty($group)) {
+ $group = 'neutral';
+ }
+ if (!$this->loggedInUser) {
+ $user_language = 'n/a';
+ }
+ else {
+ $user_language = !empty($this->loggedInUser->language) ? $this->loggedInUser->language : 'neutral';
+ }
+
+ $msg = t("Identified !language language page. [Prefix: !prefix, User: !user_language, Group: !group]",
+ array(
+ '!language' => $langcode,
+ '!prefix' => $prefix,
+ '!user_language' => $user_language,
+ '!group' => $group,
+ )
+ );
+ return $this->assertPattern('/\<html.*(xml\:)?lang="' . $langcode . '"/US', $msg, 'Language');
+ }
+}
+
diff --git a/tests/og_testcase.php b/tests/og_testcase.php
index efb1174..eb7deaf 100644
--- a/tests/og_testcase.php
+++ b/tests/og_testcase.php
@@ -14,13 +14,16 @@ class OgTestCase extends DrupalWebTestCase {
* The content type name.
* @param $selective
* The group's visibility (e.g. open, moderated, etc').
+ * @param $args
+ * Other node fields to be saved with the node object.
* @return
* The newly created node id.
*/
- function addOgGroup($type, $selective = OG_OPEN) {
+ function addOgGroup($type, $selective = OG_OPEN, $args = array()) {
$edit = array();
$edit['og_description'] = $this->randomName(16);
$edit['og_selective'] = $selective;
+ $edit = array_merge($edit, $args);
// Keys that should be present when the node is loaded.
$keys = array(
@@ -32,6 +35,8 @@ class OgTestCase extends DrupalWebTestCase {
'og_language',
'og_private',
);
+ $keys = array_merge($keys, array_keys($args));
+
$og_type = t('Group node');
return $this->_addOgContent($type, $og_type, $edit, $keys);
}