From b62f85ca970d118ec13cdb1e7ab4fc868bb17d59 Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Wed, 8 Feb 2012 20:03:32 +0900 Subject: [PATCH] Issue #998256 by justafish, Dave Reid: Please let modules know about the original URL alias in hook_path_update(). --- core/includes/path.inc | 30 +++++++----- core/modules/simpletest/tests/path.test | 46 +++++++++++++++++++ core/modules/simpletest/tests/path_test.info | 6 +++ .../modules/simpletest/tests/path_test.module | 22 +++++++++ 4 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 core/modules/simpletest/tests/path_test.info create mode 100644 core/modules/simpletest/tests/path_test.module diff --git a/core/includes/path.inc b/core/includes/path.inc index 44bf3fef8845..b49d42b27ac5 100644 --- a/core/includes/path.inc +++ b/core/includes/path.inc @@ -431,21 +431,27 @@ function path_load($conditions) { * - langcode: (optional) The language code of the alias. */ function path_save(&$path) { - $path += array('pid' => NULL, 'langcode' => LANGUAGE_NONE); + $path += array('langcode' => LANGUAGE_NONE); - // Insert or update the alias. - $status = drupal_write_record('url_alias', $path, (!empty($path['pid']) ? 'pid' : array())); + // Load the stored alias, if any. + if (!empty($path['pid']) && !isset($path['original'])) { + $path['original'] = path_load($path['pid']); + } - // Verify that a record was written. - if ($status) { - if ($status === SAVED_NEW) { - module_invoke_all('path_insert', $path); - } - else { - module_invoke_all('path_update', $path); - } - drupal_clear_path_cache($path['source']); + if (empty($path['pid'])) { + drupal_write_record('url_alias', $path); + module_invoke_all('path_insert', $path); + } + else { + drupal_write_record('url_alias', $path, array('pid')); + module_invoke_all('path_update', $path); } + + // Clear internal properties. + unset($path['original']); + + // Clear the static alias cache. + drupal_clear_path_cache($path['source']); } /** diff --git a/core/modules/simpletest/tests/path.test b/core/modules/simpletest/tests/path.test index 18dab6b8982d..308ef644a556 100644 --- a/core/modules/simpletest/tests/path.test +++ b/core/modules/simpletest/tests/path.test @@ -333,3 +333,49 @@ class PathLookupTest extends DrupalWebTestCase { $this->assertEqual(drupal_lookup_path('source', $path['alias']), $path['source'], t('Newer alias record is returned when comparing two LANGUAGE_NONE paths with the same alias.')); } } + +/** + * Tests the path_save() function. + */ +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() { + // Enable a helper module that implements hook_path_update(). + parent::setUp('path_test'); + path_test_reset(); + } + + /** + * Tests that path_save() makes the original path available to modules. + */ + function testDrupalSaveOriginalPath() { + $account = $this->drupalCreateUser(); + $uid = $account->uid; + $name = $account->name; + + // Create a language-neutral alias. + $path = array( + 'source' => "user/$uid", + 'alias' => 'foo', + ); + $path_original = $path; + path_save($path); + + // Alter the path. + $path['alias'] = 'bar'; + path_save($path); + + // Test to see if the original alias is available to modules during + // hook_path_update(). + $results = variable_get('path_test_results', array()); + $this->assertIdentical($results['hook_path_update']['original']['alias'], $path_original['alias'], t('Old path alias available to modules during hook_path_update.')); + $this->assertIdentical($results['hook_path_update']['original']['source'], $path_original['source'], t('Old path alias available to modules during hook_path_update.')); + } +} diff --git a/core/modules/simpletest/tests/path_test.info b/core/modules/simpletest/tests/path_test.info new file mode 100644 index 000000000000..d2573a466110 --- /dev/null +++ b/core/modules/simpletest/tests/path_test.info @@ -0,0 +1,6 @@ +name = "Hook path tests" +description = "Support module for path hook testing." +package = Testing +version = VERSION +core = 8.x +hidden = TRUE diff --git a/core/modules/simpletest/tests/path_test.module b/core/modules/simpletest/tests/path_test.module new file mode 100644 index 000000000000..01116753a2db --- /dev/null +++ b/core/modules/simpletest/tests/path_test.module @@ -0,0 +1,22 @@ +<?php + +/** + * @file + * Helper module for the path tests. + */ + +/** + * Resets the path test results. + */ +function path_test_reset() { + variable_set('path_test_results', array()); +} + +/** + * Implements hook_path_update(). + */ +function path_test_path_update($path) { + $results = variable_get('path_test_results', array()); + $results['hook_path_update'] = $path; + variable_set('path_test_results', $results); +} -- GitLab