From 1945b09ddd7208abca10011cf3414752a0da6517 Mon Sep 17 00:00:00 2001 From: Dries <dries@buytaert.net> Date: Sun, 3 Jun 2012 09:51:57 -0400 Subject: [PATCH] - Patch #1593360 by aspilicious, Rob Loach: Convert path tests to PSR-0. --- .../lib/Drupal/path/Tests/PathAliasTest.php | 208 +++++++ .../Drupal/path/Tests/PathLanguageTest.php | 151 +++++ .../Drupal/path/Tests/PathLanguageUiTest.php | 83 +++ .../path/Tests/PathTaxonomyTermTest.php | 79 +++ .../lib/Drupal/path/Tests/PathTestBase.php | 31 ++ core/modules/path/path.info | 1 - core/modules/path/path.test | 519 ------------------ 7 files changed, 552 insertions(+), 520 deletions(-) create mode 100644 core/modules/path/lib/Drupal/path/Tests/PathAliasTest.php create mode 100644 core/modules/path/lib/Drupal/path/Tests/PathLanguageTest.php create mode 100644 core/modules/path/lib/Drupal/path/Tests/PathLanguageUiTest.php create mode 100644 core/modules/path/lib/Drupal/path/Tests/PathTaxonomyTermTest.php create mode 100644 core/modules/path/lib/Drupal/path/Tests/PathTestBase.php delete mode 100644 core/modules/path/path.test diff --git a/core/modules/path/lib/Drupal/path/Tests/PathAliasTest.php b/core/modules/path/lib/Drupal/path/Tests/PathAliasTest.php new file mode 100644 index 000000000000..af8b0c7d0011 --- /dev/null +++ b/core/modules/path/lib/Drupal/path/Tests/PathAliasTest.php @@ -0,0 +1,208 @@ +<?php + +/** + * @file + * Definition of Drupal\path\Tests\PathAliasTest. + */ + +namespace Drupal\path\Tests; + +/** + * Tests path alias functionality. + */ +class PathAliasTest extends PathTestBase { + 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); + } + + /** + * Tests 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('Save')); + + // Visit the system path for the node and confirm a cache entry is + // created. + cache('path')->flush(); + $this->drupalGet($edit['source']); + $this->assertTrue(cache('path')->get($edit['source']), t('Cache entry was created.')); + + // Visit the alias for the node and confirm a cache entry is created. + cache('path')->flush(); + $this->drupalGet($edit['alias']); + $this->assertTrue(cache('path')->get($edit['source']), t('Cache entry was created.')); + } + + /** + * Tests 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('Save')); + + // Confirm that the alias works. + $this->drupalGet($edit['alias']); + $this->assertText($node1->title, 'Alias works.'); + $this->assertResponse(200); + + // Change alias to one containing "exotic" characters. + $pid = $this->getPID($edit['alias']); + + $previous = $edit['alias']; + $edit['alias'] = "- ._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters. + "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string. + "éøïвβä¸åœ‹æ›¸Ûž"; // Characters from various non-ASCII alphabets. + $this->drupalPost('admin/config/search/path/edit/' . $pid, $edit, t('Save')); + + // Confirm that the alias works. + $this->drupalGet($edit['alias']); + $this->assertText($node1->title, '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('Save')); + + // 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/edit/' . $pid, array(), t('Delete')); + $this->drupalPost(NULL, 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); + + // Create a really long alias. + $edit = array(); + $edit['source'] = 'node/' . $node1->nid; + $alias = $this->randomName(128); + $edit['alias'] = $alias; + // The alias is shortened to 50 characters counting the elipsis. + $truncated_alias = substr($alias, 0, 47); + $this->drupalPost('admin/config/search/path/add', $edit, t('Save')); + $this->assertNoText($alias, 'The untruncated alias was not found.'); + // The 'truncated' alias will always be found. + $this->assertText($truncated_alias, 'The truncated alias was found.'); + } + + /** + * Tests 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, 'Alias works.'); + $this->assertResponse(200); + + // Change alias to one containing "exotic" characters. + $previous = $edit['path[alias]']; + $edit['path[alias]'] = "- ._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters. + "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string. + "éøïвβä¸åœ‹æ›¸Ûž"; // Characters from various non-ASCII alphabets. + $this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save')); + + // Confirm that the alias works. + $this->drupalGet($edit['path[alias]']); + $this->assertText($node1->title, 'Changed alias works.'); + $this->assertResponse(200); + + // Make sure 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. + // 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); + } + + /** + * Returns the path ID. + * + * @param $alias + * A string containing an aliased path. + * + * @return int + * Integer representing the path ID. + */ + function getPID($alias) { + return db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", array(':alias' => $alias))->fetchField(); + } + + /** + * Tests that duplicate aliases fail validation. + */ + function testDuplicateNodeAlias() { + // Create one node with a random alias. + $node_one = $this->drupalCreateNode(); + $edit = array(); + $edit['path[alias]'] = $this->randomName(); + $this->drupalPost('node/' . $node_one->nid . '/edit', $edit, t('Save')); + + // Now create another node and try to set the same alias. + $node_two = $this->drupalCreateNode(); + $this->drupalPost('node/' . $node_two->nid . '/edit', $edit, t('Save')); + $this->assertText(t('The alias is already in use.')); + $this->assertFieldByXPath("//input[@name='path[alias]' and contains(@class, 'error')]", $edit['path[alias]'], 'Textfield exists and has the error class.'); + } +} diff --git a/core/modules/path/lib/Drupal/path/Tests/PathLanguageTest.php b/core/modules/path/lib/Drupal/path/Tests/PathLanguageTest.php new file mode 100644 index 000000000000..096729fc4dcc --- /dev/null +++ b/core/modules/path/lib/Drupal/path/Tests/PathLanguageTest.php @@ -0,0 +1,151 @@ +<?php + +/** + * @file + * Definition of Drupal\path\Tests\PathLanguageTest. + */ + +namespace Drupal\path\Tests; + +/** + * Tests URL aliases for translated nodes. + */ +class PathLanguageTest extends PathTestBase { + 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. + $this->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($this->web_user); + + // Enable French language. + $edit = array(); + $edit['predefined_langcode'] = 'fr'; + + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Enable URL language detection and selection. + $edit = array('language_interface[enabled][language-url]' => 1); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + } + + /** + * Test alias functionality through the admin interfaces. + */ + function testAliasTranslation() { + // Set 'page' content type to enable translation. + variable_set('node_type_language_page', TRANSLATION_ENABLED); + + $english_node = $this->drupalCreateNode(array('type' => 'page')); + $english_alias = $this->randomName(); + + // Edit the node to set language and path. + $edit = array(); + $edit['langcode'] = 'en'; + $edit['path[alias]'] = $english_alias; + $this->drupalPost('node/' . $english_node->nid . '/edit', $edit, t('Save')); + + // Confirm that the alias works. + $this->drupalGet($english_alias); + $this->assertText($english_node->title, 'Alias works.'); + + // Translate the node into French. + $this->drupalGet('node/' . $english_node->nid . '/translate'); + $this->clickLink(t('add translation')); + $edit = array(); + $langcode = LANGUAGE_NOT_SPECIFIED; + $edit["title"] = $this->randomName(); + $edit["body[$langcode][0][value]"] = $this->randomName(); + $french_alias = $this->randomName(); + $edit['path[alias]'] = $french_alias; + $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"]); + $this->assertTrue(($french_node), 'Node found in database.'); + + // Confirm that the alias works. + $this->drupalGet('fr/' . $edit['path[alias]']); + $this->assertText($french_node->title, 'Alias for French translation works.'); + + // Confirm that the alias is returned by url(). Languages are cached on + // many levels, and we need to clear those caches. + drupal_static_reset('language_list'); + drupal_static_reset('language_url_outbound_alter'); + drupal_static_reset('language_url_rewrite_url'); + $languages = language_list(); + $url = url('node/' . $french_node->nid, array('language' => $languages[$french_node->langcode])); + $this->assertTrue(strpos($url, $edit['path[alias]']), t('URL contains the path alias.')); + + // Confirm that the alias works even when changing language negotiation + // options. Enable User language detection and selection over URL one. + $edit = array( + 'language_interface[enabled][language-user]' => 1, + 'language_interface[weight][language-user]' => -9, + 'language_interface[enabled][language-url]' => 1, + 'language_interface[weight][language-url]' => -8, + ); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Change user language preference. + $edit = array('preferred_langcode' => 'fr'); + $this->drupalPost("user/{$this->web_user->uid}/edit", $edit, t('Save')); + + // Check that the English alias works. In this situation French is the + // current UI and content language, while URL language is English (since we + // do not have a path prefix we fall back to the site's default language). + // We need to ensure that the user language preference is not taken into + // account while determining the path alias language, because if this + // happens we have no way to check that the path alias is valid: there is no + // path alias for French matching the english alias. So drupal_lookup_path() + // needs to use the URL language to check whether the alias is valid. + $this->drupalGet($english_alias); + $this->assertText($english_node->title, 'Alias for English translation works.'); + + // Check that the French alias works. + $this->drupalGet("fr/$french_alias"); + $this->assertText($french_node->title, 'Alias for French translation works.'); + + // Disable URL language negotiation. + $edit = array('language_interface[enabled][language-url]' => FALSE); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + + // Check that the English alias still works. + $this->drupalGet($english_alias); + $this->assertText($english_node->title, 'Alias for English translation works.'); + + // Check that the French alias is not available. We check the unprefixed + // alias because we disabled URL language negotiation above. In this + // situation only aliases in the default language and language neutral ones + // should keep working. + $this->drupalGet($french_alias); + $this->assertResponse(404, t('Alias for French translation is unavailable when URL language negotiation is disabled.')); + + // drupal_lookup_path() has an internal static cache. Check to see that + // it has the appropriate contents at this point. + drupal_lookup_path('wipe'); + $french_node_path = drupal_lookup_path('source', $french_alias, $french_node->langcode); + $this->assertEqual($french_node_path, 'node/' . $french_node->nid, t('Normal path works.')); + // Second call should return the same path. + $french_node_path = drupal_lookup_path('source', $french_alias, $french_node->langcode); + $this->assertEqual($french_node_path, 'node/' . $french_node->nid, t('Normal path is the same.')); + + // Confirm that the alias works. + $french_node_alias = drupal_lookup_path('alias', 'node/' . $french_node->nid, $french_node->langcode); + $this->assertEqual($french_node_alias, $french_alias, t('Alias works.')); + // Second call should return the same alias. + $french_node_alias = drupal_lookup_path('alias', 'node/' . $french_node->nid, $french_node->langcode); + $this->assertEqual($french_node_alias, $french_alias, t('Alias is the same.')); + } +} diff --git a/core/modules/path/lib/Drupal/path/Tests/PathLanguageUiTest.php b/core/modules/path/lib/Drupal/path/Tests/PathLanguageUiTest.php new file mode 100644 index 000000000000..f1f56d621083 --- /dev/null +++ b/core/modules/path/lib/Drupal/path/Tests/PathLanguageUiTest.php @@ -0,0 +1,83 @@ +<?php + +/** + * @file + * Definition of Drupal\path\Tests\PathLanguageUiTest. + */ + +namespace Drupal\path\Tests; + +/** + * Tests the user interface for creating path aliases, with languages. + */ +class PathLanguageUiTest extends PathTestBase { + public static function getInfo() { + return array( + 'name' => 'Path aliases with languages', + 'description' => 'Confirm that the Path module user interface works with languages.', + 'group' => 'Path', + ); + } + + function setUp() { + parent::setUp('path', 'locale'); + + // Create and login user. + $web_user = $this->drupalCreateUser(array('edit any page content', 'create page content', 'administer url aliases', 'create url aliases', 'administer languages', 'access administration pages')); + $this->drupalLogin($web_user); + + // Enable French language. + $edit = array(); + $edit['predefined_langcode'] = 'fr'; + + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); + + // Enable URL language detection and selection. + $edit = array('language_interface[enabled][language-url]' => 1); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + } + + /** + * Tests that a language-neutral URL alias works. + */ + function testLanguageNeutralUrl() { + $name = $this->randomName(8); + $edit = array(); + $edit['source'] = 'admin/config/search/path'; + $edit['alias'] = $name; + $this->drupalPost('admin/config/search/path/add', $edit, t('Save')); + + $this->drupalGet($name); + $this->assertText(t('Filter aliases'), 'Language-neutral URL alias works'); + } + + /** + * Tests that a default language URL alias works. + */ + function testDefaultLanguageUrl() { + $name = $this->randomName(8); + $edit = array(); + $edit['source'] = 'admin/config/search/path'; + $edit['alias'] = $name; + $edit['langcode'] = 'en'; + $this->drupalPost('admin/config/search/path/add', $edit, t('Save')); + + $this->drupalGet($name); + $this->assertText(t('Filter aliases'), 'English URL alias works'); + } + + /** + * Tests that a non-default language URL alias works. + */ + function testNonDefaultUrl() { + $name = $this->randomName(8); + $edit = array(); + $edit['source'] = 'admin/config/search/path'; + $edit['alias'] = $name; + $edit['langcode'] = 'fr'; + $this->drupalPost('admin/config/search/path/add', $edit, t('Save')); + + $this->drupalGet('fr/' . $name); + $this->assertText(t('Filter aliases'), 'Foreign URL alias works'); + } +} diff --git a/core/modules/path/lib/Drupal/path/Tests/PathTaxonomyTermTest.php b/core/modules/path/lib/Drupal/path/Tests/PathTaxonomyTermTest.php new file mode 100644 index 000000000000..fffb7eaad6f3 --- /dev/null +++ b/core/modules/path/lib/Drupal/path/Tests/PathTaxonomyTermTest.php @@ -0,0 +1,79 @@ +<?php + +/** + * @file + * Definition of Drupal\path\Tests\PathTaxonomyTermTest. + */ + +namespace Drupal\path\Tests; + +/** + * Tests URL aliases for taxonomy terms. + */ +class PathTaxonomyTermTest extends PathTestBase { + public static function getInfo() { + return array( + 'name' => 'Taxonomy term URL aliases', + 'description' => 'Tests URL aliases for taxonomy terms.', + 'group' => 'Path', + ); + } + + function setUp() { + parent::setUp(array('taxonomy')); + + // Create a Tags vocabulary for the Article node type. + $vocabulary = entity_create('taxonomy_vocabulary', array( + 'name' => t('Tags'), + 'machine_name' => 'tags', + )); + $vocabulary->save(); + + // Create and login user. + $web_user = $this->drupalCreateUser(array('administer url aliases', 'administer taxonomy', 'access administration pages')); + $this->drupalLogin($web_user); + } + + /** + * Tests alias functionality through the admin interfaces. + */ + function testTermAlias() { + // Create a term in the default 'Tags' vocabulary with URL alias. + $vocabulary = taxonomy_vocabulary_load(1); + $description = $this->randomName();; + $edit = array(); + $edit['name'] = $this->randomName(); + $edit['description[value]'] = $description; + $edit['path[alias]'] = $this->randomName(); + $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save')); + + // Confirm that the alias works. + $this->drupalGet($edit['path[alias]']); + $this->assertText($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($description, 'Term can be accessed on changed URL alias.'); + + // Confirm that the old alias no longer works. + $this->drupalGet($edit['path[alias]']); + $this->assertNoText($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($description, 'Old URL alias has been removed after altering.'); + $this->assertResponse(404, 'Old URL alias returns 404.'); + } +} diff --git a/core/modules/path/lib/Drupal/path/Tests/PathTestBase.php b/core/modules/path/lib/Drupal/path/Tests/PathTestBase.php new file mode 100644 index 000000000000..120d5d2cf51b --- /dev/null +++ b/core/modules/path/lib/Drupal/path/Tests/PathTestBase.php @@ -0,0 +1,31 @@ +<?php + +/** + * @file + * Definition of Drupal\path\Tests\PathTestBase. + */ + +namespace Drupal\path\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Provides a base class for testing the Path module. + */ +class PathTestBase extends WebTestBase { + function setUp() { + $modules = func_get_args(); + if (isset($modules[0]) && is_array($modules[0])) { + $modules = $modules[0]; + } + $modules[] = 'node'; + $modules[] = 'path'; + parent::setUp($modules); + + // Create Basic page and Article node types. + if ($this->profile != 'standard') { + $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); + $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); + } + } +} diff --git a/core/modules/path/path.info b/core/modules/path/path.info index 55b6f9c0061a..7323d1bc1545 100644 --- a/core/modules/path/path.info +++ b/core/modules/path/path.info @@ -3,5 +3,4 @@ description = Allows users to rename URLs. package = Core version = VERSION core = 8.x -files[] = path.test configure = admin/config/search/path diff --git a/core/modules/path/path.test b/core/modules/path/path.test deleted file mode 100644 index c742ad5356f1..000000000000 --- a/core/modules/path/path.test +++ /dev/null @@ -1,519 +0,0 @@ -<?php - -/** - * @file - * Tests for the Path module. - */ - -use Drupal\simpletest\WebTestBase; - -/** - * Provides a base class for testing the Path module. - */ -class PathTestCase extends WebTestBase { - function setUp() { - $modules = func_get_args(); - if (isset($modules[0]) && is_array($modules[0])) { - $modules = $modules[0]; - } - $modules[] = 'node'; - $modules[] = 'path'; - parent::setUp($modules); - - // Create Basic page and Article node types. - if ($this->profile != 'standard') { - $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); - $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); - } - } -} - -/** - * Tests path alias functionality. - */ -class PathAliasTestCase extends PathTestCase { - 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); - } - - /** - * Tests 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('Save')); - - // Visit the system path for the node and confirm a cache entry is - // created. - cache('path')->flush(); - $this->drupalGet($edit['source']); - $this->assertTrue(cache('path')->get($edit['source']), t('Cache entry was created.')); - - // Visit the alias for the node and confirm a cache entry is created. - cache('path')->flush(); - $this->drupalGet($edit['alias']); - $this->assertTrue(cache('path')->get($edit['source']), t('Cache entry was created.')); - } - - /** - * Tests 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('Save')); - - // Confirm that the alias works. - $this->drupalGet($edit['alias']); - $this->assertText($node1->title, 'Alias works.'); - $this->assertResponse(200); - - // Change alias to one containing "exotic" characters. - $pid = $this->getPID($edit['alias']); - - $previous = $edit['alias']; - $edit['alias'] = "- ._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters. - "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string. - "éøïвβä¸åœ‹æ›¸Ûž"; // Characters from various non-ASCII alphabets. - $this->drupalPost('admin/config/search/path/edit/' . $pid, $edit, t('Save')); - - // Confirm that the alias works. - $this->drupalGet($edit['alias']); - $this->assertText($node1->title, '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('Save')); - - // 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/edit/' . $pid, array(), t('Delete')); - $this->drupalPost(NULL, 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); - - // Create a really long alias. - $edit = array(); - $edit['source'] = 'node/' . $node1->nid; - $alias = $this->randomName(128); - $edit['alias'] = $alias; - // The alias is shortened to 50 characters counting the elipsis. - $truncated_alias = substr($alias, 0, 47); - $this->drupalPost('admin/config/search/path/add', $edit, t('Save')); - $this->assertNoText($alias, 'The untruncated alias was not found.'); - // The 'truncated' alias will always be found. - $this->assertText($truncated_alias, 'The truncated alias was found.'); - } - - /** - * Tests 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, 'Alias works.'); - $this->assertResponse(200); - - // Change alias to one containing "exotic" characters. - $previous = $edit['path[alias]']; - $edit['path[alias]'] = "- ._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters. - "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string. - "éøïвβä¸åœ‹æ›¸Ûž"; // Characters from various non-ASCII alphabets. - $this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save')); - - // Confirm that the alias works. - $this->drupalGet($edit['path[alias]']); - $this->assertText($node1->title, 'Changed alias works.'); - $this->assertResponse(200); - - // Make sure 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. - // 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); - } - - /** - * Returns the path ID. - * - * @param $alias - * A string containing an aliased path. - * - * @return int - * Integer representing the path ID. - */ - function getPID($alias) { - return db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", array(':alias' => $alias))->fetchField(); - } - - /** - * Tests that duplicate aliases fail validation. - */ - function testDuplicateNodeAlias() { - // Create one node with a random alias. - $node_one = $this->drupalCreateNode(); - $edit = array(); - $edit['path[alias]'] = $this->randomName(); - $this->drupalPost('node/' . $node_one->nid . '/edit', $edit, t('Save')); - - // Now create another node and try to set the same alias. - $node_two = $this->drupalCreateNode(); - $this->drupalPost('node/' . $node_two->nid . '/edit', $edit, t('Save')); - $this->assertText(t('The alias is already in use.')); - $this->assertFieldByXPath("//input[@name='path[alias]' and contains(@class, 'error')]", $edit['path[alias]'], 'Textfield exists and has the error class.'); - } -} - -/** - * Tests URL aliases for taxonomy terms. - */ -class PathTaxonomyTermTestCase extends PathTestCase { - public static function getInfo() { - return array( - 'name' => 'Taxonomy term URL aliases', - 'description' => 'Tests URL aliases for taxonomy terms.', - 'group' => 'Path', - ); - } - - function setUp() { - parent::setUp(array('taxonomy')); - - // Create a Tags vocabulary for the Article node type. - $vocabulary = entity_create('taxonomy_vocabulary', array( - 'name' => t('Tags'), - 'machine_name' => 'tags', - )); - $vocabulary->save(); - - // Create and login user. - $web_user = $this->drupalCreateUser(array('administer url aliases', 'administer taxonomy', 'access administration pages')); - $this->drupalLogin($web_user); - } - - /** - * Tests alias functionality through the admin interfaces. - */ - function testTermAlias() { - // Create a term in the default 'Tags' vocabulary with URL alias. - $vocabulary = taxonomy_vocabulary_load(1); - $description = $this->randomName();; - $edit = array(); - $edit['name'] = $this->randomName(); - $edit['description[value]'] = $description; - $edit['path[alias]'] = $this->randomName(); - $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save')); - - // Confirm that the alias works. - $this->drupalGet($edit['path[alias]']); - $this->assertText($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($description, 'Term can be accessed on changed URL alias.'); - - // Confirm that the old alias no longer works. - $this->drupalGet($edit['path[alias]']); - $this->assertNoText($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($description, 'Old URL alias has been removed after altering.'); - $this->assertResponse(404, 'Old URL alias returns 404.'); - } -} - -/** - * Tests URL aliases for translated nodes. - */ -class PathLanguageTestCase extends PathTestCase { - 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. - $this->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($this->web_user); - - // Enable French language. - $edit = array(); - $edit['predefined_langcode'] = 'fr'; - - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Enable URL language detection and selection. - $edit = array('language_interface[enabled][language-url]' => 1); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - } - - /** - * Test alias functionality through the admin interfaces. - */ - function testAliasTranslation() { - // Set 'page' content type to enable translation. - variable_set('node_type_language_page', TRANSLATION_ENABLED); - - $english_node = $this->drupalCreateNode(array('type' => 'page')); - $english_alias = $this->randomName(); - - // Edit the node to set language and path. - $edit = array(); - $edit['langcode'] = 'en'; - $edit['path[alias]'] = $english_alias; - $this->drupalPost('node/' . $english_node->nid . '/edit', $edit, t('Save')); - - // Confirm that the alias works. - $this->drupalGet($english_alias); - $this->assertText($english_node->title, 'Alias works.'); - - // Translate the node into French. - $this->drupalGet('node/' . $english_node->nid . '/translate'); - $this->clickLink(t('add translation')); - $edit = array(); - $langcode = LANGUAGE_NOT_SPECIFIED; - $edit["title"] = $this->randomName(); - $edit["body[$langcode][0][value]"] = $this->randomName(); - $french_alias = $this->randomName(); - $edit['path[alias]'] = $french_alias; - $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"]); - $this->assertTrue(($french_node), 'Node found in database.'); - - // Confirm that the alias works. - $this->drupalGet('fr/' . $edit['path[alias]']); - $this->assertText($french_node->title, 'Alias for French translation works.'); - - // Confirm that the alias is returned by url(). Languages are cached on - // many levels, and we need to clear those caches. - drupal_static_reset('language_list'); - drupal_static_reset('language_url_outbound_alter'); - drupal_static_reset('language_url_rewrite_url'); - $languages = language_list(); - $url = url('node/' . $french_node->nid, array('language' => $languages[$french_node->langcode])); - $this->assertTrue(strpos($url, $edit['path[alias]']), t('URL contains the path alias.')); - - // Confirm that the alias works even when changing language negotiation - // options. Enable User language detection and selection over URL one. - $edit = array( - 'language_interface[enabled][language-user]' => 1, - 'language_interface[weight][language-user]' => -9, - 'language_interface[enabled][language-url]' => 1, - 'language_interface[weight][language-url]' => -8, - ); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Change user language preference. - $edit = array('preferred_langcode' => 'fr'); - $this->drupalPost("user/{$this->web_user->uid}/edit", $edit, t('Save')); - - // Check that the English alias works. In this situation French is the - // current UI and content language, while URL language is English (since we - // do not have a path prefix we fall back to the site's default language). - // We need to ensure that the user language preference is not taken into - // account while determining the path alias language, because if this - // happens we have no way to check that the path alias is valid: there is no - // path alias for French matching the english alias. So drupal_lookup_path() - // needs to use the URL language to check whether the alias is valid. - $this->drupalGet($english_alias); - $this->assertText($english_node->title, 'Alias for English translation works.'); - - // Check that the French alias works. - $this->drupalGet("fr/$french_alias"); - $this->assertText($french_node->title, 'Alias for French translation works.'); - - // Disable URL language negotiation. - $edit = array('language_interface[enabled][language-url]' => FALSE); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - - // Check that the English alias still works. - $this->drupalGet($english_alias); - $this->assertText($english_node->title, 'Alias for English translation works.'); - - // Check that the French alias is not available. We check the unprefixed - // alias because we disabled URL language negotiation above. In this - // situation only aliases in the default language and language neutral ones - // should keep working. - $this->drupalGet($french_alias); - $this->assertResponse(404, t('Alias for French translation is unavailable when URL language negotiation is disabled.')); - - // drupal_lookup_path() has an internal static cache. Check to see that - // it has the appropriate contents at this point. - drupal_lookup_path('wipe'); - $french_node_path = drupal_lookup_path('source', $french_alias, $french_node->langcode); - $this->assertEqual($french_node_path, 'node/' . $french_node->nid, t('Normal path works.')); - // Second call should return the same path. - $french_node_path = drupal_lookup_path('source', $french_alias, $french_node->langcode); - $this->assertEqual($french_node_path, 'node/' . $french_node->nid, t('Normal path is the same.')); - - // Confirm that the alias works. - $french_node_alias = drupal_lookup_path('alias', 'node/' . $french_node->nid, $french_node->langcode); - $this->assertEqual($french_node_alias, $french_alias, t('Alias works.')); - // Second call should return the same alias. - $french_node_alias = drupal_lookup_path('alias', 'node/' . $french_node->nid, $french_node->langcode); - $this->assertEqual($french_node_alias, $french_alias, t('Alias is the same.')); - } -} - -/** - * Tests the user interface for creating path aliases, with languages. - */ -class PathLanguageUITestCase extends PathTestCase { - public static function getInfo() { - return array( - 'name' => 'Path aliases with languages', - 'description' => 'Confirm that the Path module user interface works with languages.', - 'group' => 'Path', - ); - } - - function setUp() { - parent::setUp('path', 'locale'); - - // Create and login user. - $web_user = $this->drupalCreateUser(array('edit any page content', 'create page content', 'administer url aliases', 'create url aliases', 'administer languages', 'access administration pages')); - $this->drupalLogin($web_user); - - // Enable French language. - $edit = array(); - $edit['predefined_langcode'] = 'fr'; - - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Enable URL language detection and selection. - $edit = array('language_interface[enabled][language-url]' => 1); - $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); - } - - /** - * Tests that a language-neutral URL alias works. - */ - function testLanguageNeutralURLs() { - $name = $this->randomName(8); - $edit = array(); - $edit['source'] = 'admin/config/search/path'; - $edit['alias'] = $name; - $this->drupalPost('admin/config/search/path/add', $edit, t('Save')); - - $this->drupalGet($name); - $this->assertText(t('Filter aliases'), 'Language-neutral URL alias works'); - } - - /** - * Tests that a default language URL alias works. - */ - function testDefaultLanguageURLs() { - $name = $this->randomName(8); - $edit = array(); - $edit['source'] = 'admin/config/search/path'; - $edit['alias'] = $name; - $edit['langcode'] = 'en'; - $this->drupalPost('admin/config/search/path/add', $edit, t('Save')); - - $this->drupalGet($name); - $this->assertText(t('Filter aliases'), 'English URL alias works'); - } - - /** - * Tests that a non-default language URL alias works. - */ - function testNonDefaultURLs() { - $name = $this->randomName(8); - $edit = array(); - $edit['source'] = 'admin/config/search/path'; - $edit['alias'] = $name; - $edit['langcode'] = 'fr'; - $this->drupalPost('admin/config/search/path/add', $edit, t('Save')); - - $this->drupalGet('fr/' . $name); - $this->assertText(t('Filter aliases'), 'Foreign URL alias works'); - } - -} -- GitLab