diff --git a/pathologic.module b/pathologic.module index bfa016b..06d01d6 100755 --- a/pathologic.module +++ b/pathologic.module @@ -127,6 +127,7 @@ function _pathologic_replace($matches) { // abuse eval() and friends; abuse globals; or abuse drupal_static(). The // latter is the least offensive, I think. $settings = drupal_static('_pathologic_filter'); + $langs = language_list(); // Now parse the URL $parts = parse_url($matches[2]); // Do some early tests to see if we should just give up now. @@ -176,6 +177,14 @@ function _pathologic_replace($matches) { else { $parts['trimmed_path'] = $parts['path']; } + // we want to remove the langcode prefix from the trimmed path if it already exists + // since it will be added by the language attribute + if (isset($langs[$settings['langcode']])) { + $lang_prefix = $langs[$settings['langcode']]->prefix . "/"; + if (strpos($parts['trimmed_path'], $lang_prefix) === 0) { + $parts['trimmed_path'] = drupal_substr($parts['trimmed_path'], drupal_strlen($lang_prefix)); + } + } if ($parts['trimmed_path'] === '') { // Okay, if we just ended up with an empty string, then what happened is // they linked to the front page of their Drupal installation using non- @@ -216,7 +225,6 @@ function _pathologic_replace($matches) { return $matches[0]; } // Okay, format the URL. - $langs = language_list(); $url = url( $parts['trimmed_path'], array( diff --git a/pathologic.test b/pathologic.test index 7318d21..156209d 100644 --- a/pathologic.test +++ b/pathologic.test @@ -141,3 +141,76 @@ class PathologicTestCase extends DrupalWebTestCase { ); } } + +class PathologicTestLanguageCase extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'Pathologic path filtering language handling', + 'description' => 'Test Pathologic’s path translation and conversion with language prefix.', + 'group' => 'Filter', + ); + } + + function setUp() { + parent::setUp('locale', 'locale_test', 'pathologic'); + + // Create and login user. + $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'administer url aliases', 'access administration pages', 'create page content', 'edit any page content')); + $this->drupalLogin($admin_user); + + // Add language. + $edit = array('langcode' => 'fr'); + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Set "Basic page" content type to use multilingual support. + $this->drupalGet('admin/structure/types/manage/page'); + $edit = array('language_content_type' => 1); + $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type')); + + // Enable URL language detection and selection. + $edit = array('language[enabled][locale-url]' => 1); + $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings')); + + // Change user language preference. + $edit = array('language' => 'fr'); + $this->drupalPost("user/{$admin_user->uid}/edit", $edit, t('Save')); + } + + function testPathologicLanguage() { + // Build a phony filter + $filter = new stdClass; + $filter->callback = '_pathologic'; + $filter->settings = array( + 'absolute' => TRUE, + 'local_paths' => "http://example.com/qux/abc", + ); + $filter->format = 0; + + // Create a node. + $title_value = $this->randomName(8); + $custom_path = $this->randomName(8) . "/oranges"; + $edit = array(); + $edit['title'] = $title_value; + $edit['language'] = 'fr'; + $edit['path[alias]'] = $custom_path; + $this->drupalPost('node/add/page', $edit, t('Save')); + $node = $this->drupalGetNodeByTitle($edit['title']); + + // Clear the path lookup cache. + drupal_lookup_path('wipe'); + drupal_static_reset('language_list'); + drupal_static_reset('locale_url_outbound_alter'); + + // Get the language object + $languages = language_list(); + $lang = $languages['fr']; + + // Test with path language prefix + $this->assertEqual( + _pathologic_filter('', $filter, NULL, 'fr', NULL, NULL), + '', + t('URLs with language prefix') + ); + $this->drupalLogout(); + } +}