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()
|
|
|
|
{
|
|
|
|
$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-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(count($created ?? []), 1);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($created[0]->Code, 'newchildgroup1');
|
|
|
|
|
|
|
|
$updated = $results->Updated()->toArray();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(count($updated ?? []), 1);
|
2021-11-03 02:26:16 +01:00
|
|
|
$this->assertEquals($updated[0]->Code, 'newgroup1-2');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($updated[0]->Title, 'New Group 1');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImportPermissions()
|
|
|
|
{
|
|
|
|
$loader = new GroupCsvBulkLoader();
|
|
|
|
$results = $loader->load(__DIR__ . '/GroupCsvBulkLoaderTest/GroupCsvBulkLoaderTest_withExisting.csv');
|
|
|
|
|
|
|
|
$created = $results->Created()->toArray();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(count($created ?? []), 1);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($created[0]->Code, 'newgroup1');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals($created[0]->Permissions()->column('Code'), ['CODE1']);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$updated = $results->Updated()->toArray();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(count($updated ?? []), 1);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($updated[0]->Code, 'existinggroup');
|
|
|
|
$array1=$updated[0]->Permissions()->column('Code');
|
2020-04-20 19:58:09 +02:00
|
|
|
$array2=['CODE1', 'CODE2'];
|
2016-12-16 05:34:21 +01:00
|
|
|
sort($array1);
|
|
|
|
sort($array2);
|
|
|
|
$this->assertEquals($array1, $array2);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|