2025-08-09 02:51:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature\Http\Controllers\Api\V1;
|
|
|
|
|
|
|
|
|
|
use App\Models\Feed;
|
|
|
|
|
use App\Models\Language;
|
|
|
|
|
use App\Models\PlatformAccount;
|
|
|
|
|
use App\Models\PlatformChannel;
|
|
|
|
|
use App\Models\PlatformInstance;
|
2025-08-09 13:48:25 +02:00
|
|
|
use App\Models\Route;
|
2025-08-09 02:51:18 +02:00
|
|
|
use App\Models\Setting;
|
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class OnboardingControllerTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
2026-03-08 14:18:28 +01:00
|
|
|
|
2025-08-09 02:51:18 +02:00
|
|
|
// Create a language for testing
|
|
|
|
|
Language::factory()->create([
|
|
|
|
|
'id' => 1,
|
|
|
|
|
'short_code' => 'en',
|
|
|
|
|
'name' => 'English',
|
|
|
|
|
'native_name' => 'English',
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_status_shows_needs_onboarding_when_no_components_exist(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/status');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => [
|
|
|
|
|
'needs_onboarding' => true,
|
|
|
|
|
'current_step' => 'platform',
|
|
|
|
|
'has_platform_account' => false,
|
|
|
|
|
'has_feed' => false,
|
|
|
|
|
'has_channel' => false,
|
2025-08-09 13:48:25 +02:00
|
|
|
'has_route' => false,
|
2025-08-09 02:51:18 +02:00
|
|
|
'onboarding_skipped' => false,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_status_shows_feed_step_when_platform_account_exists(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
PlatformAccount::factory()->create(['is_active' => true]);
|
|
|
|
|
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/status');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => [
|
|
|
|
|
'needs_onboarding' => true,
|
|
|
|
|
'current_step' => 'feed',
|
|
|
|
|
'has_platform_account' => true,
|
|
|
|
|
'has_feed' => false,
|
|
|
|
|
'has_channel' => false,
|
2025-08-09 13:48:25 +02:00
|
|
|
'has_route' => false,
|
2025-08-09 02:51:18 +02:00
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_status_shows_channel_step_when_platform_account_and_feed_exist(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
2025-08-10 15:20:28 +02:00
|
|
|
$language = Language::first();
|
2025-08-09 02:51:18 +02:00
|
|
|
PlatformAccount::factory()->create(['is_active' => true]);
|
2025-08-10 15:20:28 +02:00
|
|
|
Feed::factory()->language($language)->create(['is_active' => true]);
|
2025-08-09 02:51:18 +02:00
|
|
|
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/status');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => [
|
|
|
|
|
'needs_onboarding' => true,
|
|
|
|
|
'current_step' => 'channel',
|
|
|
|
|
'has_platform_account' => true,
|
|
|
|
|
'has_feed' => true,
|
|
|
|
|
'has_channel' => false,
|
2025-08-09 13:48:25 +02:00
|
|
|
'has_route' => false,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_status_shows_route_step_when_platform_account_feed_and_channel_exist(): void
|
2025-08-09 13:48:25 +02:00
|
|
|
{
|
2025-08-10 15:20:28 +02:00
|
|
|
$language = Language::first();
|
2025-08-09 13:48:25 +02:00
|
|
|
PlatformAccount::factory()->create(['is_active' => true]);
|
2025-08-10 15:20:28 +02:00
|
|
|
Feed::factory()->language($language)->create(['is_active' => true]);
|
2025-08-09 13:48:25 +02:00
|
|
|
PlatformChannel::factory()->create(['is_active' => true]);
|
|
|
|
|
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/status');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => [
|
|
|
|
|
'needs_onboarding' => true,
|
|
|
|
|
'current_step' => 'route',
|
|
|
|
|
'has_platform_account' => true,
|
|
|
|
|
'has_feed' => true,
|
|
|
|
|
'has_channel' => true,
|
|
|
|
|
'has_route' => false,
|
2025-08-09 02:51:18 +02:00
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_status_shows_no_onboarding_needed_when_all_components_exist(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
2025-08-10 15:20:28 +02:00
|
|
|
$language = Language::first();
|
2025-08-09 02:51:18 +02:00
|
|
|
PlatformAccount::factory()->create(['is_active' => true]);
|
2025-08-10 15:20:28 +02:00
|
|
|
Feed::factory()->language($language)->create(['is_active' => true]);
|
2025-08-09 02:51:18 +02:00
|
|
|
PlatformChannel::factory()->create(['is_active' => true]);
|
2025-08-09 13:48:25 +02:00
|
|
|
Route::factory()->create(['is_active' => true]);
|
2025-08-09 02:51:18 +02:00
|
|
|
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/status');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => [
|
|
|
|
|
'needs_onboarding' => false,
|
|
|
|
|
'current_step' => null,
|
|
|
|
|
'has_platform_account' => true,
|
|
|
|
|
'has_feed' => true,
|
|
|
|
|
'has_channel' => true,
|
2025-08-09 13:48:25 +02:00
|
|
|
'has_route' => true,
|
2025-08-09 02:51:18 +02:00
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_status_shows_no_onboarding_needed_when_skipped(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
// No components exist but onboarding is skipped
|
|
|
|
|
Setting::create([
|
|
|
|
|
'key' => 'onboarding_skipped',
|
|
|
|
|
'value' => 'true',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/status');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => [
|
|
|
|
|
'needs_onboarding' => false,
|
|
|
|
|
'current_step' => null,
|
|
|
|
|
'has_platform_account' => false,
|
|
|
|
|
'has_feed' => false,
|
|
|
|
|
'has_channel' => false,
|
2025-08-09 13:48:25 +02:00
|
|
|
'has_route' => false,
|
2025-08-09 02:51:18 +02:00
|
|
|
'onboarding_skipped' => true,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_options_returns_languages_and_platform_instances(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
PlatformInstance::factory()->create([
|
|
|
|
|
'platform' => 'lemmy',
|
|
|
|
|
'url' => 'https://lemmy.world',
|
|
|
|
|
'name' => 'Lemmy World',
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/options');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJsonStructure([
|
|
|
|
|
'success',
|
|
|
|
|
'data' => [
|
|
|
|
|
'languages' => [
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => ['id', 'short_code', 'name', 'native_name', 'is_active'],
|
2025-08-09 02:51:18 +02:00
|
|
|
],
|
|
|
|
|
'platform_instances' => [
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => ['id', 'platform', 'url', 'name', 'description', 'is_active'],
|
|
|
|
|
],
|
|
|
|
|
],
|
2025-08-09 02:51:18 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_complete_onboarding_returns_success(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
$response = $this->postJson('/api/v1/onboarding/complete');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
2026-03-08 14:18:28 +01:00
|
|
|
'data' => ['completed' => true],
|
2025-08-09 02:51:18 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_skip_onboarding_creates_setting(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
$response = $this->postJson('/api/v1/onboarding/skip');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
2026-03-08 14:18:28 +01:00
|
|
|
'data' => ['skipped' => true],
|
2025-08-09 02:51:18 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('settings', [
|
|
|
|
|
'key' => 'onboarding_skipped',
|
|
|
|
|
'value' => 'true',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_skip_onboarding_updates_existing_setting(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
// Create existing setting with false value
|
|
|
|
|
Setting::create([
|
|
|
|
|
'key' => 'onboarding_skipped',
|
|
|
|
|
'value' => 'false',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->postJson('/api/v1/onboarding/skip');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('settings', [
|
|
|
|
|
'key' => 'onboarding_skipped',
|
|
|
|
|
'value' => 'true',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Ensure only one setting exists
|
|
|
|
|
$this->assertEquals(1, Setting::where('key', 'onboarding_skipped')->count());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_reset_skip_removes_setting(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
// Create skipped setting
|
|
|
|
|
Setting::create([
|
|
|
|
|
'key' => 'onboarding_skipped',
|
|
|
|
|
'value' => 'true',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->postJson('/api/v1/onboarding/reset-skip');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
2026-03-08 14:18:28 +01:00
|
|
|
'data' => ['reset' => true],
|
2025-08-09 02:51:18 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('settings', [
|
|
|
|
|
'key' => 'onboarding_skipped',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_reset_skip_works_when_no_setting_exists(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
$response = $this->postJson('/api/v1/onboarding/reset-skip');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
2026-03-08 14:18:28 +01:00
|
|
|
'data' => ['reset' => true],
|
2025-08-09 02:51:18 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
public function test_onboarding_flow_integration(): void
|
2025-08-09 02:51:18 +02:00
|
|
|
{
|
|
|
|
|
// 1. Initial status - needs onboarding
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/status');
|
|
|
|
|
$response->assertJson(['data' => ['needs_onboarding' => true, 'current_step' => 'platform']]);
|
|
|
|
|
|
|
|
|
|
// 2. Skip onboarding
|
|
|
|
|
$response = $this->postJson('/api/v1/onboarding/skip');
|
|
|
|
|
$response->assertJson(['data' => ['skipped' => true]]);
|
|
|
|
|
|
|
|
|
|
// 3. Status after skip - no longer needs onboarding
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/status');
|
|
|
|
|
$response->assertJson(['data' => ['needs_onboarding' => false, 'onboarding_skipped' => true]]);
|
|
|
|
|
|
|
|
|
|
// 4. Reset skip
|
|
|
|
|
$response = $this->postJson('/api/v1/onboarding/reset-skip');
|
|
|
|
|
$response->assertJson(['data' => ['reset' => true]]);
|
|
|
|
|
|
|
|
|
|
// 5. Status after reset - needs onboarding again
|
|
|
|
|
$response = $this->getJson('/api/v1/onboarding/status');
|
|
|
|
|
$response->assertJson(['data' => ['needs_onboarding' => true, 'onboarding_skipped' => false]]);
|
|
|
|
|
}
|
2026-03-08 14:18:28 +01:00
|
|
|
}
|