mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX Ensure string "0" is not considered a NULL value when CsvBulkLoader imports values from a CSV file
MINOR Updated tests to check boolean values in CsvBulkLoaderTest git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@70895 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
bdd56a11d0
commit
30bedae1a8
@ -229,7 +229,7 @@ abstract class BulkLoader extends ViewableData {
|
||||
* @return boolean
|
||||
*/
|
||||
protected function isNullValue($val, $fieldName = null) {
|
||||
return (empty($val));
|
||||
return (empty($val) && $val !== '0');
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,10 +23,11 @@ class CsvBulkLoaderTest extends SapphireTest {
|
||||
$this->assertEquals(4, $results->Count(), 'Test correct count of imported data');
|
||||
|
||||
// Test that columns were correctly imported
|
||||
$obj = Dataobject::get_one("CsvBulkLoaderTest_Player", "FirstName = 'John'");
|
||||
$obj = DataObject::get_one("CsvBulkLoaderTest_Player", "FirstName = 'John'");
|
||||
$this->assertNotNull($obj);
|
||||
$this->assertEquals("He's a good guy", $obj->Biography);
|
||||
$this->assertEquals("1988-01-31", $obj->Birthday);
|
||||
$this->assertEquals("1", $obj->IsRegistered);
|
||||
|
||||
fclose($file);
|
||||
}
|
||||
@ -44,7 +45,8 @@ class CsvBulkLoaderTest extends SapphireTest {
|
||||
'FirstName',
|
||||
'Biography',
|
||||
null, // ignored column
|
||||
'Birthday'
|
||||
'Birthday',
|
||||
'IsRegistered'
|
||||
);
|
||||
$loader->hasHeaderRow = false;
|
||||
$results = $loader->load($filepath);
|
||||
@ -53,10 +55,15 @@ class CsvBulkLoaderTest extends SapphireTest {
|
||||
$this->assertEquals(4, $results->Count(), 'Test correct count of imported data');
|
||||
|
||||
// Test that columns were correctly imported
|
||||
$obj = Dataobject::get_one("CsvBulkLoaderTest_Player", "FirstName = 'John'");
|
||||
$obj = DataObject::get_one("CsvBulkLoaderTest_Player", "FirstName = 'John'");
|
||||
$this->assertNotNull($obj);
|
||||
$this->assertEquals("He's a good guy", $obj->Biography);
|
||||
$this->assertEquals("1988-01-31", $obj->Birthday);
|
||||
$this->assertEquals("1", $obj->IsRegistered);
|
||||
|
||||
$obj2 = DataObject::get_one('CsvBulkLoaderTest_Player', "FirstName = 'Jane'");
|
||||
$this->assertNotNull($obj2);
|
||||
$this->assertEquals('0', $obj2->IsRegistered);
|
||||
|
||||
fclose($file);
|
||||
}
|
||||
@ -133,7 +140,6 @@ class CsvBulkLoaderTest extends SapphireTest {
|
||||
$player = DataObject::get_by_id('CsvBulkLoaderTest_Player', 1);
|
||||
$this->assertEquals($player->FirstName, 'JohnUpdated', 'Test updating of existing records works');
|
||||
$this->assertEquals($player->Biography, 'He\'s a good guy', 'Test retaining of previous information on duplicate when overwriting with blank field');
|
||||
|
||||
}
|
||||
|
||||
function testLoadWithCustomImportMethods() {
|
||||
@ -142,12 +148,14 @@ class CsvBulkLoaderTest extends SapphireTest {
|
||||
$loader->columnMap = array(
|
||||
'FirstName' => '->importFirstName',
|
||||
'Biography' => 'Biography',
|
||||
'Birthday' => 'Birthday'
|
||||
'Birthday' => 'Birthday',
|
||||
'IsRegistered' => 'IsRegistered'
|
||||
);
|
||||
$results = $loader->load($filepath);
|
||||
$player = DataObject::get_by_id('CsvBulkLoaderTest_Player', 1);
|
||||
$this->assertEquals($player->FirstName, 'Customized John');
|
||||
$this->assertEquals($player->Biography, "He's a good guy");
|
||||
$this->assertEquals($player->IsRegistered, "1");
|
||||
}
|
||||
|
||||
protected function getLineCount(&$file) {
|
||||
@ -185,6 +193,7 @@ class CsvBulkLoaderTest_Player extends DataObject implements TestOnly {
|
||||
'Biography' => 'HTMLText',
|
||||
'Birthday' => 'Date',
|
||||
'ExternalIdentifier' => 'Varchar(255)', // used for uniqueness checks on passed property
|
||||
'IsRegistered' => 'Boolean'
|
||||
);
|
||||
|
||||
static $has_one = array(
|
||||
|
@ -1,4 +1 @@
|
||||
"John","He's a good guy","ignored","31/01/1988"
|
||||
"Jane","She is awesome.\nSo awesome that she gets multiple rows and \"escaped\" strings in her biography","ignored","31/01/1982"
|
||||
"Jamie","Pretty old\, with an escaped comma","ignored","31/01/1882"
|
||||
"Järg","Unicode FTW","ignored","31/06/1982"
|
||||
John,He's a good guy,ignored,31/01/88,1
Jane,"She is awesome.\nSo awesome that she gets multiple rows and \escaped\"" strings in her biography""",ignored,31/01/82,0
Jamie,"Pretty old\, with an escaped comma",ignored,31/01/1882,1
Järg,Unicode FTW,ignored,31/06/1982,1
|
Can't render this file because it contains an unexpected character in line 2 and column 70.
|
@ -1,6 +1,2 @@
|
||||
"FirstName","Biography","Birthday"
|
||||
"John","He's a good guy","31/01/1988"
|
||||
"Jane","She is awesome.
|
||||
So awesome that she gets multiple rows and \"escaped\" strings in her biography","31/01/1982"
|
||||
"Jamie","Pretty old\, with an escaped comma","31/01/1882"
|
||||
"Järg","Unicode FTW","31/06/1982"
|
||||
FirstName,Biography,Birthday,IsRegistered
John,He's a good guy,31/01/88,1
Jane,"She is awesome.
|
||||
So awesome that she gets multiple rows and \escaped\"" strings in her biography""",31/01/82,0
Jamie,"Pretty old\, with an escaped comma",31/01/1882,1
Järg,Unicode FTW,31/06/1982,1
|
Can't render this file because it contains an unexpected character in line 4 and column 45.
|
Loading…
Reference in New Issue
Block a user