Skip to content

Commit

Permalink
FIX Check value is not NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Jan 16, 2023
1 parent 2c8fc03 commit 15d53ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion code/Extension/FormSpamProtectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\SpamProtection\Extension;

use LogicException;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
Expand Down Expand Up @@ -84,6 +85,7 @@ public static function get_protector($options = null)
* Activates the spam protection module.
*
* @param array $options
* @throws LogicException when get_protector method returns NULL.
* @return Object
*/
public function enableSpamProtection($options = array())
Expand All @@ -106,7 +108,11 @@ public function enableSpamProtection($options = array())
// set custom mapping on this form
$protector = self::get_protector($options);

if (isset($options['mapping'])) {
if ($protector === null) {
throw new LogicException('No spam protector has been set. Null is not valid value.');
}

if ($protector && isset($options['mapping'])) {
$protector->setFieldMapping($options['mapping']);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/FormSpamProtectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ protected function setUp(): void
$this->form->disableSecurityToken();
}

public function testEnableSpamProtectionThrowsException()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No spam protector has been set. Null is not valid value.');

$this->form->enableSpamProtection();
}

public function testEnableSpamProtection()
{
Config::modify()->set(
Expand Down

0 comments on commit 15d53ee

Please sign in to comment.