212 lines
No EOL
7.6 KiB
PHP
212 lines
No EOL
7.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Http\Controllers\Api\V1;
|
|
|
|
use Domains\Platform\Models\PlatformInstance;
|
|
use Domains\Platform\Models\PlatformAccount;
|
|
use Domains\Platform\Models\PlatformChannel;
|
|
use Domains\Settings\Models\Language;
|
|
use Domains\Platform\Services\ChannelLanguageDetectionService;
|
|
use Tests\TestCase;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Mockery;
|
|
|
|
class ChannelLanguageIntegrationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private PlatformInstance $platformInstance;
|
|
private PlatformAccount $platformAccount;
|
|
private Language $englishLanguage;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// Create test data
|
|
$this->platformInstance = PlatformInstance::factory()->create([
|
|
'url' => 'https://lemmy.example.com',
|
|
'platform' => 'lemmy'
|
|
]);
|
|
|
|
$this->platformAccount = PlatformAccount::factory()->create([
|
|
'instance_url' => $this->platformInstance->url,
|
|
'is_active' => true,
|
|
'settings' => ['api_token' => 'test-token']
|
|
]);
|
|
|
|
$this->englishLanguage = Language::factory()->create([
|
|
'short_code' => 'en',
|
|
'name' => 'English',
|
|
'is_active' => true
|
|
]);
|
|
|
|
// Set default language
|
|
config(['languages.default' => 'en']);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Mockery::close();
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_platform_channels_controller_store_with_successful_language_detection(): void
|
|
{
|
|
// Mock the ChannelLanguageDetectionService
|
|
$mockService = Mockery::mock(ChannelLanguageDetectionService::class);
|
|
$mockService->shouldReceive('detectChannelLanguages')
|
|
->with('test-community', $this->platformInstance->id)
|
|
->once()
|
|
->andReturn([
|
|
'language_id' => $this->englishLanguage->id,
|
|
'matched_languages' => [$this->englishLanguage->id],
|
|
'community_details' => ['community' => ['id' => 123]]
|
|
]);
|
|
|
|
// Replace the service in the container before making the request
|
|
$this->app->bind(ChannelLanguageDetectionService::class, function () use ($mockService) {
|
|
return $mockService;
|
|
});
|
|
|
|
$response = $this->postJson('/api/v1/platform-channels', [
|
|
'platform_instance_id' => $this->platformInstance->id,
|
|
'channel_id' => 'test-community',
|
|
'name' => 'test-community',
|
|
'display_name' => 'Test Community',
|
|
'description' => 'A test community',
|
|
'is_active' => true
|
|
]);
|
|
|
|
$response->assertStatus(201);
|
|
$response->assertJsonStructure([
|
|
'success',
|
|
'data' => [
|
|
'id',
|
|
'name',
|
|
'language_id',
|
|
'platform_instance_id'
|
|
],
|
|
'message'
|
|
]);
|
|
|
|
// Verify channel was created with detected language
|
|
$this->assertDatabaseHas('platform_channels', [
|
|
'name' => 'test-community',
|
|
'language_id' => $this->englishLanguage->id,
|
|
'platform_instance_id' => $this->platformInstance->id
|
|
]);
|
|
}
|
|
|
|
public function test_platform_channels_controller_store_fails_when_language_detection_fails(): void
|
|
{
|
|
// Mock the ChannelLanguageDetectionService to throw exception
|
|
$mockService = Mockery::mock(ChannelLanguageDetectionService::class);
|
|
$mockService->shouldReceive('detectChannelLanguages')
|
|
->with('nonexistent-community', $this->platformInstance->id)
|
|
->once()
|
|
->andThrow(new \Domains\Platform\Exceptions\ChannelException(
|
|
'Channel not found on instance'
|
|
));
|
|
|
|
// Bind the mock to the container
|
|
$this->app->instance(ChannelLanguageDetectionService::class, $mockService);
|
|
|
|
$response = $this->postJson('/api/v1/platform-channels', [
|
|
'platform_instance_id' => $this->platformInstance->id,
|
|
'channel_id' => 'nonexistent-community',
|
|
'name' => 'nonexistent-community',
|
|
'display_name' => 'Nonexistent Community',
|
|
'description' => 'A community that does not exist',
|
|
'is_active' => true
|
|
]);
|
|
|
|
$response->assertStatus(422);
|
|
$response->assertJson([
|
|
'success' => false,
|
|
'message' => 'Channel not found on instance'
|
|
]);
|
|
|
|
// Verify channel was not created
|
|
$this->assertDatabaseMissing('platform_channels', [
|
|
'name' => 'nonexistent-community'
|
|
]);
|
|
}
|
|
|
|
public function test_onboarding_controller_create_channel_with_language_detection(): void
|
|
{
|
|
// Mock the ChannelLanguageDetectionService
|
|
$mockService = Mockery::mock(ChannelLanguageDetectionService::class);
|
|
$mockService->shouldReceive('detectChannelLanguages')
|
|
->with('technology', $this->platformInstance->id)
|
|
->once()
|
|
->andReturn([
|
|
'language_id' => $this->englishLanguage->id,
|
|
'matched_languages' => [$this->englishLanguage->id],
|
|
'community_details' => ['community' => ['id' => 456]]
|
|
]);
|
|
|
|
// Bind the mock to the container
|
|
$this->app->instance(ChannelLanguageDetectionService::class, $mockService);
|
|
|
|
$response = $this->postJson('/api/v1/onboarding/channel', [
|
|
'name' => 'technology',
|
|
'platform_instance_id' => $this->platformInstance->id,
|
|
'description' => 'Technology discussions'
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonStructure([
|
|
'success',
|
|
'data' => [
|
|
'id',
|
|
'name',
|
|
'language_id',
|
|
'platform_instance_id'
|
|
],
|
|
'message'
|
|
]);
|
|
|
|
// Verify channel was created with detected language
|
|
$this->assertDatabaseHas('platform_channels', [
|
|
'name' => 'technology',
|
|
'language_id' => $this->englishLanguage->id,
|
|
'platform_instance_id' => $this->platformInstance->id
|
|
]);
|
|
}
|
|
|
|
public function test_onboarding_controller_handles_fallback_language_gracefully(): void
|
|
{
|
|
// Mock the ChannelLanguageDetectionService to return fallback
|
|
$mockService = Mockery::mock(ChannelLanguageDetectionService::class);
|
|
$mockService->shouldReceive('detectChannelLanguages')
|
|
->with('test-channel', $this->platformInstance->id)
|
|
->once()
|
|
->andReturn([
|
|
'language_id' => $this->englishLanguage->id,
|
|
'matched_languages' => [$this->englishLanguage->id],
|
|
'fallback_used' => true,
|
|
'original_error' => 'API unavailable'
|
|
]);
|
|
|
|
// Bind the mock to the container
|
|
$this->app->instance(ChannelLanguageDetectionService::class, $mockService);
|
|
|
|
$response = $this->postJson('/api/v1/onboarding/channel', [
|
|
'name' => 'test-channel',
|
|
'platform_instance_id' => $this->platformInstance->id,
|
|
'description' => 'Test channel'
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonFragment([
|
|
'message' => 'Channel created successfully and linked to platform account. Note: Used default language due to detection issue.'
|
|
]);
|
|
|
|
// Verify channel was created with fallback language
|
|
$this->assertDatabaseHas('platform_channels', [
|
|
'name' => 'test-channel',
|
|
'language_id' => $this->englishLanguage->id
|
|
]);
|
|
}
|
|
} |