fedi-feed-router/backend/tests/Unit/Services/LanguageServiceTest.php

226 lines
7.9 KiB
PHP
Raw Permalink Normal View History

2025-08-16 10:47:50 +02:00
<?php
namespace Tests\Unit\Services;
use Domains\Settings\Services\LanguageService;
use Domains\Settings\Models\Language;
use Domains\Feed\Models\Feed;
use Domains\Platform\Models\PlatformChannel;
use Domains\Platform\Models\PlatformInstance;
use Domains\Feed\Services\LanguageConsistencyValidator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class LanguageServiceTest extends TestCase
{
use RefreshDatabase;
private LanguageService $languageService;
private Language $language;
private Feed $feed;
private PlatformChannel $channel;
protected function setUp(): void
{
parent::setUp();
$this->languageService = app(LanguageService::class);
// Create test data
$this->language = Language::factory()->create(['is_active' => true]);
$this->feed = Feed::factory()->create(['is_active' => true]);
$platformInstance = PlatformInstance::factory()->create();
$this->channel = PlatformChannel::factory()->create([
'platform_instance_id' => $platformInstance->id,
'language_id' => $this->language->id,
'is_active' => true
]);
// Create feed-language relationship
$this->feed->languages()->attach($this->language->id, [
'url' => $this->feed->url,
'is_active' => true,
'is_primary' => true
]);
}
public function test_get_available_for_routes_returns_languages_with_feeds_and_channels(): void
{
$languages = $this->languageService->getAvailableForRoutes();
$this->assertCount(1, $languages);
$this->assertEquals($this->language->id, $languages->first()->id);
}
public function test_get_feeds_by_language_returns_paginated_results(): void
{
$result = $this->languageService->getFeedsByLanguage($this->language->id);
$this->assertInstanceOf(\Illuminate\Pagination\LengthAwarePaginator::class, $result);
$this->assertCount(1, $result->items());
$this->assertEquals($this->feed->id, $result->items()[0]->id);
}
public function test_get_feeds_by_language_with_search(): void
{
$result = $this->languageService->getFeedsByLanguage(
$this->language->id,
$this->feed->name
);
$this->assertCount(1, $result->items());
// Test search that shouldn't match
$result = $this->languageService->getFeedsByLanguage(
$this->language->id,
'nonexistent'
);
$this->assertCount(0, $result->items());
}
public function test_get_channels_by_language_returns_paginated_results(): void
{
$result = $this->languageService->getChannelsByLanguage($this->language->id);
$this->assertInstanceOf(\Illuminate\Pagination\LengthAwarePaginator::class, $result);
$this->assertCount(1, $result->items());
$this->assertEquals($this->channel->id, $result->items()[0]->id);
}
public function test_get_language_statistics(): void
{
$stats = $this->languageService->getLanguageStatistics($this->language->id);
$this->assertEquals(1, $stats->active_feeds_count);
$this->assertEquals(1, $stats->active_channels_count);
$this->assertEquals(0, $stats->active_routes_count);
}
public function test_get_common_languages_between_feed_and_channel(): void
{
$commonLanguages = $this->languageService->getCommonLanguages(
$this->feed->id,
$this->channel->id
);
$this->assertCount(1, $commonLanguages);
$this->assertEquals($this->language->id, $commonLanguages[0]['id']);
}
public function test_get_common_languages_with_no_match(): void
{
// Create another language and channel that doesn't match the feed
$otherLanguage = Language::factory()->create(['is_active' => true]);
$platformInstance = PlatformInstance::factory()->create();
$otherChannel = PlatformChannel::factory()->create([
'platform_instance_id' => $platformInstance->id,
'language_id' => $otherLanguage->id,
'is_active' => true
]);
$commonLanguages = $this->languageService->getCommonLanguages(
$this->feed->id,
$otherChannel->id
);
$this->assertCount(0, $commonLanguages);
}
public function test_can_language_be_used_for_routes(): void
{
$canBeUsed = $this->languageService->canLanguageBeUsedForRoutes($this->language->id);
$this->assertTrue($canBeUsed);
// Test with language that has no feeds or channels
$unusableLanguage = Language::factory()->create(['is_active' => true]);
$canBeUsed = $this->languageService->canLanguageBeUsedForRoutes($unusableLanguage->id);
$this->assertFalse($canBeUsed);
}
public function test_get_language_usage_summary(): void
{
$summary = $this->languageService->getLanguageUsageSummary();
$this->assertIsArray($summary);
$this->assertGreaterThanOrEqual(1, count($summary));
$languageData = collect($summary)->where('id', $this->language->id)->first();
$this->assertNotNull($languageData);
$this->assertEquals(1, $languageData['active_feeds_count']);
$this->assertEquals(1, $languageData['active_channels_count']);
$this->assertEquals(1, $languageData['can_create_routes']);
}
public function test_find_optimal_language(): void
{
$optimalLanguage = $this->languageService->findOptimalLanguage(
$this->feed->id,
$this->channel->id
);
$this->assertNotNull($optimalLanguage);
$this->assertEquals($this->language->id, $optimalLanguage['id']);
}
public function test_find_optimal_language_with_no_common_languages(): void
{
// Create another language and channel that doesn't match the feed
$otherLanguage = Language::factory()->create(['is_active' => true]);
$platformInstance = PlatformInstance::factory()->create();
$otherChannel = PlatformChannel::factory()->create([
'platform_instance_id' => $platformInstance->id,
'language_id' => $otherLanguage->id,
'is_active' => true
]);
$optimalLanguage = $this->languageService->findOptimalLanguage(
$this->feed->id,
$otherChannel->id
);
$this->assertNull($optimalLanguage);
}
public function test_validate_route_language(): void
{
$result = $this->languageService->validateRouteLanguage(
$this->feed->id,
$this->channel->id,
$this->language->id
);
$this->assertTrue($result['is_valid']);
$this->assertTrue($result['feed_supports_language']);
$this->assertTrue($result['channel_supports_language']);
}
public function test_batch_validate_routes(): void
{
$routeData = [
[
'feed_id' => $this->feed->id,
'platform_channel_id' => $this->channel->id,
'language_id' => $this->language->id
]
];
$results = $this->languageService->batchValidateRoutes($routeData);
$this->assertCount(1, $results);
$this->assertTrue($results[0]['is_valid']);
}
public function test_get_languages_with_counts(): void
{
$languages = $this->languageService->getLanguagesWithCounts();
$this->assertGreaterThanOrEqual(1, $languages->count());
$language = $languages->where('id', $this->language->id)->first();
$this->assertNotNull($language);
$this->assertEquals(1, $language->active_feeds_count);
$this->assertEquals(1, $language->active_channels_count);
}
}