fedi-feed-router/tests/Unit/Models/ArticleTest.php

197 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2025-08-03 20:59:09 +02:00
<?php
namespace Tests\Unit\Models;
use App\Events\ArticleApproved;
use App\Events\NewArticleFetched;
use App\Models\Article;
use App\Models\Feed;
use App\Models\Setting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
2025-08-10 04:16:53 +02:00
use Illuminate\Support\Facades\Http;
2025-08-03 20:59:09 +02:00
use Tests\TestCase;
class ArticleTest extends TestCase
{
use RefreshDatabase;
2025-08-10 04:16:53 +02:00
protected function setUp(): void
{
parent::setUp();
// Mock HTTP requests to prevent external calls
Http::fake([
'*' => Http::response('', 500)
]);
// Don't fake events globally - let individual tests control this
}
2025-08-10 21:18:20 +02:00
public function test_is_valid_returns_false_when_approval_status_is_pending(): void
2025-08-03 20:59:09 +02:00
{
$article = Article::factory()->make([
2025-08-10 21:18:20 +02:00
'approval_status' => 'pending',
2025-08-03 20:59:09 +02:00
]);
$this->assertFalse($article->isValid());
}
2025-08-10 21:18:20 +02:00
public function test_is_valid_returns_false_when_approval_status_is_rejected(): void
2025-08-03 20:59:09 +02:00
{
$article = Article::factory()->make([
2025-08-10 21:18:20 +02:00
'approval_status' => 'rejected',
2025-08-03 20:59:09 +02:00
]);
$this->assertFalse($article->isValid());
}
2025-08-10 21:18:20 +02:00
public function test_is_valid_returns_true_when_approval_status_is_approved(): void
2025-08-03 20:59:09 +02:00
{
$article = Article::factory()->make([
2025-08-10 21:18:20 +02:00
'approval_status' => 'approved',
2025-08-03 20:59:09 +02:00
]);
$this->assertTrue($article->isValid());
}
public function test_is_approved_returns_true_for_approved_status(): void
{
$article = Article::factory()->make(['approval_status' => 'approved']);
$this->assertTrue($article->isApproved());
}
public function test_is_approved_returns_false_for_non_approved_status(): void
{
$article = Article::factory()->make(['approval_status' => 'pending']);
$this->assertFalse($article->isApproved());
}
public function test_is_pending_returns_true_for_pending_status(): void
{
$article = Article::factory()->make(['approval_status' => 'pending']);
$this->assertTrue($article->isPending());
}
public function test_is_rejected_returns_true_for_rejected_status(): void
{
$article = Article::factory()->make(['approval_status' => 'rejected']);
$this->assertTrue($article->isRejected());
}
2025-08-10 21:18:20 +02:00
public function test_approve_updates_status_and_triggers_event(): void
2025-08-03 20:59:09 +02:00
{
$feed = Feed::factory()->create();
$article = Article::factory()->create([
'feed_id' => $feed->id,
'approval_status' => 'pending',
]);
Event::fake();
$article->approve('test_user');
$article->refresh();
$this->assertEquals('approved', $article->approval_status);
Event::assertDispatched(ArticleApproved::class, function ($event) use ($article) {
return $event->article->id === $article->id;
});
}
public function test_approve_without_approved_by_parameter(): void
{
$feed = Feed::factory()->create();
$article = Article::factory()->create([
'feed_id' => $feed->id,
'approval_status' => 'pending',
]);
Event::fake();
$article->approve();
$article->refresh();
$this->assertEquals('approved', $article->approval_status);
}
2025-08-10 21:18:20 +02:00
public function test_reject_updates_status(): void
2025-08-03 20:59:09 +02:00
{
$feed = Feed::factory()->create();
$article = Article::factory()->create([
'feed_id' => $feed->id,
'approval_status' => 'pending',
]);
$article->reject('test_user');
$article->refresh();
$this->assertEquals('rejected', $article->approval_status);
}
public function test_can_be_published_returns_false_for_invalid_article(): void
{
$article = Article::factory()->make([
2025-08-10 21:18:20 +02:00
'approval_status' => 'rejected', // rejected = not valid
2025-08-03 20:59:09 +02:00
]);
$this->assertFalse($article->canBePublished());
}
public function test_can_be_published_requires_approval_when_approvals_enabled(): void
{
// Create a setting that enables approvals
Setting::create(['key' => 'enable_publishing_approvals', 'value' => '1']);
2025-08-10 21:18:20 +02:00
$pendingArticle = Article::factory()->make([
2025-08-03 20:59:09 +02:00
'approval_status' => 'pending',
]);
2025-08-10 21:18:20 +02:00
$approvedArticle = Article::factory()->make([
2025-08-03 20:59:09 +02:00
'approval_status' => 'approved',
]);
2025-08-10 21:18:20 +02:00
$this->assertFalse($pendingArticle->canBePublished());
$this->assertTrue($approvedArticle->canBePublished());
2025-08-03 20:59:09 +02:00
}
public function test_can_be_published_returns_true_when_approvals_disabled(): void
{
// Make sure approvals are disabled (default behavior)
Setting::where('key', 'enable_publishing_approvals')->delete();
$article = Article::factory()->make([
2025-08-10 21:18:20 +02:00
'approval_status' => 'approved', // Only approved articles can be published
2025-08-03 20:59:09 +02:00
]);
$this->assertTrue($article->canBePublished());
}
public function test_feed_relationship(): void
{
$feed = Feed::factory()->create();
$article = Article::factory()->create(['feed_id' => $feed->id]);
$this->assertInstanceOf(Feed::class, $article->feed);
$this->assertEquals($feed->id, $article->feed->id);
}
public function test_article_creation_fires_new_article_fetched_event(): void
{
$eventFired = false;
2025-08-10 04:16:53 +02:00
// Listen for the event using a closure
2025-08-03 20:59:09 +02:00
Event::listen(NewArticleFetched::class, function ($event) use (&$eventFired) {
$eventFired = true;
});
2025-08-10 04:16:53 +02:00
2025-08-03 20:59:09 +02:00
$feed = Feed::factory()->create();
Article::factory()->create(['feed_id' => $feed->id]);
2025-08-10 04:16:53 +02:00
$this->assertTrue($eventFired, 'NewArticleFetched event was not fired');
2025-08-03 20:59:09 +02:00
}
}