Skip to content

Commit

Permalink
fix: require one regular character to submit posts, comments, and mes…
Browse files Browse the repository at this point in the history
…sages
  • Loading branch information
wescopeland committed Sep 6, 2024
1 parent d267bd9 commit cb8ae82
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 7 deletions.
12 changes: 9 additions & 3 deletions app/Community/Livewire/Forms/ForumTopicCommentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
namespace App\Community\Livewire\Forms;

use App\Models\ForumTopicComment;
use App\Support\Rules\ContainsRegularCharacter;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\RedirectResponse;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Features\SupportRedirects\Redirector;
use Livewire\Form;

class ForumTopicCommentForm extends Form
{
use AuthorizesRequests;

#[Validate('required|max:60000')]
public string $body = '';

#[Locked]
Expand All @@ -31,7 +30,14 @@ public function setForumTopicComment(ForumTopicComment $forumTopicComment): void
public function update(): RedirectResponse|Redirector
{
$this->authorize('update', [ForumTopicComment::class, $this->forumTopicComment]);
$this->validate();
$this->validate([
'body' => [
'required',
'string',
'max:60000',
new ContainsRegularCharacter(),
],
]);

editTopicComment($this->forumTopicComment->id, $this->body);

Expand Down
8 changes: 7 additions & 1 deletion app/Community/Requests/MessageRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

namespace App\Community\Requests;

use App\Support\Rules\ContainsRegularCharacter;
use Illuminate\Foundation\Http\FormRequest;

class MessageRequest extends FormRequest
{
public function rules(): array
{
return [
'body' => 'required|string|max:60000',
'body' => [
'required',
'string',
'max:60000',
new ContainsRegularCharacter(),
],
'recipient' => 'required_without:thread_id|exists:UserAccounts,User',
'thread_id' => 'nullable|integer',
'title' => 'required_without:thread_id|string|max:255',
Expand Down
19 changes: 19 additions & 0 deletions app/Support/Rules/ContainsRegularCharacter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Support\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class ContainsRegularCharacter implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// Check for at least one letter, number, punctuation mark, or symbol.
$requireOneRegularCharacter = '/[\p{L}\p{N}\p{P}\p{S}]/u';

if (!preg_match($requireOneRegularCharacter, $value)) {
$fail('validation.contains_regular_characters')->translate();
}
}
}
2 changes: 1 addition & 1 deletion lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
/*
* Strict validation rules
*/

'contains_regular_character' => 'The :attribute must contain at least one regular character.',
'ctype_alnum' => 'The :attribute must only contain unaccented letters and numbers.',

/*
Expand Down
8 changes: 7 additions & 1 deletion public/request/comment/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Comment;
use App\Models\Ticket;
use App\Models\User;
use App\Support\Rules\ContainsRegularCharacter;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;

Expand All @@ -14,7 +15,12 @@
}

$input = Validator::validate(Arr::wrap(request()->post()), [
'body' => 'required|string|max:2000',
'body' => [
'required',
'string',
'max:2000',
new ContainsRegularCharacter(),
],
'commentable_id' => 'required|integer',
'commentable_type' => 'required|integer',
]);
Expand Down
8 changes: 7 additions & 1 deletion public/request/forum-topic-comment/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Models\ForumTopic;
use App\Models\User;
use App\Support\Rules\ContainsRegularCharacter;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;

Expand All @@ -11,7 +12,12 @@

$input = Validator::validate(Arr::wrap(request()->post()), [
'topic' => 'required|integer|exists:ForumTopic,ID',
'body' => 'required|string|max:60000',
'body' => [
'required',
'string',
'max:60000',
new ContainsRegularCharacter(),
],
]);

$userModel = User::firstWhere('User', $user);
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/Rules/ContainsRegularCharacterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Unit\Rules;

use App\Support\Rules\ContainsRegularCharacter;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;

class ContainsRegularCharacterTest extends TestCase
{
public function testItPassesWhenInputContainsRegularCharacters(): void
{
$data = ['body' => 'This is a valid comment with letters and symbols!'];

$validator = Validator::make($data, [
'body' => ['required', 'string', new ContainsRegularCharacter()],
]);

$this->assertFalse($validator->fails());
}

public function testItFailsWhenInputOnlyContainsControlCharacters(): void
{
$data = ['body' => "\u{200B}\u{200E}\u{200F}"];

$validator = Validator::make($data, [
'body' => ['required', 'string', new ContainsRegularCharacter()],
]);

$this->assertTrue($validator->fails());
}

public function testItFailsWhenInputIsEmpty(): void
{
$data = ['body' => ''];

$validator = Validator::make($data, [
'body' => ['required', 'string', new ContainsRegularCharacter()],
]);

$this->assertTrue($validator->fails());
}

public function testItPassesWhenInputIsOnlySymbols(): void
{
$data = ['body' => '***!!!'];

$validator = Validator::make($data, [
'body' => ['required', 'string', new ContainsRegularCharacter()],
]);

$this->assertFalse($validator->fails());
}
}

0 comments on commit cb8ae82

Please sign in to comment.