Skip to content

Commit

Permalink
Merge pull request #222 from pinkary-project/fix/questions-to-be-answ…
Browse files Browse the repository at this point in the history
…ered-more-than-once

[Fix] questions to be answered more than once
  • Loading branch information
nunomaduro authored Apr 15, 2024
2 parents ac8b2aa + dc61f3b commit b4a4eac
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
24 changes: 22 additions & 2 deletions app/Livewire/Questions/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Livewire\Questions;

use App\Models\Question;
use App\Models\User;
use App\Rules\NoBlankCharacters;
use Illuminate\Http\Request;
use Illuminate\View\View;
Expand All @@ -27,13 +28,32 @@ final class Edit extends Component
/**
* Updates the question with the given answer.
*/
public function update(): void
public function update(Request $request): void
{
if (! auth()->check()) {
to_route('login');

return;
}

$this->validate([
'answer' => ['required', 'string', 'max:1000', new NoBlankCharacters],
]);

$question = Question::findOrFail($this->questionId);
$user = type($request->user())->as(User::class);

$question = Question::query()
->whereNull('answer')
->where('is_reported', false)
->where('is_ignored', false)
->find($this->questionId);

if (is_null($question)) {
$this->dispatch('notification.created', message: 'Sorry, something unexpected happened. Please try again.');
$this->redirectRoute('profile.show', ['username' => $user->username], navigate: true);

return;
}

$this->authorize('update', $question);

Expand Down
46 changes: 46 additions & 0 deletions tests/Unit/Livewire/Questions/EditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,49 @@
'answer' => 'The answer field cannot contain blank characters.',
]);
});

test('cannot answer a question that has already been answered', function () {
$this->question->update([
'answer' => 'Hello World',
'answered_at' => now(),
]);

$component = Livewire::test(Edit::class, [
'questionId' => $this->question->id,
]);

$component->set('answer', 'Hello World');

$component->call('update');

$component->assertDispatched('notification.created', message: 'Sorry, something unexpected happened. Please try again.');

$component->assertRedirect(route('profile.show', ['username' => $this->question->to->username]));
});

test('cannot answer a question that has been reported or ignored', function () {
$this->question->update([
'is_reported' => true,
]);

$component = Livewire::test(Edit::class, [
'questionId' => $this->question->id,
]);

$component->set('answer', 'Hello World');

$component->call('update');

$component->assertDispatched('notification.created', message: 'Sorry, something unexpected happened. Please try again.');

$this->question->update([
'is_reported' => false,
'is_ignored' => true,
]);

$component->call('update');

$component->assertDispatched('notification.created', message: 'Sorry, something unexpected happened. Please try again.');

$component->assertRedirect(route('profile.show', ['username' => $this->question->to->username]));
});

0 comments on commit b4a4eac

Please sign in to comment.