diff --git a/plugins/FeedsUserProcessor.inc b/plugins/FeedsUserProcessor.inc index 7117e57..5c65bf1 100644 --- a/plugins/FeedsUserProcessor.inc +++ b/plugins/FeedsUserProcessor.inc @@ -438,7 +438,7 @@ class FeedsUserProcessor extends FeedsProcessor { if (!$role && !empty($mapping['autocreate'])) { // Create new role if role doesn't exist. $role = new stdClass(); - $role->name = $role_name; + $role->name = $value; user_role_save($role); $role = user_role_load_by_name($role->name); } diff --git a/tests/feeds_processor_user.test b/tests/feeds_processor_user.test index 9b7398a..81372a7 100644 --- a/tests/feeds_processor_user.test +++ b/tests/feeds_processor_user.test @@ -339,7 +339,7 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase { /** * Tests that roles can be revoked and that only allowed roles are revoked. */ - public function testRevokeRoles() { + public function testRoleTargetRevokeRoles() { // Create manager role. $manager_rid = $this->drupalCreateRole(array('access content'), 'manager'); // Create editor role. @@ -361,14 +361,13 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase { $editor_rid => $editor_rid, $tester_rid => $tester_rid, ), - 'autocreate' => TRUE, ), )); - // Create account for Mortica with roles "manager" and "editor". In the - // source only "editor" is specified. Mortica should keep both roles. + // Create account for Morticia with roles "manager" and "editor". In the + // source only "editor" is specified. Morticia should keep both roles. user_save(drupal_anonymous_user(), array( - 'name' => 'Mortica', + 'name' => 'Morticia', 'mail' => 'morticia@example.com', 'pass' => 'mort', 'status' => 1, @@ -426,4 +425,53 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase { $this->assertTrue(isset($account->roles[$tester_rid]), 'Gomez has the tester role.'); $this->assertEqual(2, count($account->roles), 'Gomez has two roles.'); } + + /** + * Tests if no roles are revoked if the option "Revoke roles" is disabled. + */ + public function testRoleTargetNoRevokeRoles() { + // Create manager role. + $manager_rid = $this->drupalCreateRole(array('access content'), 'manager'); + // Create editor role. + $editor_rid = $this->drupalCreateRole(array('access content'), 'editor'); + + // Set to update existing users. + $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2)); + + // Add mapping to role. Set option to not revoke roles. + $this->addMappings('user_import', array( + 4 => array( + 'source' => 'roles', + 'target' => 'roles_list', + 'allowed_roles' => array( + $manager_rid => FALSE, + $editor_rid => $editor_rid, + ), + 'revoke_roles' => FALSE, + ), + )); + + // Create account for Pugsley with roles "manager" and "editor". Pugsley has + // no roles, but roles should not be revoked, so Pugsley should keep all + // roles. + user_save(drupal_anonymous_user(), array( + 'name' => 'Pugsley', + 'mail' => 'pugsley@example.com', + 'pass' => 'pugs', + 'status' => 1, + 'roles' => array( + $manager_rid => $manager_rid, + $editor_rid => $editor_rid, + ), + )); + + // Import CSV file. + $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users_roles.csv'); + + // Assert that Pugsley kept all roles. + $account = user_load_by_name('Pugsley'); + $this->assertTrue(isset($account->roles[$manager_rid]), 'Pugsley still has the manager role.'); + $this->assertTrue(isset($account->roles[$editor_rid]), 'Pugsley still has the editor role.'); + $this->assertEqual(3, count($account->roles), 'Pugsley has three roles.'); + } }