Skip to content

Commit 45bf394

Browse files
committed
Delete articles
1 parent 971b1e6 commit 45bf394

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

app/Http/Controllers/Articles/ArticlesController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Http\Middleware\RedirectIfUnconfirmed;
88
use App\Http\Requests\ArticleRequest;
99
use App\Jobs\CreateArticle;
10+
use App\Jobs\DeleteArticle;
1011
use App\Jobs\UpdateArticle;
1112
use App\Models\Article;
1213
use App\Models\Tag;
@@ -68,5 +69,12 @@ public function update(ArticleRequest $request, Article $article)
6869

6970
public function delete(Article $article)
7071
{
72+
$this->authorize(ArticlePolicy::DELETE, $article);
73+
74+
$this->dispatchNow(new DeleteArticle($article));
75+
76+
$this->success('articles.deleted');
77+
78+
return redirect()->route('articles');
7179
}
7280
}

app/Jobs/DeleteArticle.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Article;
6+
7+
final class DeleteArticle
8+
{
9+
private $article;
10+
11+
public function __construct(Article $article)
12+
{
13+
$this->article = $article;
14+
}
15+
16+
public function handle()
17+
{
18+
$this->article->delete();
19+
}
20+
}

resources/lang/en/articles.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
'created' => 'Article successfully created!',
66
'updated' => 'Article successfully updated!',
7+
'deleted' => 'Article successfully deleted!',
78

89
];

tests/Feature/ArticleTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,31 @@ public function an_article_may_not_updated_to_contain_an_http_image_url()
119119
$response->assertSessionHas('error', 'Something went wrong. Please review the fields below.');
120120
$response->assertSessionHasErrors(['body' => 'The body field contains at least one image with an HTTP link.']);
121121
}
122+
123+
/** @test */
124+
public function users_can_delete_their_own_articles()
125+
{
126+
$user = $this->createUser();
127+
factory(Article::class)->create([
128+
'author_id' => $user->id(),
129+
'slug' => 'my-first-article',
130+
]);
131+
132+
$this->loginAs($user);
133+
134+
$this->delete('/articles/my-first-article')
135+
->assertRedirectedTo('/articles')
136+
->assertSessionHas('success', 'Article successfully deleted!');
137+
}
138+
139+
/** @test */
140+
public function users_cannot_delete_an_article_they_do_not_own()
141+
{
142+
factory(Article::class)->create(['slug' => 'my-first-thread']);
143+
144+
$this->login();
145+
146+
$this->delete('/articles/my-first-thread')
147+
->assertForbidden();
148+
}
122149
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tests\Integration\Jobs;
4+
5+
use App\Jobs\DeleteArticle;
6+
use App\Models\Article;
7+
use Illuminate\Foundation\Testing\DatabaseMigrations;
8+
use Tests\TestCase;
9+
10+
class DeleteArticleTest extends TestCase
11+
{
12+
use DatabaseMigrations;
13+
14+
/** @test */
15+
public function an_article_can_be_deleted()
16+
{
17+
$article = factory(Article::class)->create();
18+
19+
$this->dispatch(new DeleteArticle($article));
20+
21+
$this->assertDatabaseMissing('articles', ['id' => $article->id()]);
22+
}
23+
}

0 commit comments

Comments
 (0)