2017-05-08 13:57:52 +02:00
|
|
|
<?php
|
|
|
|
|
2019-04-23 08:22:18 +02:00
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use Sunnysideup\DMS\Forms\DMSJsonField;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2017-05-24 04:29:51 +02:00
|
|
|
class DMSJsonFieldTest extends SapphireTest
|
2017-05-08 13:57:52 +02:00
|
|
|
{
|
|
|
|
public function testJsonFieldConstructorMultiWays()
|
|
|
|
{
|
2017-05-24 04:29:51 +02:00
|
|
|
$jsonField = new DMSJsonField('MyJsonField', new FieldList(
|
2017-05-08 13:57:52 +02:00
|
|
|
new TextField('FirstName', 'Given name'),
|
|
|
|
new TextField('Surname', 'Last name')
|
|
|
|
));
|
|
|
|
$this->assertEquals($jsonField->FieldList()->count(), 2);
|
|
|
|
$this->assertNotNull($jsonField->FieldList()->dataFieldByName('MyJsonField[FirstName]'));
|
|
|
|
|
2017-05-24 04:29:51 +02:00
|
|
|
$jsonField = new DMSJsonField('MyJsonField', array(new TextField('FirstName', 'Given name'),
|
2017-05-08 13:57:52 +02:00
|
|
|
new TextField('Surname', 'Last name')));
|
|
|
|
$this->assertEquals($jsonField->FieldList()->count(), 2);
|
|
|
|
$this->assertNotNull($jsonField->FieldList()->dataFieldByName('MyJsonField[FirstName]'));
|
|
|
|
|
2017-05-24 04:29:51 +02:00
|
|
|
$jsonField = new DMSJsonField(
|
2017-05-08 13:57:52 +02:00
|
|
|
'MyJsonField',
|
|
|
|
new TextField('FirstName', 'Given name'),
|
|
|
|
new TextField('Surname', 'Last name')
|
|
|
|
);
|
|
|
|
$this->assertEquals($jsonField->FieldList()->count(), 2);
|
|
|
|
$this->assertNotNull($jsonField->FieldList()->dataFieldByName('MyJsonField[FirstName]'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testJsonFieldDataValueCouldDealWithArray()
|
|
|
|
{
|
2017-05-24 04:29:51 +02:00
|
|
|
$jsonField = new DMSJsonField('MyJsonField', new FieldList(
|
2017-05-08 13:57:52 +02:00
|
|
|
new TextField('FirstName', 'Given name'),
|
|
|
|
new TextField('Surname', 'Last name')
|
|
|
|
));
|
|
|
|
$jsonField->setValue($value = array(
|
|
|
|
'MyJsonField'=>array(
|
|
|
|
'FirstName' => 'Normann',
|
|
|
|
'Surname' => 'Lou',
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals($jsonField->dataValue(), Convert::array2json($value));
|
|
|
|
$jsonField->setValue($value = array(
|
|
|
|
'MyJsonField'=>array(),
|
|
|
|
));
|
|
|
|
$this->assertNull($jsonField->dataValue());
|
|
|
|
}
|
|
|
|
}
|