jonnybarnes.uk/tests/Feature/Admin/ArticlesTest.php
Jonny Barnes 1dfa17abca
Some checks failed
PHP Unit / PHPUnit test suite (pull_request) Has been cancelled
Laravel Pint / Laravel Pint (pull_request) Has been cancelled
Update Laravel to v12
2025-04-01 21:10:30 +01:00

122 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use App\Models\Article;
use App\Models\User;
use Faker\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class ArticlesTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function adminArticlesPageLoads(): void
{
$user = User::factory()->make();
$response = $this->actingAs($user)
->get('/admin/blog');
$response->assertSeeText('Select article to edit:');
}
#[Test]
public function adminCanLoadFormToCreateArticle(): void
{
$user = User::factory()->make();
$response = $this->actingAs($user)
->get('/admin/blog/create');
$response->assertSeeText('Title (URL)');
}
#[Test]
public function admiNCanCreateNewArticle(): void
{
$user = User::factory()->make();
$this->actingAs($user)
->post('/admin/blog', [
'title' => 'Test Title',
'main' => 'Article content',
]);
$this->assertDatabaseHas('articles', ['title' => 'Test Title']);
}
#[Test]
public function adminCanCreateNewArticleWithFile(): void
{
$user = User::factory()->make();
$faker = Factory::create();
$text = $faker->text;
if ($fh = fopen(sys_get_temp_dir() . '/article.md', 'w')) {
fwrite($fh, $text);
fclose($fh);
}
$path = sys_get_temp_dir() . '/article.md';
$file = new UploadedFile($path, 'article.md', 'text/plain', null, true);
$this->actingAs($user)
->post('/admin/blog', [
'title' => 'Uploaded Article',
'article' => $file,
]);
$this->assertDatabaseHas('articles', [
'title' => 'Uploaded Article',
'main' => $text,
]);
}
#[Test]
public function articleCanLoadFormToEditArticle(): void
{
$user = User::factory()->make();
$article = Article::factory()->create([
'main' => 'This is *my* new blog. It uses `Markdown`.',
]);
$response = $this->actingAs($user)
->get('/admin/blog/' . $article->id . '/edit');
$response->assertSeeText('This is *my* new blog. It uses `Markdown`.');
}
#[Test]
public function adminCanEditArticle(): void
{
$user = User::factory()->make();
$article = Article::factory()->create();
$this->actingAs($user)
->post('/admin/blog/' . $article->id, [
'_method' => 'PUT',
'title' => 'My New Blog',
'main' => 'This article has been edited',
]);
$this->assertDatabaseHas('articles', [
'title' => 'My New Blog',
'main' => 'This article has been edited',
]);
}
#[Test]
public function adminCanDeleteArticle(): void
{
$user = User::factory()->make();
$article = Article::factory()->create();
$this->actingAs($user)
->post('/admin/blog/' . $article->id, [
'_method' => 'DELETE',
]);
$this->assertSoftDeleted('articles', [
'title' => $article->title,
]);
}
}