1 locale.test | LocalePathFunctionalTest::testPathLanguageConfiguration() |
Test if a language can be associated with a URL alias.
File
- core/
modules/ locale/ tests/ locale.test, line 1717 - Tests for locale.module.
Class
- LocalePathFunctionalTest
- Functional tests for configuring a different URL alias per language.
Code
function testPathLanguageConfiguration() {
global $base_url;
// User to add and remove language.
$admin_user = $this->backdropCreateUser(array('administer languages', 'create page content', 'administer url aliases', 'create url aliases', 'access administration pages'));
// Add custom language.
$this->backdropLogin($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,
'native' => $name,
'direction' => '0',
);
$this->backdropPost('admin/config/regional/language/add', $edit, t('Add custom language'));
// Set path prefix.
$edit = array("prefix[$langcode]" => $prefix);
$this->backdropPost('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
// Check that the "xx" home page is readily available because path prefix
// negotiation is pre-configured.
$this->backdropGet($prefix);
$this->assertText(t('Welcome to Backdrop'), 'The "xx" home page is readily available.');
// Create a node.
$node = $this->backdropCreateNode(array('type' => 'page'));
// Create a URL alias in default language (English).
$path = 'admin/config/urls/path/add';
$english_path = $this->randomName(8);
$edit = array(
'source' => 'node/' . $node->nid,
'alias' => $english_path,
'langcode' => 'en',
);
$this->backdropPost($path, $edit, t('Save URL alias'));
// Create a URL alias in new custom language.
$custom_language_path = $this->randomName(8);
$edit = array(
'source' => 'node/' . $node->nid,
'alias' => $custom_language_path,
'langcode' => $langcode,
);
$this->backdropPost($path, $edit, t('Save URL alias'));
// Confirm English language URL alias works.
$this->backdropGet($english_path);
$this->assertText($node->title, 'English alias works.');
// Confirm custom language URL alias works.
$this->backdropGet($prefix . '/' . $custom_language_path);
$this->assertText($node->title, '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_NONE,
);
path_save($edit);
$lookup_path = backdrop_lookup_path('alias', 'node/' . $node->nid, 'en');
$this->assertEqual($english_path, $lookup_path, 'English language alias has priority.');
// Same check for language 'xx'.
$lookup_path = backdrop_lookup_path('alias', 'node/' . $node->nid, $prefix);
$this->assertEqual($custom_language_path, $lookup_path, 'Custom language alias has priority.');
path_delete($edit);
// Create language nodes to check priority of aliases.
$first_node = $this->backdropCreateNode(array('type' => 'post', 'promote' => 1));
$second_node = $this->backdropCreateNode(array('type' => 'post', 'promote' => 1));
// Assign a custom URL 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 URL alias to second node with LANGUAGE_NONE.
$edit = array(
'source' => 'node/' . $second_node->nid,
'alias' => $custom_path,
'langcode' => LANGUAGE_NONE,
);
path_save($edit);
// Test that both node titles link to our URL alias.
$this->backdropGet('<front>');
$custom_path_url = base_path() . (config_get('system.core', 'clean_url') ? $custom_path : '?q=' . $custom_path);
$elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $first_node->title));
$this->assertTrue(!empty($elements), 'First node links to the URL alias.');
$elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $second_node->title));
$this->assertTrue(!empty($elements), 'Second node links to the URL alias.');
// Confirm that the custom path leads to the first node.
$this->backdropGet($custom_path);
$this->assertText($first_node->title, 'Custom alias returns first node.');
// Confirm that the custom path with prefix leads to the second node.
$this->backdropGet($prefix . '/' . $custom_path);
$this->assertText($second_node->title, 'Custom alias with prefix returns second node.');
}