fixes
This commit is contained in:
61
tests/Feature/DashboardFavoritesTest.php
Normal file
61
tests/Feature/DashboardFavoritesTest.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DashboardFavoritesTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
public function test_guest_is_redirected_from_favorites(): void
|
||||
{
|
||||
$this->get('/dashboard/favorites')->assertRedirect('/login');
|
||||
}
|
||||
|
||||
public function test_authenticated_user_sees_favourites_and_can_remove(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$art = Artwork::factory()->create(['user_id' => $user->id, 'title' => 'Fav Artwork']);
|
||||
|
||||
$favTable = Schema::hasTable('user_favorites') ? 'user_favorites' : (Schema::hasTable('favourites') ? 'favourites' : null);
|
||||
if (! $favTable) {
|
||||
$this->markTestSkipped('No favorites table available in schema');
|
||||
return;
|
||||
}
|
||||
|
||||
// insert using whichever timestamp column exists on the fav table
|
||||
$col = null;
|
||||
foreach (['datum', 'created_at', 'created', 'date'] as $c) {
|
||||
if (Schema::hasColumn($favTable, $c)) {
|
||||
$col = $c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$insert = [
|
||||
'user_id' => $user->id,
|
||||
'artwork_id' => $art->id,
|
||||
];
|
||||
if ($col) {
|
||||
$insert[$col] = now();
|
||||
}
|
||||
|
||||
DB::table($favTable)->insert($insert);
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('dashboard.favorites'))
|
||||
->assertOk()
|
||||
->assertSee('Fav Artwork');
|
||||
|
||||
$this->actingAs($user)
|
||||
->delete(route('dashboard.favorites.destroy', ['artwork' => $art->id]))
|
||||
->assertRedirect(route('dashboard.favorites'));
|
||||
|
||||
$this->assertDatabaseMissing($favTable, ['user_id' => $user->id, 'artwork_id' => $art->id]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user