2009-12-02 10:40:38 +01:00
|
|
|
<?php
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Security\Tests;
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\GroupCsvBulkLoader;
|
|
|
|
use SilverStripe\Security\Group;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class GroupCsvBulkLoaderTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'GroupCsvBulkLoaderTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testNewImport()
|
|
|
|
{
|
|
|
|
$loader = new GroupCsvBulkLoader();
|
|
|
|
$results = $loader->load(__DIR__ . '/GroupCsvBulkLoaderTest/GroupCsvBulkLoaderTest.csv');
|
|
|
|
$created = $results->Created()->toArray();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(count($created ?? []), 2);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($created[0]->Code, 'newgroup1');
|
|
|
|
$this->assertEquals($created[0]->ParentID, 0);
|
|
|
|
$this->assertEquals($created[1]->Code, 'newchildgroup1');
|
|
|
|
$this->assertEquals($created[1]->ParentID, $created[0]->ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOverwriteExistingImport()
|
|
|
|
{
|
2022-05-25 01:42:12 +02:00
|
|
|
// This group will be overwritten.
|
2016-12-16 05:34:21 +01:00
|
|
|
$existinggroup = new Group();
|
|
|
|
$existinggroup->Title = 'Old Group Title';
|
|
|
|
$existinggroup->Code = 'newgroup1';
|
|
|
|
$existinggroup->write();
|
|
|
|
|
|
|
|
$loader = new GroupCsvBulkLoader();
|
|
|
|
$results = $loader->load(__DIR__ . '/GroupCsvBulkLoaderTest/GroupCsvBulkLoaderTest.csv');
|
|
|
|
|
|
|
|
$created = $results->Created()->toArray();
|
2022-05-25 01:42:12 +02:00
|
|
|
$this->assertEquals(1, count($created));
|
|
|
|
$this->assertEquals('newchildgroup1', $created[0]->Code);
|
|
|
|
$this->assertEquals('New Child Group 1', $created[0]->Title);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-05-25 01:42:12 +02:00
|
|
|
// This overrides the group because the code matches, which takes precedence over the ID.
|
2016-12-16 05:34:21 +01:00
|
|
|
$updated = $results->Updated()->toArray();
|
2022-05-25 01:42:12 +02:00
|
|
|
$this->assertEquals(1, count($updated));
|
|
|
|
$this->assertEquals('newgroup1', $updated[0]->Code);
|
|
|
|
$this->assertEquals('New Group 1', $updated[0]->Title);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testImportPermissions()
|
|
|
|
{
|
|
|
|
$loader = new GroupCsvBulkLoader();
|
|
|
|
$results = $loader->load(__DIR__ . '/GroupCsvBulkLoaderTest/GroupCsvBulkLoaderTest_withExisting.csv');
|
|
|
|
|
|
|
|
$created = $results->Created()->toArray();
|
2022-05-25 01:42:12 +02:00
|
|
|
$this->assertEquals(1, count($created));
|
|
|
|
$this->assertEquals('newgroup1', $created[0]->Code);
|
|
|
|
$this->assertEquals(['CODE1'], $created[0]->Permissions()->column('Code'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$updated = $results->Updated()->toArray();
|
2022-05-25 01:42:12 +02:00
|
|
|
$this->assertEquals(1, count($updated));
|
|
|
|
$this->assertEquals('existinggroup', $updated[0]->Code);
|
|
|
|
$actual = $updated[0]->Permissions()->column('Code');
|
|
|
|
$expected = ['CODE1', 'CODE2'];
|
|
|
|
sort($actual);
|
|
|
|
sort($expected);
|
|
|
|
$this->assertEquals($expected, $actual);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|