2025-08-05 21:53:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature\Http\Controllers\Api\V1;
|
|
|
|
|
|
2025-08-15 16:39:18 +02:00
|
|
|
use Domains\Platform\Models\PlatformAccount;
|
|
|
|
|
use Domains\Platform\Models\PlatformChannel;
|
|
|
|
|
use Domains\Platform\Models\PlatformInstance;
|
2025-08-16 09:00:46 +02:00
|
|
|
use Domains\Platform\Services\ChannelLanguageDetectionService;
|
|
|
|
|
use Domains\Settings\Models\Language;
|
2025-08-05 21:53:49 +02:00
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
use Tests\TestCase;
|
2025-08-16 09:00:46 +02:00
|
|
|
use Mockery;
|
2025-08-05 21:53:49 +02:00
|
|
|
|
|
|
|
|
class PlatformChannelsControllerTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2025-08-16 09:00:46 +02:00
|
|
|
protected function tearDown(): void
|
|
|
|
|
{
|
|
|
|
|
Mockery::close();
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 21:53:49 +02:00
|
|
|
public function test_index_returns_successful_response(): void
|
|
|
|
|
{
|
|
|
|
|
$instance = PlatformInstance::factory()->create();
|
|
|
|
|
PlatformChannel::factory()->count(3)->create(['platform_instance_id' => $instance->id]);
|
|
|
|
|
|
|
|
|
|
$response = $this->getJson('/api/v1/platform-channels');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJsonStructure([
|
|
|
|
|
'success',
|
|
|
|
|
'message',
|
|
|
|
|
'data' => [
|
|
|
|
|
'*' => [
|
|
|
|
|
'id',
|
|
|
|
|
'platform_instance_id',
|
|
|
|
|
'channel_id',
|
|
|
|
|
'name',
|
|
|
|
|
'display_name',
|
|
|
|
|
'description',
|
|
|
|
|
'is_active',
|
|
|
|
|
'created_at',
|
|
|
|
|
'updated_at',
|
|
|
|
|
'platform_instance'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
])
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Platform channels retrieved successfully.'
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_store_creates_platform_channel_successfully(): void
|
|
|
|
|
{
|
|
|
|
|
$instance = PlatformInstance::factory()->create();
|
2025-08-14 22:01:15 +02:00
|
|
|
|
|
|
|
|
// Create a platform account for this instance first
|
|
|
|
|
PlatformAccount::factory()->create([
|
|
|
|
|
'instance_url' => $instance->url,
|
2025-08-16 09:00:46 +02:00
|
|
|
'is_active' => true,
|
|
|
|
|
'settings' => ['api_token' => 'test-token']
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Create a language for the test
|
|
|
|
|
$language = Language::factory()->create([
|
|
|
|
|
'short_code' => 'en',
|
|
|
|
|
'name' => 'English',
|
2025-08-14 22:01:15 +02:00
|
|
|
'is_active' => true
|
|
|
|
|
]);
|
2025-08-05 21:53:49 +02:00
|
|
|
|
2025-08-16 09:00:46 +02:00
|
|
|
// Mock the ChannelLanguageDetectionService
|
|
|
|
|
$mockService = Mockery::mock(ChannelLanguageDetectionService::class);
|
|
|
|
|
$mockService->shouldReceive('detectChannelLanguages')
|
|
|
|
|
->with('Test Channel', $instance->id)
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn([
|
|
|
|
|
'language_id' => $language->id,
|
|
|
|
|
'matched_languages' => [$language->id],
|
|
|
|
|
'community_details' => ['community' => ['id' => 123]]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->app->bind(ChannelLanguageDetectionService::class, function () use ($mockService) {
|
|
|
|
|
return $mockService;
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-05 21:53:49 +02:00
|
|
|
$data = [
|
|
|
|
|
'platform_instance_id' => $instance->id,
|
|
|
|
|
'channel_id' => 'test_channel',
|
|
|
|
|
'name' => 'Test Channel',
|
|
|
|
|
'display_name' => 'Test Channel Display',
|
|
|
|
|
'description' => 'A test channel',
|
|
|
|
|
'is_active' => true
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$response = $this->postJson('/api/v1/platform-channels', $data);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(201)
|
|
|
|
|
->assertJsonStructure([
|
|
|
|
|
'success',
|
|
|
|
|
'message',
|
|
|
|
|
'data' => [
|
|
|
|
|
'id',
|
|
|
|
|
'platform_instance_id',
|
|
|
|
|
'channel_id',
|
|
|
|
|
'name',
|
|
|
|
|
'display_name',
|
|
|
|
|
'description',
|
|
|
|
|
'is_active',
|
|
|
|
|
'created_at',
|
|
|
|
|
'updated_at',
|
|
|
|
|
]
|
|
|
|
|
])
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
2025-08-14 22:01:15 +02:00
|
|
|
'message' => 'Platform channel created successfully and linked to platform account!'
|
2025-08-05 21:53:49 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('platform_channels', [
|
|
|
|
|
'platform_instance_id' => $instance->id,
|
|
|
|
|
'channel_id' => 'test_channel',
|
|
|
|
|
'name' => 'Test Channel',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_store_validates_required_fields(): void
|
|
|
|
|
{
|
|
|
|
|
$response = $this->postJson('/api/v1/platform-channels', []);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(422)
|
|
|
|
|
->assertJsonValidationErrors(['platform_instance_id', 'channel_id', 'name']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_store_validates_platform_instance_exists(): void
|
|
|
|
|
{
|
|
|
|
|
$data = [
|
|
|
|
|
'platform_instance_id' => 999,
|
|
|
|
|
'channel_id' => 'test_channel',
|
|
|
|
|
'name' => 'Test Channel'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$response = $this->postJson('/api/v1/platform-channels', $data);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(422)
|
|
|
|
|
->assertJsonValidationErrors(['platform_instance_id']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_show_returns_platform_channel_successfully(): void
|
|
|
|
|
{
|
|
|
|
|
$instance = PlatformInstance::factory()->create();
|
|
|
|
|
$channel = PlatformChannel::factory()->create(['platform_instance_id' => $instance->id]);
|
|
|
|
|
|
|
|
|
|
$response = $this->getJson("/api/v1/platform-channels/{$channel->id}");
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJsonStructure([
|
|
|
|
|
'success',
|
|
|
|
|
'message',
|
|
|
|
|
'data' => [
|
|
|
|
|
'id',
|
|
|
|
|
'platform_instance_id',
|
|
|
|
|
'channel_id',
|
|
|
|
|
'name',
|
|
|
|
|
'display_name',
|
|
|
|
|
'description',
|
|
|
|
|
'is_active',
|
|
|
|
|
'created_at',
|
|
|
|
|
'updated_at',
|
|
|
|
|
'platform_instance'
|
|
|
|
|
]
|
|
|
|
|
])
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Platform channel retrieved successfully.',
|
|
|
|
|
'data' => [
|
|
|
|
|
'id' => $channel->id,
|
|
|
|
|
'name' => $channel->name,
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_update_modifies_platform_channel_successfully(): void
|
|
|
|
|
{
|
|
|
|
|
$instance = PlatformInstance::factory()->create();
|
|
|
|
|
$channel = PlatformChannel::factory()->create(['platform_instance_id' => $instance->id]);
|
|
|
|
|
|
|
|
|
|
$updateData = [
|
|
|
|
|
'name' => 'Updated Channel',
|
|
|
|
|
'display_name' => 'Updated Display Name',
|
|
|
|
|
'description' => 'Updated description',
|
|
|
|
|
'is_active' => false
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$response = $this->putJson("/api/v1/platform-channels/{$channel->id}", $updateData);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Platform channel updated successfully!'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('platform_channels', [
|
|
|
|
|
'id' => $channel->id,
|
|
|
|
|
'name' => 'Updated Channel',
|
|
|
|
|
'display_name' => 'Updated Display Name',
|
|
|
|
|
'is_active' => false,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_destroy_deletes_platform_channel_successfully(): void
|
|
|
|
|
{
|
|
|
|
|
$instance = PlatformInstance::factory()->create();
|
|
|
|
|
$channel = PlatformChannel::factory()->create(['platform_instance_id' => $instance->id]);
|
|
|
|
|
|
|
|
|
|
$response = $this->deleteJson("/api/v1/platform-channels/{$channel->id}");
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Platform channel deleted successfully!'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('platform_channels', [
|
|
|
|
|
'id' => $channel->id
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_toggle_activates_inactive_channel(): void
|
|
|
|
|
{
|
|
|
|
|
$instance = PlatformInstance::factory()->create();
|
|
|
|
|
$channel = PlatformChannel::factory()->create([
|
|
|
|
|
'platform_instance_id' => $instance->id,
|
|
|
|
|
'is_active' => false
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->postJson("/api/v1/platform-channels/{$channel->id}/toggle");
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Platform channel activated successfully!'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('platform_channels', [
|
|
|
|
|
'id' => $channel->id,
|
|
|
|
|
'is_active' => true
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_toggle_deactivates_active_channel(): void
|
|
|
|
|
{
|
|
|
|
|
$instance = PlatformInstance::factory()->create();
|
|
|
|
|
$channel = PlatformChannel::factory()->create([
|
|
|
|
|
'platform_instance_id' => $instance->id,
|
|
|
|
|
'is_active' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->postJson("/api/v1/platform-channels/{$channel->id}/toggle");
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Platform channel deactivated successfully!'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('platform_channels', [
|
|
|
|
|
'id' => $channel->id,
|
|
|
|
|
'is_active' => false
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|