120 lines
6.1 KiB
PHP
120 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Migrations;
|
|
|
|
use App\Domains\Core\Tenant\Models\Tenant;
|
|
use App\Domains\Core\Tenant\Resources\TenantResource;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Tests\TestCase;
|
|
|
|
class RestoreEventsMigrationTest extends TestCase
|
|
{
|
|
public function test_it_moves_single_event_data_and_leaves_multi_event_selection_empty(): void
|
|
{
|
|
Schema::create('tenants', function (Blueprint $table): void {
|
|
$table->id();
|
|
$table->string('codigo')->unique();
|
|
$table->string('nombre');
|
|
$table->string('storefront_website_type_code');
|
|
$table->string('search_product_layout')->default('column_with_image');
|
|
$table->string('search_group_layout')->default('paginated');
|
|
$table->string('event_title')->nullable();
|
|
$table->string('event_location')->nullable();
|
|
$table->text('event_date_text')->nullable();
|
|
});
|
|
Schema::create('event_dates', function (Blueprint $table): void {
|
|
$table->id();
|
|
$table->string('tenant_code');
|
|
$table->date('date');
|
|
$table->time('time_start');
|
|
$table->time('time_end');
|
|
});
|
|
Schema::create('social_media', function (Blueprint $table): void {
|
|
$table->id();
|
|
$table->string('code')->unique();
|
|
$table->string('icon');
|
|
$table->string('name');
|
|
});
|
|
Schema::create('tenant_social_media', function (Blueprint $table): void {
|
|
$table->id();
|
|
$table->string('tenant_code');
|
|
$table->string('social_media_code');
|
|
$table->string('url');
|
|
$table->unsignedInteger('orden');
|
|
$table->timestamps();
|
|
});
|
|
|
|
DB::table('tenants')->insert([
|
|
['codigo' => 'one', 'nombre' => 'Uno', 'storefront_website_type_code' => 'onticket',
|
|
'event_title' => 'Evento uno', 'event_location' => 'Rosario', 'event_date_text' => '1 de octubre'],
|
|
['codigo' => 'many', 'nombre' => 'Varios', 'storefront_website_type_code' => 'onticket_multi_event',
|
|
'event_title' => 'Evento varios', 'event_location' => 'Santa Fe', 'event_date_text' => '2 de octubre'],
|
|
]);
|
|
DB::table('event_dates')->insert([
|
|
'tenant_code' => 'one', 'date' => '2026-10-01', 'time_start' => '10:00:00', 'time_end' => '12:00:00',
|
|
]);
|
|
DB::table('social_media')->insert(['code' => 'instagram', 'icon' => 'instagram', 'name' => 'Instagram']);
|
|
DB::table('tenant_social_media')->insert([
|
|
'tenant_code' => 'one', 'social_media_code' => 'instagram',
|
|
'url' => 'https://instagram.com/example', 'orden' => 0,
|
|
]);
|
|
|
|
$migration = require database_path('migrations/2026_09_18_000500_restore_events.php');
|
|
$migration->up();
|
|
|
|
$one = DB::table('events')->where('tenant_code', 'one')->first();
|
|
$many = DB::table('events')->where('tenant_code', 'many')->first();
|
|
|
|
$this->assertSame('Evento uno', $one->title);
|
|
$this->assertSame('Rosario', $one->location);
|
|
$this->assertSame('1 de octubre', $one->date_text);
|
|
$this->assertSame($one->id, DB::table('tenants')->where('codigo', 'one')->value('active_event_id'));
|
|
$this->assertNull(DB::table('tenants')->where('codigo', 'many')->value('active_event_id'));
|
|
$this->assertSame($one->id, DB::table('event_dates')->value('event_id'));
|
|
$this->assertSame('https://instagram.com/example', DB::table('event_social_media')->value('url'));
|
|
$this->assertFalse(Schema::hasColumn('tenants', 'event_title'));
|
|
$this->assertNotNull($many);
|
|
|
|
$tenant = Tenant::query()->where('codigo', 'one')->firstOrFail();
|
|
$this->assertSame($one->id, $tenant->activeEvent->id);
|
|
$this->assertCount(1, $tenant->events);
|
|
$this->assertCount(1, $tenant->activeEvent->dates);
|
|
$this->assertSame('instagram', $tenant->activeEvent->socialMedia->first()->code);
|
|
|
|
DB::table('event_social_media')->where('event_id', $one->id)->update([
|
|
'url' => 'https://instagram.com/event-only',
|
|
]);
|
|
DB::table('social_media')->insert(['code' => 'whatsapp', 'icon' => 'whatsapp', 'name' => 'WhatsApp']);
|
|
DB::table('tenant_social_media')->insert([
|
|
['tenant_code' => 'one', 'social_media_code' => 'whatsapp',
|
|
'url' => 'https://wa.me/123', 'orden' => 1],
|
|
['tenant_code' => 'many', 'social_media_code' => 'whatsapp',
|
|
'url' => 'https://wa.me/456', 'orden' => 1],
|
|
]);
|
|
|
|
$socialMigration = require database_path('migrations/2026_09_18_000600_associate_active_event_social_media.php');
|
|
$socialMigration->up();
|
|
$socialMigration->up();
|
|
$this->assertSame(2, DB::table('event_social_media')->where('event_id', $one->id)->count());
|
|
$this->assertSame('https://instagram.com/event-only', DB::table('event_social_media')
|
|
->where('event_id', $one->id)->where('social_media_code', 'instagram')->value('url'));
|
|
$this->assertSame('https://wa.me/123', DB::table('event_social_media')
|
|
->where('event_id', $one->id)->where('social_media_code', 'whatsapp')->value('url'));
|
|
$this->assertSame(0, DB::table('event_social_media')->where('event_id', $many->id)->count());
|
|
|
|
$tenant->load(['socialMedia', 'activeEvent.socialMedia', 'activeEvent.dates']);
|
|
foreach (['headerLogo', 'footerLogo', 'favicon', 'headerBackgroundImage',
|
|
'footerBackgroundImage', 'storefrontWebsiteType'] as $relation) {
|
|
$tenant->setRelation($relation, null);
|
|
}
|
|
$bootstrap = TenantResource::make($tenant)->toArray(request());
|
|
$this->assertSame('https://instagram.com/event-only', $bootstrap['social_media'][0]['url']);
|
|
$this->assertSame('https://instagram.com/event-only', $bootstrap['event']['social_media'][0]['url']);
|
|
|
|
$migration->down();
|
|
$this->assertSame('Evento uno', DB::table('tenants')->where('codigo', 'one')->value('event_title'));
|
|
}
|
|
}
|