Skip to content

Commit bb37257

Browse files
committed
Scope series to user
1 parent c028415 commit bb37257

File tree

5 files changed

+120
-9
lines changed

5 files changed

+120
-9
lines changed

app/Http/Controllers/Articles/ArticlesController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use App\Models\Series;
1414
use App\Models\Tag;
1515
use App\Policies\ArticlePolicy;
16+
use Illuminate\Http\Request;
1617

1718
class ArticlesController extends Controller
1819
{
@@ -32,11 +33,11 @@ public function show(Article $article)
3233
]);
3334
}
3435

35-
public function create()
36+
public function create(Request $request)
3637
{
3738
$tags = Tag::all();
3839
$selectedTags = old('tags', []);
39-
$series = Series::all();
40+
$series = $request->user()->series;
4041
$selectedSeries = old('series');
4142

4243
return view('articles.create', [
@@ -56,13 +57,13 @@ public function store(ArticleRequest $request)
5657
return redirect()->route('articles.show', $article->slug());
5758
}
5859

59-
public function edit(Article $article)
60+
public function edit(Request $request, Article $article)
6061
{
6162
$this->authorize(ArticlePolicy::UPDATE, $article);
6263

6364
$selectedTags = old('tags', $article->tags()->pluck('id')->toArray());
64-
$series = Series::all();
65-
$selectedSeries = old('series', $article->series->id);
65+
$series = $request->user()->series;
66+
$selectedSeries = old('series', $article->series_id);
6667

6768
return view('articles.edit', [
6869
'article' => $article,

app/Http/Requests/ArticleRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Requests;
44

5+
use App\Rules\AuthorOwnsSeriesRule;
56
use App\Rules\HttpImageRule;
67
use App\User;
78

@@ -14,8 +15,8 @@ public function rules()
1415
'body' => ['required', new HttpImageRule],
1516
'tags' => 'array|nullable',
1617
'tags.*' => 'exists:tags,id',
17-
'series' => 'exists:series,id|nullable',
1818
'original_url' => 'url|nullable',
19+
'series' => ['nullable', new AuthorOwnsSeriesRule],
1920
];
2021
}
2122

app/Rules/AuthorOwnsSeriesRule.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Rules;
4+
5+
use App\Models\Series;
6+
use Illuminate\Contracts\Validation\Rule;
7+
use Illuminate\Support\Facades\Auth;
8+
use Illuminate\Validation\Concerns\ValidatesAttributes;
9+
10+
final class AuthorOwnsSeriesRule implements Rule
11+
{
12+
use ValidatesAttributes;
13+
14+
public function passes($attribute, $value): bool
15+
{
16+
return Series::where('author_id', Auth::id())->exists();
17+
}
18+
19+
public function message(): string
20+
{
21+
return 'The :attribute field does not belong to you.';
22+
}
23+
}

app/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Helpers\HasTimestamps;
66
use App\Helpers\ModelHelpers;
77
use App\Models\Reply;
8+
use App\Models\Series;
89
use App\Models\Thread;
910
use Illuminate\Database\Eloquent\Relations\HasMany;
1011
use Illuminate\Foundation\Auth\User as Authenticatable;
@@ -214,6 +215,11 @@ public function replyAble(): HasMany
214215
return $this->hasMany(Reply::class, 'author_id');
215216
}
216217

218+
public function series(): HasMany
219+
{
220+
return $this->hasMany(Series::class, 'author_id');
221+
}
222+
217223
/**
218224
* @todo Make this work with Eloquent instead of a collection
219225
*/

tests/Feature/ArticleTest.php

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,31 @@ public function users_cannot_create_an_article_when_not_logged_in()
1818
->seePageIs('/login');
1919
}
2020

21+
/** @test */
22+
public function users_cannot_see_series_they_do_not_own_when_creating_a_series()
23+
{
24+
$user = $this->createUser();
25+
factory(Series::class)->create(['title' => 'This should be seen', 'author_id' => $user->id]);
26+
factory(Series::class)->create(['title' => 'This should not be seen']);
27+
28+
$this->loginAs($user);
29+
30+
$this->get('/articles/create')
31+
->see('This should be seen')
32+
->dontSee('This should not be seen');
33+
}
34+
2135
/** @test */
2236
public function users_can_create_an_article()
2337
{
38+
$user = $this->createUser();
2439
$tag = factory(Tag::class)->create(['name' => 'Test Tag']);
25-
$series = factory(Series::class)->create(['title' => 'Test series']);
40+
$series = factory(Series::class)->create([
41+
'title' => 'Test series',
42+
'author_id' => $user->id,
43+
]);
2644

27-
$this->login();
45+
$this->loginAs($user);
2846

2947
$this->post('/articles', [
3048
'title' => 'Using database migrations',
@@ -36,6 +54,25 @@ public function users_can_create_an_article()
3654
->assertSessionHas('success', 'Article successfully created!');
3755
}
3856

57+
/** @test */
58+
public function users_cannot_create_an_article_using_a_series_they_do_not_own()
59+
{
60+
$tag = factory(Tag::class)->create(['name' => 'Test Tag']);
61+
$series = factory(Series::class)->create(['title' => 'Test series']);
62+
63+
$this->login();
64+
65+
$response = $this->post('/articles', [
66+
'title' => 'Using database migrations',
67+
'body' => 'This article will go into depth on working with database migrations.',
68+
'tags' => [$tag->id()],
69+
'series' => $series->id(),
70+
]);
71+
72+
$response->assertSessionHas('error', 'Something went wrong. Please review the fields below.');
73+
$response->assertSessionHasErrors(['series' => 'The series field does not belong to you.']);
74+
}
75+
3976
/** @test */
4077
public function users_cannot_create_an_article_with_a_title_that_is_too_long()
4178
{
@@ -63,12 +100,30 @@ public function an_article_may_not_contain_an_http_image_url()
63100
$response->assertSessionHasErrors(['body' => 'The body field contains at least one image with an HTTP link.']);
64101
}
65102

103+
/** @test */
104+
public function users_cannot_see_series_they_do_not_own_when_editing_an_article()
105+
{
106+
$user = $this->createUser();
107+
factory(Article::class)->create(['slug' => 'my-first-article', 'author_id' => $user->id]);
108+
factory(Series::class)->create(['title' => 'This should be seen', 'author_id' => $user->id]);
109+
factory(Series::class)->create(['title' => 'This should not be seen']);
110+
111+
$this->loginAs($user);
112+
113+
$this->get('/articles/my-first-article/edit')
114+
->see('This should be seen')
115+
->dontSee('This should not be seen');
116+
}
117+
66118
/** @test */
67119
public function users_can_edit_an_article()
68120
{
69121
$user = $this->createUser();
70122
$tag = factory(Tag::class)->create(['name' => 'Test Tag']);
71-
$series = factory(Series::class)->create(['title' => 'Test series']);
123+
$series = factory(Series::class)->create([
124+
'title' => 'Test series',
125+
'author_id' => $user->id,
126+
]);
72127

73128
factory(Article::class)->create([
74129
'author_id' => $user->id(),
@@ -87,6 +142,31 @@ public function users_can_edit_an_article()
87142
->assertSessionHas('success', 'Article successfully updated!');
88143
}
89144

145+
/** @test */
146+
public function users_cannot_edit_an_article_using_a_series_they_do_not_own()
147+
{
148+
$user = $this->createUser();
149+
$tag = factory(Tag::class)->create(['name' => 'Test Tag']);
150+
$series = factory(Series::class)->create(['title' => 'Test series']);
151+
152+
factory(Article::class)->create([
153+
'author_id' => $user->id(),
154+
'slug' => 'my-first-article',
155+
]);
156+
157+
$this->loginAs($user);
158+
159+
$response = $this->put('/articles/my-first-article', [
160+
'title' => 'Using database migrations',
161+
'body' => 'This article will go into depth on working with database migrations.',
162+
'tags' => [$tag->id()],
163+
'series' => $series->id(),
164+
]);
165+
166+
$response->assertSessionHas('error', 'Something went wrong. Please review the fields below.');
167+
$response->assertSessionHasErrors(['series' => 'The series field does not belong to you.']);
168+
}
169+
90170
/** @test */
91171
public function users_cannot_edit_an_article_with_a_title_that_is_too_long()
92172
{

0 commit comments

Comments
 (0)