294 lines
9.8 KiB
PHP
294 lines
9.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Feature\Http\Controllers\Api\V1;
|
||
|
|
|
||
|
|
use Domains\Feed\Models\Feed;
|
||
|
|
use Domains\Feed\Models\Route;
|
||
|
|
use Domains\Platform\Models\PlatformChannel;
|
||
|
|
use Domains\Settings\Models\Language;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class RoutingControllerLanguageTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_store_route_requires_language_id(): void
|
||
|
|
{
|
||
|
|
$language = Language::factory()->create();
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create(['language_id' => $language->id]);
|
||
|
|
|
||
|
|
$response = $this->postJson('/api/v1/routing', [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
// 'language_id' is missing
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertStatus(422);
|
||
|
|
$response->assertJsonValidationErrors(['language_id']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_store_route_validates_language_consistency_with_feed(): void
|
||
|
|
{
|
||
|
|
$language1 = Language::factory()->create();
|
||
|
|
$language2 = Language::factory()->create();
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create(['language_id' => $language1->id]);
|
||
|
|
|
||
|
|
// Attach only language2 to feed, but request language1
|
||
|
|
$feed->languages()->attach($language2->id, [
|
||
|
|
'url' => $feed->url,
|
||
|
|
'is_active' => true,
|
||
|
|
'is_primary' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response = $this->postJson('/api/v1/routing', [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language1->id,
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertStatus(422);
|
||
|
|
$response->assertJsonValidationErrors(['language_id']);
|
||
|
|
$this->assertStringContainsString('The selected feed does not support this language',
|
||
|
|
$response->json('errors.language_id.0'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_store_route_validates_language_consistency_with_channel(): void
|
||
|
|
{
|
||
|
|
$language1 = Language::factory()->create();
|
||
|
|
$language2 = Language::factory()->create();
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create(['language_id' => $language2->id]);
|
||
|
|
|
||
|
|
// Attach language1 to feed, but channel has language2
|
||
|
|
$feed->languages()->attach($language1->id, [
|
||
|
|
'url' => $feed->url,
|
||
|
|
'is_active' => true,
|
||
|
|
'is_primary' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response = $this->postJson('/api/v1/routing', [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language1->id,
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertStatus(422);
|
||
|
|
$response->assertJsonValidationErrors(['language_id']);
|
||
|
|
$this->assertStringContainsString('The selected channel does not support this language',
|
||
|
|
$response->json('errors.language_id.0'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_store_route_succeeds_with_valid_language_consistency(): void
|
||
|
|
{
|
||
|
|
$language = Language::factory()->create();
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create(['language_id' => $language->id]);
|
||
|
|
|
||
|
|
// Attach language to feed
|
||
|
|
$feed->languages()->attach($language->id, [
|
||
|
|
'url' => $feed->url,
|
||
|
|
'is_active' => true,
|
||
|
|
'is_primary' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response = $this->postJson('/api/v1/routing', [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertStatus(201);
|
||
|
|
$response->assertJson([
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'Routing configuration created successfully!',
|
||
|
|
'data' => [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('routes', [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_update_route_validates_language_consistency(): void
|
||
|
|
{
|
||
|
|
$language1 = Language::factory()->create();
|
||
|
|
$language2 = Language::factory()->create();
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create(['language_id' => $language1->id]);
|
||
|
|
|
||
|
|
// Attach both languages to feed
|
||
|
|
$feed->languages()->attach([
|
||
|
|
$language1->id => [
|
||
|
|
'url' => $feed->url,
|
||
|
|
'is_active' => true,
|
||
|
|
'is_primary' => true
|
||
|
|
],
|
||
|
|
$language2->id => [
|
||
|
|
'url' => $feed->url,
|
||
|
|
'is_active' => true,
|
||
|
|
'is_primary' => false
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Create route with language1
|
||
|
|
$route = Route::create([
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language1->id,
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Try to update to language2, which feed supports but channel doesn't
|
||
|
|
$response = $this->putJson("/api/v1/routing/{$feed->id}/{$channel->id}", [
|
||
|
|
'language_id' => $language2->id,
|
||
|
|
'is_active' => false,
|
||
|
|
'priority' => 75
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertStatus(422);
|
||
|
|
$response->assertJsonValidationErrors(['language_id']);
|
||
|
|
$this->assertStringContainsString('The selected channel does not support this language',
|
||
|
|
$response->json('errors.language_id.0'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_update_route_succeeds_with_valid_language(): void
|
||
|
|
{
|
||
|
|
$language = Language::factory()->create();
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create(['language_id' => $language->id]);
|
||
|
|
|
||
|
|
// Attach language to feed
|
||
|
|
$feed->languages()->attach($language->id, [
|
||
|
|
'url' => $feed->url,
|
||
|
|
'is_active' => true,
|
||
|
|
'is_primary' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Create route
|
||
|
|
$route = Route::create([
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response = $this->putJson("/api/v1/routing/{$feed->id}/{$channel->id}", [
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'is_active' => false,
|
||
|
|
'priority' => 75
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertStatus(200);
|
||
|
|
$response->assertJson([
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'Routing configuration updated successfully!',
|
||
|
|
'data' => [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'is_active' => false,
|
||
|
|
'priority' => 75
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('routes', [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'is_active' => false,
|
||
|
|
'priority' => 75
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_index_includes_language_relationship(): void
|
||
|
|
{
|
||
|
|
$language = Language::factory()->create();
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create(['language_id' => $language->id]);
|
||
|
|
|
||
|
|
// Create route
|
||
|
|
Route::create([
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response = $this->getJson('/api/v1/routing');
|
||
|
|
|
||
|
|
$response->assertStatus(200);
|
||
|
|
$response->assertJson([
|
||
|
|
'success' => true,
|
||
|
|
'data' => [
|
||
|
|
[
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'language' => [
|
||
|
|
'id' => $language->id,
|
||
|
|
'name' => $language->name,
|
||
|
|
'short_code' => $language->short_code
|
||
|
|
]
|
||
|
|
]
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_show_includes_language_relationship(): void
|
||
|
|
{
|
||
|
|
$language = Language::factory()->create();
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create(['language_id' => $language->id]);
|
||
|
|
|
||
|
|
// Create route
|
||
|
|
Route::create([
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'is_active' => true,
|
||
|
|
'priority' => 50
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response = $this->getJson("/api/v1/routing/{$feed->id}/{$channel->id}");
|
||
|
|
|
||
|
|
$response->assertStatus(200);
|
||
|
|
$response->assertJson([
|
||
|
|
'success' => true,
|
||
|
|
'data' => [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'language' => [
|
||
|
|
'id' => $language->id,
|
||
|
|
'name' => $language->name,
|
||
|
|
'short_code' => $language->short_code
|
||
|
|
]
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|