Merge pull request #79 from creative-commoners/pulls/3.1/write-without-parent

FIX Allow fields without parents to be programatically created
This commit is contained in:
Maxime Rainville 2022-03-17 15:18:02 +13:00 committed by GitHub
commit 68d550b817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View File

@ -9,6 +9,7 @@ use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup; use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormField; use SilverStripe\Forms\FormField;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\UnsavedRelationList; use SilverStripe\ORM\UnsavedRelationList;
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension; use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
use SilverStripe\UserForms\Model\EditableFormField; use SilverStripe\UserForms\Model\EditableFormField;
@ -112,8 +113,11 @@ class EditableSpamProtectionField extends EditableFormField
} }
// Get all candidates of the above types // Get all candidates of the above types
return $this $parent = $this->Parent();
->Parent() if (!$parent) {
return DataList::create(EditableFormField::class);
}
return $parent
->Fields() ->Fields()
->filter('ClassName', $typesInherit) ->filter('ClassName', $typesInherit)
->exclude('Title', ''); // Ignore this field and those without titles ->exclude('Title', ''); // Ignore this field and those without titles

View File

@ -22,7 +22,8 @@
"require-dev": { "require-dev": {
"sminnee/phpunit": "^5.7", "sminnee/phpunit": "^5.7",
"silverstripe/versioned": "^1.0", "silverstripe/versioned": "^1.0",
"squizlabs/php_codesniffer": "^3.0" "squizlabs/php_codesniffer": "^3.0",
"silverstripe/userforms": "^5"
}, },
"extra": { "extra": {
"expose": [ "expose": [

View File

@ -2,6 +2,8 @@
namespace SilverStripe\SpamProtection\Tests; namespace SilverStripe\SpamProtection\Tests;
use ReflectionClass;
use SilverStripe\ORM\DataList;
use SilverStripe\UserForms\Model\UserDefinedForm; use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
@ -153,4 +155,24 @@ class EditableSpamProtectionFieldTest extends SapphireTest
return $editableFormFieldMock; return $editableFormFieldMock;
} }
public function testGetCandidateFieldsParentStatus()
{
$field = new EditableSpamProtectionField();
$field->Name = 'MyField';
$reflection = new ReflectionClass($field);
$method = $reflection->getMethod('getCandidateFields');
$method->setAccessible(true);
// Assert with no parent
$list = $method->invoke($field);
$this->assertTrue($list instanceof DataList);
// Assert with parent
$page = new UserDefinedForm();
$page->write();
$field->ParentID = $page->ID;
$field->ParentClass = get_class($page);
$field->write();
$list = $method->invoke($field);
$this->assertTrue($list instanceof DataList);
}
} }