Skip to content
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
16 changes: 13 additions & 3 deletions app/Rules/ProfanityCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ class ProfanityCheck implements Rule
{
public function passes($attribute, $value)
{
$words = explode(' ', $value);
if (! is_string($value)) {
return true;
}

foreach (app(GeneralSettings::class)->profanity_words as $profanityWord) {
$terms = preg_split('/\s+/u', trim((string) $profanityWord), -1, PREG_SPLIT_NO_EMPTY);

if (empty($terms)) {
continue;
}

$phrase = implode('\s+', array_map(fn (string $term) => preg_quote($term, '/'), $terms));

foreach ($words as $word) {
if (in_array($word, app(GeneralSettings::class)->profanity_words)) {
if (preg_match('/(?<![\p{L}\p{N}])' . $phrase . '(?![\p{L}\p{N}])/iu', $value) === 1) {
return false;
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Rules/ProfanityCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use App\Rules\ProfanityCheck;
use App\Settings\GeneralSettings;

beforeEach(function () {
GeneralSettings::fake([
'profanity_words' => ['fuck', 'asshole', 'dick', 'screw you'],
]);
});

test('content containing a profanity word fails', function (string $content) {
expect((new ProfanityCheck())->passes('content', $content))->toBeFalse();
})->with([
'exact word' => 'fuck',
'different casing' => 'Fuck this feature',
'trailing punctuation' => 'What the fuck!',
'surrounded by punctuation' => '(asshole)',
'separated by a newline' => "Nice idea\ndick",
'multi word entry' => 'Screw you',
'multi word entry across whitespace' => "screw\n you, really",
]);

test('content without a profanity word passes', function (string $content) {
expect((new ProfanityCheck())->passes('content', $content))->toBeTrue();
})->with([
'clean sentence' => 'Please add dark mode',
'word containing a profanity word' => 'I love reading Dickens',
'multi word entry split by other words' => 'Screw the lid on, thank you',
]);

test('empty profanity entries are ignored', function () {
GeneralSettings::fake(['profanity_words' => ['', ' ']]);

expect((new ProfanityCheck())->passes('content', 'Please add dark mode'))->toBeTrue();
});
Loading