Skip to content
Snippets Groups Projects
Commit e280ce25 authored by megachriz's avatar megachriz Committed by MegaChriz
Browse files

Issue #2485059 by MegaChriz: Added delete protection for user id 1.

parent ea2c278f
No related branches found
No related tags found
No related merge requests found
...@@ -179,6 +179,16 @@ class FeedsUserProcessor extends FeedsProcessor { ...@@ -179,6 +179,16 @@ class FeedsUserProcessor extends FeedsProcessor {
* Delete multiple user accounts. * Delete multiple user accounts.
*/ */
protected function entityDeleteMultiple($uids) { protected function entityDeleteMultiple($uids) {
// Prevent user 1 from being deleted.
if (in_array(1, $uids)) {
$uids = array_diff($uids, array(1));
// But do delete the associated feeds item.
db_delete('feeds_item')
->condition('entity_type', $this->entityType())
->condition('entity_id', 1)
->execute();
}
user_delete_multiple($uids); user_delete_multiple($uids);
} }
...@@ -378,6 +388,18 @@ class FeedsUserProcessor extends FeedsProcessor { ...@@ -378,6 +388,18 @@ class FeedsUserProcessor extends FeedsProcessor {
return 0; return 0;
} }
/**
* Overrides FeedsProcessor::initEntitiesToBeRemoved().
*
* Removes user 1 from the list of entities to be removed.
*/
protected function initEntitiesToBeRemoved(FeedsSource $source, FeedsState $state) {
parent::initEntitiesToBeRemoved($source, $state);
// Prevent user 1 from being deleted.
unset($state->removeList[1]);
}
/** /**
* Overrides FeedsProcessor::clean(). * Overrides FeedsProcessor::clean().
* *
......
...@@ -916,6 +916,87 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase { ...@@ -916,6 +916,87 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase {
$this->assertText("Failed importing 'Gomez'. User's timezone is not valid."); $this->assertText("Failed importing 'Gomez'. User's timezone is not valid.");
} }
/**
* Tests if user 1 cannot be deleted using the delete non-existing feature.
*/
public function testUser1ProtectionWhenDeletingNonExistent() {
// Set to delete non-existing users.
$this->setSettings('user_import', 'FeedsFileFetcher', array());
$this->setSettings('user_import', 'FeedsUserProcessor', array(
'update_existing' => FEEDS_UPDATE_EXISTING,
'update_non_existent' => 'delete',
));
// Set mail address of user 1 to "fester@example.com". An user with this
// mail address is missing in the feed later.
$account = user_load(1);
$edit['mail'] = 'fester@example.com';
user_save($account, $edit);
// Import the first file, which contains the mail address of user 1.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Updated 1 user');
// Ensure the username of user 1 was updated.
$account = user_load(1, TRUE);
$this->assertEqual('Fester', $account->name);
// Now import the second file, where the mail address of user 1 is missing.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users2.csv');
$this->assertNoText('Removed 1 user');
// Ensure that user 1 still exists.
$account = db_select('users')
->fields('users')
->condition('uid', 1)
->execute()
->fetch();
$this->assertTrue(is_object($account), 'User 1 still exists.');
}
/**
* Tests if user 1 cannot be deleted using the delete form.
*/
public function testUser1ProtectionWhenDeletingAll() {
// Set to update existing users.
$this->setSettings('user_import', 'FeedsFileFetcher', array());
$this->setSettings('user_import', 'FeedsUserProcessor', array(
'update_existing' => FEEDS_UPDATE_EXISTING,
));
// Set mail address of user 1 to "fester@example.com".
$account = user_load(1);
$edit['mail'] = 'fester@example.com';
user_save($account, $edit);
// Import a file that contains the mail address of user 1.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Updated 1 user');
// Ensure the username of user 1 was updated.
$account = user_load(1, TRUE);
$this->assertEqual('Fester', $account->name);
// Now delete all items. User 1 should not be deleted.
$this->drupalPost('import/user_import/delete-items', array(), 'Delete');
// Ensure that user 1 still exists.
$account = db_select('users')
->fields('users')
->condition('uid', 1)
->execute()
->fetch();
$this->assertTrue(is_object($account), 'User 1 still exists.');
// But ensure that the associated feeds item did got deleted.
$count = db_select('feeds_item')
->fields('feeds_item')
->condition('entity_type', 'user')
->condition('entity_id', 1)
->countQuery()
->execute()
->fetchField();
$this->assertEqual(0, $count, 'The feeds item for user 1 was deleted.');
}
/** /**
* Log in an imported user. * Log in an imported user.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment