304 lines
11 KiB
Plaintext
304 lines
11 KiB
Plaintext
<?php
|
|
// $Id$
|
|
|
|
/**
|
|
* @file
|
|
* Tests for the path module
|
|
*/
|
|
|
|
class PathTestCase extends DrupalWebTestCase {
|
|
public static function getInfo() {
|
|
return array(
|
|
'name' => 'Path alias functionality',
|
|
'description' => 'Add, edit, delete, and change alias and verify its consistency in the database.',
|
|
'group' => 'Path',
|
|
);
|
|
}
|
|
|
|
function setUp() {
|
|
parent::setUp('path');
|
|
|
|
// Create test user and login.
|
|
$web_user = $this->drupalCreateUser(array('create page content', 'edit own page content', 'administer url aliases', 'create url aliases'));
|
|
$this->drupalLogin($web_user);
|
|
}
|
|
|
|
/**
|
|
* Test the path cache.
|
|
*/
|
|
function testPathCache() {
|
|
// Create test node.
|
|
$node1 = $this->drupalCreateNode();
|
|
|
|
// Create alias.
|
|
$edit = array();
|
|
$edit['source'] = 'node/' . $node1->nid;
|
|
$edit['alias'] = $this->randomName(8);
|
|
$this->drupalPost('admin/config/search/path/add', $edit, t('Create new alias'));
|
|
|
|
// Visit the system path for the node and confirm a cache entry is
|
|
// created.
|
|
cache_clear_all('*', 'cache_path', TRUE);
|
|
$this->drupalGet($edit['source']);
|
|
$this->assertTrue(cache_get($edit['source'], 'cache_path'), t('Cache entry was created.'));
|
|
|
|
// Visit the alias for the node and confirm a cache entry is created.
|
|
cache_clear_all('*', 'cache_path', TRUE);
|
|
$this->drupalGet($edit['alias']);
|
|
$this->assertTrue(cache_get($edit['source'], 'cache_path'), t('Cache entry was created.'));
|
|
}
|
|
|
|
/**
|
|
* Test alias functionality through the admin interfaces.
|
|
*/
|
|
function testAdminAlias() {
|
|
// Create test node.
|
|
$node1 = $this->drupalCreateNode();
|
|
|
|
// Create alias.
|
|
$edit = array();
|
|
$edit['source'] = 'node/' . $node1->nid;
|
|
$edit['alias'] = $this->randomName(8);
|
|
$this->drupalPost('admin/config/search/path/add', $edit, t('Create new alias'));
|
|
|
|
// Confirm that the alias works.
|
|
$this->drupalGet($edit['alias']);
|
|
$this->assertText($node1->title[LANGUAGE_NONE][0]['value'], 'Alias works.');
|
|
$this->assertResponse(200);
|
|
|
|
// Change alias.
|
|
$pid = $this->getPID($edit['alias']);
|
|
|
|
$previous = $edit['alias'];
|
|
$edit['alias'] = $this->randomName(8);
|
|
$this->drupalPost('admin/config/search/path/edit/' . $pid, $edit, t('Update alias'));
|
|
|
|
// Confirm that the alias works.
|
|
$this->drupalGet($edit['alias']);
|
|
$this->assertText($node1->title[LANGUAGE_NONE][0]['value'], 'Changed alias works.');
|
|
$this->assertResponse(200);
|
|
|
|
drupal_static_reset('drupal_lookup_path');
|
|
// Confirm that previous alias no longer works.
|
|
$this->drupalGet($previous);
|
|
$this->assertNoText($node1->title, 'Previous alias no longer works.');
|
|
$this->assertResponse(404);
|
|
|
|
// Create second test node.
|
|
$node2 = $this->drupalCreateNode();
|
|
|
|
// Set alias to second test node.
|
|
$edit['source'] = 'node/' . $node2->nid;
|
|
// leave $edit['alias'] the same
|
|
$this->drupalPost('admin/config/search/path/add', $edit, t('Create new alias'));
|
|
|
|
// Confirm no duplicate was created.
|
|
$this->assertRaw(t('The alias %alias is already in use in this language.', array('%alias' => $edit['alias'])), 'Attempt to move alias was rejected.');
|
|
|
|
// Delete alias.
|
|
$this->drupalPost('admin/config/search/path/delete/' . $pid, array(), t('Confirm'));
|
|
|
|
// Confirm that the alias no longer works.
|
|
$this->drupalGet($edit['alias']);
|
|
$this->assertNoText($node1->title, 'Alias was successfully deleted.');
|
|
$this->assertResponse(404);
|
|
}
|
|
|
|
/**
|
|
* Test alias functionality through the node interfaces.
|
|
*/
|
|
function testNodeAlias() {
|
|
// Create test node.
|
|
$node1 = $this->drupalCreateNode();
|
|
|
|
// Create alias.
|
|
$edit = array();
|
|
$edit['path[alias]'] = $this->randomName(8);
|
|
$this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));
|
|
|
|
// Confirm that the alias works.
|
|
$this->drupalGet($edit['path[alias]']);
|
|
$this->assertText($node1->title[LANGUAGE_NONE][0]['value'], 'Alias works.');
|
|
$this->assertResponse(200);
|
|
|
|
// Change alias.
|
|
$previous = $edit['path[alias]'];
|
|
$edit['path[alias]'] = $this->randomName(8);
|
|
$this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));
|
|
|
|
// Confirm that the alias works.
|
|
$this->drupalGet($edit['path[alias]']);
|
|
$this->assertText($node1->title[LANGUAGE_NONE][0]['value'], 'Changed alias works.');
|
|
$this->assertResponse(200);
|
|
|
|
// Make sure that previous alias no longer works.
|
|
$this->drupalGet($previous);
|
|
$this->assertNoText($node1->title[LANGUAGE_NONE][0]['value'], 'Previous alias no longer works.');
|
|
$this->assertResponse(404);
|
|
|
|
// Create second test node.
|
|
$node2 = $this->drupalCreateNode();
|
|
|
|
// Set alias to second test node.
|
|
// Leave $edit['path[alias]'] the same.
|
|
$this->drupalPost('node/' . $node2->nid . '/edit', $edit, t('Save'));
|
|
|
|
// Confirm that the alias didn't make a duplicate.
|
|
$this->assertText(t('The alias is already in use.'), 'Attempt to moved alias was rejected.');
|
|
|
|
// Delete alias.
|
|
$this->drupalPost('node/' . $node1->nid . '/edit', array('path[alias]' => ''), t('Save'));
|
|
|
|
// Confirm that the alias no longer works.
|
|
$this->drupalGet($edit['path[alias]']);
|
|
$this->assertNoText($node1->title, 'Alias was successfully deleted.');
|
|
$this->assertResponse(404);
|
|
}
|
|
|
|
function getPID($alias) {
|
|
return db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", array(':alias' => $alias))->fetchField();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test URL aliases for taxonomy terms.
|
|
*/
|
|
class PathTaxonomyTermTestCase extends DrupalWebTestCase {
|
|
public static function getInfo() {
|
|
return array(
|
|
'name' => 'Taxonomy term URL aliases',
|
|
'description' => 'Tests URL aliases for taxonomy terms.',
|
|
'group' => 'Path',
|
|
);
|
|
}
|
|
|
|
function setUp() {
|
|
parent::setUp('path', 'taxonomy');
|
|
|
|
// Create and login user.
|
|
$web_user = $this->drupalCreateUser(array('administer url aliases', 'administer taxonomy', 'access administration pages'));
|
|
$this->drupalLogin($web_user);
|
|
}
|
|
|
|
/**
|
|
* Test alias functionality through the admin interfaces.
|
|
*/
|
|
function testTermAlias() {
|
|
// Create a term in the default 'Tags' vocabulary with URL alias.
|
|
$edit = array();
|
|
$edit['name'] = $this->randomName();
|
|
$edit['description'] = $this->randomName();
|
|
$edit['path[alias]'] = $this->randomName();
|
|
$this->drupalPost('admin/structure/taxonomy/1/add', $edit, t('Save'));
|
|
|
|
// Confirm that the alias works.
|
|
$this->drupalGet($edit['path[alias]']);
|
|
$this->assertText($edit['description'], 'Term can be accessed on URL alias.');
|
|
|
|
// Change the term's URL alias.
|
|
$tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name", array(':name' => $edit['name']))->fetchField();
|
|
$edit2 = array();
|
|
$edit2['path[alias]'] = $this->randomName();
|
|
$this->drupalPost('taxonomy/term/' . $tid . '/edit', $edit2, t('Save'));
|
|
|
|
// Confirm that the changed alias works.
|
|
$this->drupalGet($edit2['path[alias]']);
|
|
$this->assertText($edit['description'], 'Term can be accessed on changed URL alias.');
|
|
|
|
// Confirm that the old alias no longer works.
|
|
$this->drupalGet($edit['path[alias]']);
|
|
$this->assertNoText($edit['description'], 'Old URL alias has been removed after altering.');
|
|
$this->assertResponse(404, 'Old URL alias returns 404.');
|
|
|
|
// Remove the term's URL alias.
|
|
$edit3 = array();
|
|
$edit3['path[alias]'] = '';
|
|
$this->drupalPost('taxonomy/term/' . $tid . '/edit', $edit3, t('Save'));
|
|
|
|
// Confirm that the alias no longer works.
|
|
$this->drupalGet($edit2['path[alias]']);
|
|
$this->assertNoText($edit['description'], 'Old URL alias has been removed after altering.');
|
|
$this->assertResponse(404, 'Old URL alias returns 404.');
|
|
}
|
|
}
|
|
|
|
class PathLanguageTestCase extends DrupalWebTestCase {
|
|
public static function getInfo() {
|
|
return array(
|
|
'name' => 'Path aliases with translated nodes',
|
|
'description' => 'Confirm that paths work with translated nodes',
|
|
'group' => 'Path',
|
|
);
|
|
}
|
|
|
|
function setUp() {
|
|
parent::setUp('path', 'locale', 'translation');
|
|
|
|
// Create and login user.
|
|
$web_user = $this->drupalCreateUser(array('edit any page content', 'create page content', 'administer url aliases', 'create url aliases', 'administer languages', 'translate content', 'access administration pages'));
|
|
$this->drupalLogin($web_user);
|
|
|
|
// Enable French language.
|
|
$edit = array();
|
|
$edit['langcode'] = 'fr';
|
|
|
|
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
|
|
|
|
// Set language negotiation to "Path prefix with fallback".
|
|
include_once DRUPAL_ROOT . '/includes/locale.inc';
|
|
variable_set('language_negotiation_' . LANGUAGE_TYPE_CONTENT, locale_language_negotiation_info());
|
|
variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX);
|
|
|
|
// Force inclusion of language.inc.
|
|
drupal_language_initialize();
|
|
}
|
|
|
|
/**
|
|
* Test alias functionality through the admin interfaces.
|
|
*/
|
|
function testAliasTranslation() {
|
|
// Set 'page' content type to enable translation.
|
|
variable_set('language_content_type_page', 2);
|
|
|
|
$english_node = $this->drupalCreateNode(array('type' => 'page'));
|
|
|
|
// Edit the node to set language and path.
|
|
$edit = array();
|
|
$edit['language'] = 'en';
|
|
$edit['path[alias]'] = $this->randomName();
|
|
$this->drupalPost('node/' . $english_node->nid . '/edit', $edit, t('Save'));
|
|
|
|
// Confirm that the alias works.
|
|
$this->drupalGet($edit['path[alias]']);
|
|
$this->assertText($english_node->title[LANGUAGE_NONE][0]['value'], 'Alias works.');
|
|
|
|
// Translate the node into French.
|
|
$this->drupalGet('node/' . $english_node->nid . '/translate');
|
|
$this->clickLink(t('add translation'));
|
|
$edit = array();
|
|
$langcode = 'fr';
|
|
$edit["body[$langcode][0][value]"] = $this->randomName();
|
|
$langcode = LANGUAGE_NONE;
|
|
$edit["title[$langcode][0][value]"] = $this->randomName();
|
|
$edit['path[alias]'] = $this->randomName();
|
|
$this->drupalPost(NULL, $edit, t('Save'));
|
|
|
|
// Clear the path lookup cache.
|
|
drupal_lookup_path('wipe');
|
|
|
|
// Ensure the node was created.
|
|
$french_node = $this->drupalGetNodeByTitle($edit["title[$langcode][0][value]"]);
|
|
$this->assertTrue(($french_node), 'Node found in database.');
|
|
|
|
// Confirm that the alias works.
|
|
$this->drupalGet('fr/' . $edit['path[alias]']);
|
|
$this->assertText($french_node->title[LANGUAGE_NONE][0]['value'], 'Alias for French translation works.');
|
|
|
|
// Confirm that the alias is returned by url().
|
|
drupal_static_reset('language_list');
|
|
$languages = language_list();
|
|
$url = url('node/' . $french_node->nid, array('language' => $languages[$french_node->language]));
|
|
$this->assertTrue(strpos($url, $edit['path[alias]']), t('URL contains the path alias.'));
|
|
}
|
|
}
|