diff --git a/code/EditableSpamProtectionField.php b/code/EditableSpamProtectionField.php index 6260b03..a88b3e0 100644 --- a/code/EditableSpamProtectionField.php +++ b/code/EditableSpamProtectionField.php @@ -9,6 +9,7 @@ use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldGroup; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FormField; +use SilverStripe\ORM\DataList; use SilverStripe\ORM\UnsavedRelationList; use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension; use SilverStripe\UserForms\Model\EditableFormField; @@ -112,8 +113,11 @@ class EditableSpamProtectionField extends EditableFormField } // Get all candidates of the above types - return $this - ->Parent() + $parent = $this->Parent(); + if (!$parent) { + return DataList::create(EditableFormField::class); + } + return $parent ->Fields() ->filter('ClassName', $typesInherit) ->exclude('Title', ''); // Ignore this field and those without titles diff --git a/composer.json b/composer.json index e40c309..b8318c3 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "require-dev": { "phpunit/phpunit": "^9.5", "silverstripe/versioned": "^1.0", - "squizlabs/php_codesniffer": "^3.0" + "squizlabs/php_codesniffer": "^3.0", + "silverstripe/userforms": "^5" }, "extra": { "expose": [ @@ -39,4 +40,4 @@ "license": "BSD-3-Clause", "minimum-stability": "dev", "prefer-stable": true -} \ No newline at end of file +} diff --git a/tests/EditableSpamProtectionFieldTest.php b/tests/EditableSpamProtectionFieldTest.php index 0741db8..182de53 100644 --- a/tests/EditableSpamProtectionFieldTest.php +++ b/tests/EditableSpamProtectionFieldTest.php @@ -2,6 +2,8 @@ namespace SilverStripe\SpamProtection\Tests; +use ReflectionClass; +use SilverStripe\ORM\DataList; use SilverStripe\UserForms\Model\UserDefinedForm; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; @@ -153,4 +155,24 @@ class EditableSpamProtectionFieldTest extends SapphireTest 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); + } }