diff --git a/src/Dev/CsvBulkLoader.php b/src/Dev/CsvBulkLoader.php index be6ea4c9c..3b95fa2fd 100644 --- a/src/Dev/CsvBulkLoader.php +++ b/src/Dev/CsvBulkLoader.php @@ -76,6 +76,7 @@ class CsvBulkLoader extends BulkLoader $filepath = Director::getAbsFile($filepath); $csvReader = Reader::createFromPath($filepath, 'r'); $csvReader->setDelimiter($this->delimiter); + $csvReader->stripBom(true); $tabExtractor = function ($row, $rowOffset, $iterator) { foreach ($row as &$item) { diff --git a/src/Security/Group.php b/src/Security/Group.php index 403f92819..eb72fbfe3 100755 --- a/src/Security/Group.php +++ b/src/Security/Group.php @@ -83,15 +83,6 @@ class Group extends DataObject private static $table_name = "Group"; - public function populateDefaults() - { - parent::populateDefaults(); - - if (!$this->Title) { - $this->Title = _t(__CLASS__ . '.NEWGROUP', "New Group"); - } - } - public function getAllChildren() { $doSet = new ArrayList(); @@ -105,6 +96,16 @@ class Group extends DataObject return $doSet; } + + private function getDecodedBreadcrumbs() + { + $list = Group::get()->exclude('ID', $this->ID); + $groups = ArrayList::create(); + foreach ($list as $group) { + $groups->push(['ID' => $group->ID, 'Title' => $group->getBreadcrumbs(' » ')]); + } + return $groups; + } /** * Caution: Only call on instances, not through a singleton. @@ -125,7 +126,7 @@ class Group extends DataObject $parentidfield = DropdownField::create( 'ParentID', $this->fieldLabel('Parent'), - Group::get()->exclude('ID', $this->ID)->map('ID', 'Breadcrumbs') + $this->getDecodedBreadcrumbs() )->setEmptyString(' '), new TextareaField('Description', $this->fieldLabel('Description')) ), diff --git a/tests/php/Dev/CsvBulkLoaderTest.php b/tests/php/Dev/CsvBulkLoaderTest.php index 8885e58a9..f00f15f32 100644 --- a/tests/php/Dev/CsvBulkLoaderTest.php +++ b/tests/php/Dev/CsvBulkLoaderTest.php @@ -2,6 +2,7 @@ namespace SilverStripe\Dev\Tests; +use League\Csv\Writer; use SilverStripe\Dev\Tests\CsvBulkLoaderTest\CustomLoader; use SilverStripe\Dev\Tests\CsvBulkLoaderTest\Player; use SilverStripe\Dev\Tests\CsvBulkLoaderTest\PlayerContract; @@ -300,6 +301,20 @@ class CsvBulkLoaderTest extends SapphireTest $this->assertEquals($player->FirstName, "John. He's a good guy. "); } + public function testLoadWithByteOrderMark() + { + $loader = new CsvBulkLoader(Player::class); + $loader->load($this->csvPath . 'PlayersWithHeaderAndBOM.csv'); + + $players = Player::get(); + + $this->assertCount(3, $players); + $this->assertListContains([ + ['FirstName' => 'Jamie', 'Birthday' => '1882-01-31'], + ['FirstName' => 'Järg', 'Birthday' => '1982-06-30'], + ['FirstName' => 'Jacob', 'Birthday' => '2000-04-30'], + ], $players); + } protected function getLineCount(&$file) { diff --git a/tests/php/Dev/CsvBulkLoaderTest/csv/PlayersWithHeaderAndBOM.csv b/tests/php/Dev/CsvBulkLoaderTest/csv/PlayersWithHeaderAndBOM.csv new file mode 100644 index 000000000..93a871931 --- /dev/null +++ b/tests/php/Dev/CsvBulkLoaderTest/csv/PlayersWithHeaderAndBOM.csv @@ -0,0 +1,4 @@ +FirstName,Biography,Birthday,IsRegistered +Jamie,"Pretty old\, with an escaped comma",1882-01-31,1 +Järg,"Unicode FTW",1982-06-30,1 +Jacob," Likes leading tabs in his biography",2000-04-30,0