This repository was archived by the owner on Jun 29, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Resolves #27 - Update question #41
Merged
josepostiga
merged 20 commits into
laravel-portugal:master
from
tiagof:feature/issue-27-Update-question
Oct 25, 2020
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
75905b4
init
tiagof ad251dd
Adds Question create feature
tiagof ea502b5
Added '/discussions' prefix
tiagof 09d0e3a
Minor fixes as per review
tiagof 26c1b76
Cleanup
tiagof e17b30a
Added question update
tiagof 9e16ce7
Added tests
tiagof e20c5d0
Resolves #28, #29: "Update timestamps columns on links and tags table…
ialexreis 9df3a37
Resolves #25 - An authenticated user can post a question (#40)
tiagof 4c07b31
Resolve conflicts + test tweaks
tiagof ed1622d
Merge branch 'master' into feature/issue-27-Update-question
tiagof 9cf8410
Merge remote-tracking branch 'upstream/master' into feature/issue-27-…
tiagof e15711f
Merge branch 'feature/issue-27-Update-question' of https://github.com…
tiagof 0e7f585
Code cleanup + minor improvements
tiagof 55da6e4
minor tweaks
tiagof 001740a
Merge remote-tracking branch 'upstream/master' into feature/issue-27-…
tiagof e33b171
Merge remote-tracking branch 'upstream/master' into feature/issue-27-…
tiagof 199b8c0
final tweaks/cleanup
tiagof 6bb901b
style(Discussions): minor code style fixes
7e67bdb
ci(PHPUnit): add blade files to the code coverage exclusion list
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
domains/Discussions/Controllers/QuestionsUpdateController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Domains\Discussions\Controllers; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Domains\Discussions\Models\Question; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
class QuestionsUpdateController extends Controller | ||
{ | ||
private Question $questions; | ||
|
||
public function __construct(Question $questions) | ||
{ | ||
$this->questions = $questions; | ||
} | ||
|
||
public function __invoke(int $questionId, Request $request): Response | ||
{ | ||
$question = $this->questions->findOrFail($questionId); | ||
|
||
$this->authorize('update', $question); | ||
|
||
$this->validate($request, [ | ||
'title' => ['required', 'string', 'max:255'], | ||
'description' => ['nullable', 'string'], | ||
]); | ||
|
||
$question->update([ | ||
'title' => $request->input('title'), | ||
'description' => $request->input('description', $question->description), | ||
]); | ||
|
||
return new Response('', Response::HTTP_NO_CONTENT); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Domains\Discussions\Policies; | ||
|
||
use Domains\Accounts\Models\User; | ||
use Domains\Discussions\Models\Question; | ||
use Illuminate\Auth\Access\HandlesAuthorization; | ||
|
||
class QuestionPolicy | ||
{ | ||
use HandlesAuthorization; | ||
|
||
public function update(User $user, Question $question) | ||
{ | ||
return $question->author->is($user); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
domains/Discussions/Tests/Feature/QuestionsUpdateTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Domains\Discussions\Tests\Feature; | ||
|
||
use Domains\Accounts\Database\Factories\UserFactory; | ||
use Domains\Accounts\Models\User; | ||
use Domains\Discussions\Database\Factories\QuestionFactory; | ||
use Domains\Discussions\Models\Question; | ||
use Faker\Factory; | ||
use Faker\Generator; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Str; | ||
use Laravel\Lumen\Testing\DatabaseMigrations; | ||
use Tests\TestCase; | ||
|
||
class QuestionsUpdateTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
private Generator $faker; | ||
private User $user; | ||
private Question $question; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->faker = Factory::create(); | ||
$this->user = UserFactory::new()->create(); | ||
$this->question = QuestionFactory::new(['author_id' => $this->user->id])->create(); | ||
} | ||
|
||
/** @test */ | ||
public function it_updates_questions(): void | ||
{ | ||
Carbon::setTestNow(); | ||
|
||
$payload = [ | ||
'title' => $this->faker->title, | ||
'description' => $this->faker->paragraph, | ||
]; | ||
|
||
$response = $this->actingAs($this->user) | ||
->call( | ||
'PATCH', | ||
route('discussions.questions.update', ['questionId' => $this->question->id]), | ||
$payload | ||
); | ||
|
||
$this->assertResponseStatus(Response::HTTP_NO_CONTENT); | ||
|
||
self::assertTrue($response->isEmpty()); | ||
|
||
$this->seeInDatabase('questions', [ | ||
tiagof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'id' => $this->question->id, | ||
'author_id' => $this->user->id, | ||
'title' => $payload['title'], | ||
'slug' => Str::slug($payload['title']), | ||
'description' => $payload['description'], | ||
'updated_at' => Carbon::now(), | ||
]); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_to_update_if_title_is_missing(): void | ||
{ | ||
$this->actingAs($this->user) | ||
->patch(route('discussions.questions.update', ['questionId' => $this->question->id])) | ||
->seeJsonStructure([ | ||
'title', | ||
]); | ||
} | ||
|
||
/** @test */ | ||
public function it_keeps_previous_description_if_none_is_sent(): void | ||
{ | ||
$response = $this->actingAs($this->user) | ||
->call( | ||
'PATCH', | ||
route('discussions.questions.update', ['questionId' => $this->question->id]), | ||
[ | ||
'title' => $this->faker->title, | ||
] | ||
); | ||
|
||
self::assertTrue($response->isEmpty()); | ||
self::assertEquals($this->question->description, $this->question->refresh()->description); | ||
} | ||
|
||
/** @test */ | ||
public function it_forbids_non_owner_to_update_questions(): void | ||
{ | ||
$this->actingAs(UserFactory::new()->make()) | ||
->patch( | ||
route('discussions.questions.update', ['questionId' => $this->question->id]), | ||
[ | ||
'title' => $this->faker->title, | ||
] | ||
) | ||
->assertResponseStatus(Response::HTTP_FORBIDDEN); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.