Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Check value is not NULL #88

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
maxime-rainville marked this conversation as resolved.
Show resolved Hide resolved

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
9 changes: 9 additions & 0 deletions tests/FormSpamProtectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\SpamProtection\Tests;

use LogicException;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
Expand Down Expand Up @@ -38,6 +39,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