Commit c67137ab authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #2987939 by Dave Reid, rbayliss: Fixed edge case scenario where...

Issue #2987939 by Dave Reid, rbayliss: Fixed edge case scenario where xmlsitemap_clear_directory() may accidentally delete the wrong directory on uninstall. This only happens if the xmlsitemap_path variable is different from the default value.
parent 208282aa
......@@ -368,6 +368,9 @@ function xmlsitemap_enable() {
* Implements hook_uninstall().
*/
function xmlsitemap_uninstall() {
// Remove the file cache directory.
xmlsitemap_clear_directory(NULL, TRUE);
// Remove variables.
drupal_load('module', 'xmlsitemap');
$variables = array_keys(xmlsitemap_variables());
......@@ -375,9 +378,6 @@ function xmlsitemap_uninstall() {
variable_del($variable);
}
// Remove the file cache directory.
xmlsitemap_clear_directory(NULL, TRUE);
// Remove the queue.
/** @var DrupalReliableQueueInterface $queue */
$queue = DrupalQueue::get('xmlsitemap_link_process', TRUE);
......
......@@ -861,7 +861,10 @@ function xmlsitemap_get_directory(stdClass $sitemap = NULL) {
$directory = variable_get('xmlsitemap_path', 'xmlsitemap');
}
if (!empty($sitemap->smid)) {
if (empty($directory)) {
return FALSE;
}
elseif (!empty($sitemap->smid)) {
return file_build_uri($directory . '/' . $sitemap->smid);
}
else {
......@@ -910,8 +913,12 @@ function xmlsitemap_check_all_directories() {
* Clear Directory.
*/
function xmlsitemap_clear_directory(stdClass $sitemap = NULL, $delete = FALSE) {
$directory = xmlsitemap_get_directory($sitemap);
return _xmlsitemap_delete_recursive($directory, $delete);
if ($directory = xmlsitemap_get_directory($sitemap)) {
return _xmlsitemap_delete_recursive($directory, $delete);
}
else {
return FALSE;
}
}
/**
......
......@@ -816,6 +816,58 @@ class XMLSitemapUnitTest extends XMLSitemapTestHelper {
$this->assertNoRaw('lifetime-test');
}
/**
* Test xmlsitemap_clear_directory().
*/
public function testClearDirectory() {
$directory = 'public://not-xmlsitemap';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$directory = 'public://xmlsitemap/test';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
file_unmanaged_save_data('File unrelated to XML sitemap', 'public://not-xmlsitemap/file.txt');
file_unmanaged_save_data('File unrelated to XML sitemap', 'public://file.txt');
file_unmanaged_save_data('Test contents', 'public://xmlsitemap/test/index.xml');
// Set the directory to an empty value.
variable_set('xmlsitemap_path', '');
drupal_static_reset('xmlsitemap_get_directory');
// Test that nothing was deleted.
$result = xmlsitemap_clear_directory(NULL, TRUE);
$this->assertTrue(is_file('public://xmlsitemap/test/index.xml'));
$this->assertTrue(is_dir('public://not-xmlsitemap'));
$this->assertTrue(is_file('public://file.txt'));
$this->assertFalse($result);
// Reset the value back to the default.
variable_set('xmlsitemap_path', 'xmlsitemap');
drupal_static_reset('xmlsitemap_get_directory');
// Test that only the xmlsitemap directory was deleted.
$result = xmlsitemap_clear_directory(NULL, TRUE);
$this->assertFalse(is_dir('public://xmlsitemap'));
$this->assertTrue(is_dir('public://not-xmlsitemap'));
$this->assertTrue(is_file('public://file.txt'));
$this->assertTrue($result);
}
/**
* Test that uninstalling the module correctly deletes the directory.
*/
public function testUninstallDeleteDirectory() {
$directory = 'public://test';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
// Update the directory path to a different value.
variable_set('xmlsitemap_path', 'test');
drupal_static_reset('xmlsitemap_get_directory');
// Test that running uninstall removes the correct directory (test) and not the default (xmlsitemap)
drupal_uninstall_modules(array('xmlsitemap'));
$this->assertFalse(is_dir('public://test'));
$this->assertTrue(is_dir('public://xmlsitemap'));
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment