FIX Check value is not NULL

This commit is contained in:
Sabina Talipova 2023-01-16 10:42:51 +13:00
parent 2c8fc03f2f
commit 2a55708ae8
2 changed files with 16 additions and 1 deletions

View File

@ -2,6 +2,7 @@
namespace SilverStripe\SpamProtection\Extension; namespace SilverStripe\SpamProtection\Extension;
use LogicException;
use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extension; use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
@ -84,6 +85,7 @@ class FormSpamProtectionExtension extends Extension
* Activates the spam protection module. * Activates the spam protection module.
* *
* @param array $options * @param array $options
* @throws LogicException when get_protector method returns NULL.
* @return Object * @return Object
*/ */
public function enableSpamProtection($options = array()) public function enableSpamProtection($options = array())
@ -106,7 +108,11 @@ class FormSpamProtectionExtension extends Extension
// set custom mapping on this form // set custom mapping on this form
$protector = self::get_protector($options); $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']); $protector->setFieldMapping($options['mapping']);
} }

View File

@ -2,6 +2,7 @@
namespace SilverStripe\SpamProtection\Tests; namespace SilverStripe\SpamProtection\Tests;
use LogicException;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
@ -38,6 +39,14 @@ class FormSpamProtectionExtensionTest extends SapphireTest
$this->form->disableSecurityToken(); $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() public function testEnableSpamProtection()
{ {
Config::modify()->set( Config::modify()->set(