2025-08-06 21:49:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Publishing;
|
|
|
|
|
|
|
|
|
|
use App\Enums\PlatformEnum;
|
|
|
|
|
use App\Models\Article;
|
|
|
|
|
use App\Models\Feed;
|
|
|
|
|
use App\Models\PlatformAccount;
|
|
|
|
|
use App\Models\PlatformChannel;
|
2026-02-25 23:22:05 +01:00
|
|
|
use App\Models\PlatformChannelPost;
|
2025-08-06 21:49:13 +02:00
|
|
|
use App\Models\PlatformInstance;
|
2025-08-10 16:18:09 +02:00
|
|
|
use App\Models\Route;
|
2026-03-18 17:00:56 +01:00
|
|
|
use App\Models\RouteArticle;
|
2025-08-06 21:49:13 +02:00
|
|
|
use App\Modules\Lemmy\Services\LemmyPublisher;
|
|
|
|
|
use App\Services\Log\LogSaver;
|
|
|
|
|
use App\Services\Publishing\ArticlePublishingService;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
use Mockery;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class ArticlePublishingServiceTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
protected ArticlePublishingService $service;
|
2026-03-08 14:18:28 +01:00
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
protected LogSaver $logSaver;
|
2025-08-06 21:49:13 +02:00
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
2025-08-15 02:50:42 +02:00
|
|
|
$this->logSaver = Mockery::mock(LogSaver::class);
|
|
|
|
|
$this->logSaver->shouldReceive('info')->zeroOrMoreTimes();
|
|
|
|
|
$this->logSaver->shouldReceive('warning')->zeroOrMoreTimes();
|
|
|
|
|
$this->logSaver->shouldReceive('error')->zeroOrMoreTimes();
|
|
|
|
|
$this->logSaver->shouldReceive('debug')->zeroOrMoreTimes();
|
|
|
|
|
$this->service = new ArticlePublishingService($this->logSaver);
|
2025-08-06 21:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
|
{
|
|
|
|
|
Mockery::close();
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
/**
|
|
|
|
|
* @return array{RouteArticle, PlatformChannel, PlatformAccount, Article}
|
|
|
|
|
*/
|
|
|
|
|
private function createRouteArticleWithAccount(): array
|
2025-08-06 21:49:13 +02:00
|
|
|
{
|
|
|
|
|
$feed = Feed::factory()->create();
|
2025-08-10 16:18:09 +02:00
|
|
|
$platformInstance = PlatformInstance::factory()->create();
|
|
|
|
|
$channel = PlatformChannel::factory()->create(['platform_instance_id' => $platformInstance->id]);
|
|
|
|
|
$account = PlatformAccount::factory()->create();
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
/** @var Route $route */
|
|
|
|
|
$route = Route::factory()->active()->create([
|
2025-08-10 16:18:09 +02:00
|
|
|
'feed_id' => $feed->id,
|
|
|
|
|
'platform_channel_id' => $channel->id,
|
|
|
|
|
]);
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2025-08-10 16:18:09 +02:00
|
|
|
$channel->platformAccounts()->attach($account->id, [
|
|
|
|
|
'is_active' => true,
|
2026-03-08 14:18:28 +01:00
|
|
|
'priority' => 50,
|
2025-08-10 16:18:09 +02:00
|
|
|
]);
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$article = Article::factory()->create(['feed_id' => $feed->id]);
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
/** @var RouteArticle $routeArticle */
|
|
|
|
|
$routeArticle = RouteArticle::factory()->forRoute($route)->approved()->create([
|
2025-08-10 01:26:56 +02:00
|
|
|
'article_id' => $article->id,
|
|
|
|
|
]);
|
2026-03-18 17:00:56 +01:00
|
|
|
|
|
|
|
|
return [$routeArticle, $channel, $account, $article];
|
2025-08-06 21:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
public function test_publish_route_article_returns_null_when_no_active_account(): void
|
2025-08-06 21:49:13 +02:00
|
|
|
{
|
2025-08-10 01:26:56 +02:00
|
|
|
$feed = Feed::factory()->create();
|
2026-03-18 17:00:56 +01:00
|
|
|
$channel = PlatformChannel::factory()->create();
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
/** @var Route $route */
|
|
|
|
|
$route = Route::factory()->active()->create([
|
2025-08-10 16:18:09 +02:00
|
|
|
'feed_id' => $feed->id,
|
|
|
|
|
'platform_channel_id' => $channel->id,
|
|
|
|
|
]);
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$article = Article::factory()->create(['feed_id' => $feed->id]);
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
/** @var RouteArticle $routeArticle */
|
|
|
|
|
$routeArticle = RouteArticle::factory()->forRoute($route)->approved()->create([
|
|
|
|
|
'article_id' => $article->id,
|
|
|
|
|
]);
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$result = $this->service->publishRouteArticle($routeArticle, ['title' => 'Test']);
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$this->assertNull($result);
|
2025-08-10 01:26:56 +02:00
|
|
|
$this->assertDatabaseCount('article_publications', 0);
|
2025-08-06 21:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
public function test_publish_route_article_successfully_publishes(): void
|
2025-08-06 21:49:13 +02:00
|
|
|
{
|
2026-03-18 17:00:56 +01:00
|
|
|
[$routeArticle, $channel, $account, $article] = $this->createRouteArticleWithAccount();
|
2025-08-10 16:18:09 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$publisherDouble = Mockery::mock(LemmyPublisher::class);
|
2025-08-10 01:26:56 +02:00
|
|
|
$publisherDouble->shouldReceive('publishToChannel')
|
2026-03-18 17:00:56 +01:00
|
|
|
->once()
|
|
|
|
|
->andReturn(['post_view' => ['post' => ['id' => 123]]]);
|
|
|
|
|
|
|
|
|
|
$service = Mockery::mock(ArticlePublishingService::class, [$this->logSaver])->makePartial();
|
2025-08-10 01:26:56 +02:00
|
|
|
$service->shouldAllowMockingProtectedMethods();
|
|
|
|
|
$service->shouldReceive('makePublisher')->andReturn($publisherDouble);
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$result = $service->publishRouteArticle($routeArticle, ['title' => 'Hello']);
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$this->assertNotNull($result);
|
|
|
|
|
$this->assertDatabaseHas('article_publications', [
|
|
|
|
|
'article_id' => $article->id,
|
|
|
|
|
'platform_channel_id' => $channel->id,
|
|
|
|
|
'post_id' => 123,
|
|
|
|
|
'published_by' => $account->username,
|
|
|
|
|
]);
|
2025-08-06 21:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
public function test_publish_route_article_handles_publishing_failure_gracefully(): void
|
2025-08-06 21:49:13 +02:00
|
|
|
{
|
2026-03-18 17:00:56 +01:00
|
|
|
[$routeArticle] = $this->createRouteArticleWithAccount();
|
2025-08-10 16:18:09 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$publisherDouble = Mockery::mock(LemmyPublisher::class);
|
2025-08-10 01:26:56 +02:00
|
|
|
$publisherDouble->shouldReceive('publishToChannel')
|
2026-03-18 17:00:56 +01:00
|
|
|
->once()
|
|
|
|
|
->andThrow(new Exception('network error'));
|
|
|
|
|
|
|
|
|
|
$service = Mockery::mock(ArticlePublishingService::class, [$this->logSaver])->makePartial();
|
2025-08-10 01:26:56 +02:00
|
|
|
$service->shouldAllowMockingProtectedMethods();
|
|
|
|
|
$service->shouldReceive('makePublisher')->andReturn($publisherDouble);
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$result = $service->publishRouteArticle($routeArticle, ['title' => 'Hello']);
|
2025-08-10 01:26:56 +02:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$this->assertNull($result);
|
|
|
|
|
$this->assertDatabaseCount('article_publications', 0);
|
2025-08-06 21:49:13 +02:00
|
|
|
}
|
2026-02-25 23:22:05 +01:00
|
|
|
|
|
|
|
|
public function test_publish_skips_duplicate_when_url_already_posted_to_channel(): void
|
|
|
|
|
{
|
2026-03-18 17:00:56 +01:00
|
|
|
[$routeArticle, $channel, $account, $article] = $this->createRouteArticleWithAccount();
|
2026-03-18 16:23:46 +01:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
// Simulate the URL already being posted to this channel
|
2026-02-25 23:22:05 +01:00
|
|
|
PlatformChannelPost::storePost(
|
|
|
|
|
PlatformEnum::LEMMY,
|
|
|
|
|
(string) $channel->channel_id,
|
|
|
|
|
$channel->name,
|
|
|
|
|
'999',
|
2026-03-18 17:00:56 +01:00
|
|
|
$article->url,
|
2026-02-25 23:22:05 +01:00
|
|
|
'Different Title',
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$publisherDouble = Mockery::mock(LemmyPublisher::class);
|
2026-02-25 23:22:05 +01:00
|
|
|
$publisherDouble->shouldNotReceive('publishToChannel');
|
2026-03-18 17:00:56 +01:00
|
|
|
|
|
|
|
|
$service = Mockery::mock(ArticlePublishingService::class, [$this->logSaver])->makePartial();
|
2026-02-25 23:22:05 +01:00
|
|
|
$service->shouldAllowMockingProtectedMethods();
|
|
|
|
|
$service->shouldReceive('makePublisher')->andReturn($publisherDouble);
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$result = $service->publishRouteArticle($routeArticle, ['title' => 'Some Title']);
|
2026-02-25 23:22:05 +01:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$this->assertNull($result);
|
2026-02-25 23:22:05 +01:00
|
|
|
$this->assertDatabaseCount('article_publications', 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_publish_skips_duplicate_when_title_already_posted_to_channel(): void
|
|
|
|
|
{
|
2026-03-18 17:00:56 +01:00
|
|
|
[$routeArticle, $channel, $account, $article] = $this->createRouteArticleWithAccount();
|
2026-02-25 23:22:05 +01:00
|
|
|
|
|
|
|
|
// Simulate the same title already posted with a different URL
|
|
|
|
|
PlatformChannelPost::storePost(
|
|
|
|
|
PlatformEnum::LEMMY,
|
|
|
|
|
(string) $channel->channel_id,
|
|
|
|
|
$channel->name,
|
|
|
|
|
'888',
|
|
|
|
|
'https://example.com/different-url',
|
2026-03-18 17:00:56 +01:00
|
|
|
'Breaking News',
|
2026-02-25 23:22:05 +01:00
|
|
|
);
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$publisherDouble = Mockery::mock(LemmyPublisher::class);
|
2026-02-25 23:22:05 +01:00
|
|
|
$publisherDouble->shouldNotReceive('publishToChannel');
|
2026-03-18 17:00:56 +01:00
|
|
|
|
|
|
|
|
$service = Mockery::mock(ArticlePublishingService::class, [$this->logSaver])->makePartial();
|
2026-02-25 23:22:05 +01:00
|
|
|
$service->shouldAllowMockingProtectedMethods();
|
|
|
|
|
$service->shouldReceive('makePublisher')->andReturn($publisherDouble);
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$result = $service->publishRouteArticle($routeArticle, ['title' => 'Breaking News']);
|
2026-02-25 23:22:05 +01:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$this->assertNull($result);
|
2026-02-25 23:22:05 +01:00
|
|
|
$this->assertDatabaseCount('article_publications', 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_publish_proceeds_when_no_duplicate_exists(): void
|
|
|
|
|
{
|
2026-03-18 17:00:56 +01:00
|
|
|
[$routeArticle, $channel, $account, $article] = $this->createRouteArticleWithAccount();
|
2026-02-25 23:22:05 +01:00
|
|
|
|
|
|
|
|
// Existing post in the channel has a completely different URL and title
|
|
|
|
|
PlatformChannelPost::storePost(
|
|
|
|
|
PlatformEnum::LEMMY,
|
|
|
|
|
(string) $channel->channel_id,
|
|
|
|
|
$channel->name,
|
|
|
|
|
'777',
|
|
|
|
|
'https://example.com/other-article',
|
|
|
|
|
'Totally Different Title',
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$publisherDouble = Mockery::mock(LemmyPublisher::class);
|
2026-02-25 23:22:05 +01:00
|
|
|
$publisherDouble->shouldReceive('publishToChannel')
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn(['post_view' => ['post' => ['id' => 456]]]);
|
2026-03-18 17:00:56 +01:00
|
|
|
|
|
|
|
|
$service = Mockery::mock(ArticlePublishingService::class, [$this->logSaver])->makePartial();
|
2026-02-25 23:22:05 +01:00
|
|
|
$service->shouldAllowMockingProtectedMethods();
|
|
|
|
|
$service->shouldReceive('makePublisher')->andReturn($publisherDouble);
|
|
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$result = $service->publishRouteArticle($routeArticle, ['title' => 'Unique Title']);
|
2026-02-25 23:22:05 +01:00
|
|
|
|
2026-03-18 17:00:56 +01:00
|
|
|
$this->assertNotNull($result);
|
2026-02-25 23:22:05 +01:00
|
|
|
$this->assertDatabaseHas('article_publications', [
|
|
|
|
|
'article_id' => $article->id,
|
|
|
|
|
'post_id' => 456,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2025-08-10 01:26:56 +02:00
|
|
|
}
|