Backfill avatars for existing users
This commit is contained in:
@@ -5,7 +5,10 @@ namespace Tests\Feature\Models;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Database\Seeders\UserSeeder;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -66,6 +69,44 @@ class UserTest extends TestCase
|
||||
$this->assertFalse($missing->hasUploadedAvatar());
|
||||
}
|
||||
|
||||
public function test_avatar_migration_backfills_existing_users(): void
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
Schema::table('users', function (Blueprint $table): void {
|
||||
$table->dropColumn([
|
||||
'avatar_path',
|
||||
'avatar_color',
|
||||
'avatar_is_generated',
|
||||
]);
|
||||
});
|
||||
|
||||
DB::table('users')->insert([
|
||||
'name' => 'Legacy User',
|
||||
'email' => 'legacy@example.com',
|
||||
'email_verified_at' => now(),
|
||||
'password' => 'password',
|
||||
'must_change_password_on_next_login' => false,
|
||||
'remember_token' => null,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$avatarFieldsMigration = require database_path('migrations/2026_06_24_114831_add_avatar_fields_to_users_table.php');
|
||||
$avatarFieldsMigration->up();
|
||||
|
||||
$backfillMigration = require database_path('migrations/2026_06_24_123911_backfill_existing_user_avatars.php');
|
||||
$backfillMigration->up();
|
||||
|
||||
$user = DB::table('users')->where('email', 'legacy@example.com')->first();
|
||||
|
||||
$this->assertNotNull($user->avatar_path);
|
||||
$this->assertNotNull($user->avatar_color);
|
||||
$this->assertTrue((bool) $user->avatar_is_generated);
|
||||
Storage::disk('public')->assertExists($user->avatar_path);
|
||||
$this->assertStringContainsString('LU', Storage::disk('public')->get($user->avatar_path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the admin factory state creates admin users.
|
||||
*/
|
||||
@@ -87,6 +128,8 @@ class UserTest extends TestCase
|
||||
*/
|
||||
public function test_user_seeder_creates_admin_and_regular_users(): void
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$this->seed(UserSeeder::class);
|
||||
|
||||
$admin = User::query()->where('email', 'admin@kanbangit.test')->firstOrFail();
|
||||
@@ -107,6 +150,15 @@ class UserTest extends TestCase
|
||||
'scope_id' => null,
|
||||
'scope_type' => null,
|
||||
]);
|
||||
|
||||
User::query()
|
||||
->get()
|
||||
->each(function (User $user): void {
|
||||
$this->assertTrue($user->avatar_is_generated);
|
||||
$this->assertNotNull($user->avatar_path);
|
||||
$this->assertNotNull($user->avatar_color);
|
||||
Storage::disk('public')->assertExists($user->avatar_path);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user