mirror of
https://github.com/silverstripe/silverstripe-spamprotection.git
synced 2024-10-22 14:05:59 +02:00
Move stub classes to their own files, run PSR-2 linter
This commit is contained in:
parent
3d771d8179
commit
5cc9312a8d
@ -134,12 +134,12 @@ if (class_exists('EditableFormField')) {
|
|||||||
* Using custom validateField method
|
* Using custom validateField method
|
||||||
* as Spam Protection Field implementations may have their own error messages
|
* as Spam Protection Field implementations may have their own error messages
|
||||||
* and may not be based on the field being required, e.g. Honeypot Field
|
* and may not be based on the field being required, e.g. Honeypot Field
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @param Form $form
|
* @param Form $form
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function validateField($data, $form)
|
public function validateField($data, $form)
|
||||||
{
|
{
|
||||||
$formField = $this->getFormField();
|
$formField = $this->getFormField();
|
||||||
$formField->setForm($form);
|
$formField->setForm($form);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @package spamprotection
|
* @package spamprotection
|
||||||
*
|
*
|
||||||
* @deprecated 1.0
|
* @deprecated 1.0
|
||||||
@ -12,7 +12,8 @@ class SpamProtectorManager
|
|||||||
|
|
||||||
public static function set_spam_protector($protector)
|
public static function set_spam_protector($protector)
|
||||||
{
|
{
|
||||||
Deprecation::notice('1.1',
|
Deprecation::notice(
|
||||||
|
'1.1',
|
||||||
'SpamProtectorManager::set_spam_protector() is deprecated. '.
|
'SpamProtectorManager::set_spam_protector() is deprecated. '.
|
||||||
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
|
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
|
||||||
);
|
);
|
||||||
@ -22,16 +23,19 @@ class SpamProtectorManager
|
|||||||
|
|
||||||
public static function get_spam_protector()
|
public static function get_spam_protector()
|
||||||
{
|
{
|
||||||
Deprecation::notice('1.1',
|
Deprecation::notice(
|
||||||
|
'1.1',
|
||||||
'SpamProtectorManager::get_spam_protector() is deprecated'.
|
'SpamProtectorManager::get_spam_protector() is deprecated'.
|
||||||
'Use the new config system. FormSpamProtectorExtension.default_spam_protector');
|
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
|
||||||
|
);
|
||||||
|
|
||||||
return self::$spam_protector;
|
return self::$spam_protector;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null)
|
public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null)
|
||||||
{
|
{
|
||||||
Deprecation::notice('1.1',
|
Deprecation::notice(
|
||||||
|
'1.1',
|
||||||
'SpamProtectorManager::update_form is deprecated'.
|
'SpamProtectorManager::update_form is deprecated'.
|
||||||
'Please use $form->enableSpamProtection() for adding spamprotection'
|
'Please use $form->enableSpamProtection() for adding spamprotection'
|
||||||
);
|
);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply the spam protection to the comments module if it is installed.
|
* Apply the spam protection to the comments module if it is installed.
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An extension to the {@link Form} class which provides the method
|
* An extension to the {@link Form} class which provides the method
|
||||||
* {@link enableSpamProtection()} helper.
|
* {@link enableSpamProtection()} helper.
|
||||||
*
|
*
|
||||||
* @package spamprotection
|
* @package spamprotection
|
||||||
@ -22,7 +22,7 @@ class FormSpamProtectionExtension extends Extension
|
|||||||
/**
|
/**
|
||||||
* @config
|
* @config
|
||||||
*
|
*
|
||||||
* The {@link enableSpamProtection} method will define which of the form
|
* The {@link enableSpamProtection} method will define which of the form
|
||||||
* values correlates to this form mapped fields list. Totally custom forms
|
* values correlates to this form mapped fields list. Totally custom forms
|
||||||
* and subclassed SpamProtector instances are define their own mapping
|
* and subclassed SpamProtector instances are define their own mapping
|
||||||
*
|
*
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SpamProtector base interface.
|
* SpamProtector base interface.
|
||||||
*
|
*
|
||||||
* All Protectors are required implement this interface if they want to appear
|
* All Protectors are required implement this interface if they want to appear
|
||||||
* on the form.
|
* on the form.
|
||||||
*
|
*
|
||||||
* Classes with this interface are used to generate helper lists to allow the
|
* Classes with this interface are used to generate helper lists to allow the
|
||||||
* user to select the protector.
|
* user to select the protector.
|
||||||
*
|
*
|
||||||
* @package spamprotection
|
* @package spamprotection
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -17,9 +17,9 @@ interface SpamProtector
|
|||||||
/**
|
/**
|
||||||
* Return the {@link FormField} associated with this protector.
|
* Return the {@link FormField} associated with this protector.
|
||||||
*
|
*
|
||||||
* Most spam methods will simply return a piece of HTML to be injected at
|
* Most spam methods will simply return a piece of HTML to be injected at
|
||||||
* the end of the form. If a spam method needs to inject more than one
|
* the end of the form. If a spam method needs to inject more than one
|
||||||
* form field (i.e a hidden field and a text field) then return a
|
* form field (i.e a hidden field and a text field) then return a
|
||||||
* {@link FieldGroup} from this method to include both.
|
* {@link FieldGroup} from this method to include both.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
|
@ -1,26 +1,27 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class EditableSpamProtectionFieldTest extends SapphireTest
|
class EditableSpamProtectionFieldTest extends SapphireTest
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $usesDatabase = true;
|
protected $usesDatabase = true;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
|
if (!class_exists('EditableSpamProtectionField')) {
|
||||||
|
$this->markTestSkipped('"userforms" module not installed');
|
||||||
|
}
|
||||||
|
|
||||||
Config::inst()->update(
|
Config::inst()->update(
|
||||||
'FormSpamProtectionExtension', 'default_spam_protector',
|
'FormSpamProtectionExtension',
|
||||||
|
'default_spam_protector',
|
||||||
'EditableSpamProtectionFieldTest_Protector'
|
'EditableSpamProtectionFieldTest_Protector'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testValidateFieldDoesntAddErrorOnSuccess()
|
public function testValidateFieldDoesntAddErrorOnSuccess()
|
||||||
{
|
{
|
||||||
if (!class_exists('EditableSpamProtectionField')) {
|
|
||||||
$this->markTestSkipped('"userforms" module not installed');
|
|
||||||
}
|
|
||||||
|
|
||||||
$formMock = $this->getFormMock();
|
$formMock = $this->getFormMock();
|
||||||
$formFieldMock = $this->getEditableFormFieldMock();
|
$formFieldMock = $this->getEditableFormFieldMock();
|
||||||
|
|
||||||
@ -39,10 +40,6 @@ class EditableSpamProtectionFieldTest extends SapphireTest
|
|||||||
|
|
||||||
public function testValidateFieldAddsErrorFromField()
|
public function testValidateFieldAddsErrorFromField()
|
||||||
{
|
{
|
||||||
if (!class_exists('EditableSpamProtectionField')) {
|
|
||||||
$this->markTestSkipped('"userforms" module not installed');
|
|
||||||
}
|
|
||||||
|
|
||||||
$formMock = $this->getFormMock();
|
$formMock = $this->getFormMock();
|
||||||
$formFieldMock = $this->getEditableFormFieldMock();
|
$formFieldMock = $this->getEditableFormFieldMock();
|
||||||
|
|
||||||
@ -57,17 +54,14 @@ class EditableSpamProtectionFieldTest extends SapphireTest
|
|||||||
$formMock
|
$formMock
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('addErrorMessage')
|
->method('addErrorMessage')
|
||||||
->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything());;
|
->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything());
|
||||||
|
;
|
||||||
|
|
||||||
$formFieldMock->validateField(array('MyField' => null), $formMock);
|
$formFieldMock->validateField(array('MyField' => null), $formMock);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testValidateFieldAddsDefaultError()
|
public function testValidateFieldAddsDefaultError()
|
||||||
{
|
{
|
||||||
if (!class_exists('EditableSpamProtectionField')) {
|
|
||||||
$this->markTestSkipped('"userforms" module not installed');
|
|
||||||
}
|
|
||||||
|
|
||||||
$formMock = $this->getFormMock();
|
$formMock = $this->getFormMock();
|
||||||
$formFieldMock = $this->getEditableFormFieldMock();
|
$formFieldMock = $this->getEditableFormFieldMock();
|
||||||
|
|
||||||
@ -119,17 +113,4 @@ class EditableSpamProtectionFieldTest extends SapphireTest
|
|||||||
|
|
||||||
return $editableFormFieldMock;
|
return $editableFormFieldMock;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class EditableSpamProtectionFieldTest_Protector implements SpamProtector, TestOnly
|
|
||||||
{
|
|
||||||
public function getFormField($name = null, $title = null, $value = null)
|
|
||||||
{
|
|
||||||
return new TextField($name, 'Foo', $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setFieldMapping($fieldMapping)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ class FormSpamProtectionExtensionTest extends SapphireTest
|
|||||||
* @var Form
|
* @var Form
|
||||||
*/
|
*/
|
||||||
protected $form = null;
|
protected $form = null;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
@ -20,15 +20,15 @@ class FormSpamProtectionExtensionTest extends SapphireTest
|
|||||||
new TextField('Title'),
|
new TextField('Title'),
|
||||||
new TextField('Comment'),
|
new TextField('Comment'),
|
||||||
new TextField('URL')
|
new TextField('URL')
|
||||||
), new FieldList()
|
), new FieldList());
|
||||||
);
|
|
||||||
$this->form->disableSecurityToken();
|
$this->form->disableSecurityToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEnableSpamProtection()
|
public function testEnableSpamProtection()
|
||||||
{
|
{
|
||||||
Config::inst()->update(
|
Config::inst()->update(
|
||||||
'FormSpamProtectionExtension', 'default_spam_protector',
|
'FormSpamProtectionExtension',
|
||||||
|
'default_spam_protector',
|
||||||
'FormSpamProtectionExtensionTest_FooProtector'
|
'FormSpamProtectionExtensionTest_FooProtector'
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ class FormSpamProtectionExtensionTest extends SapphireTest
|
|||||||
'protector' => 'FormSpamProtectionExtensionTest_BarProtector',
|
'protector' => 'FormSpamProtectionExtensionTest_BarProtector',
|
||||||
'title' => 'Baz',
|
'title' => 'Baz',
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
|
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,28 +66,28 @@ class FormSpamProtectionExtensionTest extends SapphireTest
|
|||||||
|
|
||||||
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
|
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInsertBefore()
|
public function testInsertBefore()
|
||||||
{
|
{
|
||||||
$form = $this->form->enableSpamProtection(array(
|
$form = $this->form->enableSpamProtection(array(
|
||||||
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
|
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
|
||||||
'insertBefore' => 'URL'
|
'insertBefore' => 'URL'
|
||||||
));
|
));
|
||||||
|
|
||||||
$fields = $form->Fields();
|
$fields = $form->Fields();
|
||||||
$this->assertEquals('Title', $fields[0]->Title());
|
$this->assertEquals('Title', $fields[0]->Title());
|
||||||
$this->assertEquals('Comment', $fields[1]->Title());
|
$this->assertEquals('Comment', $fields[1]->Title());
|
||||||
$this->assertEquals('Foo', $fields[2]->Title());
|
$this->assertEquals('Foo', $fields[2]->Title());
|
||||||
$this->assertEquals('URL', $fields[3]->Title());
|
$this->assertEquals('URL', $fields[3]->Title());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInsertBeforeMissing()
|
public function testInsertBeforeMissing()
|
||||||
{
|
{
|
||||||
$form = $this->form->enableSpamProtection(array(
|
$form = $this->form->enableSpamProtection(array(
|
||||||
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
|
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
|
||||||
'insertBefore' => 'NotAField'
|
'insertBefore' => 'NotAField'
|
||||||
));
|
));
|
||||||
|
|
||||||
// field should default to the end instead
|
// field should default to the end instead
|
||||||
$fields = $form->Fields();
|
$fields = $form->Fields();
|
||||||
$this->assertEquals('Title', $fields[0]->Title());
|
$this->assertEquals('Title', $fields[0]->Title());
|
||||||
@ -96,49 +96,3 @@ class FormSpamProtectionExtensionTest extends SapphireTest
|
|||||||
$this->assertEquals('Foo', $fields[3]->Title());
|
$this->assertEquals('Foo', $fields[3]->Title());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @package spamprotection
|
|
||||||
*/
|
|
||||||
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly
|
|
||||||
{
|
|
||||||
public function getFormField($name = null, $title = null, $value = null)
|
|
||||||
{
|
|
||||||
return new TextField($name, $title, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setFieldMapping($fieldMapping)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @package spamprotection
|
|
||||||
*/
|
|
||||||
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly
|
|
||||||
{
|
|
||||||
public function getFormField($name = null, $title = null, $value = null)
|
|
||||||
{
|
|
||||||
$title = $title ?: 'Bar';
|
|
||||||
return new TextField($name, $title, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setFieldMapping($fieldMapping)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @package spamprotection
|
|
||||||
*/
|
|
||||||
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly
|
|
||||||
{
|
|
||||||
public function getFormField($name = null, $title = null, $value = null)
|
|
||||||
{
|
|
||||||
return new TextField($name, 'Foo', $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setFieldMapping($fieldMapping)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
13
tests/Stub/EditableSpamProtectionFieldTest_Protector.php
Normal file
13
tests/Stub/EditableSpamProtectionFieldTest_Protector.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class EditableSpamProtectionFieldTest_Protector implements SpamProtector, TestOnly
|
||||||
|
{
|
||||||
|
public function getFormField($name = null, $title = null, $value = null)
|
||||||
|
{
|
||||||
|
return new TextField($name, 'Foo', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFieldMapping($fieldMapping)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
17
tests/Stub/FormSpamProtectionExtensionTest_BarProtector.php
Normal file
17
tests/Stub/FormSpamProtectionExtensionTest_BarProtector.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package spamprotection
|
||||||
|
*/
|
||||||
|
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly
|
||||||
|
{
|
||||||
|
public function getFormField($name = null, $title = null, $value = null)
|
||||||
|
{
|
||||||
|
$title = $title ?: 'Bar';
|
||||||
|
return new TextField($name, $title, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFieldMapping($fieldMapping)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
16
tests/Stub/FormSpamProtectionExtensionTest_BazProtector.php
Normal file
16
tests/Stub/FormSpamProtectionExtensionTest_BazProtector.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package spamprotection
|
||||||
|
*/
|
||||||
|
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly
|
||||||
|
{
|
||||||
|
public function getFormField($name = null, $title = null, $value = null)
|
||||||
|
{
|
||||||
|
return new TextField($name, $title, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFieldMapping($fieldMapping)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
16
tests/Stub/FormSpamProtectionExtensionTest_FooProtector.php
Normal file
16
tests/Stub/FormSpamProtectionExtensionTest_FooProtector.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package spamprotection
|
||||||
|
*/
|
||||||
|
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly
|
||||||
|
{
|
||||||
|
public function getFormField($name = null, $title = null, $value = null)
|
||||||
|
{
|
||||||
|
return new TextField($name, 'Foo', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFieldMapping($fieldMapping)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user