From 79b791f2e0e87637265f638fbfe55c7ad678f8be Mon Sep 17 00:00:00 2001 From: megachriz <megachriz@654114.no-reply.drupal.org> Date: Thu, 26 May 2016 11:35:35 +0200 Subject: [PATCH] Issue #1894542 by mikran, MegaChriz: Fixed replace roles with "Additional roles" when replacing existing users. --- plugins/FeedsUserProcessor.inc | 15 ++++++++ tests/feeds_processor_user.test | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/plugins/FeedsUserProcessor.inc b/plugins/FeedsUserProcessor.inc index 3eabdfb7..9001b9e8 100644 --- a/plugins/FeedsUserProcessor.inc +++ b/plugins/FeedsUserProcessor.inc @@ -62,6 +62,21 @@ class FeedsUserProcessor extends FeedsProcessor { // Copy the password so that we can compare it again at save. $user->feeds_original_pass = $user->pass; + + // Reset roles and status when an user is replaced. + if ($this->config['update_existing'] == FEEDS_REPLACE_EXISTING) { + $user->roles = array_filter($this->config['roles']); + $user->status = $this->config['status']; + + // Unserialize user data if it is still serialized. + if (!empty($user->data) && @unserialize($user->data)) { + $user->data = unserialize($user->data); + } + else { + $user->data = array(); + } + } + return $user; } diff --git a/tests/feeds_processor_user.test b/tests/feeds_processor_user.test index c51898bd..23843035 100644 --- a/tests/feeds_processor_user.test +++ b/tests/feeds_processor_user.test @@ -600,4 +600,72 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase { $this->assertTrue(isset($account->roles[$manager_rid]), format_string('@user has the manager role.', $vars)); } } + + /** + * Tests if roles are replaced when replacing users. + */ + public function testAdditionalRolesSettingWhenReplacingUsers() { + // Create manager role. + $manager_rid = $this->drupalCreateRole(array('access content'), 'manager'); + // Create editor role. + $editor_rid = $this->drupalCreateRole(array('access content'), 'editor'); + + // Set that the "manager" role should be assigned to every user that is + // imported. Other roles should be revoked. + $this->setSettings('user_import', 'FeedsUserProcessor', array( + "roles[$manager_rid]" => TRUE, + 'update_existing' => FEEDS_REPLACE_EXISTING, + )); + + // Create account for Morticia with no roles. Morticia should gain the + // "manager" role. + user_save(drupal_anonymous_user(), array( + 'name' => 'Morticia', + 'mail' => 'morticia@example.com', + 'pass' => 'mort', + 'status' => 1, + )); + + // Create account for Gomez and give it the "editor" role. After import + // Gomez should have lost the role "editor" and gained the role "manager". + user_save(drupal_anonymous_user(), array( + 'name' => 'Gomez', + 'mail' => 'gomez@example.com', + 'pass' => 'gome', + 'status' => 1, + 'roles' => array( + $editor_rid => $editor_rid, + ), + )); + + // Import CSV file. + $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv'); + + // Assert that Morticia has gained the role "manager". + $account = user_load_by_name('Morticia'); + $this->assertTrue(isset($account->roles[$manager_rid]), 'Morticia has the manager role.'); + $this->assertEqual(2, count($account->roles), 'Morticia has two roles.'); + + // Assert that Gomez has gained the role "manager" and but no longer has the + // "editor" role. + $account = user_load_by_name('Gomez'); + $this->assertFalse(isset($account->roles[$editor_rid]), 'Gomez no longer has the editor role.'); + $this->assertTrue(isset($account->roles[$manager_rid]), 'Gomez has the manager role.'); + $this->assertEqual(2, count($account->roles), 'Gomez has two roles.'); + + // Now remove all default roles and import again. + $this->setSettings('user_import', 'FeedsUserProcessor', array( + "roles[$manager_rid]" => FALSE, + 'skip_hash_check' => TRUE, + )); + $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv'); + + // Reset loaded users cache. + entity_get_controller('user')->resetCache(); + + // Assert that Morticia no longer has the role "manager". + $account = user_load_by_name('Morticia'); + $this->assertFalse(isset($account->roles[$manager_rid]), 'Morticia no longer has the manager role.'); + $this->assertEqual(1, count($account->roles), 'Morticia has one role.'); + } } -- GitLab