Skip to content

Commit

Permalink
Merge pull request #2 from nswdpc/feat-badge-placement
Browse files Browse the repository at this point in the history
Badge placement
  • Loading branch information
tardinha authored Oct 26, 2022
2 parents ff0de20 + f6cf13b commit 6c1228a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
9 changes: 6 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/tests export-ignore
/docs export-ignore
/client/src export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php_cs.dist
/.phpcs.xml.dist
/.phpunit.xml.dist
/.php_cs.dist export-ignore
/phpcs.xml.dist export-ignore
/phpunit.xml.dist export-ignore
/.waratah export-ignore
/code_of_conduct.md export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
$Field
<% if $Message %><span class="message $MessageType">$Message</span><% end_if %>

<% if $ReCAPTCHAv3BadgeDisplay=='field' %>
<% include NSWDPC/SpamProtection/FormBadge %>
<% end_if %>
Empty file removed tests/.gitkeep
Empty file.
86 changes: 86 additions & 0 deletions tests/EditableRecaptchaV3FieldBadgePlacementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace NSWDPC\SpamProtection\Tests;

use NSWDPC\SpamProtection\EditableRecaptchaV3Field;
use NSWDPC\SpamProtection\RecaptchaV3SpamProtector;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;

/**
* Test badge placement for editable recaptcha fields
* @author James
*/
class EditableRecaptchaV3FieldBadgePlacementTest extends SapphireTest
{

protected $usesDatabase = false;

public function testDefaultBadgePlacement() {
Config::modify()->set(RecaptchaV3SpamProtector::class, 'badge_display', RecaptchaV3SpamProtector::BADGE_DISPLAY_DEFAULT);
$field = EditableRecaptchaV3Field::create([
'Score' => 50,
'Action' => 'test/default'
]);
$field->write();

$displayOption = RecaptchaV3SpamProtector::get_badge_display();
$this->assertEquals(RecaptchaV3SpamProtector::BADGE_DISPLAY_DEFAULT, $displayOption, "ShowRecaptchaV3Badge returned empty");

$template = $field->getFormField()->FieldHolder()->forTemplate();

$this->assertTrue( strpos($template, "https://policies.google.com/privacy") === false, "Recaptcha policy link not in template");

$this->assertTrue( strpos($template, "https://policies.google.com/terms") === false, "Recaptcha T&C link not in template");
}

public function testFieldBadgePlacement() {
Config::modify()->set(RecaptchaV3SpamProtector::class, 'badge_display', RecaptchaV3SpamProtector::BADGE_DISPLAY_FIELD);
$field = EditableRecaptchaV3Field::create([
'Score' => 60,
'Action' => 'test/field'
]);
$displayOption = RecaptchaV3SpamProtector::get_badge_display();
$this->assertEquals(RecaptchaV3SpamProtector::BADGE_DISPLAY_FIELD, $displayOption, "ShowRecaptchaV3Badge returned field setting");

$template = $field->getFormField()->FieldHolder()->forTemplate();

$this->assertTrue( strpos($template, "https://policies.google.com/privacy") !== false, "Recaptcha policy link in template");

$this->assertTrue( strpos($template, "https://policies.google.com/terms") !== false, "Recaptcha T&C link in template");
}

public function testFormBadgePlacement() {
Config::modify()->set(RecaptchaV3SpamProtector::class, 'badge_display', RecaptchaV3SpamProtector::BADGE_DISPLAY_FORM);
$field = EditableRecaptchaV3Field::create([
'Score' => 20,
'Action' => 'test/form'
]);
$displayOption = RecaptchaV3SpamProtector::get_badge_display();
$this->assertEquals(RecaptchaV3SpamProtector::BADGE_DISPLAY_FORM, $displayOption, "ShowRecaptchaV3Badge returned page setting");

$template = $field->getFormField()->FieldHolder()->forTemplate();

$this->assertTrue( strpos($template, "https://policies.google.com/privacy") === false, "Recaptcha policy link not in template");

$this->assertTrue( strpos($template, "https://policies.google.com/terms") === false, "Recaptcha T&C link not in template");

}

public function testPageBadgePlacement() {
Config::modify()->set(RecaptchaV3SpamProtector::class, 'badge_display', RecaptchaV3SpamProtector::BADGE_DISPLAY_PAGE);
$field = EditableRecaptchaV3Field::create([
'Score' => 40,
'Action' => 'test/page'
]);
$displayOption = RecaptchaV3SpamProtector::get_badge_display();
$this->assertEquals(RecaptchaV3SpamProtector::BADGE_DISPLAY_PAGE, $displayOption, "ShowRecaptchaV3Badge returned page setting");

$template = $field->getFormField()->FieldHolder()->forTemplate();

$this->assertTrue( strpos($template, "https://policies.google.com/privacy") === false, "Recaptcha policy link not in template");

$this->assertTrue( strpos($template, "https://policies.google.com/terms") === false, "Recaptcha T&C link not in template");

}
}

0 comments on commit 6c1228a

Please sign in to comment.