Skip to content

Commit

Permalink
Add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Aug 5, 2023
1 parent afebce1 commit 02810ac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
5 changes: 1 addition & 4 deletions src/Form/AnyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace SilverStripe\AnyField\Form;

use Behat\Mink\Element\Element;
use DNADesign\Elemental\Controllers\ElementalAreaController;
use DNADesign\Elemental\Forms\EditFormFactory;
use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\AnyField\Services\AnyService;
use SilverStripe\Core\ClassInfo;
use SilverStripe\ORM\DataObject;

/**
Expand Down Expand Up @@ -43,6 +39,7 @@ private function guessBaseClass(): ?string
$fieldname = $fakeData[0];
};


$class = DataObject::getSchema()->hasOneComponent(get_class($record), $fieldname);
return $class;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Form/JsonField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\AnyField\Form;

use InvalidArgumentException;
use LogicException;
use MaximeRainville\SilverstripeReact\ReactFormField;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\FormField;
Expand Down Expand Up @@ -50,12 +51,11 @@ public function saveInto(DataObjectInterface $record)
$fieldname = $this->getName();

if (!$fieldname) {
return $this;
throw new LogicException(sprintf('%s: Field must have a name', static::class));
}

$service = AnyService::singleton();
$dataValue = $this->dataValue();
$value = is_string($dataValue) ? $this->parseString($this->dataValue()) : $dataValue;
$value = $this->dataValue();

if ($class = DataObject::getSchema()->hasOneComponent(get_class($record), $fieldname)) {
/** @var JsonData|DataObject $dataObject */
Expand Down
24 changes: 24 additions & 0 deletions tests/php/Form/AnyFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,29 @@ public function testSetValue()
}
}

public function testSaveNew(): void
{
$field = $this->getAnyField();
$field->setName('Link');

$this->assertEquals(
'',
$field->getBaseClass(),
'When base class is not guessable, a blank string is returned'
);

$form = new Form(Controller::curr(), 'Form', FieldList::create($field));
$parentRecord = AnyFieldTest\SingleLink::create();
$form->loadDataFrom($parentRecord);

$field->setValue([
'ClassName' => ExternalLink::class,
'Title' => 'Silverstripe CMS',
'URL' => 'https://silverstripe.org',
]);

$field->saveInto($parentRecord);
}


}

0 comments on commit 02810ac

Please sign in to comment.