1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Writing;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class WritingTest extends TestCase
{
use RefreshDatabase;
// ─── Index ───────────────────────────────────────────────────────────────
public function test_guests_can_view_index(): void
{
$response = $this->get(route('w.index'));
$response->assertOk();
}
public function test_index_lists_writings(): void
{
$writing = Writing::factory()->create();
$response = $this->get(route('w.index'));
$response->assertSee($writing->title);
}
// ─── Show ─────────────────────────────────────────────────────────────────
public function test_guests_can_view_a_writing(): void
{
$writing = Writing::factory()->create();
$response = $this->get(route('w.show', $writing));
$response->assertOk()->assertSee($writing->title);
}
public function test_show_404s_for_missing_writing(): void
{
$response = $this->get(route('w.show', 99999));
$response->assertNotFound();
}
// ─── Create ───────────────────────────────────────────────────────────────
public function test_guest_is_redirected_from_create(): void
{
$response = $this->get(route('w.create'));
$response->assertRedirect(route('login'));
}
public function test_authenticated_user_can_see_create_form(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get(route('w.create'));
$response->assertOk();
}
// ─── Store ────────────────────────────────────────────────────────────────
public function test_guest_cannot_store_writing(): void
{
$response = $this->post(route('w.store'), [
'title' => 'Guest Writing',
'content' => 'Should not be stored at all.',
]);
$response->assertRedirect(route('login'));
$this->assertDatabaseMissing('writings', ['title' => 'Guest Writing']);
}
public function test_user_can_store_writing(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('w.store'), [
'title' => 'My New Writing',
'content' => 'This is content that is long enough to pass validation.',
]);
$response->assertRedirect();
$this->assertDatabaseHas('writings', [
'title' => 'My New Writing',
'user_id' => $user->id,
]);
}
public function test_store_requires_title(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('w.store'), [
'title' => '',
'content' => 'Content long enough to pass.',
]);
$response->assertSessionHasErrors('title');
}
public function test_store_requires_title_min_3_chars(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('w.store'), [
'title' => 'AB',
'content' => 'Content long enough to pass.',
]);
$response->assertSessionHasErrors('title');
}
public function test_store_requires_content(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('w.store'), [
'title' => 'Valid Title',
'content' => '',
]);
$response->assertSessionHasErrors('content');
}
public function test_store_requires_content_min_10_chars(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('w.store'), [
'title' => 'Valid Title',
'content' => 'Too short',
]);
$response->assertSessionHasErrors('content');
}
// ─── Edit ─────────────────────────────────────────────────────────────────
public function test_guest_is_redirected_from_edit(): void
{
$writing = Writing::factory()->create();
$response = $this->get(route('w.edit', $writing));
$response->assertRedirect(route('login'));
}
public function test_owner_can_access_edit_form(): void
{
$user = User::factory()->create();
$writing = Writing::factory()->create(['user_id' => $user->id]);
$response = $this->actingAs($user)->get(route('w.edit', $writing));
$response->assertOk();
}
public function test_non_owner_cannot_access_edit_form(): void
{
$owner = User::factory()->create();
$other = User::factory()->create();
$writing = Writing::factory()->create(['user_id' => $owner->id]);
$response = $this->actingAs($other)->get(route('w.edit', $writing));
$response->assertForbidden();
}
public function test_admin_can_access_any_edit_form(): void
{
$owner = User::factory()->create();
$admin = User::factory()->create(['role' => 0]);
$writing = Writing::factory()->create(['user_id' => $owner->id]);
$response = $this->actingAs($admin)->get(route('w.edit', $writing));
$response->assertOk();
}
// ─── Update ───────────────────────────────────────────────────────────────
public function test_owner_can_update_writing(): void
{
$user = User::factory()->create();
$writing = Writing::factory()->create(['user_id' => $user->id]);
$response = $this->actingAs($user)->put(route('w.update', $writing), [
'title' => 'Updated Title',
'content' => 'Updated content that is long enough to pass validation.',
]);
$response->assertRedirect(route('w.show', $writing));
$this->assertDatabaseHas('writings', [
'id' => $writing->id,
'title' => 'Updated Title',
]);
}
public function test_non_owner_cannot_update_writing(): void
{
$owner = User::factory()->create();
$other = User::factory()->create();
$writing = Writing::factory()->create(['user_id' => $owner->id, 'title' => 'Original Title']);
$response = $this->actingAs($other)->put(route('w.update', $writing), [
'title' => 'Hijacked Title',
'content' => 'Hijacked content that is long enough.',
]);
$response->assertForbidden();
$this->assertDatabaseHas('writings', ['id' => $writing->id, 'title' => 'Original Title']);
}
public function test_admin_can_update_any_writing(): void
{
$owner = User::factory()->create();
$admin = User::factory()->create(['role' => 0]);
$writing = Writing::factory()->create(['user_id' => $owner->id]);
$response = $this->actingAs($admin)->put(route('w.update', $writing), [
'title' => 'Admin Updated Title',
'content' => 'Admin updated content that is long enough.',
]);
$response->assertRedirect(route('w.show', $writing));
$this->assertDatabaseHas('writings', ['id' => $writing->id, 'title' => 'Admin Updated Title']);
}
public function test_update_validates_title(): void
{
$user = User::factory()->create();
$writing = Writing::factory()->create(['user_id' => $user->id]);
$response = $this->actingAs($user)->put(route('w.update', $writing), [
'title' => 'AB',
'content' => 'Content long enough to pass.',
]);
$response->assertSessionHasErrors('title');
}
// ─── Destroy ─────────────────────────────────────────────────────────────
public function test_guest_cannot_delete_writing(): void
{
$writing = Writing::factory()->create();
$response = $this->delete(route('w.destroy', $writing));
$response->assertRedirect(route('login'));
$this->assertDatabaseHas('writings', ['id' => $writing->id]);
}
public function test_owner_can_delete_writing(): void
{
$user = User::factory()->create();
$writing = Writing::factory()->create(['user_id' => $user->id]);
$response = $this->actingAs($user)->delete(route('w.destroy', $writing));
$response->assertRedirect(route('w.index'));
$this->assertDatabaseMissing('writings', ['id' => $writing->id]);
}
public function test_non_owner_cannot_delete_writing(): void
{
$owner = User::factory()->create();
$other = User::factory()->create();
$writing = Writing::factory()->create(['user_id' => $owner->id]);
$response = $this->actingAs($other)->delete(route('w.destroy', $writing));
$response->assertForbidden();
$this->assertDatabaseHas('writings', ['id' => $writing->id]);
}
public function test_admin_can_delete_any_writing(): void
{
$owner = User::factory()->create();
$admin = User::factory()->create(['role' => 0]);
$writing = Writing::factory()->create(['user_id' => $owner->id]);
$response = $this->actingAs($admin)->delete(route('w.destroy', $writing));
$response->assertRedirect(route('w.index'));
$this->assertDatabaseMissing('writings', ['id' => $writing->id]);
}
}
|