123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- <?php
- class DrupalMatchPathTestCase extends DrupalWebTestCase {
- protected $front;
- public static function getInfo() {
- return array(
- 'name' => 'Drupal match path',
- 'description' => 'Tests the drupal_match_path() function to make sure it works properly.',
- 'group' => 'Path API',
- );
- }
- function setUp() {
-
- parent::setUp();
-
- $this->front = $this->randomName();
- variable_set('site_frontpage', $this->front);
-
- $this->refreshVariables();
- }
-
- function testDrupalMatchPath() {
-
- $tests = $this->drupalMatchPathTests();
- foreach ($tests as $patterns => $cases) {
- foreach ($cases as $path => $expected_result) {
- $actual_result = drupal_match_path($path, $patterns);
- $this->assertIdentical($actual_result, $expected_result, format_string('Tried matching the path <code>@path</code> to the pattern <pre>@patterns</pre> - expected @expected, got @actual.', array('@path' => $path, '@patterns' => $patterns, '@expected' => var_export($expected_result, TRUE), '@actual' => var_export($actual_result, TRUE))));
- }
- }
- }
-
- private function drupalMatchPathTests() {
- return array(
-
- 'blog/1' => array(
- 'blog/1' => TRUE,
- 'blog/2' => FALSE,
- 'test' => FALSE,
- ),
-
- 'blog/*' => array(
- 'blog/1' => TRUE,
- 'blog/2' => TRUE,
- 'blog/3/edit' => TRUE,
- 'blog/' => TRUE,
- 'blog' => FALSE,
- 'test' => FALSE,
- ),
-
- 'node/*/revisions/*' => array(
- 'node/1/revisions/3' => TRUE,
- 'node/345/revisions/test' => TRUE,
- 'node/23/edit' => FALSE,
- 'test' => FALSE,
- ),
-
- '<front>' => array(
- $this->front => TRUE,
- "$this->front/" => FALSE,
- "$this->front/edit" => FALSE,
- 'node' => FALSE,
- '' => FALSE,
- ),
-
- '<front>/*' => array(
- $this->front => FALSE,
- "$this->front/" => FALSE,
- "$this->front/edit" => FALSE,
- 'node/12' => FALSE,
- '' => FALSE,
- ),
-
- "node/*\nnode/*/edit" => array(
- 'node/1' => TRUE,
- 'node/view' => TRUE,
- 'node/32/edit' => TRUE,
- 'node/delete/edit' => TRUE,
- 'node/50/delete' => TRUE,
- 'test/example' => FALSE,
- ),
-
- "user/*\rblog/*" => array(
- 'user/1' => TRUE,
- 'blog/1' => TRUE,
- 'user/1/blog/1' => TRUE,
- 'user/blog' => TRUE,
- 'test/example' => FALSE,
- 'user' => FALSE,
- 'blog' => FALSE,
- ),
-
- "test\r\n<front>" => array(
- 'test' => TRUE,
- $this->front => TRUE,
- 'example' => FALSE,
- ),
-
- '[^/]+?/[0-9]' => array(
- 'test/1' => FALSE,
- '[^/]+?/[0-9]' => TRUE,
- ),
- );
- }
- }
- class UrlAlterFunctionalTest extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => t('URL altering'),
- 'description' => t('Tests hook_url_inbound_alter() and hook_url_outbound_alter().'),
- 'group' => t('Path API'),
- );
- }
- function setUp() {
- parent::setUp('path', 'forum', 'url_alter_test');
- }
-
- function testUrlAlter() {
- $account = $this->drupalCreateUser(array('administer url aliases'));
- $this->drupalLogin($account);
- $uid = $account->uid;
- $name = $account->name;
-
- $this->assertUrlInboundAlter("user/$name", "user/$uid");
- $this->assertUrlOutboundAlter("user/$uid", "user/$name");
-
- $path = array('source' => "user/$uid/test1", 'alias' => 'alias/test1');
- path_save($path);
- $this->assertUrlInboundAlter('alias/test1', "user/$uid/test1");
- $this->assertUrlOutboundAlter("user/$uid/test1", 'alias/test1');
-
- $edit = array('source' => "user/$name/edit", 'alias' => 'alias/test2');
- $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
- $this->assertText(t('The alias has been saved.'));
-
- $this->assertUrlInboundAlter('alias/test2', "user/$uid/edit");
- $this->assertUrlOutboundAlter("user/$uid/edit", 'alias/test2');
-
- $uid++;
- $this->assertUrlInboundAlter("user/$uid", "user/$uid");
- $this->assertUrlOutboundAlter("user/$uid", "user/$uid");
-
-
- $this->assertUrlInboundAlter('community', 'forum');
- $this->assertUrlOutboundAlter('forum', 'community');
- $forum_vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE module = 'forum'")->fetchField();
- $tid = db_insert('taxonomy_term_data')
- ->fields(array(
- 'name' => $this->randomName(),
- 'vid' => $forum_vid,
- ))
- ->execute();
- $this->assertUrlInboundAlter("community/$tid", "forum/$tid");
- $this->assertUrlOutboundAlter("forum/$tid", "community/$tid");
- }
-
- function testCurrentUrlRequestedPath() {
- $this->drupalGet('url-alter-test/bar');
- $this->assertRaw('request_path=url-alter-test/bar', 'request_path() returns the requested path.');
- $this->assertRaw('current_path=url-alter-test/foo', 'current_path() returns the internal path.');
- }
-
- function testGetQInitialized() {
- $this->drupalGet('');
- $this->assertText("\$_GET['q'] is non-empty with an empty request path.", "\$_GET['q'] is initialized with an empty request path.");
- }
-
- protected function assertUrlOutboundAlter($original, $final) {
-
- $result = url($original);
- $base_path = base_path() . (variable_get('clean_url', '0') ? '' : '?q=');
- $result = substr($result, strlen($base_path));
- $this->assertIdentical($result, $final, format_string('Altered outbound URL %original, expected %final, and got %result.', array('%original' => $original, '%final' => $final, '%result' => $result)));
- }
-
- protected function assertUrlInboundAlter($original, $final) {
-
- $result = drupal_get_normal_path($original);
- $this->assertIdentical($result, $final, format_string('Altered inbound URL %original, expected %final, and got %result.', array('%original' => $original, '%final' => $final, '%result' => $result)));
- }
- }
- class PathLookupTest extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => t('Path lookup'),
- 'description' => t('Tests that drupal_lookup_path() returns correct paths.'),
- 'group' => t('Path API'),
- );
- }
-
- function testDrupalLookupPath() {
- $account = $this->drupalCreateUser();
- $uid = $account->uid;
- $name = $account->name;
-
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'foo',
- );
- path_save($path);
- $this->assertEqual(drupal_lookup_path('alias', $path['source']), $path['alias'], 'Basic alias lookup works.');
- $this->assertEqual(drupal_lookup_path('source', $path['alias']), $path['source'], 'Basic source lookup works.');
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => "users/$name",
- 'language' => 'en',
- );
- path_save($path);
- $this->assertEqual(drupal_lookup_path('alias', $path['source']), $path['alias'], 'English alias overrides language-neutral alias.');
- $this->assertEqual(drupal_lookup_path('source', $path['alias']), $path['source'], 'English source overrides language-neutral source.');
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'bar',
- );
- path_save($path);
- $this->assertEqual(drupal_lookup_path('alias', $path['source']), "users/$name", 'English alias still returned after entering a language-neutral alias.');
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'LOL',
- 'language' => 'xx-lolspeak',
- );
- path_save($path);
- $this->assertEqual(drupal_lookup_path('alias', $path['source']), "users/$name", 'English alias still returned after entering a LOLspeak alias.');
-
- $this->assertEqual(drupal_lookup_path('alias', $path['source'], 'xx-lolspeak'), 'LOL', 'LOLspeak alias returned if we specify xx-lolspeak to drupal_lookup_path().');
-
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'users/my-new-path',
- 'language' => 'en',
- );
- path_save($path);
- $this->assertEqual(drupal_lookup_path('alias', $path['source']), $path['alias'], 'Recently created English alias returned.');
- $this->assertEqual(drupal_lookup_path('source', $path['alias']), $path['source'], 'Recently created English source returned.');
-
-
- db_delete('url_alias')
- ->condition('language', 'en')
- ->execute();
- drupal_clear_path_cache();
- $this->assertEqual(drupal_lookup_path('alias', $path['source']), 'bar', 'Path lookup falls back to recently created language-neutral alias.');
-
-
- $account2 = $this->drupalCreateUser();
- $path = array(
- 'source' => 'user/' . $account2->uid,
- 'alias' => 'bar',
- );
- path_save($path);
- $this->assertEqual(drupal_lookup_path('source', $path['alias']), $path['source'], 'Newer alias record is returned when comparing two LANGUAGE_NONE paths with the same alias.');
- }
- }
- class PathSaveTest extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => t('Path save'),
- 'description' => t('Tests that path_save() exposes the previous alias value.'),
- 'group' => t('Path API'),
- );
- }
- function setUp() {
-
- parent::setUp('path_test');
- path_test_reset();
- }
-
- function testDrupalSaveOriginalPath() {
- $account = $this->drupalCreateUser();
- $uid = $account->uid;
- $name = $account->name;
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'foo',
- );
- $path_original = $path;
- path_save($path);
-
- $path['alias'] = 'bar';
- path_save($path);
-
-
- $results = variable_get('path_test_results', array());
- $this->assertIdentical($results['hook_path_update']['original']['alias'], $path_original['alias'], 'Old path alias available to modules during hook_path_update.');
- $this->assertIdentical($results['hook_path_update']['original']['source'], $path_original['source'], 'Old path alias available to modules during hook_path_update.');
- }
- }
|