Skip to content

Commit

Permalink
Merge branch '3.2' into 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Mar 19, 2022
2 parents 514b6d7 + 228f2a3 commit a2a2198
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
8 changes: 6 additions & 2 deletions code/EditableSpamProtectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -112,8 +113,11 @@ protected function getCandidateFields()
}

// 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
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -39,4 +40,4 @@
"license": "BSD-3-Clause",
"minimum-stability": "dev",
"prefer-stable": true
}
}
22 changes: 22 additions & 0 deletions tests/EditableSpamProtectionFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -153,4 +155,24 @@ protected function getEditableFormFieldMock()

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);
}
}

0 comments on commit a2a2198

Please sign in to comment.