197 lines
No EOL
5.8 KiB
PHP
197 lines
No EOL
5.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Models;
|
|
|
|
use Domains\Article\Events\ArticleApproved;
|
|
use Domains\Article\Events\NewArticleFetched;
|
|
use Domains\Article\Models\Article;
|
|
use Domains\Feed\Models\Feed;
|
|
use Domains\Settings\Models\Setting;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Tests\TestCase;
|
|
|
|
class ArticleTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
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
|
|
}
|
|
|
|
public function test_is_valid_returns_false_when_approval_status_is_pending(): void
|
|
{
|
|
$article = Article::factory()->make([
|
|
'approval_status' => 'pending',
|
|
]);
|
|
|
|
$this->assertFalse($article->isValid());
|
|
}
|
|
|
|
public function test_is_valid_returns_false_when_approval_status_is_rejected(): void
|
|
{
|
|
$article = Article::factory()->make([
|
|
'approval_status' => 'rejected',
|
|
]);
|
|
|
|
$this->assertFalse($article->isValid());
|
|
}
|
|
|
|
public function test_is_valid_returns_true_when_approval_status_is_approved(): void
|
|
{
|
|
$article = Article::factory()->make([
|
|
'approval_status' => 'approved',
|
|
]);
|
|
|
|
$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());
|
|
}
|
|
|
|
public function test_approve_updates_status_and_triggers_event(): void
|
|
{
|
|
$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);
|
|
}
|
|
|
|
public function test_reject_updates_status(): void
|
|
{
|
|
$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([
|
|
'approval_status' => 'rejected', // rejected = not valid
|
|
]);
|
|
|
|
$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']);
|
|
|
|
$pendingArticle = Article::factory()->make([
|
|
'approval_status' => 'pending',
|
|
]);
|
|
|
|
$approvedArticle = Article::factory()->make([
|
|
'approval_status' => 'approved',
|
|
]);
|
|
|
|
$this->assertFalse($pendingArticle->canBePublished());
|
|
$this->assertTrue($approvedArticle->canBePublished());
|
|
}
|
|
|
|
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([
|
|
'approval_status' => 'approved', // Only approved articles can be published
|
|
]);
|
|
|
|
$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;
|
|
|
|
// Listen for the event using a closure
|
|
Event::listen(NewArticleFetched::class, function ($event) use (&$eventFired) {
|
|
$eventFired = true;
|
|
});
|
|
|
|
$feed = Feed::factory()->create();
|
|
Article::factory()->create(['feed_id' => $feed->id]);
|
|
|
|
$this->assertTrue($eventFired, 'NewArticleFetched event was not fired');
|
|
}
|
|
} |