From 8f098c1341b35f1d02ea205bbafd1913ba78d67c Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 10 Feb 2010 23:15:20 +0000 Subject: [PATCH] ENHANCEMENT MemberCsvBulkLoader for easy member import with group associations (merged from r94251) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@98714 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- security/MemberCsvBulkLoader.php | 79 +++++++++++++++++++ tests/security/MemberCsvBulkLoaderTest.csv | 3 + tests/security/MemberCsvBulkLoaderTest.php | 61 ++++++++++++++ tests/security/MemberCsvBulkLoaderTest.yml | 7 ++ .../MemberCsvBulkLoaderTest_withGroups.csv | 3 + 5 files changed, 153 insertions(+) create mode 100644 security/MemberCsvBulkLoader.php create mode 100644 tests/security/MemberCsvBulkLoaderTest.csv create mode 100644 tests/security/MemberCsvBulkLoaderTest.php create mode 100644 tests/security/MemberCsvBulkLoaderTest.yml create mode 100644 tests/security/MemberCsvBulkLoaderTest_withGroups.csv diff --git a/security/MemberCsvBulkLoader.php b/security/MemberCsvBulkLoader.php new file mode 100644 index 000000000..144e2f9d2 --- /dev/null +++ b/security/MemberCsvBulkLoader.php @@ -0,0 +1,79 @@ + 'Email', + ); + + function processRecord($record, $columnMap, &$results, $preview = false) { + $objID = parent::processRecord($record, $columnMap, $results, $preview); + + $_cache_groupByCode = array(); + + // Add to predefined groups + $member = DataObject::get_by_id($this->objectClass, $objID); + foreach($this->groups as $group) { + // TODO This isnt the most memory effective way to add members to a group + $member->Groups()->add($group); + } + + // Add to groups defined in CSV + if(isset($record['Groups']) && $record['Groups']) { + $groupCodes = explode(',', $record['Groups']); + foreach($groupCodes as $groupCode) { + if(!isset($_cache_groupByCode[$groupCode])) { + $group = DataObject::get_one( + 'Group', + sprintf('"Code" = \'%s\'', Convert::raw2sql($groupCode)) + ); + if(!$group) { + $group = new Group(); + $group->Code = $groupCode; + $group->Title = $groupCode; + $group->write(); + } + $member->Groups()->add($group); + } + $_cache_groupByCode[$groupCode] = $group; + } + } + + $member->destroy(); + unset($member); + + return $objID; + } + + /** + * @param Array $groups + */ + function setGroups($groups) { + $this->groups = $groups; + } + + /** + * @return Array + */ + function getGroups() { + return $this->groups; + } +} \ No newline at end of file diff --git a/tests/security/MemberCsvBulkLoaderTest.csv b/tests/security/MemberCsvBulkLoaderTest.csv new file mode 100644 index 000000000..91613047b --- /dev/null +++ b/tests/security/MemberCsvBulkLoaderTest.csv @@ -0,0 +1,3 @@ +FirstName,Surname,Email,Password,PasswordEncryption,Salt,PasswordExpiry +author1_first,author1_last,author1@test.com,21bb196c4d488023cb4e4b9df5f687c9a4d40172,sha1_v2.4,c23a94a878aa825cee69136bfd6185a874bc68801257978194, +author2_first,author2_last,author2@test.com,069b6b22dcaded75c045b7e5ceb5d1dd743f9cac,sha1_v2.4,7e8ea602668b5631bac4baaf7174f9966b6d34571257978210, \ No newline at end of file diff --git a/tests/security/MemberCsvBulkLoaderTest.php b/tests/security/MemberCsvBulkLoaderTest.php new file mode 100644 index 000000000..b6f556253 --- /dev/null +++ b/tests/security/MemberCsvBulkLoaderTest.php @@ -0,0 +1,61 @@ +load('sapphire/tests/security/MemberCsvBulkLoaderTest.csv'); + $created = $results->Created()->toArray(); + $this->assertEquals(count($created), 2); + $this->assertEquals($created[0]->Email, 'author1@test.com'); + $this->assertEquals($created[1]->Email, 'author2@test.com'); + } + + function testOverwriteExistingImport() { + $author1 = new Member(); + $author1->FirstName = 'author1_first_old'; + $author1->Email = 'author1@test.com'; + $author1->write(); + + $loader = new MemberCsvBulkLoader(); + $results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest.csv'); + $created = $results->Created()->toArray(); + $this->assertEquals(count($created), 1); + $updated = $results->Updated()->toArray(); + $this->assertEquals(count($updated), 1); + $this->assertEquals($created[0]->Email, 'author2@test.com'); + $this->assertEquals($updated[0]->Email, 'author1@test.com'); + $this->assertEquals($updated[0]->FirstName, 'author1_first'); + } + + function testAddToPredefinedGroups() { + $existinggroup = $this->objFromFixture('Group', 'existinggroup'); + + $loader = new MemberCsvBulkLoader(); + $loader->setGroups(array($existinggroup)); + + $results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest.csv'); + + $created = $results->Created()->toArray(); + $this->assertEquals($created[0]->Groups()->column('ID'), array($existinggroup->ID)); + $this->assertEquals($created[1]->Groups()->column('ID'), array($existinggroup->ID)); + } + + function testAddToCsvColumnGroupsByCode() { + $existinggroup = $this->objFromFixture('Group', 'existinggroup'); + + $loader = new MemberCsvBulkLoader(); + $results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest_withGroups.csv'); + + $newgroup = DataObject::get_one('Group', sprintf('"Code" = \'%s\'', 'newgroup')); + $this->assertEquals($newgroup->Title, 'newgroup'); + + $created = $results->Created()->toArray(); + $this->assertEquals($created[0]->Groups()->column('ID'), array($existinggroup->ID)); + $this->assertEquals($created[1]->Groups()->column('ID'), array($existinggroup->ID, $newgroup->ID)); + } +} \ No newline at end of file diff --git a/tests/security/MemberCsvBulkLoaderTest.yml b/tests/security/MemberCsvBulkLoaderTest.yml new file mode 100644 index 000000000..2028420d2 --- /dev/null +++ b/tests/security/MemberCsvBulkLoaderTest.yml @@ -0,0 +1,7 @@ +Group: + existinggroup: + Code: existinggroup +Member: + existingauthor: + Email: existingauthor@test.com + FirstName: Existing Author \ No newline at end of file diff --git a/tests/security/MemberCsvBulkLoaderTest_withGroups.csv b/tests/security/MemberCsvBulkLoaderTest_withGroups.csv new file mode 100644 index 000000000..09c19061f --- /dev/null +++ b/tests/security/MemberCsvBulkLoaderTest_withGroups.csv @@ -0,0 +1,3 @@ +FirstName,Surname,Email,Password,PasswordEncryption,Salt,PasswordExpiry,Groups +author1_first,author1_last,author1@test.com,21bb196c4d488023cb4e4b9df5f687c9a4d40172,sha1_v2.4,c23a94a878aa825cee69136bfd6185a874bc68801257978194,,existinggroup +author2_first,author2_last,author2@test.com,069b6b22dcaded75c045b7e5ceb5d1dd743f9cac,sha1_v2.4,7e8ea602668b5631bac4baaf7174f9966b6d34571257978210,,"existinggroup,newgroup" \ No newline at end of file